ctf: Re-enable the CTF parser unit tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / dialogs / ImportDialog.java
CommitLineData
291cbdbf
BH
1/**********************************************************************
2 * Copyright (c) 2012 Ericsson
cfdb727a 3 *
291cbdbf
BH
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
cfdb727a
AM
8 *
9 * Contributors:
291cbdbf
BH
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12package org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs;
13
14import java.util.ArrayList;
15import java.util.List;
16
17import org.eclipse.core.resources.IFolder;
18import org.eclipse.core.resources.IProject;
19import org.eclipse.core.resources.ResourcesPlugin;
20import org.eclipse.core.runtime.CoreException;
21import org.eclipse.core.runtime.NullProgressMonitor;
22import org.eclipse.jface.dialogs.Dialog;
23import org.eclipse.jface.dialogs.IDialogConstants;
24import org.eclipse.jface.dialogs.MessageDialog;
25import org.eclipse.jface.viewers.CheckStateChangedEvent;
26import org.eclipse.jface.viewers.CheckboxTreeViewer;
27import org.eclipse.jface.viewers.ICheckStateListener;
28import org.eclipse.jface.window.Window;
29import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
9315aeee 30import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages;
291cbdbf
BH
31import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent;
32import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.IRemoteSystemProxy;
33import org.eclipse.linuxtools.tmf.core.TmfProjectNature;
34import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
35import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
36import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem;
37import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile;
38import org.eclipse.swt.SWT;
39import org.eclipse.swt.custom.CCombo;
40import org.eclipse.swt.graphics.Point;
41import org.eclipse.swt.layout.GridData;
42import org.eclipse.swt.layout.GridLayout;
43import org.eclipse.swt.widgets.Button;
44import org.eclipse.swt.widgets.Composite;
45import org.eclipse.swt.widgets.Control;
46import org.eclipse.swt.widgets.Group;
47import org.eclipse.swt.widgets.Shell;
48import org.eclipse.swt.widgets.Text;
49import org.eclipse.swt.widgets.Tree;
50import org.eclipse.ui.model.WorkbenchContentProvider;
51import org.eclipse.ui.model.WorkbenchLabelProvider;
52
53/**
291cbdbf
BH
54 * <p>
55 * Dialog box for collecting trace import information.
56 * </p>
cfdb727a 57 *
dbd4432d 58 * @author Bernd Hufmann
291cbdbf
BH
59 */
60public class ImportDialog extends Dialog implements IImportDialog {
61
62 // ------------------------------------------------------------------------
63 // Constants
64 // ------------------------------------------------------------------------
cfdb727a 65 /** The icon file for this dialog box. */
291cbdbf 66 public static final String IMPORT_ICON_FILE = "icons/elcl16/import_trace.gif"; //$NON-NLS-1$
cfdb727a
AM
67
68 /** Parent directory for UST traces */
291cbdbf
BH
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 /**
cfdb727a 87 * The overwrite button
291cbdbf
BH
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 /**
cfdb727a 99 * List of traces to import
291cbdbf 100 */
cfdb727a 101 private final List<ImportFileInfo> fTraces = new ArrayList<ImportFileInfo>();
291cbdbf 102 /**
cfdb727a 103 * Selection index in project combo box.
291cbdbf
BH
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;
cfdb727a 110
291cbdbf
BH
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 }
cfdb727a 145
291cbdbf
BH
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) {
cfdb727a 175
291cbdbf
BH
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));
cfdb727a 187
291cbdbf 188 IRemoteSystemProxy proxy = fSession.getTargetNode().getRemoteSystemProxy();
cfdb727a 189
291cbdbf
BH
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);
cfdb727a 201
291cbdbf
BH
202 fFolderViewer.setContentProvider(new FolderContentProvider());
203 fFolderViewer.setLabelProvider(new WorkbenchLabelProvider());
cfdb727a 204
291cbdbf
BH
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());
cfdb727a 218 if (!event.getChecked()) {
291cbdbf
BH
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()]));
cfdb727a 250
291cbdbf
BH
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
cfdb727a 261
291cbdbf
BH
262 } catch (SystemMessageException e) {
263 createErrorComposite(parent, e.fillInStackTrace());
264 return fDialogComposite;
265 }
cfdb727a 266
291cbdbf
BH
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) {
79c3db85 276 createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", true); //$NON-NLS-1$
291cbdbf
BH
277 createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$
278 }
279
280 /*
281 * (non-Javadoc)
282 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
283 */
284 @Override
285 protected void okPressed() {
286 if (!fIsError) {
287 // Validate input data
288 fTraces.clear();
289
290 fProjectIndex = fCombo.getSelectionIndex();
291
292 if (fProjectIndex < 0) {
293 MessageDialog.openError(getShell(),
294 Messages.TraceControl_ImportDialogTitle,
295 Messages.TraceControl_ImportDialogNoProjectSelectedError);
296 return;
297 }
298
299 IProject project = fProjects.get(fProjectIndex);
300 IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME);
301
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$
307 return;
308 }
309
310 boolean overwriteAll = fOverwriteButton.getSelection();
311
312 Object[] checked = fFolderViewer.getCheckedElements();
313 for (int i = 0; i < checked.length; i++) {
314 IRemoteFile file = (IRemoteFile) checked[i];
cfdb727a
AM
315
316 // Only add actual trace directories
291cbdbf 317 if (file.isDirectory() && !UST_PARENT_DIRECTORY.equals(file.getName())) {
cfdb727a 318
291cbdbf
BH
319 ImportFileInfo info = new ImportFileInfo(file, file.getName(), overwriteAll);
320 String traceName = info.getLocalTraceName();
321 IFolder folder = traceFolder.getFolder(traceName);
322
cfdb727a 323 // Verify if trace directory already exists (and not overwrite)
291cbdbf
BH
324 if (folder.exists() && !overwriteAll) {
325
326 // Ask user for overwrite or new name
327 IImportConfirmationDialog conf = TraceControlDialogFactory.getInstance().getImportConfirmationDialog();
328 conf.setTraceName(traceName);
329
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());
335 }
336 fTraces.add(info);
337 }
cfdb727a 338 } else {
291cbdbf
BH
339 fTraces.add(info);
340 }
341 }
342 }
343
344 if (fTraces.isEmpty()) {
345 MessageDialog.openError(getShell(),
346 Messages.TraceControl_ImportDialogTitle,
347 Messages.TraceControl_ImportDialogNoTraceSelectedError);
348 return;
349 }
350 }
351
352 // validation successful -> call super.okPressed()
353 super.okPressed();
354 }
355
356 // ------------------------------------------------------------------------
357 // Helper methods and classes
358 // ------------------------------------------------------------------------
cfdb727a
AM
359 /**
360 * Helper class for the contents of a folder in a tracing project
361 *
362 * @author Bernd Hufmann
363 */
f455db37 364 public static class FolderContentProvider extends WorkbenchContentProvider {
291cbdbf
BH
365 @Override
366 public Object[] getChildren(Object o) {
367 if (o instanceof IRemoteFile) {
368 IRemoteFile element = (IRemoteFile) o;
369 // For our purpose, we need folders + files
370 if (!element.isDirectory()) {
371 return new Object[0];
372 }
373 }
374 return super.getChildren(o);
375 }
376 }
cfdb727a 377
291cbdbf
BH
378 /**
379 * Creates a dialog composite with an error message which can be used
380 * when an exception occurred during creation time of the dialog box.
381 * @param parent - a parent composite
cfdb727a 382 * @param e - a error causing exception
291cbdbf
BH
383 */
384 private void createErrorComposite(Composite parent, Throwable e) {
385 fIsError = true;
386 fDialogComposite.dispose();
cfdb727a 387
291cbdbf
BH
388 fDialogComposite = new Composite(parent, SWT.NONE);
389 GridLayout layout = new GridLayout(1, true);
390 fDialogComposite.setLayout(layout);
391 fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
cfdb727a 392
291cbdbf
BH
393 Text errorText = new Text(fDialogComposite, SWT.MULTI);
394 StringBuffer error = new StringBuffer();
395 error.append(Messages.TraceControl_ImportDialogCreationError);
396 error.append(System.getProperty("line.separator")); //$NON-NLS-1$
397 error.append(System.getProperty("line.separator")); //$NON-NLS-1$
398 error.append(e.toString());
399 errorText.setText(error.toString());
400 errorText.setLayoutData(new GridData(GridData.FILL_BOTH));
401 }
402
cfdb727a 403
291cbdbf 404 }
This page took 0.045618 seconds and 5 git commands to generate.