| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2005, 2008 IBM Corporation and others. |
| 3 | * Copyright (c) 2011, 2012 Ericsson. |
| 4 | * All rights reserved. This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * IBM - Initial API and implementation |
| 11 | * Bernd Hufmann - Updated for TMF |
| 12 | **********************************************************************/ |
| 13 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import org.eclipse.jface.viewers.ISelection; |
| 19 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 20 | import org.eclipse.jface.viewers.ISelectionProvider; |
| 21 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 22 | |
| 23 | /** |
| 24 | * <p> |
| 25 | * Informs all registered listeners of graph node selection change in the Frame. |
| 26 | * </p> |
| 27 | * |
| 28 | * @version 1.0 |
| 29 | * @author sveyrier |
| 30 | * |
| 31 | */ |
| 32 | public class SDWidgetSelectionProvider implements ISelectionProvider { |
| 33 | |
| 34 | // ------------------------------------------------------------------------ |
| 35 | // Attributes |
| 36 | // ------------------------------------------------------------------------ |
| 37 | |
| 38 | /** |
| 39 | * The listener list |
| 40 | */ |
| 41 | protected List<ISelectionChangedListener> fListenerList = null; |
| 42 | /** |
| 43 | * The current selection |
| 44 | */ |
| 45 | protected ISelection fCurrentSelection = null; |
| 46 | |
| 47 | // ------------------------------------------------------------------------ |
| 48 | // Constructor |
| 49 | // ------------------------------------------------------------------------ |
| 50 | /** |
| 51 | * Standard constructor |
| 52 | */ |
| 53 | protected SDWidgetSelectionProvider() { |
| 54 | fListenerList = new ArrayList<ISelectionChangedListener>(); |
| 55 | } |
| 56 | |
| 57 | // ------------------------------------------------------------------------ |
| 58 | // Methods |
| 59 | // ------------------------------------------------------------------------ |
| 60 | |
| 61 | /* |
| 62 | * (non-Javadoc) |
| 63 | * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) |
| 64 | */ |
| 65 | @Override |
| 66 | public void addSelectionChangedListener(ISelectionChangedListener listener) { |
| 67 | if (!fListenerList.contains(listener)) |
| 68 | fListenerList.add(listener); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * (non-Javadoc) |
| 73 | * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) |
| 74 | */ |
| 75 | @Override |
| 76 | public void removeSelectionChangedListener(ISelectionChangedListener listener) { |
| 77 | fListenerList.remove(listener); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * (non-Javadoc) |
| 82 | * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection) |
| 83 | */ |
| 84 | @Override |
| 85 | public void setSelection(ISelection selection) { |
| 86 | fCurrentSelection = selection; |
| 87 | for (int i = 0; i < fListenerList.size(); i++) { |
| 88 | ISelectionChangedListener list = (ISelectionChangedListener) fListenerList.get(i); |
| 89 | list.selectionChanged(new SelectionChangedEvent(this, fCurrentSelection)); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * (non-Javadoc) |
| 95 | * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection() |
| 96 | */ |
| 97 | @Override |
| 98 | public ISelection getSelection() { |
| 99 | return fCurrentSelection; |
| 100 | } |
| 101 | |
| 102 | } |