pcap: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / linuxtools / internal / lttng2 / control / ui / views / dialogs / AddContextDialog.java
CommitLineData
b793fbe1 1/**********************************************************************
60ae41e1 2 * Copyright (c) 2012, 2014 Ericsson
cfdb727a 3 *
b793fbe1
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:
b793fbe1
BH
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
8e8c0226 12package org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs;
b793fbe1
BH
13
14import java.util.ArrayList;
15import java.util.Iterator;
16import java.util.List;
17
18import org.eclipse.jface.dialogs.Dialog;
19import org.eclipse.jface.dialogs.IDialogConstants;
20import org.eclipse.jface.viewers.CheckStateChangedEvent;
21import org.eclipse.jface.viewers.CheckboxTreeViewer;
22import org.eclipse.jface.viewers.ColumnLabelProvider;
23import org.eclipse.jface.viewers.ICheckStateListener;
24import org.eclipse.jface.viewers.ITreeContentProvider;
25import org.eclipse.jface.viewers.Viewer;
8e8c0226
AM
26import org.eclipse.linuxtools.internal.lttng2.control.ui.Activator;
27import org.eclipse.linuxtools.internal.lttng2.control.ui.views.messages.Messages;
b793fbe1
BH
28import org.eclipse.swt.SWT;
29import org.eclipse.swt.graphics.Point;
30import org.eclipse.swt.layout.GridData;
31import org.eclipse.swt.layout.GridLayout;
32import org.eclipse.swt.widgets.Composite;
33import org.eclipse.swt.widgets.Control;
34import org.eclipse.swt.widgets.Group;
35import org.eclipse.swt.widgets.Shell;
36
37/**
b793fbe1
BH
38 * <p>
39 * Dialog box for collecting information about contexts to be added to channels/events.
40 * </p>
cfdb727a 41 *
dbd4432d 42 * @author Bernd Hufmann
b793fbe1
BH
43 */
44public class AddContextDialog extends Dialog implements IAddContextDialog {
45
46 // ------------------------------------------------------------------------
47 // Constants
48 // ------------------------------------------------------------------------
cfdb727a 49
b793fbe1
BH
50 /**
51 * The icon file for this dialog box.
52 */
cfdb727a 53 public static final String ADD_CONTEXT_ICON_FILE = "icons/elcl16/add-context.gif"; //$NON-NLS-1$
b793fbe1
BH
54
55 // ------------------------------------------------------------------------
56 // Attributes
57 // ------------------------------------------------------------------------
11252342 58
b793fbe1
BH
59 /**
60 * A tree viewer for displaying and selection of available contexts.
61 */
62 private CheckboxTreeViewer fContextsViewer;
11252342 63
b793fbe1
BH
64 /**
65 * A Tree model for the checkbox tree viewer.
66 */
cfdb727a 67 private final ContextModel fContextModel = new ContextModel();
11252342 68
b793fbe1
BH
69 /**
70 * The contexts to add.
71 */
e0838ca1 72 private final List<String> fSelectedContexts = new ArrayList<>();
cfdb727a 73
b793fbe1
BH
74 // ------------------------------------------------------------------------
75 // Constructors
76 // ------------------------------------------------------------------------
11252342 77
b793fbe1
BH
78 /**
79 * Constructor
80 * @param shell - a shell for the display of the dialog
81 */
82 public AddContextDialog(Shell shell) {
83 super(shell);
8a396998 84 setShellStyle(SWT.RESIZE | getShellStyle());
b793fbe1 85 }
cfdb727a 86
b793fbe1
BH
87 // ------------------------------------------------------------------------
88 // Accessors
89 // ------------------------------------------------------------------------
cfdb727a 90
b793fbe1
BH
91 @Override
92 public void setAvalibleContexts(List<String> contexts) {
93 fContextModel.setAvalibleContexts(contexts);
94 }
cfdb727a 95
b793fbe1
BH
96 @Override
97 public List<String> getContexts() {
e0838ca1 98 List<String> ret = new ArrayList<>();
b793fbe1
BH
99 ret.addAll(fSelectedContexts);
100 return ret;
101 }
102
103 // ------------------------------------------------------------------------
104 // Operations
105 // ------------------------------------------------------------------------
11252342 106
b793fbe1
BH
107 @Override
108 protected void configureShell(Shell newShell) {
109 super.configureShell(newShell);
110 newShell.setText(Messages.TraceControl_AddContextDialogTitle);
111 newShell.setImage(Activator.getDefault().loadIcon(ADD_CONTEXT_ICON_FILE));
112 }
113
b793fbe1
BH
114 @Override
115 protected Control createDialogArea(Composite parent) {
116
117 // Main dialog panel
046b6849 118 Composite dialogComposite = new Composite(parent, SWT.NONE);
b793fbe1 119 GridLayout layout = new GridLayout(1, true);
046b6849
BH
120 dialogComposite.setLayout(layout);
121 dialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
b793fbe1
BH
122
123 // Contexts list
046b6849 124 Group contextGroup = new Group(dialogComposite, SWT.SHADOW_NONE);
b793fbe1
BH
125 contextGroup.setText(Messages.TraceControl_AddContextAvailableContextsLabel);
126 layout = new GridLayout(1, true);
127 contextGroup.setLayout(layout);
128 contextGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
cfdb727a 129
b793fbe1 130 fContextsViewer = new CheckboxTreeViewer(contextGroup, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
cfdb727a 131 fContextsViewer.getTree().setToolTipText(Messages.TraceControl_AddContextAvailableContextsTooltip);
b793fbe1
BH
132
133 fContextsViewer.setContentProvider(new ContextsContentProvider());
134 fContextsViewer.setLabelProvider(new ContextsLabelProvider());
135 fContextsViewer.addCheckStateListener(new ContextCheckListener());
136 fContextsViewer.setInput(fContextModel);
137 fContextsViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
cfdb727a 138
0886cf00 139 getShell().setMinimumSize(new Point(500, 450));
cfdb727a 140
046b6849 141 return dialogComposite;
b793fbe1
BH
142 }
143
b793fbe1
BH
144 @Override
145 protected void createButtonsForButtonBar(Composite parent) {
79c3db85 146 createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", true); //$NON-NLS-1$
b793fbe1
BH
147 createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$
148 }
149
b793fbe1
BH
150 @Override
151 protected void okPressed() {
152 fSelectedContexts.clear();
153
154 Object[] checkedElements = fContextsViewer.getCheckedElements();
155 for (int i = 0; i < checkedElements.length; i++) {
156 IContextModelComponent component = (IContextModelComponent)checkedElements[i];
f455db37
BH
157 if (!Messages.TraceControl_AddContextAllLabel.equals(component.getName())) {
158 fSelectedContexts.add(component.getName());
159 }
b793fbe1 160 }
cfdb727a 161
b793fbe1
BH
162 // validation successful -> call super.okPressed()
163 super.okPressed();
164 }
cfdb727a 165
b793fbe1
BH
166 // ------------------------------------------------------------------------
167 // Helper classes and methods
168 // ------------------------------------------------------------------------
169 /**
170 * Content provider for the contexts tree
171 */
77735e82 172 public static final class ContextsContentProvider implements ITreeContentProvider {
b793fbe1
BH
173
174 @Override
175 public void dispose() {
176 }
177
178 @Override
179 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
180 }
181
182 @Override
183 public Object[] getElements(Object inputElement) {
184 return getChildren(inputElement);
185 }
186
187 @Override
188 public Object[] getChildren(Object parentElement) {
189 if (parentElement instanceof IContextModelComponent) {
190 return ((IContextModelComponent)parentElement).getChildren();
191 }
192 return null;
193 }
194
195 @Override
196 public Object getParent(Object element) {
197 if (element instanceof IContextModelComponent) {
198 return ((IContextModelComponent)element).getParent();
199 }
200 return null;
201 }
202
203 @Override
204 public boolean hasChildren(Object element) {
205 if (element instanceof IContextModelComponent) {
206 return ((IContextModelComponent)element).hasChildren();
207 }
208 return false;
209 }
210 }
211
212 /**
213 * Label provider for the contexts tree
214 */
77735e82 215 public static final class ContextsLabelProvider extends ColumnLabelProvider {
b793fbe1
BH
216 @Override
217 public String getText(Object element) {
cfdb727a 218
b793fbe1
BH
219 if ((element != null) && (element instanceof IContextModelComponent)) {
220 return ((IContextModelComponent)element).getName();
221 }
222
223 return "";//$NON-NLS-1$
224 }
225 }
cfdb727a 226
b793fbe1 227 /**
cfdb727a 228 * Check state listener for the contexts tree.
b793fbe1 229 */
77735e82 230 public final class ContextCheckListener implements ICheckStateListener {
b793fbe1
BH
231 @Override
232 public void checkStateChanged(CheckStateChangedEvent event) {
233 if (event.getChecked()) {
234 if (event.getElement() instanceof AllContexts) {
235 fContextsViewer.setSubtreeChecked(event.getElement(), true);
cfdb727a
AM
236 }
237 } else {
b793fbe1
BH
238 if (event.getElement() instanceof AllContexts) {
239 fContextsViewer.setSubtreeChecked(event.getElement(), false);
240 } else {
241 IContextModelComponent component = (IContextModelComponent) event.getElement();
242 fContextsViewer.setChecked(component.getParent(), false);
243 }
244 }
245 }
246 }
247
248 /**
249 * Model for the context tree viewer (root component)
250 */
251 public static class ContextModel implements IContextModelComponent {
252
cfdb727a
AM
253 private final AllContexts fAllContexts;
254
255 /**
256 * Constructor
257 */
b793fbe1
BH
258 public ContextModel() {
259 fAllContexts = new AllContexts(this);
260 }
261
cfdb727a
AM
262 /**
263 * Sets the available contexts
264 *
265 * @param contexts
266 * The contexts to set
267 */
b793fbe1
BH
268 public void setAvalibleContexts(List<String> contexts) {
269 fAllContexts.setAvalibleContexts(contexts);
270 }
cfdb727a 271
b793fbe1
BH
272 @Override
273 public String getName() {
274 return "root"; //$NON-NLS-1$
275 }
cfdb727a 276
b793fbe1
BH
277 @Override
278 public Object getParent() {
279 return null;
280 }
281
282 @Override
283 public Object[] getChildren() {
284 Object[] ret = new Object[1];
285 ret[0] = fAllContexts;
286 return ret;
287 }
288
289 @Override
290 public boolean hasChildren() {
291 return true;
292 }
293 }
294
295 /**
cfdb727a 296 * Model element (to select/deselect) all contexts) for the context tree viewer
b793fbe1
BH
297 */
298 public static class AllContexts implements IContextModelComponent {
299 /**
300 * The available list of contexts.
301 */
302 private List<Context> fAvailableContexts;
b793fbe1 303
cfdb727a
AM
304 private final IContextModelComponent fParent;
305
306 /**
307 * Constructor
308 *
309 * @param parent
310 * The parent component
311 */
b793fbe1
BH
312 public AllContexts(IContextModelComponent parent) {
313 fParent = parent;
314 }
cfdb727a
AM
315
316 /**
317 * Sets the available contexts
318 *
319 * @param contexts
320 * The contexts to set
321 */
b793fbe1 322 public void setAvalibleContexts(List<String> contexts) {
e0838ca1 323 fAvailableContexts = new ArrayList<>();
b793fbe1
BH
324 if (contexts != null) {
325 for (Iterator<String> iterator = contexts.iterator(); iterator.hasNext();) {
cfdb727a 326 String name = iterator.next();
b793fbe1
BH
327 fAvailableContexts.add(new Context(this, name));
328 }
329 }
330 }
331
332 @Override
333 public String getName() {
334 return Messages.TraceControl_AddContextAllLabel;
335 }
cfdb727a 336
b793fbe1
BH
337 @Override
338 public Object[] getChildren() {
339 return fAvailableContexts.toArray();
340 }
cfdb727a 341
b793fbe1
BH
342 @Override
343 public Object getParent() {
344 return fParent;
345 }
cfdb727a 346
b793fbe1
BH
347 @Override
348 public boolean hasChildren() {
349 return true;
350 }
351 }
352
353 /**
cfdb727a 354 * Model element (the context) for the context tree viewer
b793fbe1
BH
355 */
356 public static class Context implements IContextModelComponent {
357
cfdb727a
AM
358 private final String fContextName;
359 private final IContextModelComponent fParent;
360
361 /**
362 * Constructor
363 *
364 * @param parent
365 * The parent component
366 * @param name
367 * The name of this context
368 */
b793fbe1
BH
369 public Context(IContextModelComponent parent, String name) {
370 fParent = parent;
371 fContextName = name;
372 }
cfdb727a 373
b793fbe1
BH
374 @Override
375 public String getName() {
376 return fContextName;
377 }
378
379 @Override
380 public Object getParent() {
381 return fParent;
382 }
383
384 @Override
385 public Object[] getChildren() {
386 return null;
387 }
388
389 @Override
390 public boolean hasChildren() {
391 return false;
392 }
393 }
394
395 /**
cfdb727a 396 * Interface for the tree model used for the context tree viewer.
b793fbe1
BH
397 */
398 public interface IContextModelComponent {
cfdb727a
AM
399
400 /**
401 * @return The name of this component
402 */
046b6849 403 String getName();
cfdb727a
AM
404
405 /**
406 * @return The parent component
407 */
046b6849 408 Object getParent();
cfdb727a
AM
409
410 /**
411 * @return The array of children of this component
412 */
046b6849 413 Object[] getChildren();
cfdb727a
AM
414
415 /**
416 * @return If this component has children or not
417 */
046b6849 418 boolean hasChildren();
b793fbe1
BH
419 }
420}
This page took 0.073754 seconds and 5 git commands to generate.