Commit | Line | Data |
---|---|---|
291cbdbf | 1 | /********************************************************************** |
ba3a9bd2 | 2 | * Copyright (c) 2012, 2013 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 | 10 | * Bernd Hufmann - Initial API and implementation |
ba3a9bd2 | 11 | * Bernd Hufmann - Added handling of streamed traces |
d600c18f | 12 | * Marc-Andre Laperle - Use common method to get opened tmf projects |
291cbdbf BH |
13 | **********************************************************************/ |
14 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs; | |
15 | ||
16 | import java.util.ArrayList; | |
17 | import java.util.List; | |
18 | ||
19 | import org.eclipse.core.resources.IFolder; | |
20 | import org.eclipse.core.resources.IProject; | |
291cbdbf BH |
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; | |
9315aeee | 30 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
291cbdbf BH |
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; | |
291cbdbf | 33 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
d600c18f | 34 | import org.eclipse.linuxtools.tmf.ui.project.model.TraceUtils; |
cd9821de | 35 | import org.eclipse.rse.core.subsystems.RemoteChildrenContentsType; |
291cbdbf BH |
36 | import org.eclipse.rse.services.clientserver.messages.SystemMessageException; |
37 | import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem; | |
38 | import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile; | |
39 | import org.eclipse.swt.SWT; | |
40 | import org.eclipse.swt.custom.CCombo; | |
41 | import org.eclipse.swt.graphics.Point; | |
42 | import org.eclipse.swt.layout.GridData; | |
43 | import org.eclipse.swt.layout.GridLayout; | |
44 | import org.eclipse.swt.widgets.Button; | |
45 | import org.eclipse.swt.widgets.Composite; | |
46 | import org.eclipse.swt.widgets.Control; | |
47 | import org.eclipse.swt.widgets.Group; | |
48 | import org.eclipse.swt.widgets.Shell; | |
49 | import org.eclipse.swt.widgets.Text; | |
50 | import org.eclipse.swt.widgets.Tree; | |
51 | import org.eclipse.ui.model.WorkbenchContentProvider; | |
52 | import org.eclipse.ui.model.WorkbenchLabelProvider; | |
53 | ||
54 | /** | |
291cbdbf BH |
55 | * <p> |
56 | * Dialog box for collecting trace import information. | |
57 | * </p> | |
cfdb727a | 58 | * |
dbd4432d | 59 | * @author Bernd Hufmann |
291cbdbf BH |
60 | */ |
61 | public class ImportDialog extends Dialog implements IImportDialog { | |
62 | ||
63 | // ------------------------------------------------------------------------ | |
64 | // Constants | |
65 | // ------------------------------------------------------------------------ | |
cfdb727a | 66 | /** The icon file for this dialog box. */ |
291cbdbf | 67 | public static final String IMPORT_ICON_FILE = "icons/elcl16/import_trace.gif"; //$NON-NLS-1$ |
cfdb727a AM |
68 | |
69 | /** Parent directory for UST traces */ | |
291cbdbf BH |
70 | public static final String UST_PARENT_DIRECTORY = "ust"; //$NON-NLS-1$ |
71 | ||
cd9821de BH |
72 | /** Name of metadata file of trace */ |
73 | public static final String METADATA_FILE_NAME = "metadata"; //$NON-NLS-1$ | |
74 | ||
75 | /** Default name of kernel traces */ | |
76 | public static final String DEFAULT_KERNEL_TRACE_NAME = "kernel"; //$NON-NLS-1$ | |
77 | ||
291cbdbf BH |
78 | // ------------------------------------------------------------------------ |
79 | // Attributes | |
80 | // ------------------------------------------------------------------------ | |
81 | /** | |
82 | * The dialog composite. | |
83 | */ | |
84 | private Composite fDialogComposite = null; | |
85 | /** | |
86 | * The checkbox tree viewer for selecting available traces | |
87 | */ | |
88 | private CheckboxTreeViewer fFolderViewer; | |
89 | /** | |
90 | * The combo box for selecting a project. | |
91 | */ | |
92 | private CCombo fCombo; | |
93 | /** | |
cfdb727a | 94 | * The overwrite button |
291cbdbf BH |
95 | */ |
96 | private Button fOverwriteButton; | |
97 | /** | |
98 | * List of available LTTng 2.0 projects | |
99 | */ | |
100 | private List<IProject> fProjects; | |
101 | /** | |
102 | * The parent where the new node should be added. | |
103 | */ | |
104 | private TraceSessionComponent fSession = null; | |
cd9821de BH |
105 | |
106 | /** | |
107 | * The name of the default project name | |
108 | */ | |
109 | private String fDefaultProjectName = null; | |
291cbdbf | 110 | /** |
cfdb727a | 111 | * List of traces to import |
291cbdbf | 112 | */ |
cfdb727a | 113 | private final List<ImportFileInfo> fTraces = new ArrayList<ImportFileInfo>(); |
291cbdbf | 114 | /** |
cfdb727a | 115 | * Selection index in project combo box. |
291cbdbf BH |
116 | */ |
117 | private int fProjectIndex; | |
118 | /** | |
119 | * Flag to indicate that something went wrong when creating the dialog box. | |
120 | */ | |
121 | private boolean fIsError = false; | |
cfdb727a | 122 | |
291cbdbf BH |
123 | // ------------------------------------------------------------------------ |
124 | // Constructors | |
125 | // ------------------------------------------------------------------------ | |
126 | /** | |
127 | * Constructor | |
128 | * @param shell - a shell for the display of the dialog | |
129 | */ | |
130 | public ImportDialog(Shell shell) { | |
131 | super(shell); | |
132 | setShellStyle(SWT.RESIZE); | |
133 | } | |
134 | ||
135 | // ------------------------------------------------------------------------ | |
136 | // Accessors | |
137 | // ------------------------------------------------------------------------ | |
11252342 | 138 | |
291cbdbf BH |
139 | @Override |
140 | public List<ImportFileInfo> getTracePathes() { | |
141 | List<ImportFileInfo> retList = new ArrayList<ImportFileInfo>(); | |
142 | retList.addAll(fTraces); | |
143 | return retList; | |
144 | } | |
145 | ||
291cbdbf BH |
146 | @Override |
147 | public IProject getProject() { | |
148 | return fProjects.get(fProjectIndex); | |
149 | } | |
cfdb727a | 150 | |
291cbdbf BH |
151 | @Override |
152 | public void setSession(TraceSessionComponent session) { | |
153 | fSession = session; | |
154 | } | |
155 | ||
cd9821de BH |
156 | @Override |
157 | public void setDefaultProject(String defaultProject) { | |
158 | fDefaultProjectName = defaultProject; | |
159 | } | |
160 | ||
291cbdbf BH |
161 | // ------------------------------------------------------------------------ |
162 | // Operations | |
163 | // ------------------------------------------------------------------------ | |
11252342 | 164 | |
291cbdbf BH |
165 | @Override |
166 | protected void configureShell(Shell newShell) { | |
167 | super.configureShell(newShell); | |
168 | newShell.setText(Messages.TraceControl_ImportDialogTitle); | |
169 | newShell.setImage(Activator.getDefault().loadIcon(IMPORT_ICON_FILE)); | |
170 | } | |
171 | ||
291cbdbf BH |
172 | @Override |
173 | protected Control createDialogArea(Composite parent) { | |
cfdb727a | 174 | |
291cbdbf BH |
175 | // Main dialog panel |
176 | fDialogComposite = new Composite(parent, SWT.NONE); | |
177 | GridLayout layout = new GridLayout(1, true); | |
178 | fDialogComposite.setLayout(layout); | |
179 | fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
180 | ||
291cbdbf | 181 | try { |
cd9821de | 182 | createRemoteComposite(); |
291cbdbf BH |
183 | } catch (SystemMessageException e) { |
184 | createErrorComposite(parent, e.fillInStackTrace()); | |
185 | return fDialogComposite; | |
186 | } | |
291cbdbf BH |
187 | return fDialogComposite; |
188 | } | |
189 | ||
291cbdbf BH |
190 | @Override |
191 | protected void createButtonsForButtonBar(Composite parent) { | |
79c3db85 | 192 | createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", true); //$NON-NLS-1$ |
cd9821de | 193 | createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$ |
291cbdbf BH |
194 | } |
195 | ||
291cbdbf BH |
196 | @Override |
197 | protected void okPressed() { | |
198 | if (!fIsError) { | |
f3b33d40 | 199 | |
291cbdbf BH |
200 | // Validate input data |
201 | fTraces.clear(); | |
202 | ||
203 | fProjectIndex = fCombo.getSelectionIndex(); | |
204 | ||
205 | if (fProjectIndex < 0) { | |
206 | MessageDialog.openError(getShell(), | |
207 | Messages.TraceControl_ImportDialogTitle, | |
208 | Messages.TraceControl_ImportDialogNoProjectSelectedError); | |
209 | return; | |
210 | } | |
211 | ||
212 | IProject project = fProjects.get(fProjectIndex); | |
213 | IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); | |
214 | ||
215 | if (!traceFolder.exists()) { | |
216 | // Invalid LTTng 2.0 project | |
217 | MessageDialog.openError(getShell(), | |
218 | Messages.TraceControl_ImportDialogTitle, | |
219 | Messages.TraceControl_ImportDialogInvalidTracingProject + " (" + TmfTraceFolder.TRACE_FOLDER_NAME + ")"); //$NON-NLS-1$//$NON-NLS-2$ | |
220 | return; | |
221 | } | |
222 | ||
223 | boolean overwriteAll = fOverwriteButton.getSelection(); | |
224 | ||
225 | Object[] checked = fFolderViewer.getCheckedElements(); | |
226 | for (int i = 0; i < checked.length; i++) { | |
227 | IRemoteFile file = (IRemoteFile) checked[i]; | |
cd9821de BH |
228 | if (!file.isDirectory() && file.getName().equals(METADATA_FILE_NAME)) { |
229 | IRemoteFile trace = file.getParentRemoteFile(); | |
230 | IRemoteFile parent = trace.getParentRemoteFile(); | |
231 | boolean isKernel = false; | |
232 | if (trace.getName().equals(DEFAULT_KERNEL_TRACE_NAME)) { | |
233 | isKernel = true; | |
234 | } | |
235 | StringBuffer traceName = new StringBuffer(); | |
236 | traceName.append(trace.getName()); | |
237 | traceName.insert(0, '-'); | |
238 | ||
589d0d33 BH |
239 | String path = fSession.isSnapshotSession() ? fSession.getSnapshotInfo().getSnapshotPath() : fSession.getSessionPath(); |
240 | ||
241 | while (!parent.getAbsolutePath().equals(path)) { | |
cd9821de BH |
242 | traceName.insert(0, parent.getName()); |
243 | traceName.insert(0, '-'); | |
244 | parent = parent.getParentRemoteFile(); | |
245 | } | |
246 | traceName.insert(0, parent.getName()); | |
cfdb727a | 247 | |
cd9821de BH |
248 | ImportFileInfo info = new ImportFileInfo(trace, traceName.toString(), overwriteAll, isKernel); |
249 | IFolder folder = traceFolder.getFolder(traceName.toString()); | |
291cbdbf | 250 | |
cfdb727a | 251 | // Verify if trace directory already exists (and not overwrite) |
291cbdbf BH |
252 | if (folder.exists() && !overwriteAll) { |
253 | ||
254 | // Ask user for overwrite or new name | |
255 | IImportConfirmationDialog conf = TraceControlDialogFactory.getInstance().getImportConfirmationDialog(); | |
cd9821de | 256 | conf.setTraceName(traceName.toString()); |
291cbdbf BH |
257 | |
258 | // Don't add trace to list if dialog was cancelled. | |
259 | if (conf.open() == Window.OK) { | |
260 | info.setOverwrite(conf.isOverwrite()); | |
261 | if (!conf.isOverwrite()) { | |
262 | info.setLocalTraceName(conf.getNewTraceName()); | |
263 | } | |
264 | fTraces.add(info); | |
265 | } | |
cfdb727a | 266 | } else { |
291cbdbf BH |
267 | fTraces.add(info); |
268 | } | |
269 | } | |
270 | } | |
271 | ||
272 | if (fTraces.isEmpty()) { | |
273 | MessageDialog.openError(getShell(), | |
274 | Messages.TraceControl_ImportDialogTitle, | |
275 | Messages.TraceControl_ImportDialogNoTraceSelectedError); | |
276 | return; | |
277 | } | |
278 | } | |
279 | ||
280 | // validation successful -> call super.okPressed() | |
281 | super.okPressed(); | |
282 | } | |
283 | ||
284 | // ------------------------------------------------------------------------ | |
285 | // Helper methods and classes | |
286 | // ------------------------------------------------------------------------ | |
11252342 | 287 | |
cfdb727a AM |
288 | /** |
289 | * Helper class for the contents of a folder in a tracing project | |
290 | * | |
291 | * @author Bernd Hufmann | |
292 | */ | |
f455db37 | 293 | public static class FolderContentProvider extends WorkbenchContentProvider { |
291cbdbf BH |
294 | @Override |
295 | public Object[] getChildren(Object o) { | |
296 | if (o instanceof IRemoteFile) { | |
297 | IRemoteFile element = (IRemoteFile) o; | |
298 | // For our purpose, we need folders + files | |
299 | if (!element.isDirectory()) { | |
300 | return new Object[0]; | |
301 | } | |
302 | } | |
303 | return super.getChildren(o); | |
304 | } | |
305 | } | |
cfdb727a | 306 | |
291cbdbf BH |
307 | /** |
308 | * Creates a dialog composite with an error message which can be used | |
309 | * when an exception occurred during creation time of the dialog box. | |
310 | * @param parent - a parent composite | |
cfdb727a | 311 | * @param e - a error causing exception |
291cbdbf BH |
312 | */ |
313 | private void createErrorComposite(Composite parent, Throwable e) { | |
314 | fIsError = true; | |
315 | fDialogComposite.dispose(); | |
cfdb727a | 316 | |
291cbdbf BH |
317 | fDialogComposite = new Composite(parent, SWT.NONE); |
318 | GridLayout layout = new GridLayout(1, true); | |
319 | fDialogComposite.setLayout(layout); | |
320 | fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
cfdb727a | 321 | |
291cbdbf BH |
322 | Text errorText = new Text(fDialogComposite, SWT.MULTI); |
323 | StringBuffer error = new StringBuffer(); | |
324 | error.append(Messages.TraceControl_ImportDialogCreationError); | |
325 | error.append(System.getProperty("line.separator")); //$NON-NLS-1$ | |
326 | error.append(System.getProperty("line.separator")); //$NON-NLS-1$ | |
327 | error.append(e.toString()); | |
328 | errorText.setText(error.toString()); | |
329 | errorText.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
330 | } | |
331 | ||
d600c18f | 332 | private void createRemoteComposite() throws SystemMessageException{ |
f3b33d40 BH |
333 | Group contextGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); |
334 | contextGroup.setText(Messages.TraceControl_ImportDialogTracesGroupName); | |
335 | GridLayout layout = new GridLayout(1, true); | |
336 | contextGroup.setLayout(layout); | |
337 | contextGroup.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
338 | ||
339 | IRemoteSystemProxy proxy = fSession.getTargetNode().getRemoteSystemProxy(); | |
340 | ||
341 | IFileServiceSubSystem fsss = proxy.getFileServiceSubSystem(); | |
342 | ||
589d0d33 BH |
343 | final String path = fSession.isSnapshotSession() ? fSession.getSnapshotInfo().getSnapshotPath() : fSession.getSessionPath(); |
344 | final IRemoteFile remoteFolder = fsss.getRemoteFileObject(path, new NullProgressMonitor()); | |
345 | // make sure that remote directory is read and not cached | |
346 | remoteFolder.markStale(true, true); | |
f3b33d40 BH |
347 | |
348 | fFolderViewer = new CheckboxTreeViewer(contextGroup, SWT.BORDER); | |
349 | GridData data = new GridData(GridData.FILL_BOTH); | |
350 | Tree tree = fFolderViewer.getTree(); | |
351 | tree.setLayoutData(data); | |
352 | tree.setFont(fDialogComposite.getFont()); | |
353 | tree.setToolTipText(Messages.TraceControl_ImportDialogTracesTooltip); | |
354 | ||
355 | fFolderViewer.setContentProvider(new FolderContentProvider()); | |
356 | fFolderViewer.setLabelProvider(new WorkbenchLabelProvider()); | |
357 | ||
358 | fFolderViewer.addCheckStateListener(new ICheckStateListener() { | |
359 | @Override | |
360 | public void checkStateChanged(CheckStateChangedEvent event) { | |
361 | Object elem = event.getElement(); | |
362 | if (elem instanceof IRemoteFile) { | |
363 | IRemoteFile element = (IRemoteFile) elem; | |
364 | if (!element.isDirectory()) { | |
365 | // A trick to keep selection of a file in sync with the directory | |
366 | boolean p = fFolderViewer.getChecked((element.getParentRemoteFile())); | |
367 | fFolderViewer.setChecked(element, p); | |
368 | return; | |
369 | } | |
370 | fFolderViewer.setSubtreeChecked(event.getElement(), event.getChecked()); | |
371 | if (!event.getChecked()) { | |
372 | fFolderViewer.setChecked(element.getParentRemoteFile(), false); | |
373 | } | |
374 | } | |
375 | } | |
376 | }); | |
377 | fFolderViewer.setInput(remoteFolder); | |
378 | ||
cd9821de | 379 | Object[] children = remoteFolder.getContents(RemoteChildrenContentsType.getInstance()); |
589d0d33 BH |
380 | // children can be null if there the path doesn't exist. This happens when a trace |
381 | // session hadn't been started and no output was created. | |
382 | if (children != null) { | |
383 | // Select all traces by default | |
384 | for (int i = 0; i < children.length; i++) { | |
385 | fFolderViewer.setSubtreeChecked(children[i], true); | |
386 | } | |
cd9821de BH |
387 | } |
388 | ||
f3b33d40 BH |
389 | Group projectGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); |
390 | projectGroup.setText(Messages.TraceControl_ImportDialogProjectsGroupName); | |
391 | layout = new GridLayout(1, true); | |
392 | projectGroup.setLayout(layout); | |
393 | projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
394 | ||
395 | fProjects = new ArrayList<IProject>(); | |
396 | List<String> projectNames = new ArrayList<String>(); | |
cd9821de | 397 | |
d600c18f MAL |
398 | for (IProject project : TraceUtils.getOpenedTmfProjects()) { |
399 | fProjects.add(project); | |
400 | projectNames.add(project.getName()); | |
f3b33d40 BH |
401 | } |
402 | ||
403 | fCombo = new CCombo(projectGroup, SWT.READ_ONLY); | |
404 | fCombo.setToolTipText(Messages.TraceControl_ImportDialogProjectsTooltip); | |
405 | fCombo.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 1, 1)); | |
406 | fCombo.setItems(projectNames.toArray(new String[projectNames.size()])); | |
407 | ||
cd9821de BH |
408 | if (fDefaultProjectName != null) { |
409 | int select = projectNames.indexOf(fDefaultProjectName); | |
410 | fCombo.select(select); | |
411 | } | |
412 | ||
f3b33d40 BH |
413 | Group overrideGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); |
414 | layout = new GridLayout(1, true); | |
415 | overrideGroup.setLayout(layout); | |
416 | overrideGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
417 | ||
418 | fOverwriteButton = new Button(overrideGroup, SWT.CHECK); | |
419 | fOverwriteButton.setText(Messages.TraceControl_ImportDialogOverwriteButtonText); | |
420 | getShell().setMinimumSize(new Point(500, 400)); | |
421 | } | |
422 | ||
291cbdbf | 423 | } |