Commit | Line | Data |
---|---|---|
b793fbe1 | 1 | /********************************************************************** |
11252342 | 2 | * Copyright (c) 2012, 2013 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 | **********************************************************************/ | |
12 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs; | |
13 | ||
14 | import java.util.ArrayList; | |
15 | import java.util.Iterator; | |
16 | import java.util.List; | |
17 | ||
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; | |
9315aeee | 27 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
b793fbe1 BH |
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; | |
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 | */ |
44 | public 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 | * The dialog composite. | |
61 | */ | |
62 | private Composite fDialogComposite; | |
11252342 | 63 | |
b793fbe1 BH |
64 | /** |
65 | * A tree viewer for displaying and selection of available contexts. | |
66 | */ | |
67 | private CheckboxTreeViewer fContextsViewer; | |
11252342 | 68 | |
b793fbe1 BH |
69 | /** |
70 | * A Tree model for the checkbox tree viewer. | |
71 | */ | |
cfdb727a | 72 | private final ContextModel fContextModel = new ContextModel(); |
11252342 | 73 | |
b793fbe1 BH |
74 | /** |
75 | * The contexts to add. | |
76 | */ | |
cfdb727a AM |
77 | private final List<String> fSelectedContexts = new ArrayList<String>(); |
78 | ||
b793fbe1 BH |
79 | // ------------------------------------------------------------------------ |
80 | // Constructors | |
81 | // ------------------------------------------------------------------------ | |
11252342 | 82 | |
b793fbe1 BH |
83 | /** |
84 | * Constructor | |
85 | * @param shell - a shell for the display of the dialog | |
86 | */ | |
87 | public AddContextDialog(Shell shell) { | |
88 | super(shell); | |
89 | setShellStyle(SWT.RESIZE); | |
90 | } | |
cfdb727a | 91 | |
b793fbe1 BH |
92 | // ------------------------------------------------------------------------ |
93 | // Accessors | |
94 | // ------------------------------------------------------------------------ | |
cfdb727a | 95 | |
b793fbe1 BH |
96 | @Override |
97 | public void setAvalibleContexts(List<String> contexts) { | |
98 | fContextModel.setAvalibleContexts(contexts); | |
99 | } | |
cfdb727a | 100 | |
b793fbe1 BH |
101 | @Override |
102 | public List<String> getContexts() { | |
103 | List<String> ret = new ArrayList<String>(); | |
104 | ret.addAll(fSelectedContexts); | |
105 | return ret; | |
106 | } | |
107 | ||
108 | // ------------------------------------------------------------------------ | |
109 | // Operations | |
110 | // ------------------------------------------------------------------------ | |
11252342 | 111 | |
b793fbe1 BH |
112 | @Override |
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)); | |
117 | } | |
118 | ||
b793fbe1 BH |
119 | @Override |
120 | protected Control createDialogArea(Composite parent) { | |
121 | ||
122 | // Main dialog panel | |
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)); | |
127 | ||
128 | // Contexts list | |
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)); | |
cfdb727a | 134 | |
b793fbe1 | 135 | fContextsViewer = new CheckboxTreeViewer(contextGroup, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); |
cfdb727a | 136 | fContextsViewer.getTree().setToolTipText(Messages.TraceControl_AddContextAvailableContextsTooltip); |
b793fbe1 BH |
137 | |
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)); | |
cfdb727a | 143 | |
0886cf00 | 144 | getShell().setMinimumSize(new Point(500, 450)); |
cfdb727a | 145 | |
b793fbe1 BH |
146 | return fDialogComposite; |
147 | } | |
148 | ||
b793fbe1 BH |
149 | @Override |
150 | protected void createButtonsForButtonBar(Composite parent) { | |
79c3db85 | 151 | createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", true); //$NON-NLS-1$ |
b793fbe1 BH |
152 | createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$ |
153 | } | |
154 | ||
b793fbe1 BH |
155 | @Override |
156 | protected void okPressed() { | |
157 | fSelectedContexts.clear(); | |
158 | ||
159 | Object[] checkedElements = fContextsViewer.getCheckedElements(); | |
160 | for (int i = 0; i < checkedElements.length; i++) { | |
161 | IContextModelComponent component = (IContextModelComponent)checkedElements[i]; | |
f455db37 BH |
162 | if (!Messages.TraceControl_AddContextAllLabel.equals(component.getName())) { |
163 | fSelectedContexts.add(component.getName()); | |
164 | } | |
b793fbe1 | 165 | } |
cfdb727a | 166 | |
b793fbe1 BH |
167 | // validation successful -> call super.okPressed() |
168 | super.okPressed(); | |
169 | } | |
cfdb727a | 170 | |
b793fbe1 BH |
171 | // ------------------------------------------------------------------------ |
172 | // Helper classes and methods | |
173 | // ------------------------------------------------------------------------ | |
174 | /** | |
175 | * Content provider for the contexts tree | |
176 | */ | |
177 | final public static class ContextsContentProvider implements ITreeContentProvider { | |
178 | ||
179 | @Override | |
180 | public void dispose() { | |
181 | } | |
182 | ||
183 | @Override | |
184 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
185 | } | |
186 | ||
187 | @Override | |
188 | public Object[] getElements(Object inputElement) { | |
189 | return getChildren(inputElement); | |
190 | } | |
191 | ||
192 | @Override | |
193 | public Object[] getChildren(Object parentElement) { | |
194 | if (parentElement instanceof IContextModelComponent) { | |
195 | return ((IContextModelComponent)parentElement).getChildren(); | |
196 | } | |
197 | return null; | |
198 | } | |
199 | ||
200 | @Override | |
201 | public Object getParent(Object element) { | |
202 | if (element instanceof IContextModelComponent) { | |
203 | return ((IContextModelComponent)element).getParent(); | |
204 | } | |
205 | return null; | |
206 | } | |
207 | ||
208 | @Override | |
209 | public boolean hasChildren(Object element) { | |
210 | if (element instanceof IContextModelComponent) { | |
211 | return ((IContextModelComponent)element).hasChildren(); | |
212 | } | |
213 | return false; | |
214 | } | |
215 | } | |
216 | ||
217 | /** | |
218 | * Label provider for the contexts tree | |
219 | */ | |
220 | final public static class ContextsLabelProvider extends ColumnLabelProvider { | |
221 | @Override | |
222 | public String getText(Object element) { | |
cfdb727a | 223 | |
b793fbe1 BH |
224 | if ((element != null) && (element instanceof IContextModelComponent)) { |
225 | return ((IContextModelComponent)element).getName(); | |
226 | } | |
227 | ||
228 | return "";//$NON-NLS-1$ | |
229 | } | |
230 | } | |
cfdb727a | 231 | |
b793fbe1 | 232 | /** |
cfdb727a | 233 | * Check state listener for the contexts tree. |
b793fbe1 BH |
234 | */ |
235 | final public class ContextCheckListener implements ICheckStateListener { | |
236 | @Override | |
237 | public void checkStateChanged(CheckStateChangedEvent event) { | |
238 | if (event.getChecked()) { | |
239 | if (event.getElement() instanceof AllContexts) { | |
240 | fContextsViewer.setSubtreeChecked(event.getElement(), true); | |
cfdb727a AM |
241 | } |
242 | } else { | |
b793fbe1 BH |
243 | if (event.getElement() instanceof AllContexts) { |
244 | fContextsViewer.setSubtreeChecked(event.getElement(), false); | |
245 | } else { | |
246 | IContextModelComponent component = (IContextModelComponent) event.getElement(); | |
247 | fContextsViewer.setChecked(component.getParent(), false); | |
248 | } | |
249 | } | |
250 | } | |
251 | } | |
252 | ||
253 | /** | |
254 | * Model for the context tree viewer (root component) | |
255 | */ | |
256 | public static class ContextModel implements IContextModelComponent { | |
257 | ||
cfdb727a AM |
258 | private final AllContexts fAllContexts; |
259 | ||
260 | /** | |
261 | * Constructor | |
262 | */ | |
b793fbe1 BH |
263 | public ContextModel() { |
264 | fAllContexts = new AllContexts(this); | |
265 | } | |
266 | ||
cfdb727a AM |
267 | /** |
268 | * Sets the available contexts | |
269 | * | |
270 | * @param contexts | |
271 | * The contexts to set | |
272 | */ | |
b793fbe1 BH |
273 | public void setAvalibleContexts(List<String> contexts) { |
274 | fAllContexts.setAvalibleContexts(contexts); | |
275 | } | |
cfdb727a | 276 | |
b793fbe1 BH |
277 | @Override |
278 | public String getName() { | |
279 | return "root"; //$NON-NLS-1$ | |
280 | } | |
cfdb727a | 281 | |
b793fbe1 BH |
282 | @Override |
283 | public Object getParent() { | |
284 | return null; | |
285 | } | |
286 | ||
287 | @Override | |
288 | public Object[] getChildren() { | |
289 | Object[] ret = new Object[1]; | |
290 | ret[0] = fAllContexts; | |
291 | return ret; | |
292 | } | |
293 | ||
294 | @Override | |
295 | public boolean hasChildren() { | |
296 | return true; | |
297 | } | |
298 | } | |
299 | ||
300 | /** | |
cfdb727a | 301 | * Model element (to select/deselect) all contexts) for the context tree viewer |
b793fbe1 BH |
302 | */ |
303 | public static class AllContexts implements IContextModelComponent { | |
304 | /** | |
305 | * The available list of contexts. | |
306 | */ | |
307 | private List<Context> fAvailableContexts; | |
b793fbe1 | 308 | |
cfdb727a AM |
309 | private final IContextModelComponent fParent; |
310 | ||
311 | /** | |
312 | * Constructor | |
313 | * | |
314 | * @param parent | |
315 | * The parent component | |
316 | */ | |
b793fbe1 BH |
317 | public AllContexts(IContextModelComponent parent) { |
318 | fParent = parent; | |
319 | } | |
cfdb727a AM |
320 | |
321 | /** | |
322 | * Sets the available contexts | |
323 | * | |
324 | * @param contexts | |
325 | * The contexts to set | |
326 | */ | |
b793fbe1 BH |
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();) { | |
cfdb727a | 331 | String name = iterator.next(); |
b793fbe1 BH |
332 | fAvailableContexts.add(new Context(this, name)); |
333 | } | |
334 | } | |
335 | } | |
336 | ||
337 | @Override | |
338 | public String getName() { | |
339 | return Messages.TraceControl_AddContextAllLabel; | |
340 | } | |
cfdb727a | 341 | |
b793fbe1 BH |
342 | @Override |
343 | public Object[] getChildren() { | |
344 | return fAvailableContexts.toArray(); | |
345 | } | |
cfdb727a | 346 | |
b793fbe1 BH |
347 | @Override |
348 | public Object getParent() { | |
349 | return fParent; | |
350 | } | |
cfdb727a | 351 | |
b793fbe1 BH |
352 | @Override |
353 | public boolean hasChildren() { | |
354 | return true; | |
355 | } | |
356 | } | |
357 | ||
358 | /** | |
cfdb727a | 359 | * Model element (the context) for the context tree viewer |
b793fbe1 BH |
360 | */ |
361 | public static class Context implements IContextModelComponent { | |
362 | ||
cfdb727a AM |
363 | private final String fContextName; |
364 | private final IContextModelComponent fParent; | |
365 | ||
366 | /** | |
367 | * Constructor | |
368 | * | |
369 | * @param parent | |
370 | * The parent component | |
371 | * @param name | |
372 | * The name of this context | |
373 | */ | |
b793fbe1 BH |
374 | public Context(IContextModelComponent parent, String name) { |
375 | fParent = parent; | |
376 | fContextName = name; | |
377 | } | |
cfdb727a | 378 | |
b793fbe1 BH |
379 | @Override |
380 | public String getName() { | |
381 | return fContextName; | |
382 | } | |
383 | ||
384 | @Override | |
385 | public Object getParent() { | |
386 | return fParent; | |
387 | } | |
388 | ||
389 | @Override | |
390 | public Object[] getChildren() { | |
391 | return null; | |
392 | } | |
393 | ||
394 | @Override | |
395 | public boolean hasChildren() { | |
396 | return false; | |
397 | } | |
398 | } | |
399 | ||
400 | /** | |
cfdb727a | 401 | * Interface for the tree model used for the context tree viewer. |
b793fbe1 BH |
402 | */ |
403 | public interface IContextModelComponent { | |
cfdb727a AM |
404 | |
405 | /** | |
406 | * @return The name of this component | |
407 | */ | |
b793fbe1 | 408 | public String getName(); |
cfdb727a AM |
409 | |
410 | /** | |
411 | * @return The parent component | |
412 | */ | |
b793fbe1 | 413 | public Object getParent(); |
cfdb727a AM |
414 | |
415 | /** | |
416 | * @return The array of children of this component | |
417 | */ | |
b793fbe1 | 418 | public Object[] getChildren(); |
cfdb727a AM |
419 | |
420 | /** | |
421 | * @return If this component has children or not | |
422 | */ | |
b793fbe1 BH |
423 | public boolean hasChildren(); |
424 | } | |
425 | } |