| 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.model.impl; |
| 13 | |
| 14 | import java.util.ArrayList; |
| 15 | import java.util.Iterator; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import org.eclipse.core.runtime.ListenerList; |
| 19 | import org.eclipse.linuxtools.internal.lttng2.core.control.model.TargetNodeState; |
| 20 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
| 21 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent; |
| 22 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponentChangedListener; |
| 23 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService; |
| 24 | import org.eclipse.swt.graphics.Image; |
| 25 | |
| 26 | /** |
| 27 | * <p> |
| 28 | * Base implementation for trace control component. |
| 29 | * </p> |
| 30 | * |
| 31 | * @author Bernd Hufmann |
| 32 | */ |
| 33 | public class TraceControlComponent implements ITraceControlComponent { |
| 34 | |
| 35 | // ------------------------------------------------------------------------ |
| 36 | // Constants |
| 37 | // ------------------------------------------------------------------------ |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | // Attributes |
| 41 | // ------------------------------------------------------------------------ |
| 42 | /** |
| 43 | * The name of the component |
| 44 | */ |
| 45 | private String fName = ""; //$NON-NLS-1$ |
| 46 | /** |
| 47 | * The image to be displayed for the component. |
| 48 | */ |
| 49 | private Image fImage = null; |
| 50 | /** |
| 51 | * The tool tip to be displayed for the component. |
| 52 | */ |
| 53 | private String fToolTip = null; |
| 54 | /** |
| 55 | * The parent component. |
| 56 | */ |
| 57 | private ITraceControlComponent fParent = null; |
| 58 | /** |
| 59 | * The list if children components. |
| 60 | */ |
| 61 | private final List<ITraceControlComponent> fChildren = new ArrayList<ITraceControlComponent>(); |
| 62 | /** |
| 63 | * The list of listeners to be notified about changes. |
| 64 | */ |
| 65 | private final ListenerList fListeners = new ListenerList(); |
| 66 | |
| 67 | // ------------------------------------------------------------------------ |
| 68 | // Constructors |
| 69 | // ------------------------------------------------------------------------ |
| 70 | |
| 71 | /** |
| 72 | * Constructor |
| 73 | * @param name - the name of the component. |
| 74 | */ |
| 75 | public TraceControlComponent(String name) { |
| 76 | this(name, null); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Constructor |
| 81 | * @param name - the name of the component. |
| 82 | * @param parent - the parent component. |
| 83 | */ |
| 84 | public TraceControlComponent(String name, ITraceControlComponent parent) { |
| 85 | fName = name; |
| 86 | fParent = parent; |
| 87 | } |
| 88 | |
| 89 | // ------------------------------------------------------------------------ |
| 90 | // Accessors |
| 91 | // ------------------------------------------------------------------------ |
| 92 | /* |
| 93 | * (non-Javadoc) |
| 94 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#getName() |
| 95 | */ |
| 96 | @Override |
| 97 | public String getName() { |
| 98 | return fName; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * (non-Javadoc) |
| 103 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#setName(java.lang.String) |
| 104 | */ |
| 105 | @Override |
| 106 | public void setName(String name) { |
| 107 | fName = name; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * (non-Javadoc) |
| 112 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#getImage() |
| 113 | */ |
| 114 | @Override |
| 115 | public Image getImage() { |
| 116 | return fImage; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * (non-Javadoc) |
| 121 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#setImage(java.lang.String) |
| 122 | */ |
| 123 | @Override |
| 124 | public void setImage(String path) { |
| 125 | fImage = Activator.getDefault().loadIcon(path); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * (non-Javadoc) |
| 130 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#setImage(org.eclipse.swt.graphics.Image) |
| 131 | */ |
| 132 | @Override |
| 133 | public void setImage(Image image) { |
| 134 | fImage = image; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * (non-Javadoc) |
| 139 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#getToolTip() |
| 140 | */ |
| 141 | @Override |
| 142 | public String getToolTip() { |
| 143 | return fToolTip; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * (non-Javadoc) |
| 148 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#setToolTip(java.lang.String) |
| 149 | */ |
| 150 | @Override |
| 151 | public void setToolTip(String toolTip) { |
| 152 | fToolTip = toolTip; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * (non-Javadoc) |
| 157 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#getTargetNodeState() |
| 158 | */ |
| 159 | @Override |
| 160 | public TargetNodeState getTargetNodeState() { |
| 161 | if (getParent() != null) { |
| 162 | return getParent().getTargetNodeState(); |
| 163 | } |
| 164 | return TargetNodeState.DISCONNECTED; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * (non-Javadoc) |
| 169 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#setTargetNodeState(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent.TargetNodeState) |
| 170 | */ |
| 171 | @Override |
| 172 | public void setTargetNodeState(TargetNodeState state) { |
| 173 | if (getParent() != null) { |
| 174 | getParent().setTargetNodeState(state); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * (non-Javadoc) |
| 180 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#getParent() |
| 181 | */ |
| 182 | @Override |
| 183 | public ITraceControlComponent getParent() { |
| 184 | return fParent; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * (non-Javadoc) |
| 189 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#setParent(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent) |
| 190 | */ |
| 191 | @Override |
| 192 | public void setParent(ITraceControlComponent parent) { |
| 193 | fParent = parent; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * (non-Javadoc) |
| 198 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#getChildren() |
| 199 | */ |
| 200 | @Override |
| 201 | public ITraceControlComponent[] getChildren() { |
| 202 | return fChildren.toArray(new ITraceControlComponent[fChildren.size()]); |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponents#etChildren(java.util.List) |
| 207 | */ |
| 208 | @Override |
| 209 | public void setChildren(List<ITraceControlComponent> children) { |
| 210 | for (Iterator<ITraceControlComponent> iterator = children.iterator(); iterator.hasNext();) { |
| 211 | ITraceControlComponent traceControlComponent = iterator.next(); |
| 212 | fChildren.add(traceControlComponent); |
| 213 | fireComponentChanged(this); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * (non-Javadoc) |
| 219 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#getChild(java.lang.String) |
| 220 | */ |
| 221 | @Override |
| 222 | public ITraceControlComponent getChild(String name) { |
| 223 | ITraceControlComponent child = null; |
| 224 | for (int i = 0; i < fChildren.size(); i++) { |
| 225 | if (fChildren.get(i).getName().equals(name)) { |
| 226 | child = fChildren.get(i); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | return child; |
| 231 | } |
| 232 | /* |
| 233 | * (non-Javadoc) |
| 234 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#getChildren(java.lang.Class) |
| 235 | */ |
| 236 | @Override |
| 237 | public List<ITraceControlComponent> getChildren(Class<? extends ITraceControlComponent> clazz) { |
| 238 | List<ITraceControlComponent> list = new ArrayList<ITraceControlComponent>(); |
| 239 | |
| 240 | for (Iterator<ITraceControlComponent> iterator = fChildren.iterator(); iterator.hasNext();) { |
| 241 | ITraceControlComponent child = iterator.next(); |
| 242 | if (child.getClass() == clazz) { |
| 243 | list.add(child); |
| 244 | } |
| 245 | } |
| 246 | return list; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * (non-Javadoc) |
| 251 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#getControlService() |
| 252 | */ |
| 253 | @Override |
| 254 | public ILttngControlService getControlService() { |
| 255 | if (getParent() != null) { |
| 256 | return getParent().getControlService(); |
| 257 | } |
| 258 | return null; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * (non-Javadoc) |
| 263 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#setControlService(org.eclipse.linuxtools.internal.lttng2.ui.views.control.service.ILttngControlService) |
| 264 | */ |
| 265 | @Override |
| 266 | public void setControlService(ILttngControlService service) { |
| 267 | if (getParent() != null) { |
| 268 | getParent().setControlService(service); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // ------------------------------------------------------------------------ |
| 273 | // Operations |
| 274 | // ------------------------------------------------------------------------ |
| 275 | /* |
| 276 | * (non-Javadoc) |
| 277 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#dispose() |
| 278 | */ |
| 279 | @Override |
| 280 | public void dispose() { |
| 281 | // default implementation |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * (non-Javadoc) |
| 286 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#addChild(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent) |
| 287 | */ |
| 288 | @Override |
| 289 | public void addChild(ITraceControlComponent component) { |
| 290 | if (component != null) { |
| 291 | fChildren.add(component); |
| 292 | } |
| 293 | fireComponentAdded(this, component); |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * (non-Javadoc) |
| 298 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#removeChild(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent) |
| 299 | */ |
| 300 | @Override |
| 301 | public void removeChild(ITraceControlComponent component) { |
| 302 | if (component != null) { |
| 303 | fChildren.remove(component); |
| 304 | component.dispose(); |
| 305 | } |
| 306 | fireComponentRemoved(this, component); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * (non-Javadoc) |
| 311 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#removeAllChildren() |
| 312 | */ |
| 313 | @Override |
| 314 | public void removeAllChildren() { |
| 315 | for (Iterator<ITraceControlComponent> iterator = fChildren.iterator(); iterator.hasNext();) { |
| 316 | ITraceControlComponent child = iterator.next(); |
| 317 | child.removeAllChildren(); |
| 318 | } |
| 319 | fChildren.clear(); |
| 320 | // fireCompenentChanged(this); |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * (non-Javadoc) |
| 325 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#containsChild(java.lang.String) |
| 326 | */ |
| 327 | @Override |
| 328 | public boolean containsChild(String name) { |
| 329 | boolean retValue = false; |
| 330 | for (int i = 0; i < fChildren.size(); i++) { |
| 331 | if (fChildren.get(i).getName().equals(name)) { |
| 332 | retValue = true; |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | return retValue; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * (non-Javadoc) |
| 341 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#hasChildren() |
| 342 | */ |
| 343 | @Override |
| 344 | public boolean hasChildren() { |
| 345 | return !fChildren.isEmpty(); |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * (non-Javadoc) |
| 350 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#addComponentListener(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponentChangedListener) |
| 351 | */ |
| 352 | @Override |
| 353 | public void addComponentListener(ITraceControlComponentChangedListener listener) { |
| 354 | if (fParent != null) { |
| 355 | fParent.addComponentListener(listener); |
| 356 | } else { |
| 357 | fListeners.add(listener); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * (non-Javadoc) |
| 363 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#removeComponentListener(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponentChangedListener) |
| 364 | */ |
| 365 | @Override |
| 366 | public void removeComponentListener(ITraceControlComponentChangedListener listener) { |
| 367 | if (fParent != null) { |
| 368 | fParent.removeComponentListener(listener); |
| 369 | } else { |
| 370 | fListeners.remove(listener); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * (non-Javadoc) |
| 376 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#fireCompenentAdded(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent, org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent) |
| 377 | */ |
| 378 | @Override |
| 379 | public void fireComponentAdded(ITraceControlComponent parent, ITraceControlComponent component) { |
| 380 | if (component == null) { |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | if (fParent != null) { |
| 385 | fParent.fireComponentAdded(parent, component); |
| 386 | } else { |
| 387 | Object[] listeners = fListeners.getListeners(); |
| 388 | for (int i = 0; i < listeners.length; i++) { |
| 389 | ITraceControlComponentChangedListener listener = (ITraceControlComponentChangedListener) listeners[i]; |
| 390 | listener.componentAdded(parent, component); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * (non-Javadoc) |
| 397 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#fireCompenentRemoved(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent, org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent) |
| 398 | */ |
| 399 | @Override |
| 400 | public void fireComponentRemoved(ITraceControlComponent parent, ITraceControlComponent component) { |
| 401 | if (component == null) { |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | if (fParent != null) { |
| 406 | fParent.fireComponentRemoved(parent, component); |
| 407 | } else { |
| 408 | Object[] listeners = fListeners.getListeners(); |
| 409 | for (int i = 0; i < listeners.length; i++) { |
| 410 | ITraceControlComponentChangedListener listener = (ITraceControlComponentChangedListener) listeners[i]; |
| 411 | listener.componentRemoved(parent, component); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * (non-Javadoc) |
| 418 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent#fireCompenentChanged(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent) |
| 419 | */ |
| 420 | @Override |
| 421 | public void fireComponentChanged(ITraceControlComponent component) { |
| 422 | if (component == null) { |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | if (fParent != null) { |
| 427 | fParent.fireComponentChanged(component); |
| 428 | } else { |
| 429 | Object[] listeners = fListeners.getListeners(); |
| 430 | for (int i = 0; i < listeners.length; i++) { |
| 431 | ITraceControlComponentChangedListener listener = (ITraceControlComponentChangedListener) listeners[i]; |
| 432 | listener.componentChanged(component); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * (non-Javadoc) |
| 439 | * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class) |
| 440 | */ |
| 441 | @Override |
| 442 | public Object getAdapter(Class adapter) { |
| 443 | return null; |
| 444 | } |
| 445 | } |