001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.mobility.svgcore.view.svg;
043:
044: import java.awt.Image;
045: import java.awt.event.KeyEvent;
046: import java.util.MissingResourceException;
047: import javax.swing.AbstractAction;
048: import javax.swing.Action;
049: import javax.swing.ImageIcon;
050: import javax.swing.InputMap;
051: import javax.swing.JComponent;
052: import javax.swing.JMenuItem;
053: import javax.swing.KeyStroke;
054: import org.openide.util.NbBundle;
055: import org.openide.util.Utilities;
056: import org.openide.util.actions.Presenter;
057: import org.openide.windows.TopComponent;
058:
059: /**
060: *
061: * @author Pavel Benes
062: */
063: public abstract class AbstractSVGAction extends AbstractAction
064: implements Presenter.Popup {
065: public static final String ICON_PATH_PREFIX = "org/netbeans/modules/mobility/svgcore/resources/"; //NOI18N
066: public static final String LBL_ID_PREFIX = "LBL_"; //NOI18N
067: public static final String ICON_ID_PREFIX = "ICON_"; //NOI18N
068: public static final String HINT_ID_PREFIX = "HINT_"; //NOI18N
069: public static final String KEY_ID_PREFIX = "KEY_"; //NOI18N
070:
071: protected final String m_name;
072: protected final String m_label;
073: protected final String m_hint;
074: protected final ImageIcon m_icon;
075: protected final int m_toolbarPos;
076:
077: public AbstractSVGAction(String name) {
078: this (name, true);
079: }
080:
081: public AbstractSVGAction(String name, boolean isEnabled) {
082: this (name, isEnabled, 100);
083: }
084:
085: public AbstractSVGAction(String name, boolean isEnabled,
086: int toolBarPos) {
087: m_name = name;
088: m_label = getMessage(LBL_ID_PREFIX + name);
089:
090: m_icon = getIcon(ICON_ID_PREFIX + name);
091: setIcon(m_icon);
092:
093: String hint;
094: try {
095: hint = getMessage(HINT_ID_PREFIX + name);
096: } catch (MissingResourceException e) {
097: hint = m_label;
098: }
099: m_hint = hint;
100:
101: try {
102: String key = getCleanMessage(KEY_ID_PREFIX + name);
103: KeyStroke stroke = KeyStroke.getKeyStroke(key);
104: assert stroke != null : "Invalid key stroke: " + name; //NOI18N
105: putValue(Action.ACCELERATOR_KEY, stroke);
106: } catch (MissingResourceException e) {
107: }
108:
109: setDescription(hint);
110: setEnabled(isEnabled);
111: m_toolbarPos = toolBarPos;
112: }
113:
114: protected final void setIcon(ImageIcon icon) {
115: if (icon != null) {
116: putValue(Action.SMALL_ICON, icon);
117: }
118: }
119:
120: protected final void setDescription(String hint) {
121: KeyStroke stroke = (KeyStroke) getValue(Action.ACCELERATOR_KEY);
122: if (stroke != null) {
123: hint += " ("; //NOI18N
124: String str = KeyEvent.getKeyModifiersText(stroke
125: .getModifiers());
126: if (str.length() > 0) {
127: hint += str + "+"; //NOI18N
128: }
129: hint += KeyEvent.getKeyText(stroke.getKeyCode()) + ")"; //NOI18N
130: }
131: putValue(Action.SHORT_DESCRIPTION, hint);
132: }
133:
134: public String getActionID() {
135: return m_name;
136: }
137:
138: public JMenuItem getPopupPresenter() {
139: JMenuItem menu = new JMenuItem(this );
140: menu.setText(getLabel());
141: menu.setToolTipText(null);
142: menu.setIcon(null);
143: return menu;
144: }
145:
146: public int getPositionInToolbar() {
147: return m_toolbarPos;
148: }
149:
150: public void registerAction(TopComponent tc) {
151: KeyStroke ks = (KeyStroke) getValue(Action.ACCELERATOR_KEY);
152: if (ks != null) {
153: InputMap inputMap = tc
154: .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
155: inputMap.put(ks, m_name);
156: tc.getActionMap().put(m_name, this );
157: }
158: }
159:
160: protected String getLabel() {
161: return m_label;
162: }
163:
164: public static String getMessage(String msgId) {
165: return NbBundle.getMessage(SVGViewTopComponent.class, msgId);
166: }
167:
168: public static String getCleanMessage(String msgId) {
169: String msg = getMessage(msgId);
170:
171: if (msg != null && msg.length() > 0) {
172: // remove the (m,n) suffix that gets attached when NbBundle.DEBUG
173: // property is set to true
174: int p;
175: if ((p = msg.lastIndexOf('(')) != -1) {
176: int q;
177: if ((q = msg.indexOf(')', p)) != -1) {
178: StringBuilder sb = new StringBuilder(msg);
179: sb.delete(p, q + 1);
180: msg = sb.toString().trim();
181: }
182: }
183: }
184: return msg;
185: }
186:
187: public static ImageIcon getIcon(String bundleKey) {
188: try {
189: String iconFileName = getCleanMessage(bundleKey).trim();
190: String iconPath = ICON_PATH_PREFIX + iconFileName;
191: Image img = Utilities.loadImage(iconPath);
192: assert img != null : "Icon not found: " + iconPath; //NOI18N
193: return new ImageIcon(img);
194: } catch (MissingResourceException e) {
195: // no icon is provided
196: return null;
197: }
198: }
199: }
|