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