KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > LabelRetargetAction


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.actions;
12
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.action.LegacyActionTools;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.jface.util.PropertyChangeEvent;
17
18 /**
19  * A <code>LabelRetargetAction</code> extends the behavior of
20  * RetargetAction. It will track the enable state, label, and
21  * tool tip text of the target action..
22  * <p>
23  * This class may be instantiated. It is not intented to be subclassed.
24  * </p>
25  *
26  * @since 2.0
27  */

28 public class LabelRetargetAction extends RetargetAction {
29     private String JavaDoc defaultText;
30
31     private String JavaDoc defaultToolTipText;
32
33     private ImageDescriptor defaultHoverImage;
34
35     private ImageDescriptor defaultImage;
36
37     private ImageDescriptor defaultDisabledImage;
38
39     private String JavaDoc acceleratorText;
40
41     /**
42      * Constructs a LabelRetargetAction with the given action id and text.
43      *
44      * @param actionID the retargetable action id
45      * @param text the action's text, or <code>null</code> if there is no text
46      */

47     public LabelRetargetAction(String JavaDoc actionID, String JavaDoc text) {
48         this(actionID, text, IAction.AS_UNSPECIFIED);
49     }
50
51     /**
52      * Constructs a RetargetAction with the given action id, text and style.
53      *
54      * @param actionID the retargetable action id
55      * @param text the action's text, or <code>null</code> if there is no text
56      * @param style one of <code>AS_PUSH_BUTTON</code>, <code>AS_CHECK_BOX</code>,
57      * <code>AS_DROP_DOWN_MENU</code>, <code>AS_RADIO_BUTTON</code>, and
58      * <code>AS_UNSPECIFIED</code>.
59      * @since 3.0
60      */

61     public LabelRetargetAction(String JavaDoc actionID, String JavaDoc text, int style) {
62         super(actionID, text, style);
63         this.defaultText = text;
64         this.defaultToolTipText = text;
65         acceleratorText = LegacyActionTools.extractAcceleratorText(text);
66     }
67
68     /**
69      * The action handler has changed. Update self.
70      */

71     protected void propagateChange(PropertyChangeEvent event) {
72         super.propagateChange(event);
73         String JavaDoc prop = event.getProperty();
74         if (prop.equals(IAction.TEXT)) {
75             String JavaDoc str = (String JavaDoc) event.getNewValue();
76             super.setText(appendAccelerator(str));
77         } else if (prop.equals(IAction.TOOL_TIP_TEXT)) {
78             String JavaDoc str = (String JavaDoc) event.getNewValue();
79             super.setToolTipText(str);
80         } else if (prop.equals(IAction.IMAGE)) {
81             updateImages(getActionHandler());
82         }
83     }
84
85     /**
86      * Sets the action handler. Update self.
87      */

88     protected void setActionHandler(IAction handler) {
89         // Run the default behavior.
90
super.setActionHandler(handler);
91
92         // Now update the label, tooltip and images.
93
if (handler == null) {
94             super.setText(defaultText);
95             super.setToolTipText(defaultToolTipText);
96         } else {
97             // If no text is specified by the handler, use the default text. Fixes 22529.
98
String JavaDoc handlerText = handler.getText();
99             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 JavaDoc text) {
136         super.setText(text);
137         acceleratorText = LegacyActionTools.extractAcceleratorText(text);
138         defaultText = text;
139     }
140
141     /**
142      * Sets the tooltip text to the given text.
143      * The value <code>null</code> clears the tooltip text.
144      */

145     public void setToolTipText(String JavaDoc text) {
146         super.setToolTipText(text);
147         defaultToolTipText = text;
148     }
149
150     /**
151      * Ensures the accelerator is correct in the text (handlers are not
152      * allowed to change the accelerator).
153      */

154     private String JavaDoc appendAccelerator(String JavaDoc newText) {
155         if (newText == null) {
156             return null;
157         }
158
159         // Remove any accelerator
160
String JavaDoc str = removeAcceleratorText(newText);
161         // Append our accelerator
162
if (acceleratorText != null) {
163             str = str + acceleratorText;
164         }
165         return str;
166     }
167
168     /**
169      * Updates the images for this action based on the given handler.
170      */

171     private void updateImages(IAction handler) {
172         if (handler == null) {
173             super.setHoverImageDescriptor(defaultHoverImage);
174             super.setImageDescriptor(defaultImage);
175             super.setDisabledImageDescriptor(defaultDisabledImage);
176         } else {
177             // use the default images if the handler has no images set
178
ImageDescriptor hoverImage = handler.getHoverImageDescriptor();
179             ImageDescriptor image = handler.getImageDescriptor();
180             ImageDescriptor disabledImage = handler
181                     .getDisabledImageDescriptor();
182             if (hoverImage != null || image != null || disabledImage != null) {
183                 super.setHoverImageDescriptor(hoverImage);
184                 super.setImageDescriptor(image);
185                 super.setDisabledImageDescriptor(disabledImage);
186             } else {
187                 super.setHoverImageDescriptor(defaultHoverImage);
188                 super.setImageDescriptor(defaultImage);
189                 super.setDisabledImageDescriptor(defaultDisabledImage);
190             }
191         }
192     }
193
194 }
195
Popular Tags