Commit | Line | Data |
---|---|---|
6e651d8b MAL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Marc-Andre Laperle - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.importexport; | |
14 | ||
15 | import java.io.File; | |
16 | import java.lang.reflect.InvocationTargetException; | |
195355a9 MAL |
17 | import java.text.MessageFormat; |
18 | import java.util.ArrayList; | |
6e651d8b MAL |
19 | import java.util.List; |
20 | ||
21 | import org.eclipse.core.resources.IProject; | |
22 | import org.eclipse.core.resources.ResourcesPlugin; | |
23 | import org.eclipse.core.runtime.IProgressMonitor; | |
24 | import org.eclipse.core.runtime.IStatus; | |
195355a9 | 25 | import org.eclipse.jface.dialogs.IDialogConstants; |
6e651d8b MAL |
26 | import org.eclipse.jface.dialogs.IDialogSettings; |
27 | import org.eclipse.jface.dialogs.MessageDialog; | |
28 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
29 | import org.eclipse.jface.viewers.CheckboxTreeViewer; | |
30 | import org.eclipse.jface.viewers.IStructuredSelection; | |
31 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
32 | import org.eclipse.linuxtools.internal.tmf.ui.project.model.TmfImportHelper; | |
195355a9 | 33 | import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.AbstractTracePackageOperation; |
6e651d8b MAL |
34 | import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.AbstractTracePackageWizardPage; |
35 | import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement; | |
36 | import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement; | |
37 | import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement; | |
38 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; | |
39 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfNavigatorContentProvider; | |
40 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry; | |
41 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
42 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
43 | import org.eclipse.linuxtools.tmf.ui.project.model.TraceUtils; | |
44 | import org.eclipse.swt.SWT; | |
45 | import org.eclipse.swt.events.SelectionAdapter; | |
46 | import org.eclipse.swt.events.SelectionEvent; | |
47 | import org.eclipse.swt.events.TraverseEvent; | |
48 | import org.eclipse.swt.events.TraverseListener; | |
49 | import org.eclipse.swt.layout.GridData; | |
50 | import org.eclipse.swt.layout.GridLayout; | |
51 | import org.eclipse.swt.widgets.Button; | |
52 | import org.eclipse.swt.widgets.Combo; | |
53 | import org.eclipse.swt.widgets.Composite; | |
54 | import org.eclipse.swt.widgets.Event; | |
55 | import org.eclipse.swt.widgets.Label; | |
56 | import org.eclipse.swt.widgets.Listener; | |
57 | import org.eclipse.swt.widgets.Text; | |
58 | import org.eclipse.ui.dialogs.ElementListSelectionDialog; | |
59 | import org.eclipse.ui.model.WorkbenchLabelProvider; | |
60 | ||
61 | /** | |
62 | * Wizard page for the import trace package wizard | |
63 | * | |
64 | * @author Marc-Andre Laperle | |
65 | */ | |
66 | public class ImportTracePackageWizardPage extends AbstractTracePackageWizardPage { | |
67 | ||
68 | private static final String ICON_PATH = "icons/wizban/trace_import_wiz.png"; //$NON-NLS-1$ | |
69 | private static final String PAGE_NAME = "ImportTracePackagePage"; //$NON-NLS-1$ | |
70 | private static final String STORE_PROJECT_NAME_ID = PAGE_NAME + ".STORE_PROJECT_NAME_ID"; //$NON-NLS-1$ | |
71 | ||
72 | private String fValidatedFilePath; | |
73 | private TmfTraceFolder fTmfTraceFolder; | |
74 | private Text fProjectText; | |
75 | private List<IProject> fOpenedTmfProjects; | |
76 | ||
77 | /** | |
78 | * Constructor for the import trace package wizard page | |
79 | * | |
80 | * @param selection | |
81 | * the current object selection | |
82 | */ | |
83 | public ImportTracePackageWizardPage(IStructuredSelection selection) { | |
84 | super(PAGE_NAME, Messages.ImportTracePackageWizardPage_Title, Activator.getDefault().getImageDescripterFromPath(ICON_PATH), selection); | |
85 | ||
86 | if (getSelection().getFirstElement() instanceof TmfTraceFolder) { | |
87 | fTmfTraceFolder = (TmfTraceFolder) getSelection().getFirstElement(); | |
88 | } | |
89 | } | |
90 | ||
91 | @Override | |
92 | public void createControl(Composite parent) { | |
93 | initializeDialogUnits(parent); | |
94 | ||
95 | Composite composite = new Composite(parent, SWT.NULL); | |
96 | composite.setLayout(new GridLayout()); | |
97 | composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | |
98 | | GridData.HORIZONTAL_ALIGN_FILL)); | |
99 | composite.setFont(parent.getFont()); | |
100 | ||
101 | createFilePathGroup(composite, Messages.ImportTracePackageWizardPage_FromArchive, SWT.OPEN); | |
102 | createElementViewer(composite); | |
103 | createButtonsGroup(composite); | |
104 | if (fTmfTraceFolder == null) { | |
105 | createProjectSelectionGroup(composite); | |
106 | } | |
107 | ||
108 | restoreWidgetValues(); | |
109 | setMessage(Messages.ImportTracePackageWizardPage_Message); | |
110 | updatePageCompletion(); | |
111 | ||
112 | setControl(composite); | |
113 | } | |
114 | ||
115 | private void createProjectSelectionGroup(Composite parent) { | |
116 | ||
117 | Composite projectSelectionGroup = new Composite(parent, SWT.NONE); | |
118 | GridLayout layout = new GridLayout(); | |
119 | layout.numColumns = 3; | |
120 | projectSelectionGroup.setLayout(layout); | |
121 | projectSelectionGroup.setLayoutData(new GridData( | |
122 | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL)); | |
123 | ||
124 | Label projectLabel = new Label(projectSelectionGroup, SWT.NONE); | |
125 | projectLabel.setText(Messages.ImportTracePackageWizardPage_Project); | |
126 | ||
127 | fProjectText = new Text(projectSelectionGroup, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY); | |
128 | GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | |
129 | | GridData.GRAB_HORIZONTAL); | |
130 | data.grabExcessHorizontalSpace = true; | |
131 | fProjectText.setLayoutData(data); | |
132 | ||
133 | fOpenedTmfProjects = TraceUtils.getOpenedTmfProjects(); | |
134 | ||
135 | // No project to import to, create a default project if it doesn't exist | |
136 | if (fOpenedTmfProjects.isEmpty()) { | |
137 | IProject defaultProject = ResourcesPlugin.getWorkspace().getRoot().getProject(TmfCommonConstants.DEFAULT_TRACE_PROJECT_NAME); | |
138 | if (!defaultProject.exists()) { | |
139 | IProject project = TmfProjectRegistry.createProject(TmfCommonConstants.DEFAULT_TRACE_PROJECT_NAME, null, null); | |
140 | TmfImportHelper.forceFolderRefresh(project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME)); | |
141 | fOpenedTmfProjects.add(project); | |
142 | } | |
143 | } | |
144 | ||
145 | if (!fOpenedTmfProjects.isEmpty()) { | |
146 | selectProject(fOpenedTmfProjects.get(0)); | |
147 | } | |
148 | ||
149 | Button button = new Button(projectSelectionGroup, | |
150 | SWT.PUSH); | |
151 | button.setText(Messages.ImportTracePackageWizardPage_SelectProjectButton); | |
152 | button.addListener(SWT.Selection, new Listener() { | |
153 | @Override | |
154 | public void handleEvent(Event event) { | |
155 | ElementListSelectionDialog d = new ElementListSelectionDialog(getContainer().getShell(), new WorkbenchLabelProvider()); | |
156 | ||
157 | d.setBlockOnOpen(true); | |
158 | d.setTitle(Messages.ImportTracePackageWizardPage_SelectProjectDialogTitle); | |
159 | ||
160 | d.setElements(fOpenedTmfProjects.toArray(new IProject[] {})); | |
161 | ||
162 | d.open(); | |
163 | if (d.getFirstResult() != null) { | |
164 | IProject project = (IProject) d.getFirstResult(); | |
165 | selectProject(project); | |
166 | } | |
167 | } | |
168 | }); | |
169 | setButtonLayoutData(button); | |
170 | } | |
171 | ||
172 | @Override | |
173 | protected void restoreWidgetValues() { | |
174 | super.restoreWidgetValues(); | |
175 | IDialogSettings settings = getDialogSettings(); | |
176 | if (settings != null && fProjectText != null) { | |
177 | ||
178 | // Restore last selected project | |
179 | String projectName = settings.get(STORE_PROJECT_NAME_ID); | |
180 | if (projectName != null && !projectName.isEmpty()) { | |
181 | for (IProject project : fOpenedTmfProjects) { | |
182 | if (project.getName().equals(projectName)) { | |
183 | selectProject(project); | |
184 | break; | |
185 | } | |
186 | } | |
187 | } | |
188 | } | |
189 | } | |
190 | ||
191 | @Override | |
192 | protected void saveWidgetValues() { | |
193 | super.saveWidgetValues(); | |
194 | ||
195 | IDialogSettings settings = getDialogSettings(); | |
196 | if (settings != null) { | |
197 | settings.put(STORE_PROJECT_NAME_ID, fTmfTraceFolder.getProject().getResource().getName()); | |
198 | } | |
199 | } | |
200 | ||
201 | private void selectProject(IProject project) { | |
202 | fProjectText.setText(project.getName()); | |
203 | new TmfNavigatorContentProvider().getChildren(project); | |
204 | fTmfTraceFolder = TmfProjectRegistry.getProject(project).getTracesFolder(); | |
205 | updatePageCompletion(); | |
206 | } | |
207 | ||
208 | @Override | |
209 | protected boolean determinePageCompletion() { | |
210 | return super.determinePageCompletion() && fTmfTraceFolder != null; | |
211 | } | |
212 | ||
213 | @Override | |
214 | protected Object createElementViewerInput() { | |
215 | ||
216 | final TracePackageExtractManifestOperation op = new TracePackageExtractManifestOperation(getFilePathValue()); | |
217 | ||
218 | try { | |
219 | getContainer().run(true, true, new IRunnableWithProgress() { | |
220 | ||
221 | @Override | |
222 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
223 | monitor.beginTask(Messages.ImportTracePackageWizardPage_ReadingPackage, 10); | |
224 | op.run(monitor); | |
225 | monitor.done(); | |
226 | } | |
227 | ||
228 | }); | |
229 | ||
230 | IStatus status = op.getStatus(); | |
231 | if (status.getSeverity() == IStatus.ERROR) { | |
232 | handleErrorStatus(status); | |
233 | } | |
234 | } catch (InvocationTargetException e1) { | |
235 | handleError(Messages.TracePackageExtractManifestOperation_ErrorReadingManifest, e1); | |
236 | } catch (InterruptedException e1) { | |
237 | // Canceled | |
238 | } | |
239 | ||
195355a9 MAL |
240 | TracePackageElement[] resultElements = op.getResultElement(); |
241 | if (resultElements == null || resultElements.length == 0) { | |
6e651d8b MAL |
242 | return null; |
243 | } | |
244 | ||
195355a9 | 245 | return resultElements; |
6e651d8b MAL |
246 | } |
247 | ||
248 | @Override | |
249 | protected void createFilePathGroup(Composite parent, String label, int fileDialogStyle) { | |
250 | super.createFilePathGroup(parent, label, fileDialogStyle); | |
251 | ||
252 | Combo filePathCombo = getFilePathCombo(); | |
253 | filePathCombo.addSelectionListener(new SelectionAdapter() { | |
254 | @Override | |
255 | public void widgetSelected(SelectionEvent e) { | |
256 | updateWithFilePathSelection(); | |
257 | } | |
258 | }); | |
259 | ||
260 | // User can type-in path and press return to validate | |
261 | filePathCombo.addTraverseListener(new TraverseListener() { | |
262 | @Override | |
263 | public void keyTraversed(TraverseEvent e) { | |
264 | if (e.detail == SWT.TRAVERSE_RETURN) { | |
265 | e.doit = false; | |
266 | updateWithFilePathSelection(); | |
267 | } | |
268 | } | |
269 | }); | |
270 | } | |
271 | ||
272 | @Override | |
273 | protected void updateWithFilePathSelection() { | |
274 | if (!isFilePathValid()) { | |
275 | setErrorMessage(Messages.ImportTracePackageWizardPage_ErrorFileNotFound); | |
276 | getElementViewer().setInput(null); | |
277 | return; | |
278 | } | |
279 | setErrorMessage(null); | |
280 | ||
281 | getContainer().getShell().getDisplay().asyncExec(new Runnable() { | |
282 | @Override | |
283 | public void run() { | |
284 | CheckboxTreeViewer elementViewer = getElementViewer(); | |
285 | Object elementViewerInput = createElementViewerInput(); | |
286 | elementViewer.setInput(elementViewerInput); | |
287 | if (elementViewerInput != null) { | |
288 | elementViewer.expandToLevel(2); | |
289 | setAllChecked(elementViewer, false, true); | |
290 | fValidatedFilePath = getFilePathValue(); | |
291 | } | |
292 | ||
293 | updatePageCompletion(); | |
294 | } | |
295 | }); | |
296 | } | |
297 | ||
298 | private boolean isFilePathValid() { | |
299 | return new File(getFilePathValue()).exists(); | |
300 | } | |
301 | ||
302 | /** | |
303 | * Finish the wizard page | |
304 | * | |
305 | * @return true on success | |
306 | */ | |
307 | public boolean finish() { | |
308 | if (!checkForOverwrite()) { | |
309 | return false; | |
310 | } | |
311 | ||
312 | saveWidgetValues(); | |
313 | ||
195355a9 MAL |
314 | Object input = getElementViewer().getInput(); |
315 | TracePackageElement[] traceElements = (TracePackageElement[]) input; | |
316 | final TracePackageImportOperation importOperation = new TracePackageImportOperation(fValidatedFilePath, traceElements, fTmfTraceFolder); | |
6e651d8b MAL |
317 | |
318 | try { | |
319 | getContainer().run(true, true, new IRunnableWithProgress() { | |
320 | @Override | |
321 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
322 | importOperation.run(monitor); | |
323 | } | |
324 | }); | |
325 | ||
326 | IStatus status = importOperation.getStatus(); | |
327 | if (status.getSeverity() == IStatus.ERROR) { | |
328 | handleErrorStatus(status); | |
329 | } | |
330 | ||
331 | } catch (InvocationTargetException e) { | |
332 | handleError(Messages.ImportTracePackageWizardPage_ErrorOperation, e); | |
333 | } catch (InterruptedException e) { | |
334 | } | |
335 | ||
336 | return importOperation.getStatus().getSeverity() == IStatus.OK; | |
337 | } | |
338 | ||
339 | private boolean checkForOverwrite() { | |
195355a9 | 340 | TracePackageElement[] traceElements = (TracePackageElement[]) getElementViewer().getInput(); |
507b1336 | 341 | List<TracePackageTraceElement> noImportTraces = new ArrayList<>(); |
195355a9 MAL |
342 | boolean noToAll = false; |
343 | for (TracePackageElement packageElement : traceElements) { | |
344 | TracePackageTraceElement traceElement = (TracePackageTraceElement) packageElement; | |
345 | if (!AbstractTracePackageOperation.isFilesChecked(traceElement)) { | |
346 | continue; | |
347 | } | |
348 | ||
349 | if (noToAll) { | |
350 | noImportTraces.add(traceElement); | |
351 | continue; | |
352 | } | |
353 | ||
354 | String traceName = traceElement.getText(); | |
355 | if (traceExists(traceName)) { | |
356 | int returnCode = promptForOverwrite(traceName); | |
357 | final String[] response = new String[] { IDialogConstants.NO_TO_ALL_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.YES_TO_ALL_LABEL, IDialogConstants.YES_LABEL }; | |
358 | if (response[returnCode].equals(IDialogConstants.YES_TO_ALL_LABEL)) { | |
359 | break; | |
360 | } else if (response[returnCode].equals(IDialogConstants.NO_TO_ALL_LABEL)) { | |
361 | noToAll = true; | |
362 | noImportTraces.add(traceElement); | |
363 | } else if (response[returnCode].equals(IDialogConstants.NO_LABEL)) { | |
364 | noImportTraces.add(traceElement); | |
365 | } | |
366 | } | |
367 | } | |
368 | ||
369 | // Unselect the traces that the user decided not to import | |
370 | for (TracePackageTraceElement t : noImportTraces) { | |
371 | for (TracePackageElement e : t.getChildren()) { | |
372 | if (e instanceof TracePackageFilesElement) { | |
373 | ((TracePackageFilesElement) e).setChecked(false); | |
374 | } | |
375 | } | |
376 | } | |
377 | ||
378 | return true; | |
379 | } | |
6e651d8b | 380 | |
195355a9 | 381 | private boolean traceExists(String traceName) { |
6e651d8b MAL |
382 | List<TmfTraceElement> traces = fTmfTraceFolder.getTraces(); |
383 | for (TmfTraceElement t : traces) { | |
384 | if (t.getName().equals(traceName)) { | |
195355a9 | 385 | return true; |
6e651d8b MAL |
386 | } |
387 | } | |
388 | ||
195355a9 MAL |
389 | return false; |
390 | } | |
391 | ||
392 | private int promptForOverwrite(String traceName) { | |
393 | final MessageDialog dialog = new MessageDialog(getContainer() | |
394 | .getShell(), null, null, MessageFormat.format(Messages.ImportTracePackageWizardPage_AlreadyExists, traceName), | |
395 | MessageDialog.QUESTION, new String[] { | |
396 | IDialogConstants.NO_TO_ALL_LABEL, | |
397 | IDialogConstants.NO_LABEL, | |
398 | IDialogConstants.YES_TO_ALL_LABEL, | |
399 | IDialogConstants.YES_LABEL, | |
400 | }, 3) { | |
401 | @Override | |
402 | protected int getShellStyle() { | |
403 | return super.getShellStyle() | SWT.SHEET; | |
404 | } | |
405 | }; | |
406 | return dialog.open(); | |
6e651d8b MAL |
407 | } |
408 | } |