001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.packageview;
011:
012: import org.eclipse.core.resources.IContainer;
013: import org.eclipse.core.resources.IProject;
014: import org.eclipse.core.resources.ResourcesPlugin;
015:
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.events.KeyEvent;
018:
019: import org.eclipse.jface.action.IAction;
020: import org.eclipse.jface.action.IMenuManager;
021: import org.eclipse.jface.action.IToolBarManager;
022: import org.eclipse.jface.action.Separator;
023: import org.eclipse.jface.util.IPropertyChangeListener;
024: import org.eclipse.jface.util.OpenStrategy;
025: import org.eclipse.jface.util.PropertyChangeEvent;
026: import org.eclipse.jface.viewers.DoubleClickEvent;
027: import org.eclipse.jface.viewers.IStructuredSelection;
028: import org.eclipse.jface.viewers.ITreeSelection;
029: import org.eclipse.jface.viewers.OpenEvent;
030: import org.eclipse.jface.viewers.TreePath;
031: import org.eclipse.jface.viewers.TreeViewer;
032:
033: import org.eclipse.ui.IActionBars;
034: import org.eclipse.ui.IMemento;
035: import org.eclipse.ui.IWorkbenchActionConstants;
036: import org.eclipse.ui.IWorkbenchPartSite;
037: import org.eclipse.ui.IWorkingSet;
038: import org.eclipse.ui.IWorkingSetManager;
039: import org.eclipse.ui.actions.ActionFactory;
040: import org.eclipse.ui.actions.ActionGroup;
041: import org.eclipse.ui.actions.OpenInNewWindowAction;
042:
043: import org.eclipse.ui.views.framelist.BackAction;
044: import org.eclipse.ui.views.framelist.ForwardAction;
045: import org.eclipse.ui.views.framelist.Frame;
046: import org.eclipse.ui.views.framelist.FrameAction;
047: import org.eclipse.ui.views.framelist.FrameList;
048: import org.eclipse.ui.views.framelist.GoIntoAction;
049: import org.eclipse.ui.views.framelist.TreeFrame;
050: import org.eclipse.ui.views.framelist.UpAction;
051:
052: import org.eclipse.jdt.core.IClassFile;
053: import org.eclipse.jdt.core.ICompilationUnit;
054: import org.eclipse.jdt.core.IJavaElement;
055: import org.eclipse.jdt.core.IOpenable;
056: import org.eclipse.jdt.core.JavaCore;
057:
058: import org.eclipse.jdt.ui.IContextMenuConstants;
059: import org.eclipse.jdt.ui.PreferenceConstants;
060: import org.eclipse.jdt.ui.actions.BuildActionGroup;
061: import org.eclipse.jdt.ui.actions.CCPActionGroup;
062: import org.eclipse.jdt.ui.actions.CustomFiltersActionGroup;
063: import org.eclipse.jdt.ui.actions.GenerateActionGroup;
064: import org.eclipse.jdt.ui.actions.ImportActionGroup;
065: import org.eclipse.jdt.ui.actions.JavaSearchActionGroup;
066: import org.eclipse.jdt.ui.actions.JdtActionConstants;
067: import org.eclipse.jdt.ui.actions.NavigateActionGroup;
068: import org.eclipse.jdt.ui.actions.OpenProjectAction;
069: import org.eclipse.jdt.ui.actions.ProjectActionGroup;
070: import org.eclipse.jdt.ui.actions.RefactorActionGroup;
071:
072: import org.eclipse.jdt.internal.ui.actions.CompositeActionGroup;
073: import org.eclipse.jdt.internal.ui.actions.NewWizardsActionGroup;
074: import org.eclipse.jdt.internal.ui.actions.SelectAllAction;
075: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.GenerateBuildPathActionGroup;
076: import org.eclipse.jdt.internal.ui.workingsets.ViewActionGroup;
077:
078: class PackageExplorerActionGroup extends CompositeActionGroup {
079:
080: private PackageExplorerPart fPart;
081:
082: private FrameList fFrameList;
083: private GoIntoAction fZoomInAction;
084: private BackAction fBackAction;
085: private ForwardAction fForwardAction;
086: private UpAction fUpAction;
087: private GotoTypeAction fGotoTypeAction;
088: private GotoPackageAction fGotoPackageAction;
089: private GotoResourceAction fGotoResourceAction;
090: private CollapseAllAction fCollapseAllAction;
091: private SelectAllAction fSelectAllAction;
092:
093: private ToggleLinkingAction fToggleLinkingAction;
094:
095: private RefactorActionGroup fRefactorActionGroup;
096: private NavigateActionGroup fNavigateActionGroup;
097: private ViewActionGroup fViewActionGroup;
098:
099: private CustomFiltersActionGroup fCustomFiltersActionGroup;
100:
101: private IAction fGotoRequiredProjectAction;
102:
103: private ProjectActionGroup fProjectActionGroup;
104:
105: public PackageExplorerActionGroup(PackageExplorerPart part) {
106: super ();
107: fPart = part;
108: TreeViewer viewer = part.getTreeViewer();
109:
110: IPropertyChangeListener workingSetListener = new IPropertyChangeListener() {
111: public void propertyChange(PropertyChangeEvent event) {
112: doWorkingSetChanged(event);
113: }
114: };
115:
116: IWorkbenchPartSite site = fPart.getSite();
117: setGroups(new ActionGroup[] {
118: new NewWizardsActionGroup(site),
119: fNavigateActionGroup = new NavigateActionGroup(fPart),
120: new CCPActionGroup(fPart),
121: new GenerateBuildPathActionGroup(fPart),
122: new GenerateActionGroup(fPart),
123: fRefactorActionGroup = new RefactorActionGroup(fPart),
124: new ImportActionGroup(fPart),
125: new BuildActionGroup(fPart),
126: new JavaSearchActionGroup(fPart),
127: fProjectActionGroup = new ProjectActionGroup(fPart),
128: fViewActionGroup = new ViewActionGroup(fPart
129: .getRootMode(), workingSetListener, site),
130: fCustomFiltersActionGroup = new CustomFiltersActionGroup(
131: fPart, viewer), new LayoutActionGroup(fPart) });
132:
133: fViewActionGroup.fillFilters(viewer);
134:
135: PackagesFrameSource frameSource = new PackagesFrameSource(fPart);
136: fFrameList = new FrameList(frameSource);
137:
138: fZoomInAction = new GoIntoAction(fFrameList);
139: fBackAction = new BackAction(fFrameList);
140: fForwardAction = new ForwardAction(fFrameList);
141: fUpAction = new UpAction(fFrameList);
142: frameSource.connectTo(fFrameList); // connect after the actions (order of property listener)
143:
144: fGotoTypeAction = new GotoTypeAction(fPart);
145: fGotoPackageAction = new GotoPackageAction(fPart);
146: fGotoResourceAction = new GotoResourceAction(fPart);
147: fCollapseAllAction = new CollapseAllAction(fPart);
148: fToggleLinkingAction = new ToggleLinkingAction(fPart);
149: fGotoRequiredProjectAction = new GotoRequiredProjectAction(
150: fPart);
151: fSelectAllAction = new SelectAllAction(fPart.getTreeViewer());
152: }
153:
154: public void dispose() {
155: super .dispose();
156: }
157:
158: //---- Persistent state -----------------------------------------------------------------------
159:
160: /* package */void restoreFilterAndSorterState(IMemento memento) {
161: fViewActionGroup.restoreState(memento);
162: fCustomFiltersActionGroup.restoreState(memento);
163: }
164:
165: /* package */void saveFilterAndSorterState(IMemento memento) {
166: fViewActionGroup.saveState(memento);
167: fCustomFiltersActionGroup.saveState(memento);
168: }
169:
170: //---- Action Bars ----------------------------------------------------------------------------
171:
172: public void fillActionBars(IActionBars actionBars) {
173: super .fillActionBars(actionBars);
174: setGlobalActionHandlers(actionBars);
175: fillToolBar(actionBars.getToolBarManager());
176: fillViewMenu(actionBars.getMenuManager());
177: }
178:
179: /* package */void updateActionBars(IActionBars actionBars) {
180: actionBars.getToolBarManager().removeAll();
181: actionBars.getMenuManager().removeAll();
182: fillActionBars(actionBars);
183: actionBars.updateActionBars();
184: fZoomInAction.setEnabled(true);
185: }
186:
187: private void setGlobalActionHandlers(IActionBars actionBars) {
188: // Navigate Go Into and Go To actions.
189: actionBars.setGlobalActionHandler(
190: IWorkbenchActionConstants.GO_INTO, fZoomInAction);
191: actionBars.setGlobalActionHandler(ActionFactory.BACK.getId(),
192: fBackAction);
193: actionBars.setGlobalActionHandler(
194: ActionFactory.FORWARD.getId(), fForwardAction);
195: actionBars.setGlobalActionHandler(IWorkbenchActionConstants.UP,
196: fUpAction);
197: actionBars.setGlobalActionHandler(
198: IWorkbenchActionConstants.GO_TO_RESOURCE,
199: fGotoResourceAction);
200: actionBars.setGlobalActionHandler(JdtActionConstants.GOTO_TYPE,
201: fGotoTypeAction);
202: actionBars.setGlobalActionHandler(
203: JdtActionConstants.GOTO_PACKAGE, fGotoPackageAction);
204: actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL
205: .getId(), fSelectAllAction);
206:
207: fRefactorActionGroup.retargetFileMenuActions(actionBars);
208: }
209:
210: /* package */void fillToolBar(IToolBarManager toolBar) {
211: toolBar.removeAll();
212:
213: if (fBackAction.isEnabled() || fUpAction.isEnabled()
214: || fForwardAction.isEnabled()) {
215: toolBar.add(fBackAction);
216: toolBar.add(fForwardAction);
217: toolBar.add(fUpAction);
218: toolBar.add(new Separator());
219: }
220:
221: toolBar.add(fCollapseAllAction);
222: toolBar.add(fToggleLinkingAction);
223: toolBar.update(true);
224: }
225:
226: /* package */void fillViewMenu(IMenuManager menu) {
227: menu.add(new Separator());
228: menu.add(fToggleLinkingAction);
229: menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
230: menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS
231: + "-end"));//$NON-NLS-1$
232: }
233:
234: //---- Context menu -------------------------------------------------------------------------
235:
236: public void fillContextMenu(IMenuManager menu) {
237: IStructuredSelection selection = (IStructuredSelection) getContext()
238: .getSelection();
239: int size = selection.size();
240: Object element = selection.getFirstElement();
241:
242: if (element instanceof ClassPathContainer.RequiredProjectWrapper)
243: menu.appendToGroup(IContextMenuConstants.GROUP_SHOW,
244: fGotoRequiredProjectAction);
245:
246: addGotoMenu(menu, element, size);
247:
248: addOpenNewWindowAction(menu, element);
249:
250: super .fillContextMenu(menu);
251: }
252:
253: private void addGotoMenu(IMenuManager menu, Object element, int size) {
254: boolean enabled = size == 1
255: && fPart.getTreeViewer().isExpandable(element)
256: && (isGoIntoTarget(element) || element instanceof IContainer);
257: fZoomInAction.setEnabled(enabled);
258: if (enabled)
259: menu.appendToGroup(IContextMenuConstants.GROUP_GOTO,
260: fZoomInAction);
261: }
262:
263: private boolean isGoIntoTarget(Object element) {
264: if (element == null)
265: return false;
266: if (element instanceof IJavaElement) {
267: int type = ((IJavaElement) element).getElementType();
268: return type == IJavaElement.JAVA_PROJECT
269: || type == IJavaElement.PACKAGE_FRAGMENT_ROOT
270: || type == IJavaElement.PACKAGE_FRAGMENT;
271: }
272: if (element instanceof IWorkingSet) {
273: return true;
274: }
275: return false;
276: }
277:
278: private void addOpenNewWindowAction(IMenuManager menu,
279: Object element) {
280: if (element instanceof IJavaElement) {
281: element = ((IJavaElement) element).getResource();
282:
283: }
284: // fix for 64890 Package explorer out of sync when open/closing projects [package explorer] 64890
285: if (element instanceof IProject
286: && !((IProject) element).isOpen())
287: return;
288:
289: if (!(element instanceof IContainer))
290: return;
291: menu.appendToGroup(IContextMenuConstants.GROUP_OPEN,
292: new OpenInNewWindowAction(fPart.getSite()
293: .getWorkbenchWindow(), (IContainer) element));
294: }
295:
296: //---- Key board and mouse handling ------------------------------------------------------------
297:
298: /* package*/void handleDoubleClick(DoubleClickEvent event) {
299: TreeViewer viewer = fPart.getTreeViewer();
300: IStructuredSelection selection = (IStructuredSelection) event
301: .getSelection();
302: Object element = selection.getFirstElement();
303: if (viewer.isExpandable(element)) {
304: if (doubleClickGoesInto()) {
305: // don't zoom into compilation units and class files
306: if (element instanceof ICompilationUnit
307: || element instanceof IClassFile)
308: return;
309: if (element instanceof IOpenable
310: || element instanceof IContainer
311: || element instanceof IWorkingSet) {
312: fZoomInAction.run();
313: }
314: } else {
315: IAction openAction = fNavigateActionGroup
316: .getOpenAction();
317: if (openAction != null
318: && openAction.isEnabled()
319: && OpenStrategy.getOpenMethod() == OpenStrategy.DOUBLE_CLICK)
320: return;
321: if (selection instanceof ITreeSelection) {
322: TreePath[] paths = ((ITreeSelection) selection)
323: .getPathsFor(element);
324: for (int i = 0; i < paths.length; i++) {
325: viewer.setExpandedState(paths[i], !viewer
326: .getExpandedState(paths[i]));
327: }
328: } else {
329: viewer.setExpandedState(element, !viewer
330: .getExpandedState(element));
331: }
332: }
333: } else if (element instanceof IProject
334: && !((IProject) element).isOpen()) {
335: OpenProjectAction openProjectAction = fProjectActionGroup
336: .getOpenProjectAction();
337: if (openProjectAction.isEnabled()) {
338: openProjectAction.run();
339: }
340: }
341: }
342:
343: /**
344: * Called by the open action
345: * @param event the open event
346: */
347: /* package */void handleOpen(OpenEvent event) {
348: IAction openAction = fNavigateActionGroup.getOpenAction();
349: if (openAction != null && openAction.isEnabled()) {
350: openAction.run();
351: return;
352: }
353: }
354:
355: /* package */void handleKeyEvent(KeyEvent event) {
356: if (event.stateMask != 0)
357: return;
358:
359: if (event.keyCode == SWT.BS) {
360: if (fUpAction != null && fUpAction.isEnabled()) {
361: fUpAction.run();
362: event.doit = false;
363: }
364: }
365: }
366:
367: private void doWorkingSetChanged(PropertyChangeEvent event) {
368: if (ViewActionGroup.MODE_CHANGED.equals(event.getProperty())) {
369: fPart.rootModeChanged(((Integer) event.getNewValue())
370: .intValue());
371: Object oldInput = null;
372: Object newInput = null;
373: if (fPart.showProjects()) {
374: oldInput = fPart.getWorkingSetModel();
375: newInput = JavaCore.create(ResourcesPlugin
376: .getWorkspace().getRoot());
377: } else if (fPart.showWorkingSets()) {
378: oldInput = JavaCore.create(ResourcesPlugin
379: .getWorkspace().getRoot());
380: newInput = fPart.getWorkingSetModel();
381: }
382: if (oldInput != null && newInput != null) {
383: Frame frame;
384: for (int i = 0; (frame = fFrameList.getFrame(i)) != null; i++) {
385: if (frame instanceof TreeFrame) {
386: TreeFrame treeFrame = (TreeFrame) frame;
387: if (oldInput.equals(treeFrame.getInput()))
388: treeFrame.setInput(newInput);
389: }
390: }
391: }
392: } else {
393: IWorkingSet workingSet = (IWorkingSet) event.getNewValue();
394:
395: String workingSetLabel = null;
396: if (workingSet != null)
397: workingSetLabel = workingSet.getLabel();
398: fPart.setWorkingSetLabel(workingSetLabel);
399: fPart.updateTitle();
400:
401: String property = event.getProperty();
402: if (IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE
403: .equals(property)) {
404: TreeViewer viewer = fPart.getTreeViewer();
405: viewer.getControl().setRedraw(false);
406: viewer.refresh();
407: viewer.getControl().setRedraw(true);
408: }
409: }
410: }
411:
412: private boolean doubleClickGoesInto() {
413: return PreferenceConstants.DOUBLE_CLICK_GOES_INTO
414: .equals(PreferenceConstants.getPreferenceStore()
415: .getString(PreferenceConstants.DOUBLE_CLICK));
416: }
417:
418: public FrameAction getUpAction() {
419: return fUpAction;
420: }
421:
422: public FrameAction getBackAction() {
423: return fBackAction;
424: }
425:
426: public FrameAction getForwardAction() {
427: return fForwardAction;
428: }
429:
430: public ViewActionGroup getWorkingSetActionGroup() {
431: return fViewActionGroup;
432: }
433:
434: public CustomFiltersActionGroup getCustomFilterActionGroup() {
435: return fCustomFiltersActionGroup;
436: }
437:
438: public FrameList getFrameList() {
439: return fFrameList;
440: }
441: }
|