KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > discRack > BarFactory


1 package discRack;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.beans.*;
6 import java.net.URL JavaDoc;
7 import java.util.*;
8 import java.io.*;
9
10 import javax.swing.*;
11 import javax.swing.undo.*;
12 import javax.swing.event.*;
13 import javax.swing.tree.*;
14
15 /**
16  * Implements the static methods that are used to implement
17  * multilanguage support, and to create some resources as
18  * menubar, toolbars and button panels.
19  *
20  * @author Sasa Bojanic
21  * @version 1.0
22 */

23 public class BarFactory extends JPanel {
24    /** Suffix applied to the key used in resource file lookups for an image. */
25    public static final String JavaDoc imageSuffix = "Image";
26    /** Suffix applied to the key used in resource file lookups for a label. */
27    public static final String JavaDoc labelSuffix = "Label";
28    /** Suffix applied to the key used in resource file lookups for an action. */
29    public static final String JavaDoc actionSuffix = "Action";
30    /** Suffix applied to the key used in resource file lookups for a submenu. */
31    public static final String JavaDoc menuSuffix = "Menu";
32    /** Suffix applied to the key used in resource file lookups for a menuitem (instead of action). */
33    public static final String JavaDoc accelSuffix = "Accel";
34    /** Suffix applied to the key used in resource file lookups for a menuitem (instead of action) */
35    public static final String JavaDoc mnemonicSuffix = "Mnemonic";
36    /** Suffix applied to the key used in resource file lookups for tooltip text. */
37    public static final String JavaDoc tipSuffix = "Tooltip";
38
39    //********************** CREATE TOOLBARS AND THEIR COMPONENTS *****************
40
/**
41     * Creates application's toolbars. Reads resource to find out which
42     * toolbars to create. The toolbars are put into the tabbed pane.
43     * NOTE: this method is modified to accept a creation of a button
44     * after toolbars.
45     * @see #createToolbar
46     */

47    public static Component createToolBars(String JavaDoc toolbarToLoad,Map actions) {
48       String JavaDoc[] toolBars = Utils.tokenize(ResourceManager.getResourceString(toolbarToLoad)," ");
49       JPanel lastPanel = new JPanel();
50       lastPanel.setLayout(new BorderLayout());
51       JTabbedPane tabbedPane = new JTabbedPane();
52       int i;
53       for (i = 0; i<toolBars.length; i++) {
54          // if I found "-" it means that button will be created
55
if (!toolBars[i].equals("-")) {
56             String JavaDoc label = ResourceManager.getResourceString(toolBars[i]+labelSuffix);
57             final JPanel panel = new JPanel();
58             panel.setLayout(new BorderLayout());
59             final Component c = createToolbar(toolBars[i],actions);
60             panel.add(BorderLayout.WEST,c);
61             panel.add(c);
62             ImageIcon icon = null;
63             URL JavaDoc url = ResourceManager.getResource(toolBars[i]+imageSuffix);
64             if (url != null) {
65                icon = new ImageIcon(url);
66             }
67             tabbedPane.addTab(label,icon,panel,label);
68          }
69          else {
70             break;
71          }
72       }
73       tabbedPane.setPreferredSize(new Dimension(500,60));
74       tabbedPane.setSelectedIndex(i-1);
75
76       lastPanel.add(BorderLayout.WEST,tabbedPane);
77       if (i<toolBars.length-1) {
78          Component c = createButton(toolBars[i+1],actions,false);
79          lastPanel.add(BorderLayout.EAST,c);
80       }
81
82       return lastPanel;
83    }
84
85    /**
86     * Create the toolbar. By default this reads the
87     * resource file for the definition of the toolbar.
88     * @see #createButton
89     */

90    public static Component createToolbar(String JavaDoc key,Map actions) {
91       String JavaDoc label = ResourceManager.getResourceString(key+labelSuffix);
92       JToolBar toolbar = new JToolBar(label);
93       toolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE);
94       toolbar.setFloatable(false);
95       String JavaDoc[] toolKeys = Utils.tokenize(ResourceManager.getResourceString(key)," ");
96       for (int i = 0; i < toolKeys.length; i++) {
97          if (toolKeys[i].equals("-")) {
98             toolbar.add(Box.createHorizontalStrut(10));
99          } else {
100             toolbar.add(createButton(toolKeys[i],actions,false));
101          }
102       }
103       toolbar.add(Box.createHorizontalGlue());
104       return toolbar;
105    }
106
107    /**
108     * Hook through which every toolbar item is created.
109     * Creates a button to go inside of the toolbar. By default this
110     * will load an image resource. The image filename is relative to
111     * the classpath (including the '.' directory if its a part of the
112     * classpath), and may either be in a JAR file or a separate file.
113     *
114     * @param key The key in the resource file to serve as the basis
115     * of lookups.
116     */

