KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > swingclient > BarFactory


1 /* ResourceManager.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  *
9  */

10
11 package org.enhydra.shark.swingclient;
12
13 import java.awt.*;
14 import java.awt.event.*;
15 import java.beans.*;
16 import java.net.*;
17 import java.util.*;
18
19 import javax.swing.*;
20
21 /**
22 * Implements the static methods that are used to implement
23 * multilanguage support, and to create some resources as
24 * menubar, toolbars and button panels.
25 */

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

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

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

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

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

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

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

286    public static Component createButtonPanel(String JavaDoc key,Map actions) {
287       String JavaDoc label = ResourceManager.getLanguageDependentString(key+labelSuffix);
288       JPanel groupPanel=new JPanel();
289       groupPanel.setLayout(new BoxLayout(groupPanel,BoxLayout.Y_AXIS));
290
291       JPanel p = new JPanel();
292       String JavaDoc[] buttonKeys = Utils.tokenize(ResourceManager.getLanguageDependentString(key)," ");
293       for (int i = 0; i < buttonKeys.length; i++) {
294          if (buttonKeys[i].equals("-")) {
295             p.add(Box.createHorizontalStrut(5));
296          } else if (buttonKeys[i].equals("+")) {
297             groupPanel.add(p);
298             p=new JPanel();
299          } else {
300             p.add(createButton(buttonKeys[i],actions,true));
301          }
302       }
303       groupPanel.add(p);
304       return groupPanel;
305    }
306
307
308    //************************* ACTIONCHANGEDLISTENER CLASS **********************
309
// Yarked from JMenu, ideally this would be public.
310
/**
311     * Class used to change enable state of buttons.
312     */

313    private static class ActionChangedListener implements PropertyChangeListener {
314       AbstractButton button;
315
316       ActionChangedListener(AbstractButton b) {
317          super();
318          button = b;
319       }
320       public void propertyChange(PropertyChangeEvent e) {
321          String JavaDoc propertyName = e.getPropertyName();
322          if (e.getPropertyName().equals(Action.NAME) &&
323              button instanceof JMenuItem) {
324             String JavaDoc text = (String JavaDoc)e.getNewValue();
325             button.setText(text);
326          } else {
327             if (propertyName.equals("enabled")) {
328                Boolean JavaDoc enabledState = (Boolean JavaDoc)e.getNewValue();
329                button.setEnabled(enabledState.booleanValue());
330             }
331          }
332       }
333    }
334    //********************* END OF ACTIONCHANGEDLISTENER CLASS ********************
335
/**
336     * Creates ActionChangeListener object.
337     */

338    private static PropertyChangeListener createActionChangeListener(AbstractButton b) {
339       return new BarFactory.ActionChangedListener(b);
340    }
341
342
343 }
344
Popular Tags