001: package net.sourceforge.squirrel_sql.fw.gui;
002:
003: /*
004: * Copyright (C) 2001-2004 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 net.sourceforge.squirrel_sql.fw.gui.action.BaseAction;
022: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
023: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
024:
025: import javax.swing.*;
026:
027: /**
028: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
029: */
030: public class ToolBar extends JToolBar {
031: /** Logger for this class. */
032: private static ILogger s_log = LoggerController
033: .createLogger(ToolBar.class);
034:
035: public ToolBar() {
036: super ();
037: }
038:
039: public ToolBar(int orientation) {
040: super (orientation);
041: }
042:
043: public JButton add(Action action) {
044: JButton btn = super .add(action);
045: initialiseButton(action, btn);
046: return btn;
047: }
048:
049: public JToggleButton addToggleAction(IToggleAction action) {
050: JToggleButton tglBtn = new JToggleButton();
051: tglBtn.setAction(action);
052: super .add(tglBtn);
053: action.getToggleComponentHolder()
054: .addToggleableComponent(tglBtn);
055: initialiseButton(action, tglBtn);
056: return tglBtn;
057: }
058:
059: public AbstractButton add(Action action, AbstractButton btn) {
060: btn.setAction(action);
061: super .add(btn);
062: initialiseButton(action, btn);
063: return btn;
064: }
065:
066: public void setUseRolloverButtons(boolean value) {
067: putClientProperty("JToolBar.isRollover", value ? Boolean.TRUE
068: : Boolean.FALSE);
069: }
070:
071: protected void initialiseButton(Action action, AbstractButton btn) {
072: if (btn != null) {
073: btn.setRequestFocusEnabled(false);
074: btn.setText("");
075: String tt = null;
076: if (action != null) {
077: tt = (String) action.getValue(Action.SHORT_DESCRIPTION);
078: }
079: btn.setToolTipText(tt != null ? tt : "");
080: if (action != null) {
081: Icon icon = getIconFromAction(
082: action,
083: BaseAction.IBaseActionPropertyNames.ROLLOVER_ICON);
084: if (icon != null) {
085: btn.setRolloverIcon(icon);
086: btn.setRolloverSelectedIcon(icon);
087: }
088: icon = getIconFromAction(
089: action,
090: BaseAction.IBaseActionPropertyNames.DISABLED_ICON);
091: if (icon != null) {
092: btn.setDisabledIcon(icon);
093: }
094: }
095: }
096: }
097:
098: /**
099: * Retrieve an icon from the passed action for the specified key.
100: *
101: * @param action Action to retrieve icon from.
102: * @param key Key that specified the icon.
103: *
104: * @return The requested Icon or null.
105: */
106: protected Icon getIconFromAction(Action action, String key) {
107: //Object obj = action.getValue(BaseAction.IBaseActionPropertyNames.ROLLOVER_ICON);
108: Object obj = action.getValue(key);
109: if (obj != null) {
110: if (obj instanceof Icon) {
111: return (Icon) obj;
112: }
113: StringBuffer msg = new StringBuffer();
114: msg.append("Non icon object of type ").append(
115: obj.getClass().getName()).append(
116: " was stored in an Action of type ").append(
117: action.getClass().getName()).append(
118: " using the key ").append(key).append(".");
119: s_log.error(msg.toString());
120: }
121: return null;
122: }
123: }
|