117    public static Component createButton(String JavaDoc key,Map actions,boolean setText) {
118       AbstractButton b = null;
119       URL JavaDoc url = ResourceManager.getResource(key + imageSuffix);
120       if (url!=null) {
121          b = new JButton(new ImageIcon(url)) {
122             public float getAlignmentY() { return 0.5f; }
123          };
124          if (setText) {
125             b.setText(ResourceManager.getResourceString(key+labelSuffix));
126          }
127       } else {
128          b = new JButton(ResourceManager.getResourceString(key+labelSuffix)) {
129             public float getAlignmentY() { return 0.5f; }
130          };
131       }
132
133       b.setMargin(new Insets(1,1,1,1));
134       b.setRequestFocusEnabled(false);
135
136       String JavaDoc astr = ResourceManager.getResourceString(key + actionSuffix);
137       if (astr == null) {
138          astr = key;
139       }
140       Action a = (Action)actions.get(astr);
141       if (a != null) {
142          b.setActionCommand(astr);
143          b.addActionListener(a);
144          a.addPropertyChangeListener(createActionChangeListener(b));
145          b.setEnabled(a.isEnabled());
146       } else {
147          b.setEnabled(false);
148       }
149
150
151       String JavaDoc tip = ResourceManager.getResourceString(key + tipSuffix);
152       if (tip != null) {
153          b.setToolTipText(tip);
154       }
155       return b;
156
157    }
158
159    //********************* CREATING MENUBAR AND IT'S COMPONENTS ******************
160
/**
161     * Create the menubar for the app. By default this pulls the
162     * definition of the menu from the associated resource file.
163     */

164    public static JMenuBar createMenubar(String JavaDoc menubarToLoad,Map actions) {
165       JMenuItem mi;
166       JMenuBar mb = new JMenuBar();
167
168       String JavaDoc[] menuKeys = Utils.tokenize(ResourceManager.getResourceString(menubarToLoad)," ");
169       for (int i = 0; i < menuKeys.length; i++) {
170          String JavaDoc[] itemKeys = Utils.tokenize(ResourceManager.getResourceString(menuKeys[i])," ");
171          JMenu m = createMenu(menuKeys[i],itemKeys,actions);
172          if (m != null) {
173             mb.add(m);
174          }
175       }
176
177       return mb;
178    }
179
180    /**
181     * Create a menu for the app.
182     */

183    public static JMenu createMenu(String JavaDoc key,String JavaDoc[] itemKeys,Map actions) {
184       JMenu menu = new JMenu(ResourceManager.getResourceString(key + labelSuffix));
185       for (int i = 0; i < itemKeys.length; i++) {
186          if (itemKeys[i].equals("-")) {
187             menu.addSeparator();
188          } else {
189             JMenuItem mi = createMenuItem(itemKeys[i],actions);
190             menu.add(mi);
191          }
192       }
193       URL JavaDoc url = ResourceManager.getResource(key + imageSuffix);
194       if (url != null) {
195          menu.setHorizontalTextPosition(JButton.RIGHT);
196          menu.setIcon(new ImageIcon(url));
197       }
198
199       setMnemonic(menu,ResourceManager.getResourceString(key+mnemonicSuffix));
200
201       menu.setActionCommand(key);
202       return menu;
203    }
204
205    /**
206     * This is the hook through which all menu items are
207     * created.
208     */

