KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > resources > actions > PropertiesActionProvider


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.ui.internal.navigator.resources.actions;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.Platform;
15 import org.eclipse.jface.action.IMenuManager;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.ISelectionChangedListener;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.jface.window.IShellProvider;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.IActionBars;
24 import org.eclipse.ui.actions.ActionContext;
25 import org.eclipse.ui.actions.ActionFactory;
26 import org.eclipse.ui.dialogs.PropertyDialogAction;
27 import org.eclipse.ui.navigator.CommonActionProvider;
28 import org.eclipse.ui.navigator.ICommonActionExtensionSite;
29 import org.eclipse.ui.navigator.ICommonMenuConstants;
30 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
31
32 /**
33  * Adds the properties action to the menu.
34  *
35  * @since 3.2
36  *
37  */

38 public class PropertiesActionProvider extends CommonActionProvider {
39
40     private PropertyDialogAction propertiesAction;
41     private ISelectionProvider delegateSelectionProvider;
42
43     public void init(final ICommonActionExtensionSite aSite) {
44         
45         delegateSelectionProvider = new DelegateSelectionProvider( aSite.getViewSite().getSelectionProvider());
46         propertiesAction = new PropertyDialogAction(new IShellProvider() {
47             public Shell getShell() {
48                 return aSite.getViewSite().getShell();
49             }
50         },delegateSelectionProvider);
51         propertiesAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.PROPERTIES);
52     }
53
54     /*
55      * (non-Javadoc)
56      *
57      * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
58      */

59     public void fillContextMenu(IMenuManager menu) {
60         super.fillContextMenu(menu);
61         menu.appendToGroup(ICommonMenuConstants.GROUP_PROPERTIES,
62                 propertiesAction);
63     }
64     
65     /* (non-Javadoc)
66      * @see org.eclipse.ui.actions.ActionGroup#fillActionBars(org.eclipse.ui.IActionBars)
67      */

68     public void fillActionBars(IActionBars actionBars) {
69         super.fillActionBars(actionBars);
70
71         actionBars.setGlobalActionHandler(ActionFactory.PROPERTIES.getId(),
72                 propertiesAction);
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see org.eclipse.ui.actions.ActionGroup#setContext(org.eclipse.ui.actions.ActionContext)
79      */

80     public void setContext(ActionContext context) {
81         super.setContext(context);
82
83         propertiesAction.selectionChanged(delegateSelectionProvider
84                 .getSelection());
85
86     }
87
88     private class DelegateIAdaptable implements IAdaptable {
89
90         private Object JavaDoc delegate;
91
92         private DelegateIAdaptable(Object JavaDoc o) {
93             delegate = o;
94         }
95
96         /*
97          * (non-Javadoc)
98          *
99          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
100          */

101         public Object JavaDoc getAdapter(Class JavaDoc adapter) {
102             if (adapter.isInstance(delegate)) {
103                 return delegate;
104             }
105             return Platform.getAdapterManager().getAdapter(delegate, adapter);
106         }
107     }
108
109     private class DelegateSelectionProvider implements ISelectionProvider {
110
111         private ISelectionProvider delegate;
112
113         private DelegateSelectionProvider(ISelectionProvider s) {
114             delegate = s;
115         }
116
117         /*
118          * (non-Javadoc)
119          *
120          * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
121          */

122         public void addSelectionChangedListener(
123                 ISelectionChangedListener listener) {
124             delegate.addSelectionChangedListener(listener);
125
126         }
127
128         /*
129          * (non-Javadoc)
130          *
131          * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
132          */

133         public ISelection getSelection() {
134             if (delegate.getSelection() instanceof IStructuredSelection) {
135                 IStructuredSelection sSel = (IStructuredSelection) delegate
136                         .getSelection();
137                 if (sSel.getFirstElement() instanceof IAdaptable) {
138                     return sSel;
139                 }
140
141                 return new StructuredSelection(new DelegateIAdaptable(sSel
142                         .getFirstElement()));
143             }
144             return delegate.getSelection();
145         }
146
147         /*
148          * (non-Javadoc)
149          *
150          * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
151          */

152         public void removeSelectionChangedListener(
153                 ISelectionChangedListener listener) {
154             delegate.removeSelectionChangedListener(listener);
155
156         }
157
158         /*
159          * (non-Javadoc)
160          *
161          * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
162          */

163         public void setSelection(ISelection selection) {
164             delegate.setSelection(selection);
165
166         }
167
168     }
169
170 }
171
Popular Tags