KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > RetargetTextEditorAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
13 package org.eclipse.ui.texteditor;
14
15
16 import java.util.ResourceBundle JavaDoc;
17
18 import org.eclipse.swt.events.HelpEvent;
19 import org.eclipse.swt.events.HelpListener;
20
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.util.IPropertyChangeListener;
23 import org.eclipse.jface.util.PropertyChangeEvent;
24
25
26 /**
27  * Action used by an editor action bar contributor to establish placeholders in
28  * menus or action bars which can be retargeted to dynamically changing actions,
29  * for example, those which come from the active editor. This action assumes that
30  * the "wrapped" action sends out property change events in response to state
31  * changes. It uses these change notifications to adapt its enabling state and
32  * its visual presentation.
33  */

34 public final class RetargetTextEditorAction extends ResourceAction {
35
36     /** The target action. */
37     private IAction fAction;
38     /** The default label if there is no target action. */
39     private String JavaDoc fDefaultText;
40     /**
41      * The local help listener
42      * @since 2.1
43      */

44     private HelpListener fLocalHelpListener;
45     /** The listener to pick up changes of the target action. */
46     private IPropertyChangeListener fListener= new IPropertyChangeListener() {
47         public void propertyChange(PropertyChangeEvent event) {
48             update(event);
49         }
50     };
51
52     /**
53      * Creates a new action. The action configures its initial visual
54      * representation from the given resource bundle. If this action's
55      * wrapped action is set to <code>null</code> it also uses the
56      * information in the resource bundle.
57      *
58      * @param bundle the resource bundle
59      * @param prefix a prefix to be prepended to the various resource keys
60      * (described in <code>ResourceAction</code> constructor), or
61      * <code>null</code> if none
62      * @param style one of <code>IAction.AS_PUSH_BUTTON</code>, <code>IAction.AS_CHECK_BOX</code>,
63      * and <code>IAction.AS_RADIO_BUTTON</code>.
64      *
65      * @see ResourceAction#ResourceAction(ResourceBundle, String, int)
66      * @see IAction#AS_CHECK_BOX
67      * @see IAction#AS_DROP_DOWN_MENU
68      * @see IAction#AS_PUSH_BUTTON
69      * @see IAction#AS_RADIO_BUTTON
70      * @since 2.1
71      */

72     public RetargetTextEditorAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, int style) {
73         super(bundle, prefix, style);
74         fDefaultText= getText();
75         installHelpListener();
76     }
77
78     /**
79      * Creates a new action. The action configures its initial visual
80      * representation from the given resource bundle. If this action's
81      * wrapped action is set to <code>null</code> it also uses the
82      * information in the resource bundle.
83      *
84      * @param bundle the resource bundle
85      * @param prefix a prefix to be prepended to the various resource keys
86      * (described in <code>ResourceAction</code> constructor), or
87      * <code>null</code> if none
88      * @see ResourceAction#ResourceAction(ResourceBundle, String)
89      */

90     public RetargetTextEditorAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix) {
91         super(bundle, prefix);
92         fDefaultText= getText();
93         installHelpListener();
94     }
95
96     /**
97      * Creates a new action. The action configures its initial visual
98      * representation from the given resource bundle. If this action's
99      * wrapped action is set to <code>null</code> it also uses the
100      * information in the resource bundle. The action gets the given
101      * action id.
102      *
103      * @param bundle the resource bundle
104      * @param prefix a prefix to be prepended to the various resource keys
105      * (described in <code>ResourceAction</code> constructor), or <code>null</code> if none
106      * @param actionId the action id
107      * @param style one of <code>IAction.AS_PUSH_BUTTON</code>, <code>IAction.AS_CHECK_BOX</code>,
108      * and <code>IAction.AS_RADIO_BUTTON</code>.
109      *
110      * @see ResourceAction#ResourceAction(ResourceBundle, String, int)
111      * @see IAction#AS_CHECK_BOX
112      * @see IAction#AS_DROP_DOWN_MENU
113      * @see IAction#AS_PUSH_BUTTON
114      * @see IAction#AS_RADIO_BUTTON
115      * @since 2.1
116      */

