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
;
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
;
54 * <b><u>ImportDialog</u></b>
56 * Dialog box for collecting trace import information.
59 public class ImportDialog
extends Dialog
implements IImportDialog
{
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
65 * The icon file for this dialog box.
67 public static final String IMPORT_ICON_FILE
= "icons/elcl16/import_trace.gif"; //$NON-NLS-1$
69 public static final String UST_PARENT_DIRECTORY
= "ust"; //$NON-NLS-1$
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
75 * The dialog composite.
77 private Composite fDialogComposite
= null;
79 * The checkbox tree viewer for selecting available traces
81 private CheckboxTreeViewer fFolderViewer
;
83 * The combo box for selecting a project.
85 private CCombo fCombo
;
87 * The overwrite button
89 private Button fOverwriteButton
;
91 * List of available LTTng 2.0 projects
93 private List
<IProject
> fProjects
;
95 * The parent where the new node should be added.
97 private TraceSessionComponent fSession
= null;
99 * List of traces to import
101 private List
<ImportFileInfo
> fTraces
= new ArrayList
<ImportFileInfo
>();
103 * Selection index in project combo box.
105 private int fProjectIndex
;
107 * Flag to indicate that something went wrong when creating the dialog box.
109 private boolean fIsError
= false;
111 // ------------------------------------------------------------------------
113 // ------------------------------------------------------------------------
116 * @param shell - a shell for the display of the dialog
118 public ImportDialog(Shell shell
) {
120 setShellStyle(SWT
.RESIZE
);
123 // ------------------------------------------------------------------------
125 // ------------------------------------------------------------------------
128 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog#getTracePathes()
131 public List
<ImportFileInfo
> getTracePathes() {
132 List
<ImportFileInfo
> retList
= new ArrayList
<ImportFileInfo
>();
133 retList
.addAll(fTraces
);
139 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog#getProject()
142 public IProject
getProject() {
143 return fProjects
.get(fProjectIndex
);
148 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog#setSession(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent)
151 public void setSession(TraceSessionComponent session
) {
155 // ------------------------------------------------------------------------
157 // ------------------------------------------------------------------------
160 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
163 protected void configureShell(Shell newShell
) {
164 super.configureShell(newShell
);
165 newShell
.setText(Messages
.TraceControl_ImportDialogTitle
);
166 newShell
.setImage(Activator
.getDefault().loadIcon(IMPORT_ICON_FILE
));
171 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
174 protected Control
createDialogArea(Composite parent
) {
177 fDialogComposite
= new Composite(parent
, SWT
.NONE
);
178 GridLayout layout
= new GridLayout(1, true);
179 fDialogComposite
.setLayout(layout
);
180 fDialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
182 Group contextGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
183 contextGroup
.setText(Messages
.TraceControl_ImportDialogTracesGroupName
);
184 layout
= new GridLayout(1, true);
185 contextGroup
.setLayout(layout
);
186 contextGroup
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
188 IRemoteSystemProxy proxy
= fSession
.getTargetNode().getRemoteSystemProxy();
190 IFileServiceSubSystem fsss
= proxy
.getFileServiceSubSystem();
193 IRemoteFile remoteFolder
= fsss
.getRemoteFileObject(fSession
.getSessionPath(), new NullProgressMonitor());
195 fFolderViewer
= new CheckboxTreeViewer(contextGroup
, SWT
.BORDER
);
196 GridData data
= new GridData(GridData
.FILL_BOTH
);
197 Tree tree
= fFolderViewer
.getTree();
198 tree
.setLayoutData(data
);
199 tree
.setFont(parent
.getFont());
200 tree
.setToolTipText(Messages
.TraceControl_ImportDialogTracesTooltip
);
202 fFolderViewer
.setContentProvider(new FolderContentProvider());
203 fFolderViewer
.setLabelProvider(new WorkbenchLabelProvider());
205 fFolderViewer
.addCheckStateListener(new ICheckStateListener() {
207 public void checkStateChanged(CheckStateChangedEvent event
) {
208 Object elem
= event
.getElement();
209 if (elem
instanceof IRemoteFile
) {
210 IRemoteFile element
= (IRemoteFile
) elem
;
211 if (!element
.isDirectory()) {
212 // A trick to keep selection of a file in sync with the directory
213 boolean p
= fFolderViewer
.getChecked((element
.getParentRemoteFile()));
214 fFolderViewer
.setChecked(element
, p
);
217 fFolderViewer
.setSubtreeChecked(event
.getElement(), event
.getChecked());
218 if (!event
.getChecked()) {
219 fFolderViewer
.setChecked(element
.getParentRemoteFile(), false);
224 fFolderViewer
.setInput(remoteFolder
);
226 Group projectGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
227 projectGroup
.setText(Messages
.TraceControl_ImportDialogProjectsGroupName
);
228 layout
= new GridLayout(1, true);
229 projectGroup
.setLayout(layout
);
230 projectGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
232 fProjects
= new ArrayList
<IProject
>();
233 List
<String
> projectNames
= new ArrayList
<String
>();
234 for (IProject project
: ResourcesPlugin
.getWorkspace().getRoot().getProjects()) {
236 if (project
.isOpen() && project
.hasNature(TmfProjectNature
.ID
)) {
237 fProjects
.add(project
);
238 projectNames
.add(project
.getName());
240 } catch (CoreException e
) {
241 createErrorComposite(parent
, e
.fillInStackTrace());
242 return fDialogComposite
;
246 fCombo
= new CCombo(projectGroup
, SWT
.READ_ONLY
);
247 fCombo
.setToolTipText(Messages
.TraceControl_ImportDialogProjectsTooltip
);
248 fCombo
.setLayoutData(new GridData(GridData
.FILL
, GridData
.CENTER
, true, false, 1, 1));
249 fCombo
.setItems(projectNames
.toArray(new String
[projectNames
.size()]));
251 Group overrideGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
252 layout
= new GridLayout(1, true);
253 overrideGroup
.setLayout(layout
);
254 overrideGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
256 fOverwriteButton
= new Button(overrideGroup
, SWT
.CHECK
);
257 fOverwriteButton
.setText(Messages
.TraceControl_ImportDialogOverwriteButtonText
);
259 getShell().setMinimumSize(new Point(500, 400));
262 } catch (SystemMessageException e
) {
263 createErrorComposite(parent
, e
.fillInStackTrace());
264 return fDialogComposite
;
267 return fDialogComposite
;
272 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
275 protected void createButtonsForButtonBar(Composite parent
) {
276 createButton(parent
, IDialogConstants
.CANCEL_ID
, "&Cancel", true); //$NON-NLS-1$
277 createButton(parent
, IDialogConstants
.OK_ID
, "&Ok", true); //$NON-NLS-1$
282 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
285 protected void okPressed() {
287 // Validate input data
290 fProjectIndex
= fCombo
.getSelectionIndex();
292 if (fProjectIndex
< 0) {
293 MessageDialog
.openError(getShell(),
294 Messages
.TraceControl_ImportDialogTitle
,
295 Messages
.TraceControl_ImportDialogNoProjectSelectedError
);
299 IProject project
= fProjects
.get(fProjectIndex
);
300 IFolder traceFolder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
302 if (!traceFolder
.exists()) {
303 // Invalid LTTng 2.0 project
304 MessageDialog
.openError(getShell(),
305 Messages
.TraceControl_ImportDialogTitle
,
306 Messages
.TraceControl_ImportDialogInvalidTracingProject
+ " (" + TmfTraceFolder
.TRACE_FOLDER_NAME
+ ")"); //$NON-NLS-1$//$NON-NLS-2$
310 boolean overwriteAll
= fOverwriteButton
.getSelection();
312 Object
[] checked
= fFolderViewer
.getCheckedElements();
313 for (int i
= 0; i
< checked
.length
; i
++) {
314 IRemoteFile file
= (IRemoteFile
) checked
[i
];
316 // Only add actual trace directories
317 if (file
.isDirectory() && !UST_PARENT_DIRECTORY
.equals(file
.getName())) {
319 ImportFileInfo info
= new ImportFileInfo(file
, file
.getName(), overwriteAll
);
320 String traceName
= info
.getLocalTraceName();
321 IFolder folder
= traceFolder
.getFolder(traceName
);
323 // Verify if trace directory already exists (and not overwrite)
324 if (folder
.exists() && !overwriteAll
) {
326 // Ask user for overwrite or new name
327 IImportConfirmationDialog conf
= TraceControlDialogFactory
.getInstance().getImportConfirmationDialog();
328 conf
.setTraceName(traceName
);
330 // Don't add trace to list if dialog was cancelled.
331 if (conf
.open() == Window
.OK
) {
332 info
.setOverwrite(conf
.isOverwrite());
333 if (!conf
.isOverwrite()) {
334 info
.setLocalTraceName(conf
.getNewTraceName());
344 if (fTraces
.isEmpty()) {
345 MessageDialog
.openError(getShell(),
346 Messages
.TraceControl_ImportDialogTitle
,
347 Messages
.TraceControl_ImportDialogNoTraceSelectedError
);
352 // validation successful -> call super.okPressed()
356 // ------------------------------------------------------------------------
357 // Helper methods and classes
358 // ------------------------------------------------------------------------
359 public static class FolderContentProvider
extends WorkbenchContentProvider
{
361 public Object
[] getChildren(Object o
) {
362 if (o
instanceof IRemoteFile
) {
363 IRemoteFile element
= (IRemoteFile
) o
;
364 // For our purpose, we need folders + files
365 if (!element
.isDirectory()) {
366 return new Object
[0];
369 return super.getChildren(o
);
374 * Creates a dialog composite with an error message which can be used
375 * when an exception occurred during creation time of the dialog box.
376 * @param parent - a parent composite
377 * @param e - a error causing exception
379 private void createErrorComposite(Composite parent
, Throwable e
) {
381 fDialogComposite
.dispose();
383 fDialogComposite
= new Composite(parent
, SWT
.NONE
);
384 GridLayout layout
= new GridLayout(1, true);
385 fDialogComposite
.setLayout(layout
);
386 fDialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
388 Text errorText
= new Text(fDialogComposite
, SWT
.MULTI
);
389 StringBuffer error
= new StringBuffer();
390 error
.append(Messages
.TraceControl_ImportDialogCreationError
);
391 error
.append(System
.getProperty("line.separator")); //$NON-NLS-1$
392 error
.append(System
.getProperty("line.separator")); //$NON-NLS-1$
393 error
.append(e
.toString());
394 errorText
.setText(error
.toString());
395 errorText
.setLayoutData(new GridData(GridData
.FILL_BOTH
));