| 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.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 | * <p> |
| 39 | * Dialog box for collecting information about contexts to be added to channels/events. |
| 40 | * </p> |
| 41 | * |
| 42 | * @author Bernd Hufmann |
| 43 | */ |
| 44 | public class AddContextDialog extends Dialog implements IAddContextDialog { |
| 45 | |
| 46 | // ------------------------------------------------------------------------ |
| 47 | // Constants |
| 48 | // ------------------------------------------------------------------------ |
| 49 | |
| 50 | /** |
| 51 | * The icon file for this dialog box. |
| 52 | */ |
| 53 | public static final String ADD_CONTEXT_ICON_FILE = "icons/elcl16/add-context.gif"; //$NON-NLS-1$ |
| 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 | */ |
| 69 | private final ContextModel fContextModel = new ContextModel(); |
| 70 | /** |
| 71 | * The contexts to add. |
| 72 | */ |
| 73 | private final List<String> fSelectedContexts = new ArrayList<String>(); |
| 74 | |
| 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 | } |
| 86 | |
| 87 | // ------------------------------------------------------------------------ |
| 88 | // Accessors |
| 89 | // ------------------------------------------------------------------------ |
| 90 | |
| 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 | } |
| 99 | |
| 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)); |
| 144 | |
| 145 | fContextsViewer = new CheckboxTreeViewer(contextGroup, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); |
| 146 | fContextsViewer.getTree().setToolTipText(Messages.TraceControl_AddContextAvailableContextsTooltip); |
| 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)); |
| 153 | |
| 154 | getShell().setMinimumSize(new Point(500, 450)); |
| 155 | |
| 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) { |
| 165 | createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", true); //$NON-NLS-1$ |
| 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]; |
| 180 | if (!Messages.TraceControl_AddContextAllLabel.equals(component.getName())) { |
| 181 | fSelectedContexts.add(component.getName()); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // validation successful -> call super.okPressed() |
| 186 | super.okPressed(); |
| 187 | } |
| 188 | |
| 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) { |
| 241 | |
| 242 | if ((element != null) && (element instanceof IContextModelComponent)) { |
| 243 | return ((IContextModelComponent)element).getName(); |
| 244 | } |
| 245 | |
| 246 | return "";//$NON-NLS-1$ |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Check state listener for the contexts tree. |
| 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); |
| 259 | } |
| 260 | } else { |
| 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 | |
| 276 | private final AllContexts fAllContexts; |
| 277 | |
| 278 | /** |
| 279 | * Constructor |
| 280 | */ |
| 281 | public ContextModel() { |
| 282 | fAllContexts = new AllContexts(this); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Sets the available contexts |
| 287 | * |
| 288 | * @param contexts |
| 289 | * The contexts to set |
| 290 | */ |
| 291 | public void setAvalibleContexts(List<String> contexts) { |
| 292 | fAllContexts.setAvalibleContexts(contexts); |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public String getName() { |
| 297 | return "root"; //$NON-NLS-1$ |
| 298 | } |
| 299 | |
| 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 | /** |
| 319 | * Model element (to select/deselect) all contexts) for the context tree viewer |
| 320 | */ |
| 321 | public static class AllContexts implements IContextModelComponent { |
| 322 | /** |
| 323 | * The available list of contexts. |
| 324 | */ |
| 325 | private List<Context> fAvailableContexts; |
| 326 | |
| 327 | private final IContextModelComponent fParent; |
| 328 | |
| 329 | /** |
| 330 | * Constructor |
| 331 | * |
| 332 | * @param parent |
| 333 | * The parent component |
| 334 | */ |
| 335 | public AllContexts(IContextModelComponent parent) { |
| 336 | fParent = parent; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Sets the available contexts |
| 341 | * |
| 342 | * @param contexts |
| 343 | * The contexts to set |
| 344 | */ |
| 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();) { |
| 349 | String name = iterator.next(); |
| 350 | fAvailableContexts.add(new Context(this, name)); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public String getName() { |
| 357 | return Messages.TraceControl_AddContextAllLabel; |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public Object[] getChildren() { |
| 362 | return fAvailableContexts.toArray(); |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public Object getParent() { |
| 367 | return fParent; |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public boolean hasChildren() { |
| 372 | return true; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Model element (the context) for the context tree viewer |
| 378 | */ |
| 379 | public static class Context implements IContextModelComponent { |
| 380 | |
| 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 | */ |
| 392 | public Context(IContextModelComponent parent, String name) { |
| 393 | fParent = parent; |
| 394 | fContextName = name; |
| 395 | } |
| 396 | |
| 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 | /** |
| 419 | * Interface for the tree model used for the context tree viewer. |
| 420 | */ |
| 421 | public interface IContextModelComponent { |
| 422 | |
| 423 | /** |
| 424 | * @return The name of this component |
| 425 | */ |
| 426 | public String getName(); |
| 427 | |
| 428 | /** |
| 429 | * @return The parent component |
| 430 | */ |
| 431 | public Object getParent(); |
| 432 | |
| 433 | /** |
| 434 | * @return The array of children of this component |
| 435 | */ |
| 436 | public Object[] getChildren(); |
| 437 | |
| 438 | /** |
| 439 | * @return If this component has children or not |
| 440 | */ |
| 441 | public boolean hasChildren(); |
| 442 | } |
| 443 | } |