001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.ui.actions;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.core.resources.IResource;
014: import org.eclipse.core.runtime.IAdaptable;
015:
016: import org.eclipse.jface.action.IAction;
017: import org.eclipse.jface.action.IMenuManager;
018: import org.eclipse.jface.action.MenuManager;
019: import org.eclipse.jface.viewers.ISelection;
020: import org.eclipse.jface.viewers.ISelectionProvider;
021: import org.eclipse.jface.viewers.IStructuredSelection;
022:
023: import org.eclipse.ui.IActionBars;
024: import org.eclipse.ui.IViewPart;
025: import org.eclipse.ui.IWorkbenchSite;
026: import org.eclipse.ui.actions.ActionGroup;
027: import org.eclipse.ui.actions.OpenWithMenu;
028:
029: import org.eclipse.jdt.ui.IContextMenuConstants;
030:
031: import org.eclipse.jdt.internal.ui.actions.ActionMessages;
032: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
033:
034: /**
035: * Action group that adds the actions opening a new editor to the
036: * context menu and the action bar's navigate menu.
037: *
038: * <p>
039: * This class may be instantiated; it is not intended to be subclassed.
040: * </p>
041: *
042: * @since 2.0
043: */
044: public class OpenEditorActionGroup extends ActionGroup {
045:
046: private IWorkbenchSite fSite;
047: private boolean fIsEditorOwner;
048: private OpenAction fOpen;
049:
050: /**
051: * Creates a new <code>OpenActionGroup</code>. The group requires
052: * that the selection provided by the part's selection provider is of type <code>
053: * org.eclipse.jface.viewers.IStructuredSelection</code>.
054: *
055: * @param part the view part that owns this action group
056: */
057: public OpenEditorActionGroup(IViewPart part) {
058: fSite = part.getSite();
059: fOpen = new OpenAction(fSite);
060: fOpen
061: .setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EDITOR);
062: initialize(fSite.getSelectionProvider());
063: }
064:
065: /**
066: * Note: This constructor is for internal use only. Clients should not call this constructor.
067: * @param editor the Java editor
068: */
069: public OpenEditorActionGroup(JavaEditor editor) {
070: fIsEditorOwner = true;
071: fOpen = new OpenAction(editor);
072: fOpen
073: .setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EDITOR);
074: editor.setAction("OpenEditor", fOpen); //$NON-NLS-1$
075: fSite = editor.getEditorSite();
076: initialize(fSite.getSelectionProvider());
077: }
078:
079: /**
080: * Returns the open action managed by this action group.
081: *
082: * @return the open action. Returns <code>null</code> if the group
083: * doesn't provide any open action
084: */
085: public IAction getOpenAction() {
086: return fOpen;
087: }
088:
089: private void initialize(ISelectionProvider provider) {
090: ISelection selection = provider.getSelection();
091: fOpen.update(selection);
092: if (!fIsEditorOwner) {
093: provider.addSelectionChangedListener(fOpen);
094: }
095: }
096:
097: /* (non-Javadoc)
098: * Method declared in ActionGroup
099: */
100: public void fillActionBars(IActionBars actionBar) {
101: super .fillActionBars(actionBar);
102: setGlobalActionHandlers(actionBar);
103: }
104:
105: /* (non-Javadoc)
106: * Method declared in ActionGroup
107: */
108: public void fillContextMenu(IMenuManager menu) {
109: super .fillContextMenu(menu);
110: appendToGroup(menu, fOpen);
111: if (!fIsEditorOwner) {
112: addOpenWithMenu(menu);
113: }
114: }
115:
116: /*
117: * @see ActionGroup#dispose()
118: */
119: public void dispose() {
120: ISelectionProvider provider = fSite.getSelectionProvider();
121: provider.removeSelectionChangedListener(fOpen);
122: super .dispose();
123: }
124:
125: private void setGlobalActionHandlers(IActionBars actionBars) {
126: actionBars.setGlobalActionHandler(JdtActionConstants.OPEN,
127: fOpen);
128: }
129:
130: private void appendToGroup(IMenuManager menu, IAction action) {
131: if (action.isEnabled())
132: menu
133: .appendToGroup(IContextMenuConstants.GROUP_OPEN,
134: action);
135: }
136:
137: private void addOpenWithMenu(IMenuManager menu) {
138: ISelection selection = getContext().getSelection();
139: if (selection.isEmpty()
140: || !(selection instanceof IStructuredSelection))
141: return;
142: IStructuredSelection ss = (IStructuredSelection) selection;
143: if (ss.size() != 1)
144: return;
145:
146: Object o = ss.getFirstElement();
147: if (!(o instanceof IAdaptable))
148: return;
149:
150: IAdaptable element = (IAdaptable) o;
151: Object resource = element.getAdapter(IResource.class);
152: if (!(resource instanceof IFile))
153: return;
154:
155: // Create a menu.
156: IMenuManager submenu = new MenuManager(
157: ActionMessages.OpenWithMenu_label);
158: submenu
159: .add(new OpenWithMenu(fSite.getPage(), (IFile) resource));
160:
161: // Add the submenu.
162: menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
163: }
164: }
|