Commit | Line | Data |
---|---|---|
b793fbe1 BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2012 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
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; | |
27 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.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; | |
36 | ||
37 | /** | |
38 | * <b><u>AddContextDialog</u></b> | |
39 | * <p> | |
40 | * Dialog box for collecting information about contexts to be added to channels/events. | |
41 | * </p> | |
42 | */ | |
43 | public class AddContextDialog extends Dialog implements IAddContextDialog { | |
44 | ||
45 | // ------------------------------------------------------------------------ | |
46 | // Constants | |
47 | // ------------------------------------------------------------------------ | |
48 | ||
49 | /** | |
50 | * The icon file for this dialog box. | |
51 | */ | |
52 | public static final String ADD_CONTEXT_ICON_FILE = "icons/elcl16/edit.gif"; //$NON-NLS-1$ | |
53 | ||
54 | // ------------------------------------------------------------------------ | |
55 | // Attributes | |
56 | // ------------------------------------------------------------------------ | |
57 | /** | |
58 | * The dialog composite. | |
59 | */ | |
60 | private Composite fDialogComposite; | |
61 | /** | |
62 | * A tree viewer for displaying and selection of available contexts. | |
63 | */ | |
64 | private CheckboxTreeViewer fContextsViewer; | |
65 | /** | |
66 | * A Tree model for the checkbox tree viewer. | |
67 | */ | |
68 | private ContextModel fContextModel = new ContextModel(); | |
69 | /** | |
70 | * The contexts to add. | |
71 | */ | |
72 | private List<String> fSelectedContexts = new ArrayList<String>(); | |
73 | ||
74 | // ------------------------------------------------------------------------ | |
75 | // Constructors | |
76 | // ------------------------------------------------------------------------ | |
77 | /** | |
78 | * Constructor | |
79 | * @param shell - a shell for the display of the dialog | |
80 | */ | |
81 | public AddContextDialog(Shell shell) { | |
82 | super(shell); | |
83 | setShellStyle(SWT.RESIZE); | |
84 | } | |
85 | ||
86 | // ------------------------------------------------------------------------ | |
87 | // Accessors | |
88 | // ------------------------------------------------------------------------ | |
89 | ||
90 | /* | |
91 | * (non-Javadoc) | |
92 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IAddContextDialog#setAvalibleContexts(java.util.List) | |
93 | */ | |
94 | @Override | |
95 | public void setAvalibleContexts(List<String> contexts) { | |
96 | fContextModel.setAvalibleContexts(contexts); | |
97 | } | |
98 | ||
99 | /* | |
100 | * (non-Javadoc) | |
101 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IAddContextDialog#getContexts() | |
102 | */ | |
103 | @Override | |
104 | public List<String> getContexts() { | |
105 | List<String> ret = new ArrayList<String>(); | |
106 | ret.addAll(fSelectedContexts); | |
107 | return ret; | |
108 | } | |
109 | ||
110 | // ------------------------------------------------------------------------ | |
111 | // Operations | |
112 | // ------------------------------------------------------------------------ | |
113 | /* | |
114 | * (non-Javadoc) | |
115 | * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) | |
116 | */ | |
117 | @Override | |
118 | protected void configureShell(Shell newShell) { | |
119 | super.configureShell(newShell); | |
120 | newShell.setText(Messages.TraceControl_AddContextDialogTitle); | |
121 | newShell.setImage(Activator.getDefault().loadIcon(ADD_CONTEXT_ICON_FILE)); | |
122 | } | |
123 | ||
124 | /* | |
125 | * (non-Javadoc) | |
126 | * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) | |
127 | */ | |
128 | @Override | |
129 | protected Control createDialogArea(Composite parent) { | |
130 | ||
131 | // Main dialog panel | |
132 | fDialogComposite = new Composite(parent, SWT.NONE); | |
133 | GridLayout layout = new GridLayout(1, true); | |
134 | fDialogComposite.setLayout(layout); | |
135 | fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
136 | ||
137 | // Contexts list | |
138 | Group contextGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); | |
139 | contextGroup.setText(Messages.TraceControl_AddContextAvailableContextsLabel); | |
140 | layout = new GridLayout(1, true); | |
141 | contextGroup.setLayout(layout); | |
142 | contextGroup.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
143 | ||
144 | fContextsViewer = new CheckboxTreeViewer(contextGroup, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); | |
145 | fContextsViewer.getTree().setToolTipText(Messages.TraceControl_AddContextAvailableContextsTooltip); | |
146 | ||
147 | fContextsViewer.setContentProvider(new ContextsContentProvider()); | |
148 | fContextsViewer.setLabelProvider(new ContextsLabelProvider()); | |
149 | fContextsViewer.addCheckStateListener(new ContextCheckListener()); | |
150 | fContextsViewer.setInput(fContextModel); | |
151 | fContextsViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH)); | |
152 | ||
153 | getShell().setMinimumSize(new Point(500, 650)); | |
154 | ||
155 | return fDialogComposite; | |
156 | } | |
157 | ||
158 | /* | |
159 | * (non-Javadoc) | |
160 | * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) | |
161 | */ | |
162 | @Override | |
163 | protected void createButtonsForButtonBar(Composite parent) { | |
164 | createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$ | |
165 | } | |
166 | ||
167 | /* | |
168 | * (non-Javadoc) | |
169 | * @see org.eclipse.jface.dialogs.Dialog#okPressed() | |
170 | */ | |
171 | @Override | |
172 | protected void okPressed() { | |
173 | fSelectedContexts.clear(); | |
174 | ||
175 | Object[] checkedElements = fContextsViewer.getCheckedElements(); | |
176 | for (int i = 0; i < checkedElements.length; i++) { | |
177 | IContextModelComponent component = (IContextModelComponent)checkedElements[i]; | |
f455db37 BH |
178 | if (!Messages.TraceControl_AddContextAllLabel.equals(component.getName())) { |
179 | fSelectedContexts.add(component.getName()); | |
180 | } | |
b793fbe1 BH |
181 | } |
182 | ||
183 | // validation successful -> call super.okPressed() | |
184 | super.okPressed(); | |
185 | } | |
186 | ||
187 | // ------------------------------------------------------------------------ | |
188 | // Helper classes and methods | |
189 | // ------------------------------------------------------------------------ | |
190 | /** | |
191 | * Content provider for the contexts tree | |
192 | */ | |
193 | final public static class ContextsContentProvider implements ITreeContentProvider { | |
194 | ||
195 | @Override | |
196 | public void dispose() { | |
197 | } | |
198 | ||
199 | @Override | |
200 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
201 | } | |
202 | ||
203 | @Override | |
204 | public Object[] getElements(Object inputElement) { | |
205 | return getChildren(inputElement); | |
206 | } | |
207 | ||
208 | @Override | |
209 | public Object[] getChildren(Object parentElement) { | |
210 | if (parentElement instanceof IContextModelComponent) { | |
211 | return ((IContextModelComponent)parentElement).getChildren(); | |
212 | } | |
213 | return null; | |
214 | } | |
215 | ||
216 | @Override | |
217 | public Object getParent(Object element) { | |
218 | if (element instanceof IContextModelComponent) { | |
219 | return ((IContextModelComponent)element).getParent(); | |
220 | } | |
221 | return null; | |
222 | } | |
223 | ||
224 | @Override | |
225 | public boolean hasChildren(Object element) { | |
226 | if (element instanceof IContextModelComponent) { | |
227 | return ((IContextModelComponent)element).hasChildren(); | |
228 | } | |
229 | return false; | |
230 | } | |
231 | } | |
232 | ||
233 | /** | |
234 | * Label provider for the contexts tree | |
235 | */ | |
236 | final public static class ContextsLabelProvider extends ColumnLabelProvider { | |
237 | @Override | |
238 | public String getText(Object element) { | |
239 | ||
240 | if ((element != null) && (element instanceof IContextModelComponent)) { | |
241 | return ((IContextModelComponent)element).getName(); | |
242 | } | |
243 | ||
244 | return "";//$NON-NLS-1$ | |
245 | } | |
246 | } | |
247 | ||
248 | /** | |
249 | * Check state listener for the contexts tree. | |
250 | */ | |
251 | final public class ContextCheckListener implements ICheckStateListener { | |
252 | @Override | |
253 | public void checkStateChanged(CheckStateChangedEvent event) { | |
254 | if (event.getChecked()) { | |
255 | if (event.getElement() instanceof AllContexts) { | |
256 | fContextsViewer.setSubtreeChecked(event.getElement(), true); | |
257 | } | |
258 | } else { | |
259 | if (event.getElement() instanceof AllContexts) { | |
260 | fContextsViewer.setSubtreeChecked(event.getElement(), false); | |
261 | } else { | |
262 | IContextModelComponent component = (IContextModelComponent) event.getElement(); | |
263 | fContextsViewer.setChecked(component.getParent(), false); | |
264 | } | |
265 | } | |
266 | } | |
267 | } | |
268 | ||
269 | /** | |
270 | * Model for the context tree viewer (root component) | |
271 | */ | |
272 | public static class ContextModel implements IContextModelComponent { | |
273 | ||
274 | private AllContexts fAllContexts; | |
275 | ||
276 | public ContextModel() { | |
277 | fAllContexts = new AllContexts(this); | |
278 | } | |
279 | ||
280 | public void setAvalibleContexts(List<String> contexts) { | |
281 | fAllContexts.setAvalibleContexts(contexts); | |
282 | } | |
283 | ||
284 | @Override | |
285 | public String getName() { | |
286 | return "root"; //$NON-NLS-1$ | |
287 | } | |
288 | ||
289 | @Override | |
290 | public Object getParent() { | |
291 | return null; | |
292 | } | |
293 | ||
294 | @Override | |
295 | public Object[] getChildren() { | |
296 | Object[] ret = new Object[1]; | |
297 | ret[0] = fAllContexts; | |
298 | return ret; | |
299 | } | |
300 | ||
301 | @Override | |
302 | public boolean hasChildren() { | |
303 | return true; | |
304 | } | |
305 | } | |
306 | ||
307 | /** | |
308 | * Model element (to select/deselect) all contexts) for the context tree viewer | |
309 | */ | |
310 | public static class AllContexts implements IContextModelComponent { | |
311 | /** | |
312 | * The available list of contexts. | |
313 | */ | |
314 | private List<Context> fAvailableContexts; | |
315 | ||
316 | private IContextModelComponent fParent; | |
317 | ||
318 | public AllContexts(IContextModelComponent parent) { | |
319 | fParent = parent; | |
320 | } | |
321 | ||
322 | public void setAvalibleContexts(List<String> contexts) { | |
323 | fAvailableContexts = new ArrayList<Context>(); | |
324 | if (contexts != null) { | |
325 | for (Iterator<String> iterator = contexts.iterator(); iterator.hasNext();) { | |
326 | String name = (String) iterator.next(); | |
327 | fAvailableContexts.add(new Context(this, name)); | |
328 | } | |
329 | } | |
330 | } | |
331 | ||
332 | @Override | |
333 | public String getName() { | |
334 | return Messages.TraceControl_AddContextAllLabel; | |
335 | } | |
336 | ||
337 | @Override | |
338 | public Object[] getChildren() { | |
339 | return fAvailableContexts.toArray(); | |
340 | } | |
341 | ||
342 | @Override | |
343 | public Object getParent() { | |
344 | return fParent; | |
345 | } | |
346 | ||
347 | @Override | |
348 | public boolean hasChildren() { | |
349 | return true; | |
350 | } | |
351 | } | |
352 | ||
353 | /** | |
354 | * Model element (the context) for the context tree viewer | |
355 | */ | |
356 | public static class Context implements IContextModelComponent { | |
357 | ||
358 | private String fContextName; | |
359 | private IContextModelComponent fParent; | |
360 | ||
361 | public Context(IContextModelComponent parent, String name) { | |
362 | fParent = parent; | |
363 | fContextName = name; | |
364 | } | |
365 | ||
366 | @Override | |
367 | public String getName() { | |
368 | return fContextName; | |
369 | } | |
370 | ||
371 | @Override | |
372 | public Object getParent() { | |
373 | return fParent; | |
374 | } | |
375 | ||
376 | @Override | |
377 | public Object[] getChildren() { | |
378 | return null; | |
379 | } | |
380 | ||
381 | @Override | |
382 | public boolean hasChildren() { | |
383 | return false; | |
384 | } | |
385 | } | |
386 | ||
387 | /** | |
388 | * Interface for the tree model used for the context tree viewer. | |
389 | */ | |
390 | public interface IContextModelComponent { | |
391 | public String getName(); | |
392 | public Object getParent(); | |
393 | public Object[] getChildren(); | |
394 | public boolean hasChildren(); | |
395 | } | |
396 | } |