1 /**********************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Iterator
;
16 import java
.util
.List
;
18 import org
.eclipse
.jface
.dialogs
.Dialog
;
19 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
20 import org
.eclipse
.jface
.viewers
.CheckStateChangedEvent
;
21 import org
.eclipse
.jface
.viewers
.CheckboxTreeViewer
;
22 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
23 import org
.eclipse
.jface
.viewers
.ICheckStateListener
;
24 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
25 import org
.eclipse
.jface
.viewers
.Viewer
;
26 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.Activator
;
27 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.messages
.Messages
;
28 import org
.eclipse
.swt
.SWT
;
29 import org
.eclipse
.swt
.graphics
.Point
;
30 import org
.eclipse
.swt
.layout
.GridData
;
31 import org
.eclipse
.swt
.layout
.GridLayout
;
32 import org
.eclipse
.swt
.widgets
.Composite
;
33 import org
.eclipse
.swt
.widgets
.Control
;
34 import org
.eclipse
.swt
.widgets
.Group
;
35 import org
.eclipse
.swt
.widgets
.Shell
;
39 * Dialog box for collecting information about contexts to be added to channels/events.
42 * @author Bernd Hufmann
44 public class AddContextDialog
extends Dialog
implements IAddContextDialog
{
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 * The icon file for this dialog box.
53 public static final String ADD_CONTEXT_ICON_FILE
= "icons/elcl16/add-context.gif"; //$NON-NLS-1$
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
60 * The dialog composite.
62 private Composite fDialogComposite
;
65 * A tree viewer for displaying and selection of available contexts.
67 private CheckboxTreeViewer fContextsViewer
;
70 * A Tree model for the checkbox tree viewer.
72 private final ContextModel fContextModel
= new ContextModel();
75 * The contexts to add.
77 private final List
<String
> fSelectedContexts
= new ArrayList
<String
>();
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
85 * @param shell - a shell for the display of the dialog
87 public AddContextDialog(Shell shell
) {
89 setShellStyle(SWT
.RESIZE
);
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
97 public void setAvalibleContexts(List
<String
> contexts
) {
98 fContextModel
.setAvalibleContexts(contexts
);
102 public List
<String
> getContexts() {
103 List
<String
> ret
= new ArrayList
<String
>();
104 ret
.addAll(fSelectedContexts
);
108 // ------------------------------------------------------------------------
110 // ------------------------------------------------------------------------
113 protected void configureShell(Shell newShell
) {
114 super.configureShell(newShell
);
115 newShell
.setText(Messages
.TraceControl_AddContextDialogTitle
);
116 newShell
.setImage(Activator
.getDefault().loadIcon(ADD_CONTEXT_ICON_FILE
));
120 protected Control
createDialogArea(Composite parent
) {
123 fDialogComposite
= new Composite(parent
, SWT
.NONE
);
124 GridLayout layout
= new GridLayout(1, true);
125 fDialogComposite
.setLayout(layout
);
126 fDialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
129 Group contextGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
130 contextGroup
.setText(Messages
.TraceControl_AddContextAvailableContextsLabel
);
131 layout
= new GridLayout(1, true);
132 contextGroup
.setLayout(layout
);
133 contextGroup
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
135 fContextsViewer
= new CheckboxTreeViewer(contextGroup
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
136 fContextsViewer
.getTree().setToolTipText(Messages
.TraceControl_AddContextAvailableContextsTooltip
);
138 fContextsViewer
.setContentProvider(new ContextsContentProvider());
139 fContextsViewer
.setLabelProvider(new ContextsLabelProvider());
140 fContextsViewer
.addCheckStateListener(new ContextCheckListener());
141 fContextsViewer
.setInput(fContextModel
);
142 fContextsViewer
.getTree().setLayoutData(new GridData(GridData
.FILL_BOTH
));
144 getShell().setMinimumSize(new Point(500, 450));
146 return fDialogComposite
;
150 protected void createButtonsForButtonBar(Composite parent
) {
151 createButton(parent
, IDialogConstants
.CANCEL_ID
, "&Cancel", true); //$NON-NLS-1$
152 createButton(parent
, IDialogConstants
.OK_ID
, "&Ok", true); //$NON-NLS-1$
156 protected void okPressed() {
157 fSelectedContexts
.clear();
159 Object
[] checkedElements
= fContextsViewer
.getCheckedElements();
160 for (int i
= 0; i
< checkedElements
.length
; i
++) {
161 IContextModelComponent component
= (IContextModelComponent
)checkedElements
[i
];
162 if (!Messages
.TraceControl_AddContextAllLabel
.equals(component
.getName())) {
163 fSelectedContexts
.add(component
.getName());
167 // validation successful -> call super.okPressed()
171 // ------------------------------------------------------------------------
172 // Helper classes and methods
173 // ------------------------------------------------------------------------
175 * Content provider for the contexts tree
177 final public static class ContextsContentProvider
implements ITreeContentProvider
{
180 public void dispose() {
184 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
188 public Object
[] getElements(Object inputElement
) {
189 return getChildren(inputElement
);
193 public Object
[] getChildren(Object parentElement
) {
194 if (parentElement
instanceof IContextModelComponent
) {
195 return ((IContextModelComponent
)parentElement
).getChildren();
201 public Object
getParent(Object element
) {
202 if (element
instanceof IContextModelComponent
) {
203 return ((IContextModelComponent
)element
).getParent();
209 public boolean hasChildren(Object element
) {
210 if (element
instanceof IContextModelComponent
) {
211 return ((IContextModelComponent
)element
).hasChildren();
218 * Label provider for the contexts tree
220 final public static class ContextsLabelProvider
extends ColumnLabelProvider
{
222 public String
getText(Object element
) {
224 if ((element
!= null) && (element
instanceof IContextModelComponent
)) {
225 return ((IContextModelComponent
)element
).getName();
228 return "";//$NON-NLS-1$
233 * Check state listener for the contexts tree.
235 final public class ContextCheckListener
implements ICheckStateListener
{
237 public void checkStateChanged(CheckStateChangedEvent event
) {
238 if (event
.getChecked()) {
239 if (event
.getElement() instanceof AllContexts
) {
240 fContextsViewer
.setSubtreeChecked(event
.getElement(), true);
243 if (event
.getElement() instanceof AllContexts
) {
244 fContextsViewer
.setSubtreeChecked(event
.getElement(), false);
246 IContextModelComponent component
= (IContextModelComponent
) event
.getElement();
247 fContextsViewer
.setChecked(component
.getParent(), false);
254 * Model for the context tree viewer (root component)
256 public static class ContextModel
implements IContextModelComponent
{
258 private final AllContexts fAllContexts
;
263 public ContextModel() {
264 fAllContexts
= new AllContexts(this);
268 * Sets the available contexts
271 * The contexts to set
273 public void setAvalibleContexts(List
<String
> contexts
) {
274 fAllContexts
.setAvalibleContexts(contexts
);
278 public String
getName() {
279 return "root"; //$NON-NLS-1$
283 public Object
getParent() {
288 public Object
[] getChildren() {
289 Object
[] ret
= new Object
[1];
290 ret
[0] = fAllContexts
;
295 public boolean hasChildren() {
301 * Model element (to select/deselect) all contexts) for the context tree viewer
303 public static class AllContexts
implements IContextModelComponent
{
305 * The available list of contexts.
307 private List
<Context
> fAvailableContexts
;
309 private final IContextModelComponent fParent
;
315 * The parent component
317 public AllContexts(IContextModelComponent parent
) {
322 * Sets the available contexts
325 * The contexts to set
327 public void setAvalibleContexts(List
<String
> contexts
) {
328 fAvailableContexts
= new ArrayList
<Context
>();
329 if (contexts
!= null) {
330 for (Iterator
<String
> iterator
= contexts
.iterator(); iterator
.hasNext();) {
331 String name
= iterator
.next();
332 fAvailableContexts
.add(new Context(this, name
));
338 public String
getName() {
339 return Messages
.TraceControl_AddContextAllLabel
;
343 public Object
[] getChildren() {
344 return fAvailableContexts
.toArray();
348 public Object
getParent() {
353 public boolean hasChildren() {
359 * Model element (the context) for the context tree viewer
361 public static class Context
implements IContextModelComponent
{
363 private final String fContextName
;
364 private final IContextModelComponent fParent
;
370 * The parent component
372 * The name of this context
374 public Context(IContextModelComponent parent
, String name
) {
380 public String
getName() {
385 public Object
getParent() {
390 public Object
[] getChildren() {
395 public boolean hasChildren() {
401 * Interface for the tree model used for the context tree viewer.
403 public interface IContextModelComponent
{
406 * @return The name of this component
408 public String
getName();
411 * @return The parent component
413 public Object
getParent();
416 * @return The array of children of this component
418 public Object
[] getChildren();
421 * @return If this component has children or not
423 public boolean hasChildren();