| 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]; |
| 178 | fSelectedContexts.add(component.getName()); |
| 179 | } |
| 180 | |
| 181 | // validation successful -> call super.okPressed() |
| 182 | super.okPressed(); |
| 183 | } |
| 184 | |
| 185 | // ------------------------------------------------------------------------ |
| 186 | // Helper classes and methods |
| 187 | // ------------------------------------------------------------------------ |
| 188 | /** |
| 189 | * Content provider for the contexts tree |
| 190 | */ |
| 191 | final public static class ContextsContentProvider implements ITreeContentProvider { |
| 192 | |
| 193 | @Override |
| 194 | public void dispose() { |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public Object[] getElements(Object inputElement) { |
| 203 | return getChildren(inputElement); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public Object[] getChildren(Object parentElement) { |
| 208 | if (parentElement instanceof IContextModelComponent) { |
| 209 | return ((IContextModelComponent)parentElement).getChildren(); |
| 210 | } |
| 211 | return null; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public Object getParent(Object element) { |
| 216 | if (element instanceof IContextModelComponent) { |
| 217 | return ((IContextModelComponent)element).getParent(); |
| 218 | } |
| 219 | return null; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public boolean hasChildren(Object element) { |
| 224 | if (element instanceof IContextModelComponent) { |
| 225 | return ((IContextModelComponent)element).hasChildren(); |
| 226 | } |
| 227 | return false; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Label provider for the contexts tree |
| 233 | */ |
| 234 | final public static class ContextsLabelProvider extends ColumnLabelProvider { |
| 235 | @Override |
| 236 | public String getText(Object element) { |
| 237 | |
| 238 | if ((element != null) && (element instanceof IContextModelComponent)) { |
| 239 | return ((IContextModelComponent)element).getName(); |
| 240 | } |
| 241 | |
| 242 | return "";//$NON-NLS-1$ |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Check state listener for the contexts tree. |
| 248 | */ |
| 249 | final public class ContextCheckListener implements ICheckStateListener { |
| 250 | @Override |
| 251 | public void checkStateChanged(CheckStateChangedEvent event) { |
| 252 | if (event.getChecked()) { |
| 253 | if (event.getElement() instanceof AllContexts) { |
| 254 | fContextsViewer.setSubtreeChecked(event.getElement(), true); |
| 255 | } |
| 256 | } else { |
| 257 | if (event.getElement() instanceof AllContexts) { |
| 258 | fContextsViewer.setSubtreeChecked(event.getElement(), false); |
| 259 | } else { |
| 260 | IContextModelComponent component = (IContextModelComponent) event.getElement(); |
| 261 | fContextsViewer.setChecked(component.getParent(), false); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Model for the context tree viewer (root component) |
| 269 | */ |
| 270 | public static class ContextModel implements IContextModelComponent { |
| 271 | |
| 272 | private AllContexts fAllContexts; |
| 273 | |
| 274 | public ContextModel() { |
| 275 | fAllContexts = new AllContexts(this); |
| 276 | } |
| 277 | |
| 278 | public void setAvalibleContexts(List<String> contexts) { |
| 279 | fAllContexts.setAvalibleContexts(contexts); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public String getName() { |
| 284 | return "root"; //$NON-NLS-1$ |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public Object getParent() { |
| 289 | return null; |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public Object[] getChildren() { |
| 294 | Object[] ret = new Object[1]; |
| 295 | ret[0] = fAllContexts; |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public boolean hasChildren() { |
| 301 | return true; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Model element (to select/deselect) all contexts) for the context tree viewer |
| 307 | */ |
| 308 | public static class AllContexts implements IContextModelComponent { |
| 309 | /** |
| 310 | * The available list of contexts. |
| 311 | */ |
| 312 | private List<Context> fAvailableContexts; |
| 313 | |
| 314 | private IContextModelComponent fParent; |
| 315 | |
| 316 | public AllContexts(IContextModelComponent parent) { |
| 317 | fParent = parent; |
| 318 | } |
| 319 | |
| 320 | public void setAvalibleContexts(List<String> contexts) { |
| 321 | fAvailableContexts = new ArrayList<Context>(); |
| 322 | if (contexts != null) { |
| 323 | for (Iterator<String> iterator = contexts.iterator(); iterator.hasNext();) { |
| 324 | String name = (String) iterator.next(); |
| 325 | fAvailableContexts.add(new Context(this, name)); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public String getName() { |
| 332 | return Messages.TraceControl_AddContextAllLabel; |
| 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public Object[] getChildren() { |
| 337 | return fAvailableContexts.toArray(); |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public Object getParent() { |
| 342 | return fParent; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public boolean hasChildren() { |
| 347 | return true; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Model element (the context) for the context tree viewer |
| 353 | */ |
| 354 | public static class Context implements IContextModelComponent { |
| 355 | |
| 356 | private String fContextName; |
| 357 | private IContextModelComponent fParent; |
| 358 | |
| 359 | public Context(IContextModelComponent parent, String name) { |
| 360 | fParent = parent; |
| 361 | fContextName = name; |
| 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public String getName() { |
| 366 | return fContextName; |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public Object getParent() { |
| 371 | return fParent; |
| 372 | } |
| 373 | |
| 374 | @Override |
| 375 | public Object[] getChildren() { |
| 376 | return null; |
| 377 | } |
| 378 | |
| 379 | @Override |
| 380 | public boolean hasChildren() { |
| 381 | return false; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Interface for the tree model used for the context tree viewer. |
| 387 | */ |
| 388 | public interface IContextModelComponent { |
| 389 | public String getName(); |
| 390 | public Object getParent(); |
| 391 | public Object[] getChildren(); |
| 392 | public boolean hasChildren(); |
| 393 | } |
| 394 | } |