KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.ui.texteditor;
12
13
14
15 import java.util.MissingResourceException JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.resource.ImageDescriptor;
20
21 import org.eclipse.ui.PlatformUI;
22
23
24 /**
25  * An action which configures its label, image, tooltip, and description from
26  * a resource bundle using known keys.
27  * <p>
28  * Clients may subclass this abstract class to define new kinds of actions. As
29  * with <code>Action</code>, subclasses must implement the
30  * <code>IAction.run</code> method to carry out the action's semantics.
31  * </p>
32  */

33 public abstract class ResourceAction extends Action {
34
35     /**
36      * Retrieves and returns the value with the given key from the given resource
37      * bundle, or returns the given default value if there is no such resource.
38      * Convenience method for dealing gracefully with missing resources.
39      *
40      * @param bundle the resource bundle
41      * @param key the resource key
42      * @param defaultValue the default value, or <code>null</code>
43      * @return the resource value, or the given default value (which may be
44      * <code>null</code>)
45      */

46     protected static String JavaDoc getString(ResourceBundle JavaDoc bundle, String JavaDoc key, String JavaDoc defaultValue) {
47
48         String JavaDoc value= defaultValue;
49         try {
50             value= bundle.getString(key);
51         } catch (MissingResourceException JavaDoc x) {
52         }
53
54         return value;
55     }
56
57     /**
58      * Creates a new action that configures itself from the given resource
59      * bundle.
60      * <p>
61      * The following keys, prepended by the given option prefix,
62      * are used for retrieving resources from the given bundle:
63      * <ul>
64      * <li><code>"label"</code> - <code>setText</code></li>
65      * <li><code>"tooltip"</code> - <code>setToolTipText</code></li>
66      * <li><code>"image"</code> - <code>setImageDescriptor</code></li>
67      * <li><code>"description"</code> - <code>setDescription</code></li>
68      * </ul>
69      * </p>
70      *
71      * @param bundle the resource bundle
72      * @param prefix a prefix to be prepended to the various resource keys, or
73      * <code>null</code> if none
74      * @param style one of <code>IAction.AS_PUSH_BUTTON</code>, <code>IAction.AS_CHECK_BOX</code>,
75      * and <code>IAction.AS_RADIO_BUTTON</code>.
76      *
77      * @see ResourceAction#ResourceAction(ResourceBundle, String)
78      * @see org.eclipse.jface.action.IAction#AS_CHECK_BOX
79      * @see org.eclipse.jface.action.IAction#AS_DROP_DOWN_MENU
80      * @see org.eclipse.jface.action.IAction#AS_PUSH_BUTTON
81      * @see org.eclipse.jface.action.IAction#AS_RADIO_BUTTON
82      * @since 2.1
83      */

84     public ResourceAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, int style) {
85         super(null, style);
86         initialize(bundle, prefix);
87     }
88
89     /**
90      * Creates a new action that configures itself from the given resource
91      * bundle.
92      * <p>
93      * The following keys, prepended by the given option prefix,
94      * are used for retrieving resources from the given bundle:
95      * <ul>
96      * <li><code>"label"</code> - <code>setText</code></li>
97      * <li><code>"tooltip"</code> - <code>setToolTipText</code></li>
98      * <li><code>"image"</code> - <code>setImageDescriptor</code></li>
99      * <li><code>"description"</code> - <code>setDescription</code></li>
100      * </ul>
101      * </p>
102      *
103      * @param bundle the resource bundle
104      * @param prefix a prefix to be prepended to the various resource keys, or
105      * <code>null</code> if none
106      */

107     public ResourceAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix) {
108         super();
109         initialize(bundle, prefix);
110     }
111
112     /**
113      * Sets the action's help context id.
114      *
115      * @param contextId the help context id
116      */

117     public final void setHelpContextId(String JavaDoc contextId) {
118         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, contextId);
119     }
120
121     /**
122      * Initializes this action using the given bundle and prefix.
123      *
124      * @param bundle the resource bundle
125      * @param prefix a prefix to be prepended to the various resource keys, or <code>null</code> if none
126      * @since 2.1
127      */

128     protected void initialize(ResourceBundle JavaDoc bundle, String JavaDoc prefix) {
129         String JavaDoc labelKey= "label"; //$NON-NLS-1$
130
String JavaDoc tooltipKey= "tooltip"; //$NON-NLS-1$
131
String JavaDoc imageKey= "image"; //$NON-NLS-1$
132
String JavaDoc descriptionKey= "description"; //$NON-NLS-1$
133

134         if (prefix != null && prefix.length() > 0) {
135             labelKey= prefix + labelKey;
136             tooltipKey= prefix + tooltipKey;
137             imageKey= prefix + imageKey;
138             descriptionKey= prefix + descriptionKey;
139         }
140
141         setText(getString(bundle, labelKey, labelKey));
142         setToolTipText(getString(bundle, tooltipKey, null));
143         setDescription(getString(bundle, descriptionKey, null));
144
145         String JavaDoc file= getString(bundle, imageKey, null);
146         if (file != null && file.trim().length() > 0)
147             setImageDescriptor(ImageDescriptor.createFromFile(getClass(), file));
148     }
149 }
150
Popular Tags