1 /**********************************************************************
2 * Copyright (c) 2012 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 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
;
14 import java
.util
.ArrayList
;
15 import java
.util
.List
;
17 import org
.eclipse
.core
.resources
.IFolder
;
18 import org
.eclipse
.core
.resources
.IProject
;
19 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
22 import org
.eclipse
.jface
.dialogs
.Dialog
;
23 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
24 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
25 import org
.eclipse
.jface
.viewers
.CheckStateChangedEvent
;
26 import org
.eclipse
.jface
.viewers
.CheckboxTreeViewer
;
27 import org
.eclipse
.jface
.viewers
.ICheckStateListener
;
28 import org
.eclipse
.jface
.window
.Window
;
29 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.Activator
;
30 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.messages
.Messages
;
31 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.model
.impl
.TraceSessionComponent
;
32 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.remote
.IRemoteSystemProxy
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.TmfProjectNature
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
35 import org
.eclipse
.rse
.services
.clientserver
.messages
.SystemMessageException
;
36 import org
.eclipse
.rse
.subsystems
.files
.core
.servicesubsystem
.IFileServiceSubSystem
;
37 import org
.eclipse
.rse
.subsystems
.files
.core
.subsystems
.IRemoteFile
;
38 import org
.eclipse
.swt
.SWT
;
39 import org
.eclipse
.swt
.custom
.CCombo
;
40 import org
.eclipse
.swt
.graphics
.Point
;
41 import org
.eclipse
.swt
.layout
.GridData
;
42 import org
.eclipse
.swt
.layout
.GridLayout
;
43 import org
.eclipse
.swt
.widgets
.Button
;
44 import org
.eclipse
.swt
.widgets
.Composite
;
45 import org
.eclipse
.swt
.widgets
.Control
;
46 import org
.eclipse
.swt
.widgets
.Group
;
47 import org
.eclipse
.swt
.widgets
.Shell
;
48 import org
.eclipse
.swt
.widgets
.Text
;
49 import org
.eclipse
.swt
.widgets
.Tree
;
50 import org
.eclipse
.ui
.model
.WorkbenchContentProvider
;
51 import org
.eclipse
.ui
.model
.WorkbenchLabelProvider
;
55 * Dialog box for collecting trace import information.
58 * @author Bernd Hufmann
60 public class ImportDialog
extends Dialog
implements IImportDialog
{
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 * The icon file for this dialog box.
68 public static final String IMPORT_ICON_FILE
= "icons/elcl16/import_trace.gif"; //$NON-NLS-1$
70 public static final String UST_PARENT_DIRECTORY
= "ust"; //$NON-NLS-1$
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
76 * The dialog composite.
78 private Composite fDialogComposite
= null;
80 * The checkbox tree viewer for selecting available traces
82 private CheckboxTreeViewer fFolderViewer
;
84 * The combo box for selecting a project.
86 private CCombo fCombo
;
88 * The overwrite button
90 private Button fOverwriteButton
;
92 * List of available LTTng 2.0 projects
94 private List
<IProject
> fProjects
;
96 * The parent where the new node should be added.
98 private TraceSessionComponent fSession
= null;
100 * List of traces to import
102 private List
<ImportFileInfo
> fTraces
= new ArrayList
<ImportFileInfo
>();
104 * Selection index in project combo box.
106 private int fProjectIndex
;
108 * Flag to indicate that something went wrong when creating the dialog box.
110 private boolean fIsError
= false;
112 // ------------------------------------------------------------------------
114 // ------------------------------------------------------------------------
117 * @param shell - a shell for the display of the dialog
119 public ImportDialog(Shell shell
) {
121 setShellStyle(SWT
.RESIZE
);
124 // ------------------------------------------------------------------------
126 // ------------------------------------------------------------------------
129 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog#getTracePathes()
132 public List
<ImportFileInfo
> getTracePathes() {
133 List
<ImportFileInfo
> retList
= new ArrayList
<ImportFileInfo
>();
134 retList
.addAll(fTraces
);
140 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog#getProject()
143 public IProject
getProject() {
144 return fProjects
.get(fProjectIndex
);
149 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog#setSession(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent)
152 public void setSession(TraceSessionComponent session
) {
156 // ------------------------------------------------------------------------
158 // ------------------------------------------------------------------------
161 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
164 protected void configureShell(Shell newShell
) {
165 super.configureShell(newShell
);
166 newShell
.setText(Messages
.TraceControl_ImportDialogTitle
);
167 newShell
.setImage(Activator
.getDefault().loadIcon(IMPORT_ICON_FILE
));
172 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
175 protected Control
createDialogArea(Composite parent
) {
178 fDialogComposite
= new Composite(parent
, SWT
.NONE
);
179 GridLayout layout
= new GridLayout(1, true);
180 fDialogComposite
.setLayout(layout
);
181 fDialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
183 Group contextGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
184 contextGroup
.setText(Messages
.TraceControl_ImportDialogTracesGroupName
);
185 layout
= new GridLayout(1, true);
186 contextGroup
.setLayout(layout
);
187 contextGroup
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
189 IRemoteSystemProxy proxy
= fSession
.getTargetNode().getRemoteSystemProxy();
191 IFileServiceSubSystem fsss
= proxy
.getFileServiceSubSystem();
194 IRemoteFile remoteFolder
= fsss
.getRemoteFileObject(fSession
.getSessionPath(), new NullProgressMonitor());
196 fFolderViewer
= new CheckboxTreeViewer(contextGroup
, SWT
.BORDER
);
197 GridData data
= new GridData(GridData
.FILL_BOTH
);
198 Tree tree
= fFolderViewer
.getTree();
199 tree
.setLayoutData(data
);
200 tree
.setFont(parent
.getFont());
201 tree
.setToolTipText(Messages
.TraceControl_ImportDialogTracesTooltip
);
203 fFolderViewer
.setContentProvider(new FolderContentProvider());
204 fFolderViewer
.setLabelProvider(new WorkbenchLabelProvider());
206 fFolderViewer
.addCheckStateListener(new ICheckStateListener() {
208 public void checkStateChanged(CheckStateChangedEvent event
) {
209 Object elem
= event
.getElement();
210 if (elem
instanceof IRemoteFile
) {
211 IRemoteFile element
= (IRemoteFile
) elem
;
212 if (!element
.isDirectory()) {
213 // A trick to keep selection of a file in sync with the directory
214 boolean p
= fFolderViewer
.getChecked((element
.getParentRemoteFile()));
215 fFolderViewer
.setChecked(element
, p
);
218 fFolderViewer
.setSubtreeChecked(event
.getElement(), event
.getChecked());
219 if (!event
.getChecked()) {
220 fFolderViewer
.setChecked(element
.getParentRemoteFile(), false);
225 fFolderViewer
.setInput(remoteFolder
);
227 Group projectGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
228 projectGroup
.setText(Messages
.TraceControl_ImportDialogProjectsGroupName
);
229 layout
= new GridLayout(1, true);
230 projectGroup
.setLayout(layout
);
231 projectGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
233 fProjects
= new ArrayList
<IProject
>();
234 List
<String
> projectNames
= new ArrayList
<String
>();
235 for (IProject project
: ResourcesPlugin
.getWorkspace().getRoot().getProjects()) {
237 if (project
.isOpen() && project
.hasNature(TmfProjectNature
.ID
)) {
238 fProjects
.add(project
);
239 projectNames
.add(project
.getName());
241 } catch (CoreException e
) {
242 createErrorComposite(parent
, e
.fillInStackTrace());
243 return fDialogComposite
;
247 fCombo
= new CCombo(projectGroup
, SWT
.READ_ONLY
);
248 fCombo
.setToolTipText(Messages
.TraceControl_ImportDialogProjectsTooltip
);
249 fCombo
.setLayoutData(new GridData(GridData
.FILL
, GridData
.CENTER
, true, false, 1, 1));
250 fCombo
.setItems(projectNames
.toArray(new String
[projectNames
.size()]));
252 Group overrideGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
253 layout
= new GridLayout(1, true);
254 overrideGroup
.setLayout(layout
);
255 overrideGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
257 fOverwriteButton
= new Button(overrideGroup
, SWT
.CHECK
);
258 fOverwriteButton
.setText(Messages
.TraceControl_ImportDialogOverwriteButtonText
);
260 getShell().setMinimumSize(new Point(500, 400));
263 } catch (SystemMessageException e
) {
264 createErrorComposite(parent
, e
.fillInStackTrace());
265 return fDialogComposite
;
268 return fDialogComposite
;
273 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
276 protected void createButtonsForButtonBar(Composite parent
) {
277 createButton(parent
, IDialogConstants
.CANCEL_ID
, "&Cancel", true); //$NON-NLS-1$
278 createButton(parent
, IDialogConstants
.OK_ID
, "&Ok", true); //$NON-NLS-1$
283 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
286 protected void okPressed() {
288 // Validate input data
291 fProjectIndex
= fCombo
.getSelectionIndex();
293 if (fProjectIndex
< 0) {
294 MessageDialog
.openError(getShell(),
295 Messages
.TraceControl_ImportDialogTitle
,
296 Messages
.TraceControl_ImportDialogNoProjectSelectedError
);
300 IProject project
= fProjects
.get(fProjectIndex
);
301 IFolder traceFolder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
303 if (!traceFolder
.exists()) {
304 // Invalid LTTng 2.0 project
305 MessageDialog
.openError(getShell(),
306 Messages
.TraceControl_ImportDialogTitle
,
307 Messages
.TraceControl_ImportDialogInvalidTracingProject
+ " (" + TmfTraceFolder
.TRACE_FOLDER_NAME
+ ")"); //$NON-NLS-1$//$NON-NLS-2$
311 boolean overwriteAll
= fOverwriteButton
.getSelection();
313 Object
[] checked
= fFolderViewer
.getCheckedElements();
314 for (int i
= 0; i
< checked
.length
; i
++) {
315 IRemoteFile file
= (IRemoteFile
) checked
[i
];
317 // Only add actual trace directories
318 if (file
.isDirectory() && !UST_PARENT_DIRECTORY
.equals(file
.getName())) {
320 ImportFileInfo info
= new ImportFileInfo(file
, file
.getName(), overwriteAll
);
321 String traceName
= info
.getLocalTraceName();
322 IFolder folder
= traceFolder
.getFolder(traceName
);
324 // Verify if trace directory already exists (and not overwrite)
325 if (folder
.exists() && !overwriteAll
) {
327 // Ask user for overwrite or new name
328 IImportConfirmationDialog conf
= TraceControlDialogFactory
.getInstance().getImportConfirmationDialog();
329 conf
.setTraceName(traceName
);
331 // Don't add trace to list if dialog was cancelled.
332 if (conf
.open() == Window
.OK
) {
333 info
.setOverwrite(conf
.isOverwrite());
334 if (!conf
.isOverwrite()) {
335 info
.setLocalTraceName(conf
.getNewTraceName());
345 if (fTraces
.isEmpty()) {
346 MessageDialog
.openError(getShell(),
347 Messages
.TraceControl_ImportDialogTitle
,
348 Messages
.TraceControl_ImportDialogNoTraceSelectedError
);
353 // validation successful -> call super.okPressed()
357 // ------------------------------------------------------------------------
358 // Helper methods and classes
359 // ------------------------------------------------------------------------
360 public static class FolderContentProvider
extends WorkbenchContentProvider
{
362 public Object
[] getChildren(Object o
) {
363 if (o
instanceof IRemoteFile
) {
364 IRemoteFile element
= (IRemoteFile
) o
;
365 // For our purpose, we need folders + files
366 if (!element
.isDirectory()) {
367 return new Object
[0];
370 return super.getChildren(o
);
375 * Creates a dialog composite with an error message which can be used
376 * when an exception occurred during creation time of the dialog box.
377 * @param parent - a parent composite
378 * @param e - a error causing exception
380 private void createErrorComposite(Composite parent
, Throwable e
) {
382 fDialogComposite
.dispose();
384 fDialogComposite
= new Composite(parent
, SWT
.NONE
);
385 GridLayout layout
= new GridLayout(1, true);
386 fDialogComposite
.setLayout(layout
);
387 fDialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
389 Text errorText
= new Text(fDialogComposite
, SWT
.MULTI
);
390 StringBuffer error
= new StringBuffer();
391 error
.append(Messages
.TraceControl_ImportDialogCreationError
);
392 error
.append(System
.getProperty("line.separator")); //$NON-NLS-1$
393 error
.append(System
.getProperty("line.separator")); //$NON-NLS-1$
394 error
.append(e
.toString());
395 errorText
.setText(error
.toString());
396 errorText
.setLayoutData(new GridData(GridData
.FILL_BOTH
));