001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.ui.internal;
011:
012: import org.eclipse.core.runtime.IConfigurationElement;
013: import org.eclipse.core.runtime.ISafeRunnable;
014: import org.eclipse.core.runtime.Platform;
015: import org.eclipse.jface.viewers.StructuredSelection;
016: import org.eclipse.ui.IActionDelegate;
017: import org.eclipse.ui.IObjectActionDelegate;
018: import org.eclipse.ui.IPartListener2;
019: import org.eclipse.ui.IWorkbenchPart;
020: import org.eclipse.ui.IWorkbenchPartReference;
021:
022: /**
023: * An object action extension in a popup menu.
024: * <p>
025: * For backward compatibility, the delegate object can implement either
026: * <code>IActionDelegate</code> or <code>IObjectActionDelegate</code>.
027: * </p>
028: */
029: public class ObjectPluginAction extends PluginAction implements
030: IPartListener2 {
031: /**
032: * The configuration element attribute for the identifier of the action
033: * which this action is intended to override (i.e., replace).
034: */
035: public static final String ATT_OVERRIDE_ACTION_ID = "overrideActionId";//$NON-NLS-1$
036:
037: private String overrideActionId;
038:
039: private IWorkbenchPart activePart;
040:
041: public void partActivated(IWorkbenchPartReference partRef) {
042: }
043:
044: public void partBroughtToTop(IWorkbenchPartReference partRef) {
045: }
046:
047: public void partClosed(IWorkbenchPartReference partRef) {
048: if (activePart != null && partRef.getPart(false) == activePart) {
049: selectionChanged(StructuredSelection.EMPTY);
050: disposeDelegate();
051: activePart = null;
052: }
053: }
054:
055: public void partDeactivated(IWorkbenchPartReference partRef) {
056: }
057:
058: public void partHidden(IWorkbenchPartReference partRef) {
059: }
060:
061: public void partInputChanged(IWorkbenchPartReference partRef) {
062: }
063:
064: public void partOpened(IWorkbenchPartReference partRef) {
065: }
066:
067: public void partVisible(IWorkbenchPartReference partRef) {
068: }
069:
070: /**
071: * Constructs a new ObjectPluginAction.
072: *
073: * @param actionElement
074: * The configuration element used to construct this action; must
075: * not be <code>null</code>.
076: * @param id
077: * The identifier for this action; must not be <code>null</code>.
078: * @param style
079: * The style bits
080: */
081: public ObjectPluginAction(IConfigurationElement actionElement,
082: String id, int style) {
083: super (actionElement, id, style);
084: overrideActionId = actionElement
085: .getAttribute(ATT_OVERRIDE_ACTION_ID);
086: }
087:
088: /* (non-Javadoc)
089: * Method declared on PluginAction.
090: */
091: protected void initDelegate() {
092: super .initDelegate();
093: final IActionDelegate actionDelegate = getDelegate();
094: if (actionDelegate instanceof IObjectActionDelegate
095: && activePart != null) {
096: final IObjectActionDelegate objectActionDelegate = (IObjectActionDelegate) actionDelegate;
097: final ISafeRunnable runnable = new ISafeRunnable() {
098: public void run() throws Exception {
099: objectActionDelegate.setActivePart(
100: ObjectPluginAction.this , activePart);
101: }
102:
103: public void handleException(Throwable exception) {
104: // Do nothing.
105: }
106: };
107: Platform.run(runnable);
108: }
109: }
110:
111: /**
112: * Sets the active part for the delegate.
113: * <p>
114: * This method will be called every time the action appears in a popup menu.
115: * The targetPart may change with each invocation.
116: * </p>
117: *
118: * @param targetPart
119: * the new part target
120: */
121: public void setActivePart(IWorkbenchPart targetPart) {
122: if (activePart != targetPart) {
123: if (activePart != null) {
124: activePart.getSite().getPage().removePartListener(this );
125: }
126: if (targetPart != null) {
127: targetPart.getSite().getPage().addPartListener(this );
128: }
129: }
130: activePart = targetPart;
131: IActionDelegate delegate = getDelegate();
132: if (delegate instanceof IObjectActionDelegate
133: && activePart != null) {
134: final IObjectActionDelegate objectActionDelegate = (IObjectActionDelegate) delegate;
135: final ISafeRunnable runnable = new ISafeRunnable() {
136: public void run() throws Exception {
137: objectActionDelegate.setActivePart(
138: ObjectPluginAction.this , activePart);
139: }
140:
141: public void handleException(Throwable exception) {
142: // Do nothing.
143: }
144: };
145: Platform.run(runnable);
146: }
147: }
148:
149: /**
150: * Returns the action identifier this action overrides.
151: *
152: * @return the action identifier to override or <code>null</code>
153: */
154: public String getOverrideActionId() {
155: return overrideActionId;
156: }
157:
158: /* (non-Javadoc)
159: * @see org.eclipse.ui.internal.PluginAction#dispose()
160: */
161: public void dispose() {
162: if (activePart != null) {
163: activePart.getSite().getPage().removePartListener(this);
164: activePart = null;
165: }
166: super.dispose();
167: }
168: }
|