001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.util;
019:
020: import java.awt.Dimension;
021: import java.awt.Font;
022: import java.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024:
025: import javax.swing.AbstractButton;
026: import javax.swing.Action;
027: import javax.swing.Icon;
028: import javax.swing.JButton;
029: import javax.swing.JCheckBoxMenuItem;
030: import javax.swing.JMenu;
031: import javax.swing.JMenuItem;
032: import javax.swing.JToggleButton;
033: import javax.swing.SwingConstants;
034:
035: import de.finix.contelligent.client.gui.ContelligentAction;
036: import de.finix.contelligent.client.gui.ContelligentSubmenuAction;
037: import de.finix.contelligent.client.i18n.Resources;
038:
039: /**
040: * Composer for button creation following common policy.
041: */
042: final public class ButtonComposer {
043: public static JMenuItem createMenuItem(Action action) {
044: JMenuItem item = null;
045: if (action.getValue(ContelligentAction.TYPE) == ContelligentAction.TOGGLE_ACTION) {
046: item = new JCheckBoxMenuItem(action);
047: syncMenuItemAndAction((JCheckBoxMenuItem) item, action);
048: action
049: .addPropertyChangeListener(new PropertyChangeListener() {
050: public void propertyChange(
051: PropertyChangeEvent event) {
052: if (event.getSource() instanceof ContelligentAction) {
053: ContelligentAction sourceAction = (ContelligentAction) event
054: .getSource();
055: JCheckBoxMenuItem radioItem = (JCheckBoxMenuItem) sourceAction
056: .getValue(ContelligentAction.MENU_INSTANCE);
057: syncMenuItemAndAction(radioItem,
058: sourceAction);
059: }
060: }
061: });
062: // do not complain as this can be a fixed name for a view resp.
063: // module
064: if (action instanceof MnemonicAbstractAction) {
065: item.setText((String) action.getValue(Action.NAME));
066: int mnemonic = ((Integer) action
067: .getValue(Action.MNEMONIC_KEY)).intValue();
068: if (mnemonic != -1) {
069: item.setMnemonic(mnemonic);
070: }
071: } else {
072: item.setText(Resources.getLocalString((String) action
073: .getValue(Action.NAME), false));
074: }
075: // item.setIcon(null);
076: Icon icon = (Icon) action.getValue(Action.SMALL_ICON);
077: if (icon == Resources.defaultViewIcon) {
078: icon = null;
079: }
080: item.setIcon(icon);
081: } else if (action instanceof ContelligentSubmenuAction) {
082: item = ((ContelligentSubmenuAction) action).getMenuItem();
083: } else {
084: if (action.getValue(ContelligentAction.TYPE) == ContelligentAction.SUBMENU_ACTION) {
085: item = new JMenu(action);
086: } else {
087: item = new JMenuItem(action);
088: }
089: // do not complain as this can be a fixed name for a view resp.
090: // module
091: if (action instanceof MnemonicAbstractAction) {
092: item.setText((String) action.getValue(Action.NAME));
093: int mnemonic = ((Integer) action
094: .getValue(Action.MNEMONIC_KEY)).intValue();
095: if (mnemonic != -1) {
096: item.setMnemonic(mnemonic);
097: }
098: } else {
099: item.setText(Resources.getLocalString((String) action
100: .getValue(Action.NAME), false));
101: }
102: // item.setIcon(null);
103: Icon icon = (Icon) action.getValue(Action.SMALL_ICON);
104: if (icon == Resources.defaultViewIcon) {
105: icon = null;
106: }
107: item.setIcon(icon);
108: }
109: Object description = action.getValue(Action.SHORT_DESCRIPTION);
110: if (description != null && !description.equals("")) {
111: item.setToolTipText(Resources.getLocalString(
112: (String) description, false));
113: }
114: return item;
115: }
116:
117: public static JMenu createMenu(ActionGroup group) {
118: JMenu menu = new JMenu(group.getName());
119: menu.setText(Resources.getLocalString(group.getName(), false));
120: int mnemonic = group.getMnemonic();
121: if (mnemonic != -1) {
122: menu.setMnemonic(mnemonic);
123: }
124: return menu;
125: }
126:
127: public static AbstractButton createButton(Action action,
128: boolean buttonTextVisible, Font buttonFont,
129: boolean iconVisible, boolean doStandardLayout) {
130: AbstractButton button = null;
131: if (action.getValue(ContelligentAction.TYPE) == ContelligentAction.TOGGLE_ACTION) {
132: button = new JToggleButton(action);
133: action.putValue(ContelligentAction.BUTTON_INSTANCE, button);
134: syncButtonAndAction(button, action);
135: action
136: .addPropertyChangeListener(new PropertyChangeListener() {
137: public void propertyChange(
138: PropertyChangeEvent event) {
139: if (event.getSource() instanceof ContelligentAction) {
140: ContelligentAction sourceAction = (ContelligentAction) event
141: .getSource();
142: AbstractButton button = (AbstractButton) sourceAction
143: .getValue(ContelligentAction.BUTTON_INSTANCE);
144: syncButtonAndAction(button,
145: sourceAction);
146: }
147: }
148: });
149: } else {
150: button = new JButton(action);
151: }
152: if (button != null) {
153: if (iconVisible) {
154: button.setRolloverEnabled(true);
155: /*
156: * if ( action.getValue(ContelligentAction.ROLLOVER_ICON) !=
157: * null ) {
158: * button.setRolloverIcon((ImageIcon)action.getValue(ContelligentAction.ROLLOVER_ICON)); }
159: */
160: } else {
161: button.setIcon(null);
162: }
163: if (doStandardLayout) {
164: // button.setBorderPainted(false);
165: button.setMaximumSize(new Dimension(26, 28));
166: button.setPreferredSize(new Dimension(26, 28));
167: button.setHorizontalTextPosition(SwingConstants.CENTER);
168: button.setVerticalTextPosition(SwingConstants.BOTTOM);
169: }
170: if (buttonFont != null)
171: button.setFont(buttonFont);
172:
173: // do internationalization here
174: String bundleBase = (String) action
175: .getValue(ContelligentAction.RESOURCE_BUNDLE_BASE);
176: if (action instanceof MnemonicAbstractAction) {
177: button.setText((String) action.getValue(Action.NAME));
178: } else {
179: if (bundleBase != null) {
180: button.setText(Resources.getLocalStringForPackage(
181: (String) action.getValue(Action.NAME),
182: bundleBase));
183: } else {
184: button.setText(Resources.getLocalString(
185: (String) action.getValue(Action.NAME),
186: false));
187: }
188: }
189: if (!buttonTextVisible) {
190: button.setToolTipText(button.getText());
191: button.setText("");
192: } else {
193: Object description = action
194: .getValue(Action.SHORT_DESCRIPTION);
195: if (description != null && !description.equals("")) {
196: if (bundleBase != null) {
197: button.setToolTipText(Resources
198: .getLocalStringForPackage(
199: (String) description,
200: bundleBase, false));
201: } else {
202: button.setToolTipText(Resources.getLocalString(
203: (String) description, false));
204: }
205: }
206: }
207: }
208: button.setOpaque(false);
209: return button;
210: }
211:
212: private static void syncButtonAndAction(AbstractButton button,
213: Action action) {
214: if (action.getValue(ContelligentAction.STATE) == ContelligentAction.ON) {
215: button.setSelected(true);
216: } else {
217: button.setSelected(false);
218: }
219: }
220:
221: private static void syncMenuItemAndAction(JCheckBoxMenuItem item,
222: Action action) {
223: if (action.getValue(ContelligentAction.STATE) == ContelligentAction.ON) {
224: item.setSelected(true);
225: } else {
226: item.setSelected(false);
227: }
228: }
229:
230: }
|