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.MissingResourceException;
013: import java.util.ResourceBundle;
014:
015: import org.eclipse.jface.action.Action;
016: import org.eclipse.jface.resource.ImageDescriptor;
017:
018: import org.eclipse.ui.PlatformUI;
019:
020: /**
021: * An action which configures its label, image, tooltip, and description from
022: * a resource bundle using known keys.
023: * <p>
024: * Clients may subclass this abstract class to define new kinds of actions. As
025: * with <code>Action</code>, subclasses must implement the
026: * <code>IAction.run</code> method to carry out the action's semantics.
027: * </p>
028: */
029: public abstract class ResourceAction extends Action {
030:
031: /**
032: * Retrieves and returns the value with the given key from the given resource
033: * bundle, or returns the given default value if there is no such resource.
034: * Convenience method for dealing gracefully with missing resources.
035: *
036: * @param bundle the resource bundle
037: * @param key the resource key
038: * @param defaultValue the default value, or <code>null</code>
039: * @return the resource value, or the given default value (which may be
040: * <code>null</code>)
041: */
042: protected static String getString(ResourceBundle bundle,
043: String key, String defaultValue) {
044:
045: String value = defaultValue;
046: try {
047: value = bundle.getString(key);
048: } catch (MissingResourceException x) {
049: }
050:
051: return value;
052: }
053:
054: /**
055: * Creates a new action that configures itself from the given resource
056: * bundle.
057: * <p>
058: * The following keys, prepended by the given option prefix,
059: * are used for retrieving resources from the given bundle:
060: * <ul>
061: * <li><code>"label"</code> - <code>setText</code></li>
062: * <li><code>"tooltip"</code> - <code>setToolTipText</code></li>
063: * <li><code>"image"</code> - <code>setImageDescriptor</code></li>
064: * <li><code>"description"</code> - <code>setDescription</code></li>
065: * </ul>
066: * </p>
067: *
068: * @param bundle the resource bundle
069: * @param prefix a prefix to be prepended to the various resource keys, or
070: * <code>null</code> if none
071: * @param style one of <code>IAction.AS_PUSH_BUTTON</code>, <code>IAction.AS_CHECK_BOX</code>,
072: * and <code>IAction.AS_RADIO_BUTTON</code>.
073: *
074: * @see ResourceAction#ResourceAction(ResourceBundle, String)
075: * @see org.eclipse.jface.action.IAction#AS_CHECK_BOX
076: * @see org.eclipse.jface.action.IAction#AS_DROP_DOWN_MENU
077: * @see org.eclipse.jface.action.IAction#AS_PUSH_BUTTON
078: * @see org.eclipse.jface.action.IAction#AS_RADIO_BUTTON
079: * @since 2.1
080: */
081: public ResourceAction(ResourceBundle bundle, String prefix,
082: int style) {
083: super (null, style);
084: initialize(bundle, prefix);
085: }
086:
087: /**
088: * Creates a new action that configures itself from the given resource
089: * bundle.
090: * <p>
091: * The following keys, prepended by the given option prefix,
092: * are used for retrieving resources from the given bundle:
093: * <ul>
094: * <li><code>"label"</code> - <code>setText</code></li>
095: * <li><code>"tooltip"</code> - <code>setToolTipText</code></li>
096: * <li><code>"image"</code> - <code>setImageDescriptor</code></li>
097: * <li><code>"description"</code> - <code>setDescription</code></li>
098: * </ul>
099: * </p>
100: *
101: * @param bundle the resource bundle
102: * @param prefix a prefix to be prepended to the various resource keys, or
103: * <code>null</code> if none
104: */
105: public ResourceAction(ResourceBundle bundle, String prefix) {
106: super ();
107: initialize(bundle, prefix);
108: }
109:
110: /**
111: * Sets the action's help context id.
112: *
113: * @param contextId the help context id
114: */
115: public final void setHelpContextId(String contextId) {
116: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
117: contextId);
118: }
119:
120: /**
121: * Initializes this action using the given bundle and prefix.
122: *
123: * @param bundle the resource bundle
124: * @param prefix a prefix to be prepended to the various resource keys, or <code>null</code> if none
125: * @since 2.1
126: */
127: protected void initialize(ResourceBundle bundle, String prefix) {
128: String labelKey = "label"; //$NON-NLS-1$
129: String tooltipKey = "tooltip"; //$NON-NLS-1$
130: String imageKey = "image"; //$NON-NLS-1$
131: String descriptionKey = "description"; //$NON-NLS-1$
132:
133: if (prefix != null && prefix.length() > 0) {
134: labelKey = prefix + labelKey;
135: tooltipKey = prefix + tooltipKey;
136: imageKey = prefix + imageKey;
137: descriptionKey = prefix + descriptionKey;
138: }
139:
140: setText(getString(bundle, labelKey, labelKey));
141: setToolTipText(getString(bundle, tooltipKey, null));
142: setDescription(getString(bundle, descriptionKey, null));
143:
144: String file = getString(bundle, imageKey, null);
145: if (file != null && file.trim().length() > 0)
146: setImageDescriptor(ImageDescriptor.createFromFile(
147: getClass(), file));
148: }
149: }
|