| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 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 | * Francois Chouinard - Initial API and implementation |
| 11 | * Bernd Hufmann - Filled with content |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.linuxtools.internal.lttng.ui.views.control; |
| 15 | |
| 16 | import org.eclipse.jface.action.MenuManager; |
| 17 | import org.eclipse.jface.viewers.ColumnViewerToolTipSupport; |
| 18 | import org.eclipse.jface.viewers.ISelection; |
| 19 | import org.eclipse.jface.viewers.StructuredSelection; |
| 20 | import org.eclipse.jface.viewers.TreeViewer; |
| 21 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.ITraceControlComponent; |
| 22 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.ITraceControlComponentChangedListener; |
| 23 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.TraceControlRoot; |
| 24 | import org.eclipse.swt.SWT; |
| 25 | import org.eclipse.swt.widgets.Composite; |
| 26 | import org.eclipse.swt.widgets.Menu; |
| 27 | import org.eclipse.ui.part.ViewPart; |
| 28 | |
| 29 | /** |
| 30 | * <b><u>ControlView</u></b> |
| 31 | * <p> |
| 32 | * View implementation for Trace Control. |
| 33 | * </p> |
| 34 | */ |
| 35 | public class ControlView extends ViewPart implements ITraceControlComponentChangedListener { |
| 36 | |
| 37 | // ------------------------------------------------------------------------ |
| 38 | // Constants |
| 39 | // ------------------------------------------------------------------------ |
| 40 | /** |
| 41 | * View ID. |
| 42 | */ |
| 43 | public static final String ID = "org.eclipse.linuxtools.internal.lttng.ui.views.control"; //$NON-NLS-1$ |
| 44 | |
| 45 | // ------------------------------------------------------------------------ |
| 46 | // Attributes |
| 47 | // ------------------------------------------------------------------------ |
| 48 | |
| 49 | /** |
| 50 | * The tree viewer. |
| 51 | */ |
| 52 | private TreeViewer fTreeViewer = null; |
| 53 | |
| 54 | /** |
| 55 | * The trace control root node. This provides access to the whole model. |
| 56 | */ |
| 57 | private ITraceControlComponent fRoot = null; |
| 58 | |
| 59 | // ------------------------------------------------------------------------ |
| 60 | // Constructors |
| 61 | // ------------------------------------------------------------------------ |
| 62 | |
| 63 | // ------------------------------------------------------------------------ |
| 64 | // Accessors |
| 65 | // ------------------------------------------------------------------------ |
| 66 | |
| 67 | /** |
| 68 | * @return returns the trace control tree node (model). |
| 69 | */ |
| 70 | public ITraceControlComponent getTraceControlRoot() { |
| 71 | return fRoot; |
| 72 | } |
| 73 | |
| 74 | // ------------------------------------------------------------------------ |
| 75 | // Operations |
| 76 | // ------------------------------------------------------------------------ |
| 77 | |
| 78 | /* |
| 79 | * (non-Javadoc) |
| 80 | * |
| 81 | * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite) |
| 82 | */ |
| 83 | @Override |
| 84 | public void createPartControl(Composite parent) { |
| 85 | // Create tree viewer |
| 86 | fTreeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); |
| 87 | ColumnViewerToolTipSupport.enableFor(fTreeViewer); |
| 88 | |
| 89 | fTreeViewer.setContentProvider(new TraceControlContentProvider()); |
| 90 | fTreeViewer.setLabelProvider(new TraceControlLabelProvider()); |
| 91 | |
| 92 | // Create model root |
| 93 | fRoot = new TraceControlRoot(); |
| 94 | fRoot.addComponentListener(this); |
| 95 | fTreeViewer.setInput(fRoot); |
| 96 | |
| 97 | // Create context menu for the tree viewer |
| 98 | createContextMenu(); |
| 99 | |
| 100 | getSite().setSelectionProvider(fTreeViewer); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * (non-Javadoc) |
| 105 | * |
| 106 | * @see org.eclipse.ui.part.WorkbenchPart#setFocus() |
| 107 | */ |
| 108 | @Override |
| 109 | public void setFocus() { |
| 110 | fTreeViewer.getControl().setFocus(); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * (non-Javadoc) |
| 115 | * @see org.eclipse.linuxtools.internal.lttng.ui.views.control.model.ITraceControlComponentChangedListener#componentAdded(org.eclipse.linuxtools.internal.lttng.ui.views.control.model.ITraceControlComponent, org.eclipse.linuxtools.internal.lttng.ui.views.control.model.ITraceControlComponent) |
| 116 | */ |
| 117 | @Override |
| 118 | public void componentAdded(ITraceControlComponent parent, ITraceControlComponent component) { |
| 119 | componentChanged(component); |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * (non-Javadoc) |
| 124 | * @see org.eclipse.linuxtools.internal.lttng.ui.views.control.model.ITraceControlComponentChangedListener#componentRemoved(org.eclipse.linuxtools.internal.lttng.ui.views.control.model.ITraceControlComponent, org.eclipse.linuxtools.internal.lttng.ui.views.control.model.ITraceControlComponent) |
| 125 | */ |
| 126 | @Override |
| 127 | public void componentRemoved(ITraceControlComponent parent, ITraceControlComponent component) { |
| 128 | componentChanged(component); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * (non-Javadoc) |
| 133 | * @see org.eclipse.linuxtools.internal.lttng.ui.views.control.model.ITraceControlComponentChangedListener#componentChanged(org.eclipse.linuxtools.internal.lttng.ui.views.control.model.ITraceControlComponent) |
| 134 | */ |
| 135 | @Override |
| 136 | public void componentChanged(ITraceControlComponent component) { |
| 137 | if (fTreeViewer.getTree().isDisposed()) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | fTreeViewer.getTree().getDisplay().asyncExec(new Runnable() { |
| 142 | @Override |
| 143 | public void run() { |
| 144 | if (fTreeViewer.getTree().isDisposed()) { |
| 145 | return; |
| 146 | } |
| 147 | fTreeViewer.refresh(); |
| 148 | // Change selection needed |
| 149 | final ISelection sel = fTreeViewer.getSelection(); |
| 150 | fTreeViewer.setSelection(null); |
| 151 | fTreeViewer.setSelection(sel); |
| 152 | } |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Sets the selected component in the tree |
| 158 | * @param component - component to select |
| 159 | */ |
| 160 | public void setSelection(ITraceControlComponent component) { |
| 161 | StructuredSelection selection = new StructuredSelection(component); |
| 162 | fTreeViewer.setSelection(selection); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Sets the selected components in the tree |
| 167 | * @param component - array of components to select |
| 168 | */ |
| 169 | public void setSelection(ITraceControlComponent[] components) { |
| 170 | StructuredSelection selection = new StructuredSelection(components); |
| 171 | fTreeViewer.setSelection(selection); |
| 172 | } |
| 173 | |
| 174 | // public ITraceControlComponent getSelection() { |
| 175 | // ISelection selection = fTreeViewer.getSelection(); |
| 176 | // |
| 177 | // } |
| 178 | |
| 179 | // ------------------------------------------------------------------------ |
| 180 | // Helper methods |
| 181 | // ------------------------------------------------------------------------ |
| 182 | private void createContextMenu() { |
| 183 | // First we create a menu Manager |
| 184 | final MenuManager menuManager = new MenuManager(); |
| 185 | final Menu menu = menuManager.createContextMenu(fTreeViewer.getTree()); |
| 186 | // Set the MenuManager |
| 187 | fTreeViewer.getTree().setMenu(menu); |
| 188 | getSite().registerContextMenu(menuManager, fTreeViewer); |
| 189 | } |
| 190 | } |