KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > packageview > PackageExplorerActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
12 package org.eclipse.jdt.internal.ui.packageview;
13
14 import org.eclipse.core.resources.IContainer;
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.ResourcesPlugin;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.KeyEvent;
20
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.action.IToolBarManager;
24 import org.eclipse.jface.action.Separator;
25 import org.eclipse.jface.util.IPropertyChangeListener;
26 import org.eclipse.jface.util.OpenStrategy;
27 import org.eclipse.jface.util.PropertyChangeEvent;
28 import org.eclipse.jface.viewers.DoubleClickEvent;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.ITreeSelection;
31 import org.eclipse.jface.viewers.OpenEvent;
32 import org.eclipse.jface.viewers.TreePath;
33 import org.eclipse.jface.viewers.TreeViewer;
34
35 import org.eclipse.ui.IActionBars;
36 import org.eclipse.ui.IMemento;
37 import org.eclipse.ui.IWorkbenchActionConstants;
38 import org.eclipse.ui.IWorkbenchPartSite;
39 import org.eclipse.ui.IWorkingSet;
40 import org.eclipse.ui.IWorkingSetManager;
41 import org.eclipse.ui.actions.ActionFactory;
42 import org.eclipse.ui.actions.ActionGroup;
43 import org.eclipse.ui.actions.OpenInNewWindowAction;
44
45 import org.eclipse.ui.views.framelist.BackAction;
46 import org.eclipse.ui.views.framelist.ForwardAction;
47 import org.eclipse.ui.views.framelist.Frame;
48 import org.eclipse.ui.views.framelist.FrameAction;
49 import org.eclipse.ui.views.framelist.FrameList;
50 import org.eclipse.ui.views.framelist.GoIntoAction;
51 import org.eclipse.ui.views.framelist.TreeFrame;
52 import org.eclipse.ui.views.framelist.UpAction;
53
54 import org.eclipse.jdt.core.IClassFile;
55 import org.eclipse.jdt.core.ICompilationUnit;
56 import org.eclipse.jdt.core.IJavaElement;
57 import org.eclipse.jdt.core.IOpenable;
58 import org.eclipse.jdt.core.JavaCore;
59
60 import org.eclipse.jdt.ui.IContextMenuConstants;
61 import org.eclipse.jdt.ui.PreferenceConstants;
62 import org.eclipse.jdt.ui.actions.BuildActionGroup;
63 import org.eclipse.jdt.ui.actions.CCPActionGroup;
64 import org.eclipse.jdt.ui.actions.CustomFiltersActionGroup;
65 import org.eclipse.jdt.ui.actions.GenerateActionGroup;
66 import org.eclipse.jdt.ui.actions.ImportActionGroup;
67 import org.eclipse.jdt.ui.actions.JavaSearchActionGroup;
68 import org.eclipse.jdt.ui.actions.JdtActionConstants;
69 import org.eclipse.jdt.ui.actions.NavigateActionGroup;
70 import org.eclipse.jdt.ui.actions.OpenProjectAction;
71 import org.eclipse.jdt.ui.actions.ProjectActionGroup;
72 import org.eclipse.jdt.ui.actions.RefactorActionGroup;
73
74 import org.eclipse.jdt.internal.ui.actions.CompositeActionGroup;
75 import org.eclipse.jdt.internal.ui.actions.NewWizardsActionGroup;
76 import org.eclipse.jdt.internal.ui.actions.SelectAllAction;
77 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.GenerateBuildPathActionGroup;
78 import org.eclipse.jdt.internal.ui.workingsets.ViewActionGroup;
79
80 class PackageExplorerActionGroup extends CompositeActionGroup {
81
82     private PackageExplorerPart fPart;
83
84     private FrameList fFrameList;
85     private GoIntoAction fZoomInAction;
86     private BackAction fBackAction;
87     private ForwardAction fForwardAction;
88     private UpAction fUpAction;
89     private GotoTypeAction fGotoTypeAction;
90     private GotoPackageAction fGotoPackageAction;
91     private GotoResourceAction fGotoResourceAction;
92     private CollapseAllAction fCollapseAllAction;
93     private SelectAllAction fSelectAllAction;
94     
95     
96     private ToggleLinkingAction fToggleLinkingAction;
97
98     private RefactorActionGroup fRefactorActionGroup;
99     private NavigateActionGroup fNavigateActionGroup;
100     private ViewActionGroup fViewActionGroup;
101     
102     private CustomFiltersActionGroup fCustomFiltersActionGroup;
103
104     private IAction fGotoRequiredProjectAction;
105
106     private ProjectActionGroup fProjectActionGroup;
107     
108     public PackageExplorerActionGroup(PackageExplorerPart part) {
109         super();
110         fPart= part;
111         TreeViewer viewer= part.getTreeViewer();
112         
113         IPropertyChangeListener workingSetListener= new IPropertyChangeListener() {
114             public void propertyChange(PropertyChangeEvent event) {
115                 doWorkingSetChanged(event);
116             }
117         };
118         
119         IWorkbenchPartSite site = fPart.getSite();
120         setGroups(new ActionGroup[] {
121             new NewWizardsActionGroup(site),
122             fNavigateActionGroup= new NavigateActionGroup(fPart),
123             new CCPActionGroup(fPart),
124             new GenerateBuildPathActionGroup(fPart),
125             new GenerateActionGroup(fPart),
126             fRefactorActionGroup= new RefactorActionGroup(fPart),
127             new ImportActionGroup(fPart),
128             new BuildActionGroup(fPart),
129             new JavaSearchActionGroup(fPart),
130             fProjectActionGroup= new ProjectActionGroup(fPart),
131             fViewActionGroup= new ViewActionGroup(fPart.getRootMode(), workingSetListener, site),
132             fCustomFiltersActionGroup= new CustomFiltersActionGroup(fPart, viewer),
133             new LayoutActionGroup(fPart)
134         });
135         
136
137         fViewActionGroup.fillFilters(viewer);
138         
139         PackagesFrameSource frameSource= new PackagesFrameSource(fPart);
140         fFrameList= new FrameList(frameSource);
141         frameSource.connectTo(fFrameList);
142             
143         fZoomInAction= new GoIntoAction(fFrameList);
144         fBackAction= new BackAction(fFrameList);
145         fForwardAction= new ForwardAction(fFrameList);
146         fUpAction= new UpAction(fFrameList);
147         
148         fGotoTypeAction= new GotoTypeAction(fPart);
149         fGotoPackageAction= new GotoPackageAction(fPart);
150         fGotoResourceAction= new GotoResourceAction(fPart);
151         fCollapseAllAction= new CollapseAllAction(fPart);
152         fToggleLinkingAction = new ToggleLinkingAction(fPart);
153         fGotoRequiredProjectAction= new GotoRequiredProjectAction(fPart);
154         fSelectAllAction= new SelectAllAction(fPart.getTreeViewer());
155     }
156
157     public void dispose() {
158         super.dispose();
159     }
160     
161
162     //---- Persistent state -----------------------------------------------------------------------
163

164     /* package */ void restoreFilterAndSorterState(IMemento memento) {
165         fViewActionGroup.restoreState(memento);
166         fCustomFiltersActionGroup.restoreState(memento);
167     }
168     
169     /* package */ void saveFilterAndSorterState(IMemento memento) {
170         fViewActionGroup.saveState(memento);
171         fCustomFiltersActionGroup.saveState(memento);
172     }
173
174     //---- Action Bars ----------------------------------------------------------------------------
175

176     public void fillActionBars(IActionBars actionBars) {
177         super.fillActionBars(actionBars);
178         setGlobalActionHandlers(actionBars);
179         fillToolBar(actionBars.getToolBarManager());
180         fillViewMenu(actionBars.getMenuManager());
181     }
182
183     /* package */ void updateActionBars(IActionBars actionBars) {
184         actionBars.getToolBarManager().removeAll();
185         actionBars.getMenuManager().removeAll();
186         fillActionBars(actionBars);
187         actionBars.updateActionBars();
188         fZoomInAction.setEnabled(true);
189     }
190
191     private void setGlobalActionHandlers(IActionBars actionBars) {
192         // Navigate Go Into and Go To actions.
193
actionBars.setGlobalActionHandler(IWorkbenchActionConstants.GO_INTO, fZoomInAction);
194         actionBars.setGlobalActionHandler(ActionFactory.BACK.getId(), fBackAction);
195         actionBars.setGlobalActionHandler(ActionFactory.FORWARD.getId(), fForwardAction);
196         actionBars.setGlobalActionHandler(IWorkbenchActionConstants.UP, fUpAction);
197         actionBars.setGlobalActionHandler(IWorkbenchActionConstants.GO_TO_RESOURCE, fGotoResourceAction);
198         actionBars.setGlobalActionHandler(JdtActionConstants.GOTO_TYPE, fGotoTypeAction);
199         actionBars.setGlobalActionHandler(JdtActionConstants.GOTO_PACKAGE, fGotoPackageAction);
200         actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), fSelectAllAction);
201         
202         fRefactorActionGroup.retargetFileMenuActions(actionBars);
203     }
204
205     /* package */ void fillToolBar(IToolBarManager toolBar) {
206         toolBar.add(fBackAction);
207         toolBar.add(fForwardAction);
208         toolBar.add(fUpAction);
209         
210         toolBar.add(new Separator());
211         toolBar.add(fCollapseAllAction);
212         toolBar.add(fToggleLinkingAction);
213
214     }
215     
216     /* package */ void fillViewMenu(IMenuManager menu) {
217         menu.add(new Separator());
218         menu.add(fToggleLinkingAction);
219         menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
220         menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS+"-end"));//$NON-NLS-1$
221
}
222
223     //---- Context menu -------------------------------------------------------------------------
224

