KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > 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  * Harald Meister harald.meister@abacus.ch
9  *
10  */

11
12 package org.enhydra.jawe;
13
14 import java.awt.*;
15 import java.awt.event.*;
16 import javax.swing.*;
17 import javax.swing.text.*;
18 import java.util.*;
19 import java.beans.*;
20 import java.net.URL JavaDoc;
21 import org.enhydra.jawe.actions.*;
22
23 public class BarFactory {
24
25    protected AbstractEditor editor;
26
27    public BarFactory( AbstractEditor editor ) {
28       this.editor = editor;
29    }
30
31    //********************** CREATE TOOLBARS AND THEIR COMPONENTS *****************
32
/**
33      * Creates application's toolbars. Reads resource to find out which
34      * toolbars to create.
35      * NOTE: this method is modified to accept a creation of a button
36      * after toolbars.
37      * @see #createToolbar
38      */

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

82    protected Component createToolbar( String JavaDoc key, String JavaDoc label ) {
83       JToolBar toolbar = new JToolBar( label );
84       toolbar.putClientProperty( "JToolBar.isRollover", Boolean.TRUE );
85       //toolbar.setFloatable(false);
86
String JavaDoc[] toolKeys = Utils.tokenize( ResourceManager.getResourceString( key ), " " );
87       for ( int i = 0; i < toolKeys.length; i++ ) {
88          if ( toolKeys[i].equals( "-" ) ) {
89             toolbar.addSeparator(); //Harald Meister
90
}
91          else {
92             toolbar.add( createTool( toolKeys[i] ) );
93          }
94       }
95       toolbar.add( Box.createHorizontalGlue() );
96       return toolbar;
97    }
98
99    /**
100     * Hook through which every toolbar item is created.
101     * @see #createToolbarButton
102     */

103    protected Component createTool( String JavaDoc key ) {
104       return createToolbarButton( key );
105    }
106
107    /**
108     * Create a button to go inside of the toolbar. By default this
109     * will load an image resource. The image filename is relative to
110     * the classpath (including the '.' directory if its a part of the
111     * classpath), and may either be in a JAR file or a separate file.
112     *
113     * @param key The key in the resource file to serve as the basis
114     * of lookups.
115     */

116    protected Component createToolbarButton( String JavaDoc key ) {
117       Object JavaDoc specialButton = editor.getSpecialButton( key );
118       if ( specialButton instanceof JComponent &&
119            ! ( specialButton instanceof AbstractButton ) ) {
120          return ( JComponent ) specialButton;
121       }
122       else {
123          AbstractButton b = null;
124          URL JavaDoc url = ResourceManager.getResource( key + JaWEConstants.IMAGE_SUFFIX );
125          if ( specialButton instanceof AbstractButton ) {
126             b = ( AbstractButton ) specialButton;
127             b.setIcon( new ImageIcon( url ) );
128          }
129          else {
130             if ( url != null ) {
131                b = new JButton( new ImageIcon( url ) ) {
132                   public float getAlignmentY() {
133                      return 0.5f;
134                   }
135                };
136             }
137             else {
138                b = new JButton( ResourceManager.getLanguageDependentString( key +
139                    JaWEConstants.LABEL_SUFFIX ) ) {
140                   public float getAlignmentY() {
141                      return 0.5f;
142                   }
143                };
144             }
145          }
146          b.setMargin( new Insets( 1, 1, 1, 1 ) );
147          b.setRequestFocusEnabled( false );
148
149          if ( specialButton == null ) {
150             String JavaDoc astr = ResourceManager.getResourceString( key + JaWEConstants.ACTION_SUFFIX );
151             if ( astr == null ) {
152                astr = key;
153             }
154             Action a = editor.getAction( astr );
155             if ( a != null ) {
156                b.setActionCommand( astr );
157                b.addActionListener( new BarFactory.EventRedirector( a ) );
158                b.setEnabled( a.isEnabled() );
159                a.addPropertyChangeListener( new BarFactory.ActionChangedListener( b ) );
160             }
161             else {
162                b.setEnabled( false );
163             }
164          }
165
166          String JavaDoc tip = ResourceManager.getLanguageDependentString( key + JaWEConstants.TIP_SUFFIX );
167          if ( tip != null ) {
168             b.setToolTipText( tip );
169          }
170          editor.putToolbarComponent( key, b );
171          return b;
172       }
173    }
174
175    //********************* CREATING MENUBAR AND IT'S COMPONENTS ******************
176
/**
177      * Create the menubar for the app. By default this pulls the
178      * definition of the menu from the associated resource file.
179      */

180     public JMenuBar createMenubar() {
181        JMenuItem mi;
182        JMenuBar mb = new JMenuBar();
183        String JavaDoc[] menuKeys = Utils.tokenize( ResourceManager.getResourceString( editor.menubarToLoad() ),
184                                            " " );
185        for ( int i = 0; i < menuKeys.length; i++ ) {
186           String JavaDoc[] itemKeys = Utils.tokenize( ResourceManager.getResourceString( menuKeys[i] ), " " );
187           JMenu m = createMenu( menuKeys[i], itemKeys );
188           if ( m != null ) {
189              mb.add( m );
190           }
191        }
192
193        return mb;
194     }
195
196    /**
197     * Create the menubar for text view. By default this pulls the
198     * definition of the menu from the associated resource file.
199     */

200    public JMenuBar createTextViewMenubar( String JavaDoc key ) {
201
202       JMenuItem mi;
203       JMenuBar mb = new JMenuBar();
204       String JavaDoc[] menuKeys = Utils.tokenize( ResourceManager.getResourceString( key ), " " );
205       for ( int i = 0; i < menuKeys.length; i++ ) {
206          String JavaDoc[] itemKeys = Utils.tokenize( ResourceManager.getResourceString( menuKeys[i] ), " " );
207          JMenu m = createTextViewMenu( menuKeys[i], itemKeys );
208          if ( m != null ) {
209             mb.add( m );
210          }
211       }
212       return mb;
213    }
214
215    /**
216     * Create a menu for text view. By default this pulls the
217     * definition of the menu from the associated resource file.
218     */

219    public JMenu createTextViewMenu( String JavaDoc key, String JavaDoc[] itemKeys ) {
220       JMenu menu = new JMenu( ResourceManager.getLanguageDependentString( key +
221           JaWEConstants.LABEL_SUFFIX ) );
222       for ( int i = 0; i < itemKeys.length; i++ ) {
223          if ( itemKeys[i].equals( "-" ) ) {
224             menu.addSeparator();
225          }
226          else {
227             JMenuItem mi = createTextVeiwMenuItem( itemKeys[i] );
228             menu.add( mi );
229          }
230       }
231       URL JavaDoc url = ResourceManager.getResource( key + JaWEConstants.IMAGE_SUFFIX );
232       if ( url != null ) {
233          menu.setHorizontalTextPosition( JButton.RIGHT );
234          menu.setIcon( new ImageIcon( url ) );
235       }
236
237       setMnemonic( menu,
238                    ResourceManager.getLanguageDependentString( key + JaWEConstants.MNEMONIC_SUFFIX ) );
239
240       menu.setActionCommand( key );
241
242       return menu;
243    }
244
245    /**
246     * Create menu item for text view.
247     */

248    public JMenuItem createTextVeiwMenuItem( String JavaDoc cmd ) {
249       String JavaDoc subMenu = ResourceManager.getResourceString( cmd + JaWEConstants.MENU_SUFFIX );
250       if ( subMenu != null ) {
251          String JavaDoc[] itemKeys = Utils.tokenize( subMenu, " " );
252          JMenu mn = createTextViewMenu( cmd, itemKeys );
253          return mn;
254       }
255       else {
256          JMenuItem mi;
257          mi = new JMenuItem( ResourceManager.getLanguageDependentString( cmd +
258              JaWEConstants.LABEL_SUFFIX ) );
259
260          URL JavaDoc url = ResourceManager.getResource( cmd + JaWEConstants.IMAGE_SUFFIX );
261          if ( url != null ) {
262             mi.setHorizontalTextPosition( JButton.RIGHT );
263             mi.setIcon( new ImageIcon( url ) );
264          }
265
266          setAccelerator( mi,
267                          ResourceManager.getLanguageDependentString( cmd +
268              JaWEConstants.ACCEL_SUFFIX ) );
269          setMnemonic( mi,
270                       ResourceManager.getLanguageDependentString( cmd +
271              JaWEConstants.MNEMONIC_SUFFIX ) );
272
273          String JavaDoc astr = ResourceManager.getResourceString( cmd + JaWEConstants.ACTION_SUFFIX );
274          if ( astr == null ) {
275             astr = cmd;
276          }
277          mi.setActionCommand( astr );
278          Action a = getTextViewAction( astr );
279          if ( a != null ) {
280             mi.addActionListener( a );
281             mi.setEnabled( a.isEnabled() );
282          }
283          else {
284             mi.setEnabled( false );
285          }
286
287          return mi;
288       }
289    }
290
291 //create and manage actions used in TextView
292

293    Hashtable commands = new Hashtable();
294
295    public Action getTextViewAction( String JavaDoc key ) {
296       return ( Action ) commands.get( key );
297    }
298
299    public void createViewActions(JTextComponent textComponent) {
300       Action[] actions = {
301           new TextSaveAs(textComponent), new TextPrint() };
302       for ( int i = 0; i < actions.length; i++ ) {
303          Action a = actions[i];
304          commands.put( a.getValue( Action.NAME ), a );
305       }
306    }
307
308    /**
309     * Create a menu for the app. By default this pulls the
310     * definition of the menu from the associated resource file.
311     /**
312      * Create a menu for the app. By default this pulls the
313      * definition of the menu from the associated resource file.
314      */

315     protected JMenu createMenu( String JavaDoc key, String JavaDoc[] itemKeys ) {
316        JMenu menu = new JMenu( ResourceManager.getLanguageDependentString( key +
317            JaWEConstants.LABEL_SUFFIX ) );
318        for ( int i = 0; i < itemKeys.length; i++ ) {
319           if ( itemKeys[i].equals( "-" ) ) {
320              menu.addSeparator();
321           }
322           else {
323              JMenuItem mi = createMenuItem( itemKeys[i], true );
324              menu.add( mi );
325           }
326        }
327        URL JavaDoc url = ResourceManager.getResource( key + JaWEConstants.IMAGE_SUFFIX );
328        if ( url != null ) {
329           menu.setHorizontalTextPosition( JButton.RIGHT );
330           menu.setIcon( new ImageIcon( url ) );
331        }
332
333        setMnemonic( menu,
334                     ResourceManager.getLanguageDependentString( key + JaWEConstants.MNEMONIC_SUFFIX ) );
335
336        menu.setActionCommand( key );
337
338        return menu;
339     }
340
341    /**
342     * This is the hook through which all menu items are
343     * created. It registers the result with the menuitem
344     * hashtable so that it can be fetched with getMenuItem().
345     */

346    public JMenuItem createMenuItem( String JavaDoc cmd, boolean putIntoTable ) {
347       String JavaDoc subMenu = ResourceManager.getResourceString( cmd + JaWEConstants.MENU_SUFFIX );
348       if ( subMenu != null ) {
349          String JavaDoc[] itemKeys = Utils.tokenize( subMenu, " " );
350          JMenu mn = createMenu( cmd, itemKeys );
351          if ( putIntoTable ) {
352             editor.putMenuItem( cmd, mn );
353          }
354          return mn;
355       }
356       else {
357          JMenuItem mi;
358          Object JavaDoc specialItem = editor.getSpecialItem( cmd );
359          if ( specialItem instanceof JMenuItem ) {
360             mi = ( JMenuItem ) specialItem;
361             mi.setText( ResourceManager.getLanguageDependentString( cmd +
362                 JaWEConstants.LABEL_SUFFIX ) );
363          }
364          else {
365             mi = new JMenuItem( ResourceManager.getLanguageDependentString( cmd +
366                 JaWEConstants.LABEL_SUFFIX ) );
367          }
368          URL JavaDoc url = ResourceManager.getResource( cmd + JaWEConstants.IMAGE_SUFFIX );
369          if ( url != null ) {
370             mi.setHorizontalTextPosition( JButton.RIGHT );
371             mi.setIcon( new ImageIcon( url ) );
372          }
373
374          setAccelerator( mi,
375                          ResourceManager.getLanguageDependentString( cmd + JaWEConstants.ACCEL_SUFFIX ) );
376          setMnemonic( mi,
377                       ResourceManager.getLanguageDependentString( cmd + JaWEConstants.MNEMONIC_SUFFIX ) );
378
379          if ( specialItem == null ) {
380             String JavaDoc astr = ResourceManager.getResourceString( cmd + JaWEConstants.ACTION_SUFFIX );
381             if ( astr == null ) {
382                astr = cmd;
383             }
384             mi.setActionCommand( astr );
385             Action a = editor.getAction( astr );
386             if ( a != null ) {
387                mi.addActionListener( new BarFactory.EventRedirector( a ) );
388                a.addPropertyChangeListener( new BarFactory.ActionChangedListener( mi ) );
389                mi.setEnabled( a.isEnabled() );
390             }
391             else {
392                mi.setEnabled( false );
393             }
394          }
395
396          if ( putIntoTable ) {
397             editor.putMenuItem( cmd, mi );
398          }
399          return mi;
400       }
401    }
402
403    /**
404     * Create menu item with appropriate icon.
405     */

406    public static JMenuItem createMenuItem( String JavaDoc cmd ) {
407       JMenuItem mi;
408       mi = new JMenuItem( ResourceManager.getLanguageDependentString( cmd +
409           JaWEConstants.LABEL_SUFFIX ) );
410       URL JavaDoc url = ResourceManager.getResource( cmd + JaWEConstants.IMAGE_SUFFIX );
411       if ( url != null ) {
412          mi.setHorizontalTextPosition( JButton.RIGHT );
413          mi.setIcon( new ImageIcon( url ) );
414       }
415       return mi;
416    }
417
418    public static void setMnemonic( JMenuItem mi, String JavaDoc mnemonic ) {
419       if ( mnemonic != null && mnemonic.length() > 0 ) {
420          mi.setMnemonic( mnemonic.toCharArray()[0] );
421       }
422    }
423
424    public static void setAccelerator( JMenuItem mi, String JavaDoc accel ) {
425       if ( accel != null ) {
426          try {
427             int mask = 0;
428             if ( accel.startsWith( "CTRL" ) ) {
429                mask += ActionEvent.CTRL_MASK;
430                accel = accel.substring( 5 );
431             }
432             if ( accel.startsWith( "SHIFT" ) ) {
433                mask += ActionEvent.SHIFT_MASK;
434                accel = accel.substring( 6 );
435             }
436             if ( accel.startsWith( "ALT" ) ) {
437                mask += ActionEvent.ALT_MASK;
438                accel = accel.substring( 4 );
439             }
440             int key = KeyEvent.class.getField( "VK_" + accel ).getInt( null );
441             mi.setAccelerator( KeyStroke.getKeyStroke( key, mask ) );
442          }
443          catch ( Exception JavaDoc e ) {
444             System.err.println( "Error while assigning accelerator !!!" );
445          }
446       }
447    }
448
449    //***************************** EVENTREDIRECTOR CLASS ************************
450
/** This will change the source of the actionevent to graph. */
451     private class EventRedirector
452         implements ActionListener {
453
454        protected Action action;
455
456        public EventRedirector( Action a ) {
457           this.action = a;
458        }
459
460        public void actionPerformed( ActionEvent e ) {
461           e = new ActionEvent( editor.getGraph(), e.getID(), e.getActionCommand(), e.getModifiers() );
462           action.actionPerformed( e );
463        }
464     }
465
466    //************************* END OF EVENTREDIRECTOR CLASS *********************
467

468     //************************* ACTIONCHANGEDLISTENER CLASS **********************
469
/**
470       * Class used to change enable state of buttons.
471       */

472      private class ActionChangedListener
473          implements PropertyChangeListener {
474         AbstractButton button;
475
476         ActionChangedListener( AbstractButton b ) {
477            super();
478            button = b;
479         }
480
481         public void propertyChange( PropertyChangeEvent e ) {
482            String JavaDoc propertyName = e.getPropertyName();
483            if ( e.getPropertyName().equals( Action.NAME ) &&
484                 button instanceof JMenuItem ) {
485               String JavaDoc text = ( String JavaDoc ) e.getNewValue();
486               button.setText( text );
487            }
488            else {
489               if ( propertyName.equals( "enabled" ) ) {
490                  Boolean JavaDoc enabledState = ( Boolean JavaDoc ) e.getNewValue();
491                  button.setEnabled( enabledState.booleanValue() );
492               }
493            }
494         }
495      }
496      //********************* END OF ACTIONCHANGEDLISTENER CLASS ********************
497

498 }
499
Popular Tags