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.ui.texteditor;
011:
012: import java.util.ResourceBundle;
013:
014: import org.eclipse.swt.events.HelpEvent;
015: import org.eclipse.swt.events.HelpListener;
016:
017: import org.eclipse.jface.action.IAction;
018: import org.eclipse.jface.util.IPropertyChangeListener;
019: import org.eclipse.jface.util.PropertyChangeEvent;
020:
021: /**
022: * Action used by an editor action bar contributor to establish placeholders in
023: * menus or action bars which can be retargeted to dynamically changing actions,
024: * for example, those which come from the active editor. This action assumes that
025: * the "wrapped" action sends out property change events in response to state
026: * changes. It uses these change notifications to adapt its enabling state and
027: * its visual presentation.
028: */
029: public final class RetargetTextEditorAction extends ResourceAction {
030:
031: /** The target action. */
032: private IAction fAction;
033: /** The default label if there is no target action. */
034: private String fDefaultText;
035: /**
036: * The local help listener
037: * @since 2.1
038: */
039: private HelpListener fLocalHelpListener;
040: /** The listener to pick up changes of the target action. */
041: private IPropertyChangeListener fListener = new IPropertyChangeListener() {
042: public void propertyChange(PropertyChangeEvent event) {
043: update(event);
044: }
045: };
046:
047: /**
048: * Creates a new action. The action configures its initial visual
049: * representation from the given resource bundle. If this action's
050: * wrapped action is set to <code>null</code> it also uses the
051: * information in the resource bundle.
052: *
053: * @param bundle the resource bundle
054: * @param prefix a prefix to be prepended to the various resource keys
055: * (described in <code>ResourceAction</code> constructor), or
056: * <code>null</code> if none
057: * @param style one of <code>IAction.AS_PUSH_BUTTON</code>, <code>IAction.AS_CHECK_BOX</code>,
058: * and <code>IAction.AS_RADIO_BUTTON</code>.
059: *
060: * @see ResourceAction#ResourceAction(ResourceBundle, String, int)
061: * @see IAction#AS_CHECK_BOX
062: * @see IAction#AS_DROP_DOWN_MENU
063: * @see IAction#AS_PUSH_BUTTON
064: * @see IAction#AS_RADIO_BUTTON
065: * @since 2.1
066: */
067: public RetargetTextEditorAction(ResourceBundle bundle,
068: String prefix, int style) {
069: super (bundle, prefix, style);
070: fDefaultText = getText();
071: installHelpListener();
072: }
073:
074: /**
075: * Creates a new action. The action configures its initial visual
076: * representation from the given resource bundle. If this action's
077: * wrapped action is set to <code>null</code> it also uses the
078: * information in the resource bundle.
079: *
080: * @param bundle the resource bundle
081: * @param prefix a prefix to be prepended to the various resource keys
082: * (described in <code>ResourceAction</code> constructor), or
083: * <code>null</code> if none
084: * @see ResourceAction#ResourceAction(ResourceBundle, String)
085: */
086: public RetargetTextEditorAction(ResourceBundle bundle, String prefix) {
087: super (bundle, prefix);
088: fDefaultText = getText();
089: installHelpListener();
090: }
091:
092: /**
093: * Creates a new action. The action configures its initial visual
094: * representation from the given resource bundle. If this action's
095: * wrapped action is set to <code>null</code> it also uses the
096: * information in the resource bundle. The action gets the given
097: * action id.
098: *
099: * @param bundle the resource bundle
100: * @param prefix a prefix to be prepended to the various resource keys
101: * (described in <code>ResourceAction</code> constructor), or <code>null</code> if none
102: * @param actionId the action id
103: * @param style one of <code>IAction.AS_PUSH_BUTTON</code>, <code>IAction.AS_CHECK_BOX</code>,
104: * and <code>IAction.AS_RADIO_BUTTON</code>.
105: *
106: * @see ResourceAction#ResourceAction(ResourceBundle, String, int)
107: * @see IAction#AS_CHECK_BOX
108: * @see IAction#AS_DROP_DOWN_MENU
109: * @see IAction#AS_PUSH_BUTTON
110: * @see IAction#AS_RADIO_BUTTON
111: * @since 2.1
112: */
113: public RetargetTextEditorAction(ResourceBundle bundle,
114: String prefix, String actionId, int style) {
115: super (bundle, prefix, style);
116: fDefaultText = getText();
117: setId(actionId);
118: installHelpListener();
119: }
120:
121: /**
122: * Creates a new action. The action configures its initial visual
123: * representation from the given resource bundle. If this action's
124: * wrapped action is set to <code>null</code> it also uses the
125: * information in the resource bundle. The action gets the given
126: * action id.
127: *
128: * @param bundle the resource bundle
129: * @param prefix a prefix to be prepended to the various resource keys
130: * (described in <code>ResourceAction</code> constructor), or <code>null</code> if none
131: * @param actionId the action id
132: * @see ResourceAction#ResourceAction(ResourceBundle, String)
133: * @since 2.0
134: */
135: public RetargetTextEditorAction(ResourceBundle bundle,
136: String prefix, String actionId) {
137: super (bundle, prefix);
138: fDefaultText = getText();
139: setId(actionId);
140: installHelpListener();
141: }
142:
143: /**
144: * Updates to the changes of the underlying action.
145: *
146: * @param event the change event describing the state change
147: */
148: private void update(PropertyChangeEvent event) {
149: if (ENABLED.equals(event.getProperty())) {
150: Boolean bool = (Boolean) event.getNewValue();
151: setEnabled(bool.booleanValue());
152: } else if (TEXT.equals(event.getProperty()))
153: setText((String) event.getNewValue());
154: else if (TOOL_TIP_TEXT.equals(event.getProperty()))
155: setToolTipText((String) event.getNewValue());
156: else if (CHECKED.equals(event.getProperty())) {
157: Boolean bool = (Boolean) event.getNewValue();
158: setChecked(bool.booleanValue());
159: }
160: }
161:
162: /**
163: * Sets the underlying action.
164: *
165: * @param action the underlying action
166: */
167: public void setAction(IAction action) {
168:
169: if (fAction != null) {
170: fAction.removePropertyChangeListener(fListener);
171: fAction = null;
172: }
173:
174: fAction = action;
175:
176: if (fAction == null) {
177:
178: setEnabled(false);
179: if (getStyle() == AS_CHECK_BOX
180: || getStyle() == AS_RADIO_BUTTON)
181: setChecked(false);
182: setText(fDefaultText);
183: setToolTipText(""); //$NON-NLS-1$
184:
185: } else {
186:
187: setEnabled(fAction.isEnabled());
188: if (fAction.getStyle() == AS_CHECK_BOX
189: || 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: }
|