225     public void fillContextMenu(IMenuManager menu) {
226         IStructuredSelection selection= (IStructuredSelection)getContext().getSelection();
227         int size= selection.size();
228         Object JavaDoc element= selection.getFirstElement();
229         
230         if (element instanceof ClassPathContainer.RequiredProjectWrapper)
231             menu.appendToGroup(IContextMenuConstants.GROUP_SHOW, fGotoRequiredProjectAction);
232         
233         addGotoMenu(menu, element, size);
234         
235         addOpenNewWindowAction(menu, element);
236         
237         super.fillContextMenu(menu);
238     }
239     
240      private void addGotoMenu(IMenuManager menu, Object JavaDoc element, int size) {
241         boolean enabled= size == 1 && fPart.getTreeViewer().isExpandable(element) && (isGoIntoTarget(element) || element instanceof IContainer);
242         fZoomInAction.setEnabled(enabled);
243         if (enabled)
244             menu.appendToGroup(IContextMenuConstants.GROUP_GOTO, fZoomInAction);
245     }
246     
247     private boolean isGoIntoTarget(Object JavaDoc element) {
248         if (element == null)
249             return false;
250         if (element instanceof IJavaElement) {
251             int type= ((IJavaElement)element).getElementType();
252             return type == IJavaElement.JAVA_PROJECT ||
253                 type == IJavaElement.PACKAGE_FRAGMENT_ROOT ||
254                 type == IJavaElement.PACKAGE_FRAGMENT;
255         }
256         if (element instanceof IWorkingSet) {
257             return true;
258         }
259         return false;
260     }
261
262     private void addOpenNewWindowAction(IMenuManager menu, Object JavaDoc element) {
263         if (element instanceof IJavaElement) {
264             element= ((IJavaElement)element).getResource();
265             
266         }
267         // fix for 64890 Package explorer out of sync when open/closing projects [package explorer] 64890
268
if (element instanceof IProject && !((IProject)element).isOpen())
269             return;
270         
271         if (!(element instanceof IContainer))
272             return;
273         menu.appendToGroup(
274             IContextMenuConstants.GROUP_OPEN,
275             new OpenInNewWindowAction(fPart.getSite().getWorkbenchWindow(), (IContainer)element));
276     }
277
278     //---- Key board and mouse handling ------------------------------------------------------------
279

