KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > util > ViewSiteDecorator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.util;
12
13 import org.eclipse.core.runtime.PlatformObject;
14
15 import org.eclipse.swt.widgets.Shell;
16
17 import org.eclipse.jface.action.MenuManager;
18 import org.eclipse.jface.util.ListenerList;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.ISelectionChangedListener;
21 import org.eclipse.jface.viewers.ISelectionProvider;
22 import org.eclipse.jface.viewers.SelectionChangedEvent;
23
24 import org.eclipse.ui.IActionBars;
25 import org.eclipse.ui.IKeyBindingService;
26 import org.eclipse.ui.IViewSite;
27 import org.eclipse.ui.IWorkbenchPage;
28 import org.eclipse.ui.IWorkbenchPart;
29 import org.eclipse.ui.IWorkbenchWindow;
30
31 public class ViewSiteDecorator extends PlatformObject implements IViewSite {
32     
33     private IViewSite fViewSite;
34     private ISelectionConverter fSelectionConverter;
35     private ISelectionProvider fSelectionProvider;
36     
37     private class SelectionProviderDecorator implements ISelectionProvider {
38         
39         private ISelectionProvider fProvider;
40         private SelectionChangedListener fListener;
41         
42         public SelectionProviderDecorator(ISelectionProvider provider) {
43             fProvider= provider;
44         }
45         public ISelection getSelection() {
46             return fSelectionConverter.convert(fProvider.getSelection());
47         }
48         public void setSelection(ISelection selection) {
49             throw new UnsupportedOperationException JavaDoc();
50         }
51         public void addSelectionChangedListener(ISelectionChangedListener listener) {
52             if (fListener == null) {
53                 fListener= new SelectionChangedListener();
54                 fProvider.addSelectionChangedListener(fListener);
55             }
56             fListener.addListener(listener);
57         }
58         public void removeSelectionChangedListener(ISelectionChangedListener listener) {
59             if (fListener == null)
60                 return;
61             fListener.removeListener(listener);
62             if (fListener.isEmpty()) {
63                 fProvider.removeSelectionChangedListener(fListener);
64                 fListener= null;
65             }
66         }
67     }
68     
69     private class SelectionChangedListener implements ISelectionChangedListener {
70         
71         ListenerList fListeners= new ListenerList();
72         
73         public void selectionChanged(SelectionChangedEvent event) {
74             ISelection selection= fSelectionConverter.convert(event.getSelection());
75             SelectionChangedEvent newEvent= new SelectionChangedEvent(fSelectionProvider, selection);
76             Object JavaDoc[] listeners= fListeners.getListeners();
77             for (int i= 0; i < listeners.length; i++) {
78                 ((ISelectionChangedListener)listeners[i]).selectionChanged(newEvent);
79             }
80         }
81         public void addListener(ISelectionChangedListener listener) {
82             fListeners.add(listener);
83         }
84         public void removeListener(ISelectionChangedListener listener) {
85             fListeners.remove(listener);
86         }
87         public boolean isEmpty() {
88             return fListeners.isEmpty();
89         }
90     }
91     
92     public ViewSiteDecorator(IViewSite site, ISelectionConverter converter) {
93         fViewSite= site;
94         fSelectionConverter= converter;
95         ISelectionProvider provider= site.getSelectionProvider();
96         if (provider != null) {
97             fSelectionProvider= new SelectionProviderDecorator(provider);
98         }
99     }
100     /* (non-Javadoc)
101      * @see org.eclipse.ui.IWorkbenchPartSite#getPart()
102      */

103     public IWorkbenchPart getPart() {
104         return fViewSite.getPart();
105     }
106     public IActionBars getActionBars() {
107         return fViewSite.getActionBars();
108     }
109     public String JavaDoc getId() {
110         return fViewSite.getId();
111     }
112     public IKeyBindingService getKeyBindingService() {
113         return fViewSite.getKeyBindingService();
114     }
115     public String JavaDoc getPluginId() {
116         return fViewSite.getPluginId();
117     }
118     public String JavaDoc getRegisteredName() {
119         return fViewSite.getRegisteredName();
120     }
121     public String JavaDoc getSecondaryId() {
122         return fViewSite.getSecondaryId();
123     }
124     public void registerContextMenu(String JavaDoc menuId, MenuManager menuManager, ISelectionProvider selectionProvider) {
125         fViewSite.registerContextMenu(menuId, menuManager, selectionProvider);
126     }
127     public void registerContextMenu(MenuManager menuManager, ISelectionProvider selectionProvider) {
128         fViewSite.registerContextMenu(menuManager, selectionProvider);
129     }
130     public IWorkbenchPage getPage() {
131         return fViewSite.getPage();
132     }
133     public ISelectionProvider getSelectionProvider() {
134         return fSelectionProvider;
135     }
136     public Shell getShell() {
137         return fViewSite.getShell();
138     }
139     public IWorkbenchWindow getWorkbenchWindow() {
140         return fViewSite.getWorkbenchWindow();
141     }
142     public void setSelectionProvider(ISelectionProvider provider) {
143         throw new UnsupportedOperationException JavaDoc();
144     }
145 }
146
Popular Tags