209    public static JMenuItem createMenuItem(String JavaDoc cmd,Map actions) {
210       String JavaDoc subMenu = ResourceManager.getResourceString(cmd + menuSuffix);
211       if (subMenu != null) {
212          String JavaDoc[] itemKeys = Utils.tokenize(subMenu," ");
213          JMenu mn=createMenu(cmd,itemKeys,actions);
214          return mn;
215       } else {
216          JMenuItem mi;
217          mi = new JMenuItem(ResourceManager.getResourceString(cmd + labelSuffix));
218          URL JavaDoc url = ResourceManager.getResource(cmd + imageSuffix);
219          if (url != null) {
220             mi.setHorizontalTextPosition(JButton.RIGHT);
221             mi.setIcon(new ImageIcon(url));
222          }
223          setAccelerator(mi,ResourceManager.getResourceString(cmd+accelSuffix));
224          setMnemonic(mi,ResourceManager.getResourceString(cmd+mnemonicSuffix));
225
226          String JavaDoc astr = ResourceManager.getResourceString(cmd + actionSuffix);
227          if (astr == null) {
228             astr = cmd;
229          }
230          mi.setActionCommand(astr);
231          Action a = (Action)actions.get(astr);
232          if (a != null) {
233             mi.addActionListener(a);
234             a.addPropertyChangeListener(createActionChangeListener(mi));
235             mi.setEnabled(a.isEnabled());
236          }
237          return mi;
238       }
239    }
240
241    public static void setMnemonic (JMenuItem mi,String JavaDoc mnemonic) {
242       if (mnemonic != null && mnemonic.length() > 0) {
243          mi.setMnemonic(mnemonic.charAt(0));
244       }
245    }
246
247    public static void setAccelerator (JMenuItem mi,String JavaDoc accel) {
248       if (accel != null) {
249          try {
250             int mask = 0;
251             if (accel.startsWith("CTRL")) {
252                mask += ActionEvent.CTRL_MASK;
253                accel = accel.substring(5);
254             }
255             if (accel.startsWith("SHIFT")) {
256                mask += ActionEvent.SHIFT_MASK;
257                accel = accel.substring(6);
258             }
259             if (accel.startsWith("ALT")) {
260                mask += ActionEvent.ALT_MASK;
261                accel = accel.substring(4);
262             }
263             int key = KeyEvent.class.getField("VK_"+accel).getInt(null);
264             mi.setAccelerator(KeyStroke.getKeyStroke(key, mask));
265          } catch (Exception JavaDoc e) {
266             System.err.println("Error while assigning accelerator !!!");
267          }
268       }
269    }
270
271    //********************* CREATING BUTTONPANEL ******************
272
/**
273     * Create the button panel. By default this reads the
274     * resource file for the definition of the panel.
275     */

276    public static Component createButtonPanel(String JavaDoc key,Map actions) {
277       String JavaDoc label = ResourceManager.getResourceString(key+labelSuffix);
278       JPanel p = new JPanel();
279       String JavaDoc[] buttonKeys = Utils.tokenize(ResourceManager.getResourceString(key)," ");
280       for (int i = 0; i < buttonKeys.length; i++) {
281          if (buttonKeys[i].equals("-")) {
282             p.add(Box.createHorizontalStrut(5));
283          } else {
284             p.add(createButton(buttonKeys[i],actions,true));
285          }
286       }
287       return p;
288    }
289
290
291    //************************* ACTIONCHANGEDLISTENER CLASS **********************
292
// Yarked from JMenu, ideally this would be public.
293
/**
294     * Class used to change enable state of buttons.
295     */

296    private static class ActionChangedListener implements PropertyChangeListener {
297       AbstractButton button;
298
299       ActionChangedListener(AbstractButton b) {
300          super();
301          button = b;
302       }
303       public void propertyChange(PropertyChangeEvent e) {
304          String JavaDoc propertyName = e.getPropertyName();
305          if (e.getPropertyName().equals(Action.NAME) &&
306              button instanceof JMenuItem) {
307             String JavaDoc text = (String JavaDoc)e.getNewValue();
308             button.setText(text);
309          } else {
310             if (propertyName.equals("enabled")) {
311                Boolean JavaDoc enabledState = (Boolean JavaDoc)e.getNewValue();
312                button.setEnabled(enabledState.booleanValue());
313             }
314          }
315       }
316    }
317    //********************* END OF ACTIONCHANGEDLISTENER CLASS ********************
318
/**
319     * Creates ActionChangeListener object.
320     */

321    private static PropertyChangeListener createActionChangeListener(AbstractButton b) {
322       return new BarFactory.ActionChangedListener(b);
323    }
324
325 }
326
Popular Tags