KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ObjectPluginAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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;
12
13 import org.eclipse.core.runtime.IConfigurationElement;
14 import org.eclipse.core.runtime.ISafeRunnable;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.ui.IActionDelegate;
17 import org.eclipse.ui.IObjectActionDelegate;
18 import org.eclipse.ui.IWorkbenchPart;
19
20 /**
21  * An object action extension in a popup menu.
22  * <p>
23  * For backward compatibility, the delegate object can implement either
24  * <code>IActionDelegate</code> or <code>IObjectActionDelegate</code>.
25  * </p>
26  */

27 public class ObjectPluginAction extends PluginAction {
28     /**
29      * The configuration element attribute for the identifier of the action
30      * which this action is intended to override (i.e., replace).
31      */

32     public static final String JavaDoc ATT_OVERRIDE_ACTION_ID = "overrideActionId";//$NON-NLS-1$
33

34     private String JavaDoc overrideActionId;
35
36     private IWorkbenchPart activePart;
37
38     /**
39      * Constructs a new ObjectPluginAction.
40      *
41      * @param actionElement
42      * The configuration element used to construct this action; must
43      * not be <code>null</code>.
44      * @param id
45      * The identifier for this action; must not be <code>null</code>.
46      * @param style
47      * The style bits
48      */

49     public ObjectPluginAction(IConfigurationElement actionElement, String JavaDoc id,
50             int style) {
51         super(actionElement, id, style);
52         overrideActionId = actionElement.getAttribute(ATT_OVERRIDE_ACTION_ID);
53     }
54
55     /* (non-Javadoc)
56      * Method declared on PluginAction.
57      */

58     protected void initDelegate() {
59         super.initDelegate();
60         final IActionDelegate actionDelegate = getDelegate();
61         if (actionDelegate instanceof IObjectActionDelegate
62                 && activePart != null) {
63             final IObjectActionDelegate objectActionDelegate = (IObjectActionDelegate) actionDelegate;
64             final ISafeRunnable runnable = new ISafeRunnable() {
65                 public void run() throws Exception JavaDoc {
66                     objectActionDelegate.setActivePart(ObjectPluginAction.this,
67                             activePart);
68                 }
69
70                 public void handleException(Throwable JavaDoc exception) {
71                     // Do nothing.
72
}
73             };
74             Platform.run(runnable);
75         }
76     }
77
78     /**
79      * Sets the active part for the delegate.
80      * <p>
81      * This method will be called every time the action appears in a popup menu.
82      * The targetPart may change with each invocation.
83      * </p>
84      *
85      * @param targetPart
86      * the new part target
87      */

88     public void setActivePart(IWorkbenchPart targetPart) {
89         activePart = targetPart;
90         IActionDelegate delegate = getDelegate();
91         if (delegate instanceof IObjectActionDelegate && activePart != null) {
92             final IObjectActionDelegate objectActionDelegate = (IObjectActionDelegate) delegate;
93             final ISafeRunnable runnable = new ISafeRunnable() {
94                 public void run() throws Exception JavaDoc {
95                     objectActionDelegate.setActivePart(ObjectPluginAction.this,
96                             activePart);
97                 }
98
99                 public void handleException(Throwable JavaDoc exception) {
100                     // Do nothing.
101
}
102             };
103             Platform.run(runnable);
104         }
105     }
106
107     /**
108      * Returns the action identifier this action overrides.
109      *
110      * @return the action identifier to override or <code>null</code>
111      */

112     public String JavaDoc getOverrideActionId() {
113         return overrideActionId;
114     }
115 }
116
Popular Tags