Add support for importing traces to tracing project
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / dialogs / ImportDialog.java
1 /**********************************************************************
2 * Copyright (c) 2012 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 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
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;
52
53 /**
54 * <b><u>ImportDialog</u></b>
55 * <p>
56 * Dialog box for collecting trace import information.
57 * </p>
58 */
59 public class ImportDialog extends Dialog implements IImportDialog {
60
61 // ------------------------------------------------------------------------
62 // Constants
63 // ------------------------------------------------------------------------
64 /**
65 * The icon file for this dialog box.
66 */
67 public static final String IMPORT_ICON_FILE = "icons/elcl16/import_trace.gif"; //$NON-NLS-1$
68
69 public static final String UST_PARENT_DIRECTORY = "ust"; //$NON-NLS-1$
70
71 // ------------------------------------------------------------------------
72 // Attributes
73 // ------------------------------------------------------------------------
74 /**
75 * The dialog composite.
76 */
77 private Composite fDialogComposite = null;
78 /**
79 * The checkbox tree viewer for selecting available traces
80 */
81 private CheckboxTreeViewer fFolderViewer;
82 /**
83 * The combo box for selecting a project.
84 */
85 private CCombo fCombo;
86 /**
87 * The overwrite button
88 */
89 private Button fOverwriteButton;
90 /**
91 * List of available LTTng 2.0 projects
92 */
93 private List<IProject> fProjects;
94 /**
95 * The parent where the new node should be added.
96 */
97 private TraceSessionComponent fSession = null;
98 /**
99 * List of traces to import
100 */
101 private List<ImportFileInfo> fTraces = new ArrayList<ImportFileInfo>();
102 /**
103 * Selection index in project combo box.
104 */
105 private int fProjectIndex;
106 /**
107 * Flag to indicate that something went wrong when creating the dialog box.
108 */
109 private boolean fIsError = false;
110
111 // ------------------------------------------------------------------------
112 // Constructors
113 // ------------------------------------------------------------------------
114 /**
115 * Constructor
116 * @param shell - a shell for the display of the dialog
117 */
118 public ImportDialog(Shell shell) {
119 super(shell);
120 setShellStyle(SWT.RESIZE);
121 }
122
123 // ------------------------------------------------------------------------
124 // Accessors
125 // ------------------------------------------------------------------------
126 /*
127 * (non-Javadoc)
128 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog#getTracePathes()
129 */
130 @Override
131 public List<ImportFileInfo> getTracePathes() {
132 List<ImportFileInfo> retList = new ArrayList<ImportFileInfo>();
133 retList.addAll(fTraces);
134 return retList;
135 }
136
137 /*
138 * (non-Javadoc)
139 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog#getProject()
140 */
141 @Override
142 public IProject getProject() {
143 return fProjects.get(fProjectIndex);
144 }
145
146 /*
147 * (non-Javadoc)
148 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog#setSession(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent)
149 */
150 @Override
151 public void setSession(TraceSessionComponent session) {
152 fSession = session;
153 }
154
155 // ------------------------------------------------------------------------
156 // Operations
157 // ------------------------------------------------------------------------
158 /*
159 * (non-Javadoc)
160 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
161 */
162 @Override
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));
167 }
168
169 /*
170 * (non-Javadoc)
171 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
172 */
173 @Override
174 protected Control createDialogArea(Composite parent) {
175
176 // Main dialog panel
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));
181
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));
187
188 IRemoteSystemProxy proxy = fSession.getTargetNode().getRemoteSystemProxy();
189
190 IFileServiceSubSystem fsss = proxy.getFileServiceSubSystem();
191
192 try {
193 IRemoteFile remoteFolder = fsss.getRemoteFileObject(fSession.getSessionPath(), new NullProgressMonitor());
194
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);
201
202 fFolderViewer.setContentProvider(new FolderContentProvider());
203 fFolderViewer.setLabelProvider(new WorkbenchLabelProvider());
204
205 fFolderViewer.addCheckStateListener(new ICheckStateListener() {
206 @Override
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);
215 return;
216 }
217 fFolderViewer.setSubtreeChecked(event.getElement(), event.getChecked());
218 if (!event.getChecked()) {
219 fFolderViewer.setChecked(element.getParentRemoteFile(), false);
220 }
221 }
222 }
223 });
224 fFolderViewer.setInput(remoteFolder);
225
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));
231
232 fProjects = new ArrayList<IProject>();
233 List<String> projectNames = new ArrayList<String>();
234 for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
235 try {
236 if (project.isOpen() && project.hasNature(TmfProjectNature.ID)) {
237 fProjects.add(project);
238 projectNames.add(project.getName());
239 }
240 } catch (CoreException e) {
241 createErrorComposite(parent, e.fillInStackTrace());
242 return fDialogComposite;
243 }
244 }
245
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()]));
250
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));
255
256 fOverwriteButton = new Button(overrideGroup, SWT.CHECK);
257 fOverwriteButton.setText(Messages.TraceControl_ImportDialogOverwriteButtonText);
258
259 getShell().setMinimumSize(new Point(500, 400));
260
261
262 } catch (SystemMessageException e) {
263 createErrorComposite(parent, e.fillInStackTrace());
264 return fDialogComposite;
265 }
266
267 return fDialogComposite;
268 }
269
270 /*
271 * (non-Javadoc)
272 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
273 */
274 @Override
275 protected void createButtonsForButtonBar(Composite parent) {
276 createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$
277 }
278
279 /*
280 * (non-Javadoc)
281 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
282 */
283 @Override
284 protected void okPressed() {
285 if (!fIsError) {
286 // Validate input data
287 fTraces.clear();
288
289 fProjectIndex = fCombo.getSelectionIndex();
290
291 if (fProjectIndex < 0) {
292 MessageDialog.openError(getShell(),
293 Messages.TraceControl_ImportDialogTitle,
294 Messages.TraceControl_ImportDialogNoProjectSelectedError);
295 return;
296 }
297
298 IProject project = fProjects.get(fProjectIndex);
299 IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME);
300
301 if (!traceFolder.exists()) {
302 // Invalid LTTng 2.0 project
303 MessageDialog.openError(getShell(),
304 Messages.TraceControl_ImportDialogTitle,
305 Messages.TraceControl_ImportDialogInvalidTracingProject + " (" + TmfTraceFolder.TRACE_FOLDER_NAME + ")"); //$NON-NLS-1$//$NON-NLS-2$
306 return;
307 }
308
309 boolean overwriteAll = fOverwriteButton.getSelection();
310
311 Object[] checked = fFolderViewer.getCheckedElements();
312 for (int i = 0; i < checked.length; i++) {
313 IRemoteFile file = (IRemoteFile) checked[i];
314
315 // Only add actual trace directories
316 if (file.isDirectory() && !UST_PARENT_DIRECTORY.equals(file.getName())) {
317
318 ImportFileInfo info = new ImportFileInfo(file, file.getName(), overwriteAll);
319 String traceName = info.getLocalTraceName();
320 IFolder folder = traceFolder.getFolder(traceName);
321
322 // Verify if trace directory already exists (and not overwrite)
323 if (folder.exists() && !overwriteAll) {
324
325 // Ask user for overwrite or new name
326 IImportConfirmationDialog conf = TraceControlDialogFactory.getInstance().getImportConfirmationDialog();
327 conf.setTraceName(traceName);
328
329 // Don't add trace to list if dialog was cancelled.
330 if (conf.open() == Window.OK) {
331 info.setOverwrite(conf.isOverwrite());
332 if (!conf.isOverwrite()) {
333 info.setLocalTraceName(conf.getNewTraceName());
334 }
335 fTraces.add(info);
336 }
337 } else {
338 fTraces.add(info);
339 }
340 }
341 }
342
343 if (fTraces.isEmpty()) {
344 MessageDialog.openError(getShell(),
345 Messages.TraceControl_ImportDialogTitle,
346 Messages.TraceControl_ImportDialogNoTraceSelectedError);
347 return;
348 }
349 }
350
351 // validation successful -> call super.okPressed()
352 super.okPressed();
353 }
354
355 // ------------------------------------------------------------------------
356 // Helper methods and classes
357 // ------------------------------------------------------------------------
358 public class FolderContentProvider extends WorkbenchContentProvider {
359 @Override
360 public Object[] getChildren(Object o) {
361 if (o instanceof IRemoteFile) {
362 IRemoteFile element = (IRemoteFile) o;
363 // For our purpose, we need folders + files
364 if (!element.isDirectory()) {
365 return new Object[0];
366 }
367 }
368 return super.getChildren(o);
369 }
370 }
371
372 /**
373 * Creates a dialog composite with an error message which can be used
374 * when an exception occurred during creation time of the dialog box.
375 * @param parent - a parent composite
376 * @param e - a error causing exception
377 */
378 private void createErrorComposite(Composite parent, Throwable e) {
379 fIsError = true;
380 fDialogComposite.dispose();
381
382 fDialogComposite = new Composite(parent, SWT.NONE);
383 GridLayout layout = new GridLayout(1, true);
384 fDialogComposite.setLayout(layout);
385 fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
386
387 Text errorText = new Text(fDialogComposite, SWT.MULTI);
388 StringBuffer error = new StringBuffer();
389 error.append(Messages.TraceControl_ImportDialogCreationError);
390 error.append(System.getProperty("line.separator")); //$NON-NLS-1$
391 error.append(System.getProperty("line.separator")); //$NON-NLS-1$
392 error.append(e.toString());
393 errorText.setText(error.toString());
394 errorText.setLayoutData(new GridData(GridData.FILL_BOTH));
395 }
396
397
398 }
This page took 0.039988 seconds and 5 git commands to generate.