001: /*****************************************************************************************
002: * Copyright (c) 2007 Andrei Loskutov. All rights reserved. This program and the
003: * accompanying materials are made available under the terms of the BSD License which
004: * accompanies this distribution, and is available at
005: * http://www.opensource.org/licenses/bsd-license.php Contributor: Andrei Loskutov -
006: * initial API and implementation
007: ****************************************************************************************/package de.loskutov.bco.views;
008:
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.jface.viewers.IPostSelectionProvider;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.jface.viewers.ISelectionChangedListener;
016:
017: import de.loskutov.bco.BytecodeOutlinePlugin;
018:
019: /**
020: * Adapter for different selection provider in one view - text control and table control.
021: * See <a href="http://forge.objectweb.org/tracker/?func=detail&atid=100023&aid=304424&group_id=23">bug 304424</a>
022: * The main problem is, that
023: * <pre>
024: * getSite().setSelectionProvider(viewSelectionProvider);
025: * </pre>
026: * could be set only once per view, so that we cannot switch existing
027: * selection provider on the fly (or I have no idea how to do this simplier way).
028: * @author Andrei
029: */
030: class BCOViewSelectionProvider implements IPostSelectionProvider {
031:
032: private IPostSelectionProvider realProvider;
033: private List selProviders;
034: private ISelection selection;
035:
036: public BCOViewSelectionProvider() {
037: super ();
038: selProviders = new ArrayList();
039: }
040:
041: public void setCurrentSelectionProvider(
042: IPostSelectionProvider provider) {
043: if (!selProviders.contains(provider)) {
044: BytecodeOutlinePlugin
045: .log(
046: new Exception(
047: "Current selection provider is not registered yet"),
048: IStatus.WARNING);
049: return;
050: }
051: realProvider = provider;
052: if (selection != null) {
053: realProvider.setSelection(selection);
054: }
055: }
056:
057: public void registerSelectionProvider(
058: IPostSelectionProvider provider) {
059: if (!selProviders.contains(provider)) {
060: selProviders.add(provider);
061: }
062: }
063:
064: /* (non-Javadoc)
065: * @see org.eclipse.jface.viewers.IPostSelectionProvider#addPostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
066: */
067: public void addPostSelectionChangedListener(
068: ISelectionChangedListener listener) {
069: for (int i = 0; i < selProviders.size(); i++) {
070: IPostSelectionProvider provider = (IPostSelectionProvider) selProviders
071: .get(i);
072: provider.addPostSelectionChangedListener(listener);
073: }
074: }
075:
076: /* (non-Javadoc)
077: * @see org.eclipse.jface.viewers.IPostSelectionProvider#removePostSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
078: */
079: public void removePostSelectionChangedListener(
080: ISelectionChangedListener listener) {
081: for (int i = 0; i < selProviders.size(); i++) {
082: IPostSelectionProvider provider = (IPostSelectionProvider) selProviders
083: .get(i);
084: provider.removePostSelectionChangedListener(listener);
085: }
086: }
087:
088: /* (non-Javadoc)
089: * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
090: */
091: public void addSelectionChangedListener(
092: ISelectionChangedListener listener) {
093: for (int i = 0; i < selProviders.size(); i++) {
094: IPostSelectionProvider provider = (IPostSelectionProvider) selProviders
095: .get(i);
096: provider.addSelectionChangedListener(listener);
097: }
098: }
099:
100: /* (non-Javadoc)
101: * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
102: */
103: public ISelection getSelection() {
104: return realProvider != null ? realProvider.getSelection()
105: : null;
106: }
107:
108: /* (non-Javadoc)
109: * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
110: */
111: public void removeSelectionChangedListener(
112: ISelectionChangedListener listener) {
113: for (int i = 0; i < selProviders.size(); i++) {
114: IPostSelectionProvider provider = (IPostSelectionProvider) selProviders
115: .get(i);
116: provider.removeSelectionChangedListener(listener);
117: }
118: }
119:
120: /* (non-Javadoc)
121: * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
122: */
123: public void setSelection(ISelection selection) {
124: this.selection = selection;
125: if (realProvider != null) {
126: realProvider.setSelection(selection);
127: }
128: }
129:
130: }
|