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.actions;
011:
012: import org.eclipse.jface.action.IAction;
013: import org.eclipse.jface.action.LegacyActionTools;
014: import org.eclipse.jface.resource.ImageDescriptor;
015: import org.eclipse.jface.util.PropertyChangeEvent;
016:
017: /**
018: * A <code>LabelRetargetAction</code> extends the behavior of
019: * RetargetAction. It will track the enable state, label, and
020: * tool tip text of the target action..
021: * <p>
022: * This class may be instantiated. It is not intented to be subclassed.
023: * </p>
024: *
025: * @since 2.0
026: */
027: public class LabelRetargetAction extends RetargetAction {
028: private String defaultText;
029:
030: private String defaultToolTipText;
031:
032: private ImageDescriptor defaultHoverImage;
033:
034: private ImageDescriptor defaultImage;
035:
036: private ImageDescriptor defaultDisabledImage;
037:
038: private String acceleratorText;
039:
040: /**
041: * Constructs a LabelRetargetAction with the given action id and text.
042: *
043: * @param actionID the retargetable action id
044: * @param text the action's text, or <code>null</code> if there is no text
045: */
046: public LabelRetargetAction(String actionID, String text) {
047: this (actionID, text, IAction.AS_UNSPECIFIED);
048: }
049:
050: /**
051: * Constructs a RetargetAction with the given action id, text and style.
052: *
053: * @param actionID the retargetable action id
054: * @param text the action's text, or <code>null</code> if there is no text
055: * @param style one of <code>AS_PUSH_BUTTON</code>, <code>AS_CHECK_BOX</code>,
056: * <code>AS_DROP_DOWN_MENU</code>, <code>AS_RADIO_BUTTON</code>, and
057: * <code>AS_UNSPECIFIED</code>.
058: * @since 3.0
059: */
060: public LabelRetargetAction(String actionID, String text, int style) {
061: super (actionID, text, style);
062: this .defaultText = text;
063: this .defaultToolTipText = text;
064: acceleratorText = LegacyActionTools
065: .extractAcceleratorText(text);
066: }
067:
068: /**
069: * The action handler has changed. Update self.
070: */
071: protected void propagateChange(PropertyChangeEvent event) {
072: super .propagateChange(event);
073: String prop = event.getProperty();
074: if (prop.equals(IAction.TEXT)) {
075: String str = (String) event.getNewValue();
076: super .setText(appendAccelerator(str));
077: } else if (prop.equals(IAction.TOOL_TIP_TEXT)) {
078: String str = (String) event.getNewValue();
079: super .setToolTipText(str);
080: } else if (prop.equals(IAction.IMAGE)) {
081: updateImages(getActionHandler());
082: }
083: }
084:
085: /**
086: * Sets the action handler. Update self.
087: */
088: protected void setActionHandler(IAction handler) {
089: // Run the default behavior.
090: super .setActionHandler(handler);
091:
092: // Now update the label, tooltip and images.
093: if (handler == null) {
094: super .setText(defaultText);
095: super .setToolTipText(defaultToolTipText);
096: } else {
097: // If no text is specified by the handler, use the default text. Fixes 22529.
098: String handlerText = handler.getText();
099: if (handlerText == null || handlerText.length() == 0) {
100: handlerText = defaultText;
101: }
102: super .setText(appendAccelerator(handlerText));
103: super .setToolTipText(handler.getToolTipText());
104: }
105: updateImages(handler);
106: }
107:
108: /* (non-Javadoc)
109: * Method declared on IAction.
110: */
111: public void setDisabledImageDescriptor(ImageDescriptor image) {
112: super .setDisabledImageDescriptor(image);
113: defaultDisabledImage = image;
114: }
115:
116: /* (non-Javadoc)
117: * Method declared on IAction.
118: */
119: public void setHoverImageDescriptor(ImageDescriptor image) {
120: super .setHoverImageDescriptor(image);
121: defaultHoverImage = image;
122: }
123:
124: /* (non-Javadoc)
125: * Method declared on IAction.
126: */
127: public void setImageDescriptor(ImageDescriptor image) {
128: super .setImageDescriptor(image);
129: defaultImage = image;
130: }
131:
132: /**
133: * Sets the action's label text to the given value.
134: */
135: public void setText(String text) {
136: super .setText(text);
137: acceleratorText = LegacyActionTools
138: .extractAcceleratorText(text);
139: defaultText = text;
140: }
141:
142: /**
143: * Sets the tooltip text to the given text.
144: * The value <code>null</code> clears the tooltip text.
145: */
146: public void setToolTipText(String text) {
147: super .setToolTipText(text);
148: defaultToolTipText = text;
149: }
150:
151: /**
152: * Ensures the accelerator is correct in the text (handlers are not
153: * allowed to change the accelerator).
154: */
155: private String appendAccelerator(String newText) {
156: if (newText == null) {
157: return null;
158: }
159:
160: // Remove any accelerator
161: String str = removeAcceleratorText(newText);
162: // Append our accelerator
163: if (acceleratorText != null) {
164: str = str + acceleratorText;
165: }
166: return str;
167: }
168:
169: /**
170: * Updates the images for this action based on the given handler.
171: */
172: private void updateImages(IAction handler) {
173: if (handler == null) {
174: super .setHoverImageDescriptor(defaultHoverImage);
175: super .setImageDescriptor(defaultImage);
176: super .setDisabledImageDescriptor(defaultDisabledImage);
177: } else {
178: // use the default images if the handler has no images set
179: ImageDescriptor hoverImage = handler
180: .getHoverImageDescriptor();
181: ImageDescriptor image = handler.getImageDescriptor();
182: ImageDescriptor disabledImage = handler
183: .getDisabledImageDescriptor();
184: if (hoverImage != null || image != null
185: || disabledImage != null) {
186: super.setHoverImageDescriptor(hoverImage);
187: super.setImageDescriptor(image);
188: super.setDisabledImageDescriptor(disabledImage);
189: } else {
190: super.setHoverImageDescriptor(defaultHoverImage);
191: super.setImageDescriptor(defaultImage);
192: super.setDisabledImageDescriptor(defaultDisabledImage);
193: }
194: }
195: }
196:
197: }
|