280     /* package*/ void handleDoubleClick(DoubleClickEvent event) {
281         TreeViewer viewer= fPart.getTreeViewer();
282         IStructuredSelection selection= (IStructuredSelection)event.getSelection();
283         Object JavaDoc element= selection.getFirstElement();
284         if (viewer.isExpandable(element)) {
285             if (doubleClickGoesInto()) {
286                 // don't zoom into compilation units and class files
287
if (element instanceof ICompilationUnit || element instanceof IClassFile)
288                     return;
289                 if (element instanceof IOpenable || element instanceof IContainer || element instanceof IWorkingSet) {
290                     fZoomInAction.run();
291                 }
292             } else {
293                 IAction openAction= fNavigateActionGroup.getOpenAction();
294                 if (openAction != null && openAction.isEnabled() && OpenStrategy.getOpenMethod() == OpenStrategy.DOUBLE_CLICK)
295                     return;
296                 if (selection instanceof ITreeSelection) {
297                     TreePath[] paths= ((ITreeSelection)selection).getPathsFor(element);
298                     for (int i= 0; i < paths.length; i++) {
299                         viewer.setExpandedState(paths[i], !viewer.getExpandedState(paths[i]));
300                     }
301                 } else {
302                     viewer.setExpandedState(element, !viewer.getExpandedState(element));
303                 }
304             }
305         } else if (element instanceof IProject && !((IProject) element).isOpen()) {
306             OpenProjectAction openProjectAction= fProjectActionGroup.getOpenProjectAction();
307             if (openProjectAction.isEnabled()) {
308                 openProjectAction.run();
309             }
310         }
311     }
312     
313     /* package */ void handleOpen(OpenEvent event) {
314         IAction openAction= fNavigateActionGroup.getOpenAction();
315         if (openAction != null && openAction.isEnabled()) {
316             openAction.run();
317             return;
318         }
319     }
320     
321     /* package */ void handleKeyEvent(KeyEvent event) {
322         if (event.stateMask != 0)
323             return;
324         
325         if (event.keyCode == SWT.BS) {
326             if (fUpAction != null && fUpAction.isEnabled()) {
327                 fUpAction.run();
328                 event.doit= false;
329             }
330         }
331     }
332     
333     private void doWorkingSetChanged(PropertyChangeEvent event) {
334         if (ViewActionGroup.MODE_CHANGED.equals(event.getProperty())) {
335             fPart.rootModeChanged(((Integer JavaDoc)event.getNewValue()).intValue());
336             Object JavaDoc oldInput= null;
337             Object JavaDoc newInput= null;
338             if (fPart.showProjects()) {
339                 oldInput= fPart.getWorkingSetModel();
340                 newInput= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
341             } else if (fPart.showWorkingSets()) {
342                 oldInput= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
343                 newInput= fPart.getWorkingSetModel();
344             }
345             if (oldInput != null && newInput != null) {
346                 Frame frame;
347                 for (int i= 0; (frame= fFrameList.getFrame(i)) != null; i++) {
348                     if (frame instanceof TreeFrame) {
349                         TreeFrame treeFrame= (TreeFrame)frame;
350                         if (oldInput.equals(treeFrame.getInput()))
351                             treeFrame.setInput(newInput);
352                     }
353                 }
354             }
355         } else {
356             IWorkingSet workingSet= (IWorkingSet) event.getNewValue();
357             
358             String JavaDoc workingSetLabel= null;
359             if (workingSet != null)
360                 workingSetLabel= workingSet.getLabel();
361             fPart.setWorkingSetLabel(workingSetLabel);
362             fPart.updateTitle();
363     
364             String JavaDoc property= event.getProperty();
365             if (IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE.equals(property)) {
366                 TreeViewer viewer= fPart.getTreeViewer();
367                 viewer.getControl().setRedraw(false);
368                 viewer.refresh();
369                 viewer.getControl().setRedraw(true);
370             }
371         }
372     }
373
374     private boolean doubleClickGoesInto() {
375         return PreferenceConstants.DOUBLE_CLICK_GOES_INTO.equals(PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.DOUBLE_CLICK));
376     }
377
378     public FrameAction getUpAction() {
379         return fUpAction;
380     }
381
382     public FrameAction getBackAction() {
383         return fBackAction;
384     }
385     public FrameAction getForwardAction() {
386         return fForwardAction;
387     }
388
389     public ViewActionGroup getWorkingSetActionGroup() {
390         return fViewActionGroup;
391     }
392     
393     public CustomFiltersActionGroup getCustomFilterActionGroup() {
394         return fCustomFiltersActionGroup;
395     }
396     
397     public FrameList getFrameList() {
398         return fFrameList;
399     }
400 }
401
Popular Tags