Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/modama/gui/mgraph/actions/ActionFactory.java


1   /**
2    Modama project, Institute for Polymer Research Dresden
3    Copyright (C) 2003 P. Fritsche, A. Uhlig
4    http://www.modama.org
5    info@modama.org
6   
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  
21   file created: May 10, 2003
22  
23   */
24  package org.modama.gui.mgraph.actions;
25  
26  import org.modama.framework.tools.Resources;
27  import org.modama.gui.tools.Utils;
28  import org.apache.log4j.Logger;
29  
30  
31  import javax.swing.*;
32  import java.util.*;
33  
34  /**
35   * A static Class to process actions
36   * First you have to tell <code>ActionFactory</code> what actions are available.
37   * Then, a call to createXXX will make the real stuff and returns, whatever is needed.
38   */
39  public class ActionFactory
40  {
41      /**
42       * error logger
43       */
44      protected static Logger logger = Logger.getLogger(ActionFactory.class);
45  
46      /**
47       * all registered actions
48       */
49      protected static HashMap allActions = new HashMap();
50  
51      /**
52       * tell me, what actions you have, and i tell you how to handle them...
53       * @param as
54       */
55      public static void setupActions( AbstractDefaultAction[] as )
56      {
57          for( int i = 0; i < as.length; i++ )
58          {
59              AbstractDefaultAction a = as[i];
60              allActions.put( a.getName(), a);
61          }
62      }
63  
64      /**
65       * is called by the Actions, to localize themself.
66       * @param ab
67       * @param key
68       * @param actionCommand
69       * @return
70       */
71      public static AbstractButton localizeAbstractButton(
72              AbstractButton ab,
73              String key,
74              String actionCommand )
75      {
76          return localizeAbstractButton( ab, key, actionCommand, true);
77      }
78      public static AbstractButton localizeAbstractButton(
79              AbstractButton ab,
80              String key,
81              String actionCommand,
82              boolean setText)
83      {
84          ab.setText( getNameForAction(key) );
85          return ab;
86      }
87  
88      /**
89       * return the name for an action. if it find a "Insert" at the beginning, it will discarded.
90       * Insert is something like a keyword, only use it if you know what your doing...
91       * @param key
92       * @return
93       */
94      protected static String getNameForAction( String key )
95      {
96          assert key!=null && key!="";
97  
98          if( key.startsWith("Insert") ) key = key.substring(6);
99          return Resources.getString( key );
100     }
101 
102     /**
103      * creates an the Menu wih help of a MenuGenerator
104      * this will look up to the resources file, extract the value for:
105      *   <code>actions.[className]</code>
106      * and parse it and delegate the real action to the MenuGenerator-Instance
107      * @param className
108      *
109      */
110     protected static void createMenuItems( String className, MenuGenerator mg )
111     {
112         // get the string
113         String actions = Resources.getString( "actions."+className );
114         // split by " "
115         String[] items = actions.split(" ");
116         ArrayList is = new ArrayList( items.length );
117         // cycle throu the items
118         for( int i = 0; i < items.length; i++ )
119         {
120             String item = items[i].trim();
121             // "-" means separator. we cant create a JSeparator here, because its not a subclass of JMenuItem
122             // which is needed in order to return ..
123             if( item.equals("-")) mg.addSeparator();
124             // strings with "+", so we have to include the following actionmap here
125             else if( item.startsWith("+") )
126             {
127                 String name = item.substring(1);
128                 MenuGenerator mg2 = mg.createNew( name );
129                 createMenuItems( name, mg2 );
130                 mg.insertAll( mg2 );
131 
132             // ":" means same as "+", but the items are placed in a submenu
133             } else if( item.startsWith(":"))
134             {
135                 String name = item.substring(1);
136                 MenuGenerator mg2 = mg.createNew( name);
137                 createMenuItems( name, mg2 );
138                 mg.insertSub( mg2 );
139 
140             // just the normal case
141             } else if( item.length() > 0 )
142             {
143                 mg.addItem( item );
144 
145             }
146         }
147     }
148 
149     /**
150      * creates a JMenu structure for given Object. This will lookup the res file for a string
151      * <code>action.[className of o]</code> to parse it
152      * @param o
153      * @return
154      */
155     public static JMenu createMenu( Class o )
156     {
157         JMenuGenerator mg = new JMenuGenerator(Resources.getString(Utils.getClassNameWithoutPackage(o)) );
158         createMenuItems( Utils.getClassNameWithoutPackage(o), mg );
159         return (JMenu)mg.getMenu();
160     }
161 
162     /**
163      * creates a JPopupMenu structure for given Object. This will lookup the res file for a string
164      * <code>action.[className of o]</code> to parse it
165      * @param o
166      * @return
167      */
168     public static JPopupMenu createPopupMenu( Class o )
169     {
170         JPopupMenuGenerator mg = new JPopupMenuGenerator(Resources.getString(Utils.getClassNameWithoutPackage(o)));
171         createMenuItems( Utils.getClassNameWithoutPackage(o), mg );
172         return (JPopupMenu)mg.getPopupMenu();
173     }
174 
175     public static JToolBar createToolBar( Class o )
176     {
177         JToolBarGenerator mg = new JToolBarGenerator(Resources.getString(Utils.getClassNameWithoutPackage(o)) );
178         createMenuItems( Utils.getClassNameWithoutPackage(o), mg );
179         return (JToolBar)mg.getMenu();
180     }
181 
182     public static JMenu createMenu( Object o )
183     {
184         return createMenu( o.getClass() );
185     }
186     public static JPopupMenu createPopupMenu( Object o )
187     {
188         return createPopupMenu( o.getClass() );
189     }
190     public static JToolBar createToolBar( Object o )
191     {
192         return createToolBar( o.getClass() );
193     }
194 
195     protected static abstract class MenuGenerator
196     {
197         String name;
198 
199         public MenuGenerator( String name )
200         {
201             this.name = name;
202         }
203 
204         public abstract void addItem( Object o );
205         public abstract void insertAll( MenuGenerator mg );
206         public abstract void insertSub( MenuGenerator mg );
207         public abstract void addSeparator();
208 
209         public abstract ArrayList getItems();
210         public abstract Object getMenu();
211         public abstract MenuGenerator createNew( String name);
212         public String getName() { return name; }
213     }
214 
215     protected static class JMenuGenerator extends MenuGenerator
216     {
217         protected ArrayList items = new ArrayList();
218 
219         public JMenuGenerator( String name)
220         {
221             super( name );
222         }
223 
224         public void addItem( Object o )
225         {
226             if( !allActions.containsKey(o))
227             {
228                 logger.fatal("Action "+o+" does not exist", new Exception());
229                 return;
230             };
231             JMenuItem it = (JMenuItem)((AbstractDefaultAction)allActions.get(o)).getAsMenuItem((String)o);
232             if( it==null ) logger.fatal("Action "+o+" does not exist", new Exception());
233             else items.add( it );
234 
235         }
236 
237         public void addSeparator()
238         {
239             items.add( null );
240         }
241 
242         public MenuGenerator createNew( String name)
243         {
244             return new JMenuGenerator( name );
245         }
246 
247         public Object getMenu()
248         {
249             JMenu theMenu = new JMenu( name );
250             for( Iterator iterator = items.iterator(); iterator.hasNext(); )
251             {
252                 JMenuItem jMenuItem = ( JMenuItem )iterator.next();
253                 if( jMenuItem==null ) theMenu.addSeparator();
254                 else theMenu.add( jMenuItem );
255             }
256             return theMenu;
257         }
258 
259         public ArrayList getItems() { return items; }
260 
261         public void insertAll( MenuGenerator mg )
262         {
263             ArrayList is = mg.getItems();
264             for( Iterator iterator = is.iterator(); iterator.hasNext(); )
265             {
266                 items.add( iterator.next() );
267             }
268         }
269 
270         public void insertSub( MenuGenerator mg )
271         {
272             items.add( mg.getMenu() );
273 
274         }
275     }
276 
277     protected static class JPopupMenuGenerator extends JMenuGenerator
278     {
279         public JPopupMenuGenerator( String name )
280         {
281             super( name );
282         }
283 
284         public MenuGenerator createNew( String name )
285         {
286             return new JPopupMenuGenerator( name );
287         }
288 
289         public Object getPopupMenu()
290         {
291             JPopupMenu theMenu = new JPopupMenu();
292             for( Iterator iterator = items.iterator(); iterator.hasNext(); )
293             {
294                 JMenuItem jMenuItem = ( JMenuItem )iterator.next();
295                 if( jMenuItem==null ) theMenu.addSeparator();
296                 else theMenu.add( jMenuItem );
297             }
298             return theMenu;
299         }
300     }
301 
302     protected static class JToolBarGenerator extends JMenuGenerator
303     {
304         public JToolBarGenerator( String name )
305         {
306             super( name );
307         }
308 
309         public void addItem( Object o )
310         {
311             if( !allActions.containsKey(o)) return;
312             AbstractDefaultAction action = (AbstractDefaultAction) allActions.get(o);
313             JButton it = (JButton)action.getAsButton( (String)o );
314             if( it==null ) logger.fatal("Action "+o+" does not exist", new Exception());
315             else items.add( it );
316         }
317 
318         public MenuGenerator createNew( String name )
319         {
320             return new JToolBarGenerator( name );
321         }
322 
323         public Object getMenu()
324         {
325             JToolBar toolbar = new JToolBar( name );
326             for( Iterator iterator = items.iterator(); iterator.hasNext(); )
327             {
328                 JButton jButton = ( JButton )iterator.next();
329                 if( jButton == null ) toolbar.addSeparator();
330                 else toolbar.add( jButton );
331             }
332             return toolbar;
333         }
334 
335         public void insertSub( MenuGenerator mg )
336         {
337             logger.fatal("cannot add submenus to toolbar", new Exception());
338         }
339     }
340 }