117     public RetargetTextEditorAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, String JavaDoc actionId, int style) {
118         super(bundle, prefix, style);
119         fDefaultText= getText();
120         setId(actionId);
121         installHelpListener();
122     }
123
124     /**
125      * Creates a new action. The action configures its initial visual
126      * representation from the given resource bundle. If this action's
127      * wrapped action is set to <code>null</code> it also uses the
128      * information in the resource bundle. The action gets the given
129      * action id.
130      *
131      * @param bundle the resource bundle
132      * @param prefix a prefix to be prepended to the various resource keys
133      * (described in <code>ResourceAction</code> constructor), or <code>null</code> if none
134      * @param actionId the action id
135      * @see ResourceAction#ResourceAction(ResourceBundle, String)
136      * @since 2.0
137      */

138     public RetargetTextEditorAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, String JavaDoc actionId) {
139         super(bundle, prefix);
140         fDefaultText= getText();
141         setId(actionId);
142         installHelpListener();
143     }
144
145     /**
146      * Updates to the changes of the underlying action.
147      *
148      * @param event the change event describing the state change
149      */

150     private void update(PropertyChangeEvent event) {
151         if (ENABLED.equals(event.getProperty())) {
152             Boolean JavaDoc bool= (Boolean JavaDoc) event.getNewValue();
153             setEnabled(bool.booleanValue());
154         } else if (TEXT.equals(event.getProperty()))
155             setText((String JavaDoc) event.getNewValue());
156         else if (TOOL_TIP_TEXT.equals(event.getProperty()))
157             setToolTipText((String JavaDoc) event.getNewValue());
158         else if (CHECKED.equals(event.getProperty())) {
159             Boolean JavaDoc bool= (Boolean JavaDoc) event.getNewValue();
160             setChecked(bool.booleanValue());
161         }
162     }
163
164     /**
165      * Sets the underlying action.
166      *
167      * @param action the underlying action
168      */

169     public void setAction(IAction action) {
170
171         if (fAction != null) {
172             fAction.removePropertyChangeListener(fListener);
173             fAction= null;
174         }
175
176         fAction= action;
177
178         if (fAction == null) {
179
180             setEnabled(false);
181             if (getStyle() == AS_CHECK_BOX || getStyle() == AS_RADIO_BUTTON)
182                setChecked(false);
183             setText(fDefaultText);
184             setToolTipText(""); //$NON-NLS-1$
185

186         } else {
187
188             setEnabled(fAction.isEnabled());
189             if (fAction.getStyle() == AS_CHECK_BOX || fAction.getStyle() == AS_RADIO_BUTTON)
190                super.setChecked(fAction.isChecked());
191             setText(fAction.getText());
192             setToolTipText(fAction.getToolTipText());
193             fAction.addPropertyChangeListener(fListener);
194         }
195     }
196
197     /**
198      * Installs the help listener.
199      *
200      * @since 2.1
201      */

202     private void installHelpListener() {
203         super.setHelpListener(new HelpListener() {
204             public void helpRequested(HelpEvent e) {
205                 HelpListener listener= null;
206                 if (fAction != null) {
207                     // if we have a handler, see if it has a help listener
208
listener= fAction.getHelpListener();
209                     if (listener == null)
210                         // use our own help listener
211
listener= fLocalHelpListener;
212                 }
213                 if (listener != null)
214                     // pass on the event
215
listener.helpRequested(e);
216             }
217         });
218     }
219
220     /**
221      * The <code>RetargetTextEditorAction</code> implementation of this method declared on
222      * <code>IAction</code> stores the help listener in a local field. The
223      * supplied listener is only used if there is no handler.
224      *
225      * @param listener the help listener
226      * @since 2.1
227      */

228     public void setHelpListener(HelpListener listener) {
229         fLocalHelpListener= listener;
230     }
231
232     /*
233      * @see IAction#run()
234      */

235     public void run() {
236         if (fAction != null)
237             fAction.run();
238     }
239 }
240
Popular Tags