1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.lang
.reflect
.InvocationTargetException
;
18 import java
.text
.MessageFormat
;
19 import java
.util
.ArrayList
;
20 import java
.util
.HashSet
;
21 import java
.util
.List
;
24 import org
.eclipse
.core
.resources
.IFolder
;
25 import org
.eclipse
.core
.resources
.IProject
;
26 import org
.eclipse
.core
.runtime
.CoreException
;
27 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
28 import org
.eclipse
.core
.runtime
.IStatus
;
29 import org
.eclipse
.core
.runtime
.Status
;
30 import org
.eclipse
.core
.runtime
.SubMonitor
;
31 import org
.eclipse
.jdt
.annotation
.Nullable
;
32 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
33 import org
.eclipse
.jface
.viewers
.CheckboxTreeViewer
;
34 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
35 import org
.eclipse
.jface
.viewers
.ViewerSorter
;
36 import org
.eclipse
.swt
.SWT
;
37 import org
.eclipse
.swt
.custom
.CCombo
;
38 import org
.eclipse
.swt
.events
.SelectionAdapter
;
39 import org
.eclipse
.swt
.events
.SelectionEvent
;
40 import org
.eclipse
.swt
.graphics
.Image
;
41 import org
.eclipse
.swt
.layout
.GridData
;
42 import org
.eclipse
.swt
.layout
.GridLayout
;
43 import org
.eclipse
.swt
.widgets
.Composite
;
44 import org
.eclipse
.swt
.widgets
.Group
;
45 import org
.eclipse
.swt
.widgets
.ToolBar
;
46 import org
.eclipse
.swt
.widgets
.ToolItem
;
47 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.Activator
;
48 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.messages
.RemoteMessages
;
49 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.model
.RemoteGenerateManifestOperation
;
50 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.model
.RemoteImportConnectionNodeElement
;
51 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.model
.RemoteImportProfileElement
;
52 import org
.eclipse
.tracecompass
.internal
.tmf
.remote
.ui
.wizards
.fetch
.model
.RemoteImportTracesOperation
;
53 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.AbstractTracePackageOperation
;
54 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.AbstractTracePackageWizardPage
;
55 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageElement
;
56 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageFilesElement
;
57 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageLabelProvider
;
58 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageTraceElement
;
59 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.importexport
.Messages
;
60 import org
.eclipse
.tracecompass
.tmf
.core
.TmfProjectNature
;
61 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectElement
;
62 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
63 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
64 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
65 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TraceUtils
;
66 import org
.eclipse
.ui
.ISharedImages
;
67 import org
.eclipse
.ui
.PlatformUI
;
68 import org
.eclipse
.ui
.plugin
.AbstractUIPlugin
;
71 * Wizard page that connects to a remote node, lists all traces according to
72 * a given remote profile and imports the selected traces.
74 * @author Marc-Andre Laperle
75 * @author Bernd Hufmann
77 public class RemoteFetchLogWizardRemotePage
extends AbstractTracePackageWizardPage
{
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
83 private static final String PAGE_NAME
= "org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch"; //$NON-NLS-1$
84 private static final String ICON_PATH
= "icons/elcl16/fetch_log_wiz.gif"; //$NON-NLS-1$
85 private static final Image COLLAPSE_ALL_IMAGE
= PlatformUI
.getWorkbench().getSharedImages().getImage(ISharedImages
.IMG_ELCL_COLLAPSEALL
);
86 private static final Image EXPAND_ALL_IMAGE
= Activator
.getDefault().getImageFromPath("icons/elcl16/expandall.png"); //$NON-NLS-1$
87 /** Name of default project to import traces to */
88 public static final String DEFAULT_REMOTE_PROJECT_NAME
= "Remote"; //$NON-NLS-1$
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
93 private TmfTraceFolder fTmfTraceFolder
;
95 private RemoteImportProfileElement fProfile
;
96 private final Set
<RemoteImportConnectionNodeElement
> fRemoteHosts
= new HashSet
<>();
97 private boolean fOverwriteAll
;
98 private boolean fIsVisible
= false;
99 private String fDefaultProjectName
= null;
100 private CCombo fCombo
;
101 private List
<IProject
> fProjects
;
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
112 * The current selection (trace folder element)
114 * A profile to use or null
116 protected RemoteFetchLogWizardRemotePage(String title
, IStructuredSelection selection
, @Nullable RemoteImportProfileElement profile
) {
117 super(PAGE_NAME
, title
, null, selection
);
118 setImageDescriptor(AbstractUIPlugin
.imageDescriptorFromPlugin(Activator
.PLUGIN_ID
, ICON_PATH
));
120 if (selection
.getFirstElement() instanceof TmfTraceFolder
) {
121 fTmfTraceFolder
= (TmfTraceFolder
) selection
.getFirstElement();
124 if (fTmfTraceFolder
== null) {
125 // create default project
126 TmfProjectRegistry
.createProject(DEFAULT_REMOTE_PROJECT_NAME
, null, null);
127 fDefaultProjectName
= DEFAULT_REMOTE_PROJECT_NAME
;
131 setDescription(RemoteMessages
.RemoteFetchLogWizardRemotePage_Description
);
134 // ------------------------------------------------------------------------
136 // ------------------------------------------------------------------------
138 public void createControl(Composite parent
) {
139 initializeDialogUnits(parent
);
141 Composite composite
= new Composite(parent
, SWT
.NULL
);
142 composite
.setLayout(new GridLayout());
143 composite
.setLayoutData(new GridData(GridData
.VERTICAL_ALIGN_FILL
144 | GridData
.HORIZONTAL_ALIGN_FILL
));
145 composite
.setFont(parent
.getFont());
147 createToolBar(composite
);
148 createElementViewer(composite
);
150 createButtonsGroup(composite
);
152 createProjectGroup(composite
);
154 restoreWidgetValues();
155 updatePageCompletion();
157 setControl(composite
);
161 protected boolean determinePageCompletion() {
162 return getElementViewer().getCheckedElements().length
> 0;
166 protected void updateWithFilePathSelection() {
170 public void setVisible(final boolean visible
) {
171 super.setVisible(visible
);
173 fIsVisible
= visible
;
176 getContainer().getShell().getDisplay().asyncExec(new Runnable() {
183 getElementViewer().setInput(null);
187 private void createToolBar(Composite parent
) {
188 ToolBar toolBar
= new ToolBar(parent
, SWT
.HORIZONTAL
);
189 toolBar
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
191 ToolItem expandAll
= new ToolItem(toolBar
, SWT
.PUSH
);
192 expandAll
.setImage(EXPAND_ALL_IMAGE
);
193 expandAll
.setToolTipText(RemoteMessages
.RemoteFetchLogWizardRemotePage_ExpandAll
);
194 expandAll
.addSelectionListener(new SelectionAdapter() {
196 public void widgetSelected(SelectionEvent e
) {
197 getElementViewer().expandAll();
201 ToolItem collapseAll
= new ToolItem(toolBar
, SWT
.PUSH
);
202 collapseAll
.setImage(COLLAPSE_ALL_IMAGE
);
203 collapseAll
.setToolTipText(RemoteMessages
.RemoteFetchLogWizardRemotePage_CollapseAll
);
204 collapseAll
.addSelectionListener(new SelectionAdapter() {
206 public void widgetSelected(SelectionEvent e
) {
207 getElementViewer().collapseAll();
213 protected void createElementViewer(Composite compositeParent
) {
214 super.createElementViewer(compositeParent
);
216 CheckboxTreeViewer elementViewer
= getElementViewer();
217 elementViewer
.setLabelProvider(new TracePackageLabelProvider() {
219 public String
getText(Object element
) {
220 if (element
instanceof TracePackageTraceElement
) {
221 for (TracePackageElement files
: ((TracePackageTraceElement
) element
).getChildren()) {
222 if (files
instanceof TracePackageFilesElement
) {
223 return ((TracePackageFilesElement
) files
).getFileName();
227 return super.getText(element
);
230 public Image
getImage(Object element
) {
231 if (element
instanceof TracePackageTraceElement
) {
232 for (TracePackageElement files
: ((TracePackageTraceElement
) element
).getChildren()) {
233 return files
.getImage();
236 return super.getImage(element
);
239 elementViewer
.setSorter(new ViewerSorter() {
241 public int category(Object element
) {
242 if (element
instanceof TracePackageTraceElement
) {
250 private void createProjectGroup(Composite parent
) {
251 if (fDefaultProjectName
!= null) {
252 Group projectGroup
= new Group(parent
, SWT
.SHADOW_NONE
);
253 projectGroup
.setText(RemoteMessages
.RemoteFetchLogWizardRemotePage_ImportDialogProjectsGroupName
);
254 GridLayout layout
= new GridLayout(1, true);
255 projectGroup
.setLayout(layout
);
256 projectGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
258 fProjects
= new ArrayList
<>();
259 List
<String
> projectNames
= new ArrayList
<>();
261 for (IProject project
: TraceUtils
.getOpenedTmfProjects()) {
262 fProjects
.add(project
);
263 projectNames
.add(project
.getName());
266 fCombo
= new CCombo(projectGroup
, SWT
.READ_ONLY
);
267 fCombo
.setToolTipText(RemoteMessages
.RemoteFetchLogWizardRemotePage_ImportDialogProjectsGroupName
);
268 fCombo
.setLayoutData(new GridData(GridData
.FILL
, GridData
.CENTER
, true, false, 1, 1));
269 fCombo
.setItems(projectNames
.toArray(new String
[projectNames
.size()]));
270 int select
= projectNames
.indexOf(fDefaultProjectName
);
271 fCombo
.select(select
);
276 protected Object
createElementViewerInput() {
277 if (fProfile
== null) {
280 final List
<RemoteImportConnectionNodeElement
> remoteHosts
= fProfile
.getConnectionNodeElements();
282 if (remoteHosts
.size() == 0) {
283 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, RemoteMessages
.RemoteFetchLogWizardRemotePage_MissingConnectionInformation
);
286 final IStatus status
[] = new IStatus
[1];
288 getContainer().run(true, true, new IRunnableWithProgress() {
290 public void run(IProgressMonitor monitor
) throws InvocationTargetException
, InterruptedException
{
291 SubMonitor subMonitor
= SubMonitor
.convert(monitor
, remoteHosts
.size());
292 for (final RemoteImportConnectionNodeElement remoteHost
: remoteHosts
) {
293 SubMonitor child
= subMonitor
.newChild(1);
294 child
.setTaskName(MessageFormat
.format(RemoteMessages
.RemoteFetchLogWizardRemotePage_OpeningConnectionTo
, remoteHost
.getURI()));
295 status
[0] = remoteHost
.connect(checkNotNull(subMonitor
.newChild(1)));
296 if (!status
[0].isOK()) {
301 fRemoteHosts
.add(remoteHost
);
306 } catch (InvocationTargetException e
) {
308 MessageFormat
.format(RemoteMessages
.RemoteFetchLogWizardRemotePage_ConnectionError
, fProfile
.getText()),
311 } catch (InterruptedException e
) {
313 status
[0] = Status
.CANCEL_STATUS
;
316 if (!status
[0].isOK()) {
317 handleErrorStatus(status
[0]);
322 final AbstractTracePackageOperation generateManifestOperation
= new RemoteGenerateManifestOperation(fProfile
);
324 getContainer().run(true, true, new IRunnableWithProgress() {
326 public void run(IProgressMonitor monitor
)
327 throws InvocationTargetException
, InterruptedException
{
329 Messages
.ImportTracePackageWizardPage_ReadingPackage
,
331 generateManifestOperation
.run(monitor
);
336 status
[0] = generateManifestOperation
.getStatus();
337 if (status
[0].getSeverity() == IStatus
.ERROR
) {
338 handleErrorStatus(status
[0]);
341 return generateManifestOperation
.getResultElements();
342 } catch (InvocationTargetException e1
) {
344 Messages
.TracePackageExtractManifestOperation_ErrorReadingManifest
,
346 } catch (InterruptedException e1
) {
353 * Method to set input data for this wizard
357 * @param overwriteAll
358 * Overwrite existing traces without confirmation
360 public void setPageData(RemoteImportProfileElement profile
, boolean overwriteAll
) {
362 fOverwriteAll
= overwriteAll
;
366 * Finishes the wizard page.
368 * @return <code>true</code> if successful else <code>false</code>
370 public boolean finish() {
374 result
= updateViewer();
380 result
= validateProject();
385 Object
[] elements
= getElementViewer().getCheckedElements();
386 final RemoteImportTracesOperation importOperation
= new RemoteImportTracesOperation(getContainer().getShell(), fTmfTraceFolder
, elements
, fOverwriteAll
);
389 getContainer().run(true, true, new IRunnableWithProgress() {
391 public void run(IProgressMonitor monitor
) throws InvocationTargetException
, InterruptedException
{
392 importOperation
.run(monitor
);
396 } catch (InvocationTargetException e
) {
398 Messages
.TracePackageExtractManifestOperation_ErrorReadingManifest
,
401 } catch (InterruptedException e
) {
406 IStatus status
= importOperation
.getStatus();
407 if (status
.getSeverity() == IStatus
.ERROR
) {
408 handleErrorStatus(status
);
412 disconnectAllRemoteHosts();
417 * Cancels the the wizard and disconnects all open connections.
419 public void cancel() {
420 disconnectAllRemoteHosts();
423 // ------------------------------------------------------------------------
425 // ------------------------------------------------------------------------
426 private void disconnectAllRemoteHosts() {
427 for (RemoteImportConnectionNodeElement remoteHost
: fRemoteHosts
) {
428 remoteHost
.disconnect();
432 private boolean updateViewer() {
433 Object elementViewerInput
= createElementViewerInput();
434 if (elementViewerInput
== null) {
438 CheckboxTreeViewer elementViewer
= getElementViewer();
439 elementViewer
.setInput(elementViewerInput
);
440 elementViewer
.expandToLevel(3);
441 setAllChecked(elementViewer
, false, true);
442 updatePageCompletion();
447 private boolean validateProject() {
448 if (fCombo
!= null) {
449 int fProjectIndex
= fCombo
.getSelectionIndex();
450 if (fProjectIndex
< 0) {
451 handleError(RemoteMessages
.RemoteFetchLogWizardRemotePage_NoProjectSelectedError
, null);
455 IProject project
= fProjects
.get(fProjectIndex
);
456 IFolder traceFolder
= project
.getFolder(TmfTracesFolder
.TRACES_FOLDER_NAME
);
458 if (!traceFolder
.exists()) {
459 handleError(RemoteMessages
.RemoteFetchLogWizardRemotePage_InvalidTracingProject
, null);
464 if (project
.hasNature(TmfProjectNature
.ID
)) {
465 TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
466 fTmfTraceFolder
= projectElement
.getTracesFolder();
468 } catch (CoreException ex
) {
469 handleError(RemoteMessages
.RemoteFetchLogWizardRemotePage_InvalidTracingProject
, ex
);