control: Add enhanced support for loading of sessions
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / dialogs / LoadDialog.java
1 /**********************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal, 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 * Contributors:
11 * Patrick-Jeffrey Pollo Guilbert, William Enright,
12 * William Tri-Khiem Truong - Initial API and implementation
13 * Bernd Hufmann - Renamed from ProfileHandler and redesign
14 **********************************************************************/
15 package org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs;
16
17 import java.io.File;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.eclipse.core.filesystem.EFS;
22 import org.eclipse.core.filesystem.IFileStore;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.dialogs.TitleAreaDialog;
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.viewers.ISelectionChangedListener;
29 import org.eclipse.jface.viewers.ITreeContentProvider;
30 import org.eclipse.jface.viewers.LabelProvider;
31 import org.eclipse.jface.viewers.SelectionChangedEvent;
32 import org.eclipse.jface.viewers.Viewer;
33 import org.eclipse.remote.core.IRemoteConnection;
34 import org.eclipse.remote.ui.widgets.RemoteResourceBrowserWidget;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Control;
44 import org.eclipse.swt.widgets.Shell;
45 import org.eclipse.swt.widgets.Tree;
46 import org.eclipse.tracecompass.internal.lttng2.control.core.LttngProfileManager;
47 import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator;
48 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
49 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.service.LTTngControlServiceConstants;
50 import org.eclipse.ui.ISharedImages;
51 import org.eclipse.ui.PlatformUI;
52
53 /**
54 * Dialog box for collecting parameter for loading a session.
55 *
56 * @author Bernd Hufmann
57 * @author Patrick-Jeffrey Pollo Guilbert
58 * @author William Enright
59 * @author William Tri-Khiem Truong
60 *
61 */
62 public class LoadDialog extends TitleAreaDialog implements ILoadDialog {
63 // ------------------------------------------------------------------------
64 // Constants
65 // ------------------------------------------------------------------------
66 /** The icon file for this dialog box. */
67 public static final String IMPORT_ICON_FILE = "icons/elcl16/import_button.png"; //$NON-NLS-1$
68
69 // ------------------------------------------------------------------------
70 // Constants
71 // ------------------------------------------------------------------------
72 /**
73 * The dialog composite.
74 */
75 private Composite fDialogComposite = null;
76 private Button fLocalButton = null;
77 private Button fRemoteButton = null;
78
79 private Composite fLocalComposite = null;
80 private CheckboxTreeViewer fFolderViewer;
81
82 private Button fForceButton = null;
83
84 private RemoteResourceBrowserWidget fFileWidget;
85 private IRemoteConnection fConnection = null;
86
87 private List<IFileStore> fLocalFiles = null;
88 private List<IFileStore> fRemoteFiles = null;
89
90 private boolean fIsForce = true;
91
92 // ------------------------------------------------------------------------
93 // Constructors
94 // ------------------------------------------------------------------------
95 /**
96 * Constructor
97 *
98 * @param shell
99 * - a shell for the display of the dialog
100 */
101 public LoadDialog(Shell shell) {
102 super(shell);
103 setShellStyle(SWT.RESIZE | getShellStyle());
104 }
105
106 @Override
107 public List<IFileStore> getRemoteResources() {
108 return fRemoteFiles;
109 }
110
111 @Override
112 public List<IFileStore> getLocalResources() {
113 return fLocalFiles;
114 }
115
116 @Override
117 public boolean isForce() {
118 return fIsForce;
119 }
120
121 @Override
122 public void initialize (IRemoteConnection connection) {
123 fConnection = connection;
124 fIsForce = true;
125 fRemoteFiles = null;
126 fLocalFiles = null;
127 }
128
129 // ------------------------------------------------------------------------
130 // Operations
131 // ------------------------------------------------------------------------
132 @Override
133 protected void configureShell(Shell newShell) {
134 super.configureShell(newShell);
135 newShell.setText(Messages.TraceControl_LoadDialogTitle);
136 newShell.setImage(Activator.getDefault().loadIcon(IMPORT_ICON_FILE));
137 }
138
139 @Override
140 protected Control createDialogArea(Composite parent) {
141 // Main dialog panel
142 fDialogComposite = new Composite(parent, SWT.NONE);
143 GridLayout layout = new GridLayout(1, true);
144 fDialogComposite.setLayout(layout);
145 GridData data = new GridData(GridData.FILL_BOTH);
146 data.heightHint = 300;
147 fDialogComposite.setLayoutData(data);
148
149 createSelectionGroup();
150 createOptionComposite();
151 fLocalComposite = null;
152 fFileWidget = null;
153 createLocalComposite();
154 setMessage(Messages.TraceControl_SelectProfileText);
155 return fDialogComposite;
156 }
157
158 private void createSelectionGroup() {
159 Composite group = new Composite(fDialogComposite, SWT.BORDER);
160 group.setLayout(new GridLayout(2, true));
161 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
162
163 fLocalButton = new Button(group, SWT.RADIO);
164 fLocalButton.setText(Messages.TraceControl_LocalButtonText);
165 fLocalButton.setLayoutData(new GridData(GridData.FILL_BOTH));
166 fLocalButton.setSelection(true);
167
168 fRemoteButton = new Button(group, SWT.RADIO);
169 fRemoteButton.setText(Messages.TraceControl_RemoteButtonText);
170 fRemoteButton.setLayoutData(new GridData(GridData.FILL_BOTH));
171
172 fLocalButton.addSelectionListener(new SelectionAdapter() {
173 @Override
174 public void widgetSelected(SelectionEvent e) {
175 if (fLocalButton.getSelection()) {
176 disposeRemoteComposite();
177 createLocalComposite();
178 fRemoteFiles = null;
179 fDialogComposite.layout();
180 enableLocalButtons();
181 }
182 }
183 });
184
185 fRemoteButton.addSelectionListener(new SelectionAdapter() {
186 @Override
187 public void widgetSelected(SelectionEvent e) {
188 if (fRemoteButton.getSelection()) {
189 disposeLocalComposite();
190 createRemoteComposite();
191 fLocalFiles = null;
192 fDialogComposite.layout();
193 enableRemoteButtons();
194 }
195 }
196 });
197 }
198
199 private void createLocalComposite() {
200 if (fLocalComposite == null) {
201 fLocalComposite = new Composite(fDialogComposite, SWT.BORDER);
202 fLocalComposite.setLayout(new GridLayout(2, false));
203 fLocalComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
204
205 Composite viewerComposite = new Composite(fLocalComposite, SWT.NONE);
206 viewerComposite.setLayout(new GridLayout(1, false));
207 viewerComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
208
209 fFolderViewer = new CheckboxTreeViewer(viewerComposite);
210 fFolderViewer.setContentProvider(new ProfileContentProvider());
211 fFolderViewer.setLabelProvider(new ProfileLabelProvider());
212 fFolderViewer.setInput(LttngProfileManager.getProfiles());
213
214 GridData data = new GridData(GridData.FILL_BOTH);
215 Tree tree = fFolderViewer.getTree();
216 tree.setLayoutData(data);
217
218 fFolderViewer.addCheckStateListener(new ICheckStateListener() {
219 @Override
220 public void checkStateChanged(CheckStateChangedEvent event) {
221 enableLocalButtons();
222 }
223 });
224 }
225 }
226
227 /**
228 * Disposes the remote composite (if existing)
229 */
230 private void disposeLocalComposite() {
231 if (fLocalComposite != null) {
232 fLocalComposite.dispose();
233 fLocalComposite = null;
234 }
235 }
236
237 private void createRemoteComposite() {
238 if (fFileWidget == null) {
239 fFileWidget = new RemoteResourceBrowserWidget(fDialogComposite, SWT.BORDER, RemoteResourceBrowserWidget.SHOW_HIDDEN_CHECKBOX);
240 fFileWidget.setLayoutData(new GridData(GridData.FILL_BOTH));
241 fFileWidget.setInitialPath(LTTngControlServiceConstants.DEFAULT_PATH);
242 fFileWidget.setConnection(fConnection);
243 fFileWidget.addSelectionChangedListener(new ISelectionChangedListener() {
244 @Override
245 public void selectionChanged(SelectionChangedEvent event) {
246 enableRemoteButtons();
247 }
248 });
249 getButton(IDialogConstants.OK_ID).setEnabled(true);
250 }
251 }
252
253 /**
254 * Disposes the remote composite (if existing)
255 */
256 private void disposeRemoteComposite() {
257 if (fFileWidget != null) {
258 fFileWidget.dispose();
259 fFileWidget = null;
260 }
261 }
262
263 private void createOptionComposite() {
264 Composite group = new Composite(fDialogComposite, SWT.BORDER);
265 group.setLayout(new GridLayout(1, true));
266 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
267
268 fForceButton = new Button(group, SWT.CHECK);
269 fForceButton.setText(Messages.TraceControl_ForceButtonText);
270 fForceButton.setSelection(true);
271 }
272
273 private void enableLocalButtons() {
274 Object[] checked = fFolderViewer.getCheckedElements();
275 boolean enabled = (checked != null) && (checked.length > 0);
276 Button okButton = getButton(IDialogConstants.OK_ID);
277 if (okButton != null) {
278 okButton.setEnabled(enabled);
279 }
280 }
281
282 private void enableRemoteButtons() {
283 List<IFileStore> resources = fFileWidget.getResources();
284 boolean enabled = (resources != null) && (resources.size() > 0);
285 Button okButton = getButton(IDialogConstants.OK_ID);
286 if (okButton != null) {
287 okButton.setEnabled(enabled);
288 }
289 }
290
291 @Override
292 protected void createButtonsForButtonBar(Composite parent) {
293 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
294 Button button = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false);
295 button.setEnabled(false);
296 }
297
298 @Override
299 protected void okPressed() {
300 fIsForce = fForceButton.getSelection();
301 if (fFileWidget != null) {
302 fRemoteFiles = fFileWidget.getResources();
303 if (fRemoteFiles.size() > 0) {
304 super.okPressed();
305 }
306 return;
307 }
308
309 Object[] files = fFolderViewer.getCheckedElements();
310 List<IFileStore> stores = new ArrayList<>();
311 for (Object file : files) {
312 if (file instanceof File) {
313 stores.add(EFS.getLocalFileSystem().fromLocalFile((File) file));
314 }
315 }
316 if (stores.size() != 0) {
317 fLocalFiles = stores;
318 super.okPressed();
319 }
320 }
321
322 /**
323 * Helper class for the contents of a folder in a tracing project
324 *
325 * @author Bernd Hufmann
326 */
327 public static class ProfileContentProvider implements ITreeContentProvider {
328 @Override
329 public Object[] getChildren(Object o) {
330
331 if (o instanceof File[]) {
332 return (File[]) o;
333 }
334 File store = (File) o;
335 if (store.isDirectory()) {
336 return store.listFiles();
337 }
338 return new Object[0];
339 }
340
341 @Override
342 public Object getParent(Object element) {
343 return ((File) element).getParent();
344 }
345
346 @Override
347 public void dispose() {
348 }
349
350 @Override
351 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
352 }
353
354 @Override
355 public Object[] getElements(Object inputElement) {
356 return getChildren(inputElement);
357 }
358
359 @Override
360 public boolean hasChildren(Object element) {
361 return ((File) element).isDirectory();
362 }
363 }
364
365 static class ProfileLabelProvider extends LabelProvider {
366 @Override
367 public String getText(Object element) {
368 return ((File) element).getName();
369 }
370
371 @Override
372 public Image getImage(Object element) {
373 if (((File) element).isDirectory()) {
374 return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
375 }
376 return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FILE);
377 }
378 }
379 }
This page took 0.053501 seconds and 5 git commands to generate.