001: package net.sourceforge.squirrel_sql.fw.util;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.net.URL;
022: import java.util.Locale;
023: import java.util.MissingResourceException;
024: import java.util.ResourceBundle;
025:
026: import javax.swing.Action;
027: import javax.swing.Icon;
028: import javax.swing.ImageIcon;
029: import javax.swing.JCheckBoxMenuItem;
030: import javax.swing.JMenu;
031: import javax.swing.JMenuItem;
032: import javax.swing.JPopupMenu;
033: import javax.swing.KeyStroke;
034:
035: import net.sourceforge.squirrel_sql.fw.gui.action.BaseAction;
036: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
037: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
038:
039: public abstract class Resources {
040: public static final String ACCELERATOR_STRING = "SQuirreLAcceleratorString";
041:
042: private interface ActionProperties {
043: String DISABLED_IMAGE = "disabledimage";
044: String IMAGE = "image";
045: String NAME = "name";
046: String ROLLOVER_IMAGE = "rolloverimage";
047: String TOOLTIP = "tooltip";
048: }
049:
050: private interface MenuProperties {
051: String TITLE = "title";
052: String MNEMONIC = "mnemonic";
053: }
054:
055: private interface MenuItemProperties extends MenuProperties {
056: String ACCELERATOR = "accelerator";
057: }
058:
059: private interface Keys {
060: String ACTION = "action";
061: String MENU = "menu";
062: String MENU_ITEM = "menuitem";
063: }
064:
065: /** Logger for this class. */
066: private static final ILogger s_log = LoggerController
067: .createLogger(Resources.class);
068:
069: /** Applications resource bundle. */
070: private final ResourceBundle _bundle;
071:
072: /** To load resources */
073: private ClassLoader _classLoader;
074:
075: /** Path to images. */
076: private final String _imagePath;
077:
078: protected Resources(String rsrcBundleBaseName, ClassLoader cl) {
079: super ();
080: if (rsrcBundleBaseName == null
081: || rsrcBundleBaseName.trim().length() == 0) {
082: throw new IllegalArgumentException(
083: "Null or empty rsrcBundleBaseName passed");
084: }
085:
086: _classLoader = cl;
087: _bundle = ResourceBundle.getBundle(rsrcBundleBaseName, Locale
088: .getDefault(), cl);
089: _imagePath = _bundle.getString("path.images");
090: }
091:
092: public KeyStroke getKeyStroke(Action action) {
093: final String fullKey = Keys.MENU_ITEM + "."
094: + action.getClass().getName();
095:
096: String accel = getResourceString(fullKey,
097: MenuItemProperties.ACCELERATOR);
098: if (accel.length() > 0) {
099: return KeyStroke.getKeyStroke(accel);
100: }
101: return null;
102: }
103:
104: private String getAcceleratorString(Action action) {
105: try {
106: final String fullKey = Keys.MENU_ITEM + "."
107: + action.getClass().getName();
108: return getResourceString(fullKey,
109: MenuItemProperties.ACCELERATOR);
110: } catch (MissingResourceException e) {
111: return null;
112: }
113: }
114:
115: public JMenuItem addToPopupMenu(Action action, JPopupMenu menu)
116: throws MissingResourceException {
117: final String fullKey = Keys.MENU_ITEM + "."
118: + action.getClass().getName();
119: final JMenuItem item = menu.add(action);
120:
121: if (action.getValue(Action.MNEMONIC_KEY) == null) {
122: String mn = getResourceString(fullKey,
123: MenuItemProperties.MNEMONIC);
124: if (mn.length() > 0) {
125: item.setMnemonic(mn.charAt(0));
126: }
127: }
128:
129: if (action.getValue(Action.ACCELERATOR_KEY) == null) {
130: String accel = getResourceString(fullKey,
131: MenuItemProperties.ACCELERATOR);
132: if (accel.length() > 0) {
133: item.setAccelerator(KeyStroke.getKeyStroke(accel));
134: }
135: }
136:
137: String toolTipText = getToolTipTextWithAccelerator(action,
138: fullKey);
139:
140: item.setToolTipText(toolTipText);
141:
142: return item;
143: }
144:
145: public JCheckBoxMenuItem addToMenuAsCheckBoxMenuItem(Action action,
146: JMenu menu) throws MissingResourceException {
147: final JCheckBoxMenuItem item = new JCheckBoxMenuItem(action);
148: menu.add(item);
149: configureMenuItem(action, item);
150: return item;
151: }
152:
153: public JMenuItem addToMenu(Action action, JMenu menu)
154: throws MissingResourceException {
155: final JMenuItem item = menu.add(action);
156: configureMenuItem(action, item);
157: return item;
158: }
159:
160: public JMenu createMenu(String menuKey)
161: throws MissingResourceException {
162: JMenu menu = new JMenu();
163: final String fullKey = Keys.MENU + "." + menuKey;
164: menu.setText(getResourceString(fullKey, MenuProperties.TITLE));
165: String mn = getResourceString(fullKey, MenuProperties.MNEMONIC);
166: if (mn.length() >= 1) {
167: menu.setMnemonic(mn.charAt(0));
168: }
169: return menu;
170: }
171:
172: /**
173: * Setup the passed action from the resource bundle.
174: *
175: * @param action Action being setup.
176: *
177: * @throws IllegalArgumentException
178: * thrown if <TT>null</TT> <TT>action</TT> passed.
179: */
180: public void setupAction(Action action, boolean showColoricons) {
181: if (action == null) {
182: throw new IllegalArgumentException("Action == null");
183: }
184:
185: final String actionClassName = action.getClass().getName();
186: final String key = Keys.ACTION + "." + actionClassName;
187: action.putValue(Action.NAME, getResourceString(key,
188: ActionProperties.NAME));
189:
190: String shortDescription = getResourceString(key,
191: ActionProperties.TOOLTIP);
192:
193: String acceleratorString = getAcceleratorString(action);
194: if (null != acceleratorString
195: && 0 < acceleratorString.trim().length()) {
196: shortDescription += " (" + acceleratorString + ")";
197:
198: }
199:
200: action.putValue(Action.SHORT_DESCRIPTION, shortDescription);
201:
202: String accelerator = getAcceleratorString(action);
203: if (null != accelerator) {
204: action.putValue(ACCELERATOR_STRING, accelerator);
205: }
206:
207: Icon icon = null;
208: try {
209: if (showColoricons == true) {
210: icon = getIcon(key, ActionProperties.ROLLOVER_IMAGE);
211: action.putValue(Action.SMALL_ICON, icon);
212: } else {
213: icon = getIcon(key, ActionProperties.IMAGE);
214: action.putValue(Action.SMALL_ICON, icon);
215: }
216: } catch (MissingResourceException ex) {
217: try {
218: icon = getIcon(key, ActionProperties.IMAGE);
219: action.putValue(Action.SMALL_ICON, icon);
220: } catch (MissingResourceException ignore) {
221: // Ignore
222: }
223: }
224:
225: try {
226: icon = getIcon(key, ActionProperties.ROLLOVER_IMAGE);
227: action.putValue(
228: BaseAction.IBaseActionPropertyNames.ROLLOVER_ICON,
229: icon);
230: } catch (MissingResourceException ignore) {
231: // Ignore
232: }
233:
234: try {
235: icon = getIcon(key, ActionProperties.DISABLED_IMAGE);
236: action.putValue(
237: BaseAction.IBaseActionPropertyNames.DISABLED_ICON,
238: icon);
239: } catch (MissingResourceException ignore) {
240: // Ignore
241: }
242: }
243:
244: public ImageIcon getIcon(String keyName) {
245: return getIcon(keyName, "image");
246: }
247:
248: public ImageIcon getIcon(Class<?> objClass, String propName) {
249: return getIcon(objClass.getName(), propName);
250: }
251:
252: public ImageIcon getIcon(String keyName, String propName) {
253: if (keyName == null) {
254: throw new IllegalArgumentException("keyName == null");
255: }
256: if (propName == null) {
257: throw new IllegalArgumentException("propName == null");
258: }
259:
260: ImageIcon icon = null;
261:
262: String rsrcName = getResourceString(keyName, propName);
263:
264: if (rsrcName != null && rsrcName.length() > 0) {
265: icon = privateGetIcon(rsrcName);
266: if (icon == null) {
267: s_log.error("can't load image: " + rsrcName);
268: }
269: } else {
270: s_log.debug("No resource found for " + keyName + " : "
271: + propName);
272: }
273:
274: return icon;
275: }
276:
277: public String getString(String key) {
278: return _bundle.getString(key);
279: }
280:
281: public void configureMenuItem(Action action, JMenuItem item)
282: throws MissingResourceException {
283: final String fullKey = Keys.MENU_ITEM + "."
284: + action.getClass().getName();
285:
286: if (action.getValue(Action.MNEMONIC_KEY) == null) {
287: String mn = getResourceString(fullKey,
288: MenuItemProperties.MNEMONIC);
289: if (mn.length() > 0) {
290: item.setMnemonic(mn.charAt(0));
291: }
292: }
293:
294: if (action.getValue(Action.ACCELERATOR_KEY) == null) {
295: String accel = getResourceString(fullKey,
296: MenuItemProperties.ACCELERATOR);
297: if (accel.length() > 0) {
298: item.setAccelerator(KeyStroke.getKeyStroke(accel));
299: }
300: }
301:
302: String toolTipText = getToolTipTextWithAccelerator(action,
303: fullKey);
304:
305: item.setToolTipText(toolTipText);
306:
307: //item.setIcon(null);
308: }
309:
310: private String getToolTipTextWithAccelerator(Action action,
311: String fullKey) {
312: String toolTipText = (String) action
313: .getValue(Action.SHORT_DESCRIPTION);
314:
315: if (null == toolTipText) {
316: toolTipText = "";
317: }
318: try {
319: String accel = getResourceString(fullKey,
320: MenuItemProperties.ACCELERATOR);
321: if (null != accel && accel.length() > 0) {
322: toolTipText += " (" + accel + ")";
323: }
324: } catch (MissingResourceException e) {
325: // Some actions dont have accelerators
326: }
327: return toolTipText;
328: }
329:
330: protected ResourceBundle getBundle() {
331: return _bundle;
332: }
333:
334: private ImageIcon privateGetIcon(String iconName) {
335: if (iconName != null && iconName.length() > 0) {
336: URL url;
337: String imagePathName = getImagePathName(iconName);
338:
339: if (null == _classLoader) {
340: url = getClass().getResource(imagePathName);
341:
342: // This slash stuff is a ...
343: if (null == url && imagePathName.startsWith("/")) {
344: url = getClass().getResource(
345: imagePathName.substring(1));
346: } else if (null == url
347: && false == imagePathName.startsWith("/")) {
348: url = getClass().getResource("/" + imagePathName);
349: }
350:
351: } else {
352: url = _classLoader.getResource(imagePathName);
353:
354: if (null == url && imagePathName.startsWith("/")) {
355: url = _classLoader.getResource(imagePathName
356: .substring(1));
357: } else if (null == url
358: && false == imagePathName.startsWith("/")) {
359: url = _classLoader.getResource("/" + imagePathName);
360: }
361: }
362:
363: if (url != null) {
364: return new ImageIcon(url);
365: }
366: }
367: return null;
368: }
369:
370: private String getResourceString(String keyName, String propName)
371: throws MissingResourceException {
372: return _bundle.getString(keyName + "." + propName);
373: }
374:
375: private String getImagePathName(String iconName) {
376: return _imagePath + iconName;
377: }
378: }
|