Commit | Line | Data |
---|---|---|
417a4110 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 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.tracecompass.internal.tmf.remote.ui.wizards.fetch; | |
14 | ||
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
16 | ||
17 | import java.lang.reflect.InvocationTargetException; | |
18 | import java.text.MessageFormat; | |
19 | import java.util.HashSet; | |
20 | import java.util.List; | |
21 | import java.util.Set; | |
22 | ||
23 | import org.eclipse.core.runtime.IProgressMonitor; | |
24 | import org.eclipse.core.runtime.IStatus; | |
25 | import org.eclipse.core.runtime.Status; | |
26 | import org.eclipse.core.runtime.SubMonitor; | |
27 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
28 | import org.eclipse.jface.viewers.CheckboxTreeViewer; | |
29 | import org.eclipse.jface.viewers.IStructuredSelection; | |
30 | import org.eclipse.jface.viewers.ViewerSorter; | |
31 | import org.eclipse.swt.SWT; | |
32 | import org.eclipse.swt.events.SelectionAdapter; | |
33 | import org.eclipse.swt.events.SelectionEvent; | |
34 | import org.eclipse.swt.graphics.Image; | |
35 | import org.eclipse.swt.layout.GridData; | |
36 | import org.eclipse.swt.layout.GridLayout; | |
37 | import org.eclipse.swt.widgets.Composite; | |
38 | import org.eclipse.swt.widgets.ToolBar; | |
39 | import org.eclipse.swt.widgets.ToolItem; | |
40 | import org.eclipse.tracecompass.internal.tmf.remote.ui.Activator; | |
41 | import org.eclipse.tracecompass.internal.tmf.remote.ui.messages.RemoteMessages; | |
42 | import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteGenerateManifestOperation; | |
43 | import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportConnectionNodeElement; | |
44 | import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportProfileElement; | |
45 | import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportTracesOperation; | |
46 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.AbstractTracePackageOperation; | |
47 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.AbstractTracePackageWizardPage; | |
48 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement; | |
49 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement; | |
50 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageLabelProvider; | |
51 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement; | |
52 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.importexport.Messages; | |
53 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder; | |
54 | import org.eclipse.ui.ISharedImages; | |
55 | import org.eclipse.ui.PlatformUI; | |
56 | import org.eclipse.ui.plugin.AbstractUIPlugin; | |
57 | ||
58 | /** | |
59 | * Wizard page that connects to a remote node, lists all traces according to | |
60 | * a given remote profile and imports the selected traces. | |
61 | ||
62 | * @author Marc-Andre Laperle | |
63 | * @author Bernd Hufmann | |
64 | */ | |
65 | public class RemoteFetchLogWizardRemotePage extends AbstractTracePackageWizardPage { | |
66 | ||
67 | // ------------------------------------------------------------------------ | |
68 | // Constant(s) | |
69 | // ------------------------------------------------------------------------ | |
70 | /* The page name */ | |
71 | private static final String PAGE_NAME = "org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch"; //$NON-NLS-1$ | |
72 | private static final String ICON_PATH = "icons/elcl16/fetch_log_wiz.gif"; //$NON-NLS-1$ | |
73 | private static final Image COLLAPSE_ALL_IMAGE = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_ELCL_COLLAPSEALL); | |
74 | private static final Image EXPAND_ALL_IMAGE = Activator.getDefault().getImageFromPath("icons/elcl16/expandall.png"); //$NON-NLS-1$ | |
75 | ||
76 | // ------------------------------------------------------------------------ | |
77 | // Attributes(s) | |
78 | // ------------------------------------------------------------------------ | |
79 | private TmfTraceFolder fTmfTraceFolder; | |
80 | ||
81 | private RemoteImportProfileElement fProfile; | |
82 | private final Set<RemoteImportConnectionNodeElement> fRemoteHosts = new HashSet<>(); | |
83 | private boolean fOverwriteAll; | |
84 | private boolean fIsVisible = false; | |
85 | ||
86 | // ------------------------------------------------------------------------ | |
87 | // Constructors | |
88 | // ------------------------------------------------------------------------ | |
89 | /** | |
90 | * Constructor | |
91 | * | |
92 | * @param title | |
93 | * The Wizard title | |
94 | * @param selection | |
95 | * The current selection (trace folder element) | |
96 | */ | |
97 | protected RemoteFetchLogWizardRemotePage(String title, IStructuredSelection selection) { | |
98 | super(PAGE_NAME, title, null, selection); | |
99 | setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, ICON_PATH)); | |
100 | ||
101 | if (selection.getFirstElement() instanceof TmfTraceFolder) { | |
102 | fTmfTraceFolder = (TmfTraceFolder) selection.getFirstElement(); | |
103 | } | |
104 | ||
105 | if (fTmfTraceFolder == null) { | |
106 | throw new IllegalArgumentException(); | |
107 | } | |
108 | setDescription(RemoteMessages.RemoteFetchLogWizardRemotePage_Description); | |
109 | } | |
110 | ||
111 | // ------------------------------------------------------------------------ | |
112 | // Operations | |
113 | // ------------------------------------------------------------------------ | |
114 | @Override | |
115 | public void createControl(Composite parent) { | |
116 | initializeDialogUnits(parent); | |
117 | ||
118 | Composite composite = new Composite(parent, SWT.NULL); | |
119 | composite.setLayout(new GridLayout()); | |
120 | composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | |
121 | | GridData.HORIZONTAL_ALIGN_FILL)); | |
122 | composite.setFont(parent.getFont()); | |
123 | ||
124 | createToolBar(composite); | |
125 | createElementViewer(composite); | |
126 | ||
127 | createButtonsGroup(composite); | |
128 | ||
129 | restoreWidgetValues(); | |
130 | updatePageCompletion(); | |
131 | ||
132 | setControl(composite); | |
133 | } | |
134 | ||
135 | @Override | |
136 | protected boolean determinePageCompletion() { | |
137 | return getElementViewer().getCheckedElements().length > 0; | |
138 | } | |
139 | ||
140 | @Override | |
141 | protected void updateWithFilePathSelection() { | |
142 | } | |
143 | ||
144 | @Override | |
145 | public void setVisible(final boolean visible) { | |
146 | super.setVisible(visible); | |
147 | ||
148 | fIsVisible = visible; | |
149 | ||
150 | if (visible) { | |
151 | getContainer().getShell().getDisplay().asyncExec(new Runnable() { | |
152 | @Override | |
153 | public void run() { | |
154 | updateViewer(); | |
155 | } | |
156 | }); | |
157 | } else { | |
158 | getElementViewer().setInput(null); | |
159 | } | |
160 | } | |
161 | ||
162 | private void createToolBar(Composite parent) { | |
163 | ToolBar toolBar = new ToolBar(parent, SWT.HORIZONTAL); | |
164 | toolBar.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); | |
165 | ||
166 | ToolItem expandAll = new ToolItem(toolBar, SWT.PUSH); | |
167 | expandAll.setImage(EXPAND_ALL_IMAGE); | |
168 | expandAll.setToolTipText(RemoteMessages.RemoteFetchLogWizardRemotePage_ExpandAll); | |
169 | expandAll.addSelectionListener(new SelectionAdapter() { | |
170 | @Override | |
171 | public void widgetSelected(SelectionEvent e) { | |
172 | getElementViewer().expandAll(); | |
173 | } | |
174 | }); | |
175 | ||
176 | ToolItem collapseAll = new ToolItem(toolBar, SWT.PUSH); | |
177 | collapseAll.setImage(COLLAPSE_ALL_IMAGE); | |
178 | collapseAll.setToolTipText(RemoteMessages.RemoteFetchLogWizardRemotePage_CollapseAll); | |
179 | collapseAll.addSelectionListener(new SelectionAdapter() { | |
180 | @Override | |
181 | public void widgetSelected(SelectionEvent e) { | |
182 | getElementViewer().collapseAll(); | |
183 | } | |
184 | }); | |
185 | } | |
186 | ||
187 | @Override | |
188 | protected void createElementViewer(Composite compositeParent) { | |
189 | super.createElementViewer(compositeParent); | |
190 | ||
191 | CheckboxTreeViewer elementViewer = getElementViewer(); | |
192 | elementViewer.setLabelProvider(new TracePackageLabelProvider() { | |
193 | @Override | |
194 | public String getText(Object element) { | |
195 | if (element instanceof TracePackageTraceElement) { | |
196 | for (TracePackageElement files : ((TracePackageTraceElement) element).getChildren()) { | |
197 | if (files instanceof TracePackageFilesElement) { | |
198 | return ((TracePackageFilesElement) files).getFileName(); | |
199 | } | |
200 | } | |
201 | } | |
202 | return super.getText(element); | |
203 | } | |
204 | @Override | |
205 | public Image getImage(Object element) { | |
206 | if (element instanceof TracePackageTraceElement) { | |
207 | for (TracePackageElement files : ((TracePackageTraceElement) element).getChildren()) { | |
208 | return files.getImage(); | |
209 | } | |
210 | } | |
211 | return super.getImage(element); | |
212 | } | |
213 | }); | |
214 | elementViewer.setSorter(new ViewerSorter() { | |
215 | @Override | |
216 | public int category(Object element) { | |
217 | if (element instanceof TracePackageTraceElement) { | |
218 | return 1; | |
219 | } | |
220 | return 0; | |
221 | } | |
222 | }); | |
223 | } | |
224 | ||
225 | @Override | |
226 | protected Object createElementViewerInput() { | |
227 | if (fProfile == null) { | |
228 | return null; | |
229 | } | |
230 | final List<RemoteImportConnectionNodeElement> remoteHosts = fProfile.getConnectionNodeElements(); | |
231 | ||
232 | if (remoteHosts.size() == 0) { | |
233 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, RemoteMessages.RemoteFetchLogWizardRemotePage_MissingConnectionInformation); | |
234 | } | |
235 | ||
236 | final IStatus status[] = new IStatus[1]; | |
237 | try { | |
238 | getContainer().run(true, true, new IRunnableWithProgress() { | |
239 | @Override | |
240 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
241 | SubMonitor subMonitor = SubMonitor.convert(monitor, remoteHosts.size()); | |
242 | for (final RemoteImportConnectionNodeElement remoteHost : remoteHosts) { | |
243 | SubMonitor child = subMonitor.newChild(1); | |
244 | child.setTaskName(MessageFormat.format(RemoteMessages.RemoteFetchLogWizardRemotePage_OpeningConnectionTo, remoteHost.getURI())); | |
245 | status[0] = remoteHost.connect(checkNotNull(subMonitor.newChild(1))); | |
246 | if (!status[0].isOK()) { | |
247 | monitor.done(); | |
248 | return; | |
249 | } | |
250 | // cache remote host | |
251 | fRemoteHosts.add(remoteHost); | |
252 | } | |
253 | monitor.done(); | |
254 | } | |
255 | }); | |
256 | } catch (InvocationTargetException e) { | |
257 | handleError( | |
258 | MessageFormat.format(RemoteMessages.RemoteFetchLogWizardRemotePage_ConnectionError, fProfile.getText()), | |
259 | e); | |
260 | return null; | |
261 | } catch (InterruptedException e) { | |
262 | // Cancelled | |
263 | status[0] = Status.CANCEL_STATUS; | |
264 | } | |
265 | ||
266 | if (!status[0].isOK()) { | |
267 | handleErrorStatus(status[0]); | |
268 | return null; | |
269 | } | |
270 | ||
417a4110 BH |
271 | try { |
272 | final AbstractTracePackageOperation generateManifestOperation = new RemoteGenerateManifestOperation(fProfile); | |
273 | ||
274 | getContainer().run(true, true, new IRunnableWithProgress() { | |
275 | @Override | |
276 | public void run(IProgressMonitor monitor) | |
277 | throws InvocationTargetException, InterruptedException { | |
278 | monitor.beginTask( | |
279 | Messages.ImportTracePackageWizardPage_ReadingPackage, | |
280 | 10); | |
281 | generateManifestOperation.run(monitor); | |
282 | monitor.done(); | |
283 | } | |
284 | }); | |
285 | ||
286 | status[0] = generateManifestOperation.getStatus(); | |
287 | if (status[0].getSeverity() == IStatus.ERROR) { | |
288 | handleErrorStatus(status[0]); | |
289 | return null; | |
290 | } | |
08d802ea | 291 | return generateManifestOperation.getResultElements(); |
417a4110 BH |
292 | } catch (InvocationTargetException e1) { |
293 | handleError( | |
294 | Messages.TracePackageExtractManifestOperation_ErrorReadingManifest, | |
295 | e1); | |
417a4110 BH |
296 | } catch (InterruptedException e1) { |
297 | // Canceled | |
417a4110 | 298 | } |
08d802ea | 299 | return null; |
417a4110 BH |
300 | } |
301 | ||
302 | /** | |
303 | * Method to set input data for this wizard | |
304 | * | |
305 | * @param profile | |
306 | * The remote profile | |
307 | * @param overwriteAll | |
308 | * Overwrite existing traces without confirmation | |
309 | */ | |
310 | public void setPageData(RemoteImportProfileElement profile, boolean overwriteAll) { | |
311 | fProfile = profile; | |
312 | fOverwriteAll = overwriteAll; | |
313 | } | |
314 | ||
315 | /** | |
316 | * Finishes the wizard page. | |
317 | * | |
318 | * @return <code>true</code> if successful else <code>false</code> | |
319 | */ | |
320 | public boolean finish() { | |
321 | ||
322 | if (!fIsVisible) { | |
323 | boolean result = updateViewer(); | |
324 | if (!result) { | |
325 | return false; | |
326 | } | |
327 | } | |
328 | ||
329 | Object[] elements = getElementViewer().getCheckedElements(); | |
330 | final RemoteImportTracesOperation importOperation = new RemoteImportTracesOperation(getContainer().getShell(), fTmfTraceFolder, elements, fOverwriteAll); | |
331 | ||
332 | try { | |
333 | getContainer().run(true, true, new IRunnableWithProgress() { | |
334 | @Override | |
335 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
336 | importOperation.run(monitor); | |
337 | monitor.done(); | |
338 | } | |
339 | }); | |
340 | } catch (InvocationTargetException e) { | |
341 | handleError( | |
342 | Messages.TracePackageExtractManifestOperation_ErrorReadingManifest, | |
343 | e); | |
344 | return false; | |
345 | } catch (InterruptedException e) { | |
346 | // Cancelled | |
347 | return false; | |
348 | } | |
349 | ||
350 | IStatus status = importOperation.getStatus(); | |
351 | if (status.getSeverity() == IStatus.ERROR) { | |
352 | handleErrorStatus(status); | |
353 | return false; | |
354 | } | |
355 | ||
356 | disconnectAllRemoteHosts(); | |
357 | return true; | |
358 | } | |
359 | ||
360 | /** | |
361 | * Cancels the the wizard and disconnects all open connections. | |
362 | */ | |
363 | public void cancel() { | |
364 | disconnectAllRemoteHosts(); | |
365 | } | |
366 | ||
367 | // ------------------------------------------------------------------------ | |
368 | // Helper Methods | |
369 | // ------------------------------------------------------------------------ | |
370 | private void disconnectAllRemoteHosts() { | |
371 | for (RemoteImportConnectionNodeElement remoteHost : fRemoteHosts) { | |
372 | remoteHost.disconnect(); | |
373 | } | |
374 | } | |
375 | ||
376 | private boolean updateViewer() { | |
377 | Object elementViewerInput = createElementViewerInput(); | |
378 | if (elementViewerInput == null) { | |
379 | return false; | |
380 | } | |
381 | ||
382 | CheckboxTreeViewer elementViewer = getElementViewer(); | |
383 | elementViewer.setInput(elementViewerInput); | |
384 | elementViewer.expandToLevel(3); | |
385 | setAllChecked(elementViewer, false, true); | |
386 | updatePageCompletion(); | |
387 | ||
388 | return true; | |
389 | } | |
390 | ||
391 | } |