1 /**********************************************************************
2 * Copyright (c) 2012, 2014 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
.tracecompass
.internal
.lttng2
.control
.ui
.views
.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
.swt
.SWT
;
27 import org
.eclipse
.swt
.graphics
.Point
;
28 import org
.eclipse
.swt
.layout
.GridData
;
29 import org
.eclipse
.swt
.layout
.GridLayout
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.swt
.widgets
.Control
;
32 import org
.eclipse
.swt
.widgets
.Group
;
33 import org
.eclipse
.swt
.widgets
.Shell
;
34 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.Activator
;
35 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.messages
.Messages
;
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 * A tree viewer for displaying and selection of available contexts.
62 private CheckboxTreeViewer fContextsViewer
;
65 * A Tree model for the checkbox tree viewer.
67 private final ContextModel fContextModel
= new ContextModel();
70 * The contexts to add.
72 private final List
<String
> fSelectedContexts
= new ArrayList
<>();
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
80 * @param shell - a shell for the display of the dialog
82 public AddContextDialog(Shell shell
) {
84 setShellStyle(SWT
.RESIZE
| getShellStyle());
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
92 public void setAvalibleContexts(List
<String
> contexts
) {
93 fContextModel
.setAvalibleContexts(contexts
);
97 public List
<String
> getContexts() {
98 List
<String
> ret
= new ArrayList
<>();
99 ret
.addAll(fSelectedContexts
);
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
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
));
115 protected Control
createDialogArea(Composite parent
) {
118 Composite dialogComposite
= new Composite(parent
, SWT
.NONE
);
119 GridLayout layout
= new GridLayout(1, true);
120 dialogComposite
.setLayout(layout
);
121 dialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
124 Group contextGroup
= new Group(dialogComposite
, SWT
.SHADOW_NONE
);
125 contextGroup
.setText(Messages
.TraceControl_AddContextAvailableContextsLabel
);
126 layout
= new GridLayout(1, true);
127 contextGroup
.setLayout(layout
);
128 contextGroup
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
130 fContextsViewer
= new CheckboxTreeViewer(contextGroup
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
131 fContextsViewer
.getTree().setToolTipText(Messages
.TraceControl_AddContextAvailableContextsTooltip
);
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
));
139 getShell().setMinimumSize(new Point(500, 450));
141 return dialogComposite
;
145 protected void createButtonsForButtonBar(Composite parent
) {
146 createButton(parent
, IDialogConstants
.CANCEL_ID
, "&Cancel", true); //$NON-NLS-1$
147 createButton(parent
, IDialogConstants
.OK_ID
, "&Ok", true); //$NON-NLS-1$
151 protected void okPressed() {
152 fSelectedContexts
.clear();
154 Object
[] checkedElements
= fContextsViewer
.getCheckedElements();
155 for (int i
= 0; i
< checkedElements
.length
; i
++) {
156 IContextModelComponent component
= (IContextModelComponent
)checkedElements
[i
];
157 if (!Messages
.TraceControl_AddContextAllLabel
.equals(component
.getName())) {
158 fSelectedContexts
.add(component
.getName());
162 // validation successful -> call super.okPressed()
166 // ------------------------------------------------------------------------
167 // Helper classes and methods
168 // ------------------------------------------------------------------------
170 * Content provider for the contexts tree
172 public static final class ContextsContentProvider
implements ITreeContentProvider
{
175 public void dispose() {
179 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
183 public Object
[] getElements(Object inputElement
) {
184 return getChildren(inputElement
);
188 public Object
[] getChildren(Object parentElement
) {
189 if (parentElement
instanceof IContextModelComponent
) {
190 return ((IContextModelComponent
)parentElement
).getChildren();
196 public Object
getParent(Object element
) {
197 if (element
instanceof IContextModelComponent
) {
198 return ((IContextModelComponent
)element
).getParent();
204 public boolean hasChildren(Object element
) {
205 if (element
instanceof IContextModelComponent
) {
206 return ((IContextModelComponent
)element
).hasChildren();
213 * Label provider for the contexts tree
215 public static final class ContextsLabelProvider
extends ColumnLabelProvider
{
217 public String
getText(Object element
) {
219 if ((element
!= null) && (element
instanceof IContextModelComponent
)) {
220 return ((IContextModelComponent
)element
).getName();
223 return "";//$NON-NLS-1$
228 * Check state listener for the contexts tree.
230 public final class ContextCheckListener
implements ICheckStateListener
{
232 public void checkStateChanged(CheckStateChangedEvent event
) {
233 if (event
.getChecked()) {
234 if (event
.getElement() instanceof AllContexts
) {
235 fContextsViewer
.setSubtreeChecked(event
.getElement(), true);
238 if (event
.getElement() instanceof AllContexts
) {
239 fContextsViewer
.setSubtreeChecked(event
.getElement(), false);
241 IContextModelComponent component
= (IContextModelComponent
) event
.getElement();
242 fContextsViewer
.setChecked(component
.getParent(), false);
249 * Model for the context tree viewer (root component)
251 public static class ContextModel
implements IContextModelComponent
{
253 private final AllContexts fAllContexts
;
258 public ContextModel() {
259 fAllContexts
= new AllContexts(this);
263 * Sets the available contexts
266 * The contexts to set
268 public void setAvalibleContexts(List
<String
> contexts
) {
269 fAllContexts
.setAvalibleContexts(contexts
);
273 public String
getName() {
274 return "root"; //$NON-NLS-1$
278 public Object
getParent() {
283 public Object
[] getChildren() {
284 Object
[] ret
= new Object
[1];
285 ret
[0] = fAllContexts
;
290 public boolean hasChildren() {
296 * Model element (to select/deselect) all contexts) for the context tree viewer
298 public static class AllContexts
implements IContextModelComponent
{
300 * The available list of contexts.
302 private List
<Context
> fAvailableContexts
;
304 private final IContextModelComponent fParent
;
310 * The parent component
312 public AllContexts(IContextModelComponent parent
) {
317 * Sets the available contexts
320 * The contexts to set
322 public void setAvalibleContexts(List
<String
> contexts
) {
323 fAvailableContexts
= new ArrayList
<>();
324 if (contexts
!= null) {
325 for (Iterator
<String
> iterator
= contexts
.iterator(); iterator
.hasNext();) {
326 String name
= iterator
.next();
327 fAvailableContexts
.add(new Context(this, name
));
333 public String
getName() {
334 return Messages
.TraceControl_AddContextAllLabel
;
338 public Object
[] getChildren() {
339 return fAvailableContexts
.toArray();
343 public Object
getParent() {
348 public boolean hasChildren() {
354 * Model element (the context) for the context tree viewer
356 public static class Context
implements IContextModelComponent
{
358 private final String fContextName
;
359 private final IContextModelComponent fParent
;
365 * The parent component
367 * The name of this context
369 public Context(IContextModelComponent parent
, String name
) {
375 public String
getName() {
380 public Object
getParent() {
385 public Object
[] getChildren() {
390 public boolean hasChildren() {
396 * Interface for the tree model used for the context tree viewer.
398 public interface IContextModelComponent
{
401 * @return The name of this component
406 * @return The parent component
411 * @return The array of children of this component
413 Object
[] getChildren();
416 * @return If this component has children or not
418 boolean hasChildren();