1 /**********************************************************************
2 * Copyright (c) 2012 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 // ------------------------------------------------------------------------
59 * The dialog composite.
61 private Composite fDialogComposite
;
63 * A tree viewer for displaying and selection of available contexts.
65 private CheckboxTreeViewer fContextsViewer
;
67 * A Tree model for the checkbox tree viewer.
69 private ContextModel fContextModel
= new ContextModel();
71 * The contexts to add.
73 private List
<String
> fSelectedContexts
= new ArrayList
<String
>();
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
80 * @param shell - a shell for the display of the dialog
82 public AddContextDialog(Shell shell
) {
84 setShellStyle(SWT
.RESIZE
);
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
93 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IAddContextDialog#setAvalibleContexts(java.util.List)
96 public void setAvalibleContexts(List
<String
> contexts
) {
97 fContextModel
.setAvalibleContexts(contexts
);
102 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IAddContextDialog#getContexts()
105 public List
<String
> getContexts() {
106 List
<String
> ret
= new ArrayList
<String
>();
107 ret
.addAll(fSelectedContexts
);
111 // ------------------------------------------------------------------------
113 // ------------------------------------------------------------------------
116 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
119 protected void configureShell(Shell newShell
) {
120 super.configureShell(newShell
);
121 newShell
.setText(Messages
.TraceControl_AddContextDialogTitle
);
122 newShell
.setImage(Activator
.getDefault().loadIcon(ADD_CONTEXT_ICON_FILE
));
127 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
130 protected Control
createDialogArea(Composite parent
) {
133 fDialogComposite
= new Composite(parent
, SWT
.NONE
);
134 GridLayout layout
= new GridLayout(1, true);
135 fDialogComposite
.setLayout(layout
);
136 fDialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
139 Group contextGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
140 contextGroup
.setText(Messages
.TraceControl_AddContextAvailableContextsLabel
);
141 layout
= new GridLayout(1, true);
142 contextGroup
.setLayout(layout
);
143 contextGroup
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
145 fContextsViewer
= new CheckboxTreeViewer(contextGroup
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
146 fContextsViewer
.getTree().setToolTipText(Messages
.TraceControl_AddContextAvailableContextsTooltip
);
148 fContextsViewer
.setContentProvider(new ContextsContentProvider());
149 fContextsViewer
.setLabelProvider(new ContextsLabelProvider());
150 fContextsViewer
.addCheckStateListener(new ContextCheckListener());
151 fContextsViewer
.setInput(fContextModel
);
152 fContextsViewer
.getTree().setLayoutData(new GridData(GridData
.FILL_BOTH
));
154 getShell().setMinimumSize(new Point(500, 450));
156 return fDialogComposite
;
161 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
164 protected void createButtonsForButtonBar(Composite parent
) {
165 createButton(parent
, IDialogConstants
.CANCEL_ID
, "&Cancel", true); //$NON-NLS-1$
166 createButton(parent
, IDialogConstants
.OK_ID
, "&Ok", true); //$NON-NLS-1$
171 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
174 protected void okPressed() {
175 fSelectedContexts
.clear();
177 Object
[] checkedElements
= fContextsViewer
.getCheckedElements();
178 for (int i
= 0; i
< checkedElements
.length
; i
++) {
179 IContextModelComponent component
= (IContextModelComponent
)checkedElements
[i
];
180 if (!Messages
.TraceControl_AddContextAllLabel
.equals(component
.getName())) {
181 fSelectedContexts
.add(component
.getName());
185 // validation successful -> call super.okPressed()
189 // ------------------------------------------------------------------------
190 // Helper classes and methods
191 // ------------------------------------------------------------------------
193 * Content provider for the contexts tree
195 final public static class ContextsContentProvider
implements ITreeContentProvider
{
198 public void dispose() {
202 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
206 public Object
[] getElements(Object inputElement
) {
207 return getChildren(inputElement
);
211 public Object
[] getChildren(Object parentElement
) {
212 if (parentElement
instanceof IContextModelComponent
) {
213 return ((IContextModelComponent
)parentElement
).getChildren();
219 public Object
getParent(Object element
) {
220 if (element
instanceof IContextModelComponent
) {
221 return ((IContextModelComponent
)element
).getParent();
227 public boolean hasChildren(Object element
) {
228 if (element
instanceof IContextModelComponent
) {
229 return ((IContextModelComponent
)element
).hasChildren();
236 * Label provider for the contexts tree
238 final public static class ContextsLabelProvider
extends ColumnLabelProvider
{
240 public String
getText(Object element
) {
242 if ((element
!= null) && (element
instanceof IContextModelComponent
)) {
243 return ((IContextModelComponent
)element
).getName();
246 return "";//$NON-NLS-1$
251 * Check state listener for the contexts tree.
253 final public class ContextCheckListener
implements ICheckStateListener
{
255 public void checkStateChanged(CheckStateChangedEvent event
) {
256 if (event
.getChecked()) {
257 if (event
.getElement() instanceof AllContexts
) {
258 fContextsViewer
.setSubtreeChecked(event
.getElement(), true);
261 if (event
.getElement() instanceof AllContexts
) {
262 fContextsViewer
.setSubtreeChecked(event
.getElement(), false);
264 IContextModelComponent component
= (IContextModelComponent
) event
.getElement();
265 fContextsViewer
.setChecked(component
.getParent(), false);
272 * Model for the context tree viewer (root component)
274 public static class ContextModel
implements IContextModelComponent
{
276 private AllContexts fAllContexts
;
278 public ContextModel() {
279 fAllContexts
= new AllContexts(this);
282 public void setAvalibleContexts(List
<String
> contexts
) {
283 fAllContexts
.setAvalibleContexts(contexts
);
287 public String
getName() {
288 return "root"; //$NON-NLS-1$
292 public Object
getParent() {
297 public Object
[] getChildren() {
298 Object
[] ret
= new Object
[1];
299 ret
[0] = fAllContexts
;
304 public boolean hasChildren() {
310 * Model element (to select/deselect) all contexts) for the context tree viewer
312 public static class AllContexts
implements IContextModelComponent
{
314 * The available list of contexts.
316 private List
<Context
> fAvailableContexts
;
318 private IContextModelComponent fParent
;
320 public AllContexts(IContextModelComponent parent
) {
324 public void setAvalibleContexts(List
<String
> contexts
) {
325 fAvailableContexts
= new ArrayList
<Context
>();
326 if (contexts
!= null) {
327 for (Iterator
<String
> iterator
= contexts
.iterator(); iterator
.hasNext();) {
328 String name
= (String
) iterator
.next();
329 fAvailableContexts
.add(new Context(this, name
));
335 public String
getName() {
336 return Messages
.TraceControl_AddContextAllLabel
;
340 public Object
[] getChildren() {
341 return fAvailableContexts
.toArray();
345 public Object
getParent() {
350 public boolean hasChildren() {
356 * Model element (the context) for the context tree viewer
358 public static class Context
implements IContextModelComponent
{
360 private String fContextName
;
361 private IContextModelComponent fParent
;
363 public Context(IContextModelComponent parent
, String name
) {
369 public String
getName() {
374 public Object
getParent() {
379 public Object
[] getChildren() {
384 public boolean hasChildren() {
390 * Interface for the tree model used for the context tree viewer.
392 public interface IContextModelComponent
{
393 public String
getName();
394 public Object
getParent();
395 public Object
[] getChildren();
396 public boolean hasChildren();