KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > CustomizedGUI


1 /*
2   Copyright (C) 2002-2003 Renaud Pawlak, Laurent Martelli.
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui;
20
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Vector JavaDoc;
27 import org.apache.log4j.Logger;
28 import org.objectweb.jac.core.rtti.AbstractMethodItem;
29 import org.objectweb.jac.core.rtti.ClassItem;
30 import org.objectweb.jac.core.rtti.MemberItem;
31 import org.objectweb.jac.core.rtti.MethodItem;
32
33 /**
34  * This interface allows the programmer to create simple customized GUIs.
35  *
36  * <p>The idea is to define sub-panels that can contain views on the
37  * objects of the application. The geometric placement of the
38  * sub-panel is defined by the <code>setSubPanesGeometry</code> method
39  * that has to be called at contruction-time.
40  *
41  * <p>Once the geometry is chosen, you can tell a pane to contain a
42  * view on a given object by using the <code>addReferenceToPane</code>
43  * method.
44  *
45  * <p>This class must be subclassed by the porgrammer of a JAC
46  * application to provide a customized GUI.
47  *
48  * @see GuiAC
49  * @see #setSubPanesGeometry(int,int,boolean[])
50  * @see #addReferenceToPane(MemberItem,String,String[],String) */

51
52 public class CustomizedGUI {
53     static Logger logger = Logger.getLogger("gui");
54
55     //GuiAC guiAC = null;
56

57     // paneID -> {viewType,args[]}
58
Hashtable JavaDoc paneContents = new Hashtable JavaDoc();
59     // paneID -> viewType
60
Hashtable JavaDoc paneContainers = new Hashtable JavaDoc();
61     // (Integer)splitterID -> (Integer)location
62
Hashtable JavaDoc splitters = new Hashtable JavaDoc();
63     // changed paneID -> invalid_paneID
64
Hashtable JavaDoc invalidPanes = new Hashtable JavaDoc();
65
66     // WARNING: the following order is very important
67
/** The subpanes are separated by an horizontal splitter */
68     public static final int HORIZONTAL = 0;
69     /** The upper subpanes are separated by a vertical splitter */
70     public static final int HORIZONTAL_UP = 1;
71     /** The down subpanes are separated by a vertical splitter */
72     public static final int HORIZONTAL_DOWN = 2;
73     /** The subpanes are separated by a vertical splitter */
74     public static final int VERTICAL = 3;
75     /** The left subpanes are separated by an horizontal splitter */
76     public static final int VERTICAL_LEFT = 4;
77     /** The right subpanes are separated by an horizontal splitter */
78     public static final int VERTICAL_RIGHT = 5;
79
80     public static final String JavaDoc BOTTOM = "BOTTOM";
81     public static final String JavaDoc TOP = "TOP";
82
83     public void setInvalidPane(String JavaDoc changedPane, String JavaDoc invalidPane) {
84         invalidPanes.put(changedPane,invalidPane);
85     }
86
87     public String JavaDoc getInvalidPane(String JavaDoc changedPane) {
88         return (String JavaDoc)invalidPanes.get(changedPane);
89     }
90
91     /**
92      * Gets the number of sub-panes in the main window of the GUI.
93      *
94      * @return the number of panels
95      */

96     int subPanesCount= 1;
97     public int getSubPanesCount() {
98         return subPanesCount;
99     }
100
101     int geometry = 0;
102     public int getGeometry() {
103         return geometry;
104     }
105
106     boolean[] scrollings = new boolean[] {false};
107     public boolean[] getScrollings() {
108         return scrollings;
109     }
110    
111     String JavaDoc application;
112    
113     /**
114      * Get the value of application.
115      * @return value of application.
116      */

117     public String JavaDoc getApplication() {
118         return application;
119     }
120    
121     /**
122      * Set the value of application.
123      * @param v Value to assign to application.
124      */

125     public void setApplication(String JavaDoc v) {
126         this.application = v;
127     }
128    
129
130     /**
131      * Sets the geometric arrangement of the panes.
132      *
133      * <p><ul><li><code>subPanesCount == 4 && geometry == VERTICAL</code>:
134      * <pre>
135      * +-------+
136      * | 0 | 2 |
137      * +---|---+
138      * | 1 | 3 |
139      * +-------+
140      * </pre>
141      * </li>
142      * <li><code>subPanesCount == 4 && geometry == HORIZONTAL</code>:
143      * <pre>
144      * +-------+
145      * | 0 | 1 |
146      * +-------+
147      * | 2 | 3 |
148      * +-------+
149      * </pre>
150      * </li>
151      * <li><code>subPanesCount == 3 && geometry == HORIZONTAL_UP</code>:
152      * <pre>
153      * +-------+
154      * | 0 | 1 |
155      * +-------+
156      * | 2 |
157      * +-------+
158      * </pre>
159      * </li>
160      * </li>
161      * <li><code>subPanesCount == 3 && geometry == HORIZONTAL_DOWN</code>:
162      * <pre>
163      * +-------+
164      * | 2 |
165      * +-------+
166      * | 0 | 1 |
167      * +-------+
168      * </pre>
169      * </li>
170      * <li><code>subPanesCount == 3 && geometry == VERTICAL_LEFT</code>:
171      * <pre>
172      * +---+---+
173      * | 0 | |
174      * +---| 2 |
175      * | 1 | |
176      * +---+---+
177      * </pre>
178      * </li>
179      * <li><code>subPanesCount == 3 && geometry == VERTICAL_RIGHT</code>:
180      * <pre>
181      * +---+---+
182      * | | 0 |
183      * | 2 |---+
184      * | | 1 |
185      * +---+---+
186      * </pre>
187      * </li>
188      * <li><code>subPanesCount == 2 && geometry == VERTICAL</code>:
189      * <pre>
190      * +---+---+
191      * | | |
192      * | 0 | 1 |
193      * | | |
194      * +---+---+
195      * </pre>
196      * </li>
197      * <li><code>subPanesCount == 2 && geometry == HORIZONTAL</code>:
198      * <pre>
199      * +-------+
200      * | 0 |
201      * +-------+
202      * | 1 |
203      * +-------+
204      * </pre>
205      * </li>
206      *
207      * @param subPanesCount the number of subpanes in the window
208      * @param geometry the geometry = <code>VERTICAL || HORIZONTAL ||
209      * VERTICAL_LEFT || VERTICAL_RIGHT || HORIZONTAL_UP ||
210      * HORIZONTAL_DOWN</code> (see above)
211      * @param scrollings a set of string that tells if the sub-panes
212      * must be srollable or not
213      */

214     public void setSubPanesGeometry(int subPanesCount, int geometry,
215                                     boolean[] scrollings) {
216         this.subPanesCount = subPanesCount;
217         this.geometry = geometry;
218         this.scrollings = scrollings;
219     }
220
221     /**
222      * Sets the object that should be opened in the given panel id.
223      *
224      * @param paneId the panel id (see the geometry to know its
225      * placement)
226      * @param viewType of the type of the view
227      * @param args parameters to pass to the view type constructor
228      *
229      * @see #setSubPanesGeometry(int,int,boolean[])
230      */

231     public void setPaneContent(String JavaDoc paneId,
232                                String JavaDoc viewType, String JavaDoc[] args) {
233         paneContents.put(paneId,new PanelContent(viewType,args));
234     }
235
236     /**
237      * Returns the type of the object to be displayed in a pane
238      */

239     public String JavaDoc getPaneContentType(String JavaDoc paneID) {
240         return (String JavaDoc)((Object JavaDoc[])paneContents.get(paneID))[0];
241     }
242
243     public String JavaDoc[] getPaneContentArgs(String JavaDoc paneID) {
244         return (String JavaDoc[])((Object JavaDoc[])paneContents.get(paneID))[1];
245     }
246
247     public Map JavaDoc getPaneContents() {
248         return paneContents;
249     }
250
251     public void setPaneContainer(String JavaDoc paneId, String JavaDoc containerType) {
252         paneContainers.put(paneId,containerType);
253     }
254
255     public String JavaDoc getPaneContainer(String JavaDoc paneId) {
256         return (String JavaDoc)paneContainers.get(paneId);
257     }
258
259     public Map JavaDoc getPaneContainers() {
260         return paneContainers;
261     }
262
263     Hashtable JavaDoc subPaneContents = new Hashtable JavaDoc();
264    
265     public Map JavaDoc getSubPaneContents() {
266         return subPaneContents;
267     }
268
269     // PaneID -> Vector of FieldItem
270
HashMap JavaDoc targetContainers = new HashMap JavaDoc();
271
272     /**
273      * Tells a referenced object to open in a given panel when a view
274      * is asked by the user (instead of opening in a popup).
275      *
276      * @param reference the reference (can be a method's result)
277      * @param viewType the type of the view that must be opened
278      * @param paneId the panel id where the view must be opened */

279
280     public void addReferenceToPane(MemberItem reference,
281                                    String JavaDoc viewType, String JavaDoc[] viewParams,
282                                    String JavaDoc paneId) {
283         Vector JavaDoc containers = (Vector JavaDoc)targetContainers.get(reference);
284         if (containers==null) {
285             containers = new Vector JavaDoc();
286             targetContainers.put(reference,containers);
287         }
288         containers.add(new Target(paneId,viewType,viewParams));
289     }
290
291     public Map JavaDoc getTargetContainers() {
292         return targetContainers;
293     }
294
295     public List JavaDoc getFieldTargets(MemberItem reference) {
296         List JavaDoc result = null;
297         while (result==null && reference!=null) {
298             result = (List JavaDoc)targetContainers.get(reference);
299             ClassItem superClass = reference.getClassItem().getSuperclass();
300             if (superClass!=null)
301                 reference = superClass.getFieldNoError(reference.getName());
302             else
303                 reference = null;
304         }
305         return result;
306     }
307
308     int left;
309     int up;
310     int width;
311     int height;
312
313     public int getLeft() {
314         return left;
315     }
316
317     public int getUp() {
318         return up;
319     }
320
321     public int getWidth() {
322         return width;
323     }
324
325     public int getHeight() {
326         return height;
327     }
328
329     boolean geometrySet = false;
330
331     /**
332      * Returns true if the geometry of this GUI was set with
333      * <code>setPosition</code>.
334      *
335      * @see #setPosition(int,int,int,int)
336      */

337     public boolean isGeometrySet() {
338         return geometrySet;
339     }
340
341     /**
342      * Sets the dimensions and position of the window regarding to the
343      * main screen.
344      *
345      * @param left left-border pixel
346      * @param up upper-border pixel
347      * @param width in percentage regarding the screen
348      * @param height in percentage regarding the screen
349      * @see #isGeometrySet()
350      */

351
352     public void setPosition(int left, int up, int width, int height) {
353         this.up = up;
354         this.left = left;
355         this.width = width;
356         this.height = height;
357         geometrySet = true;
358     }
359
360     /**
361      * Sets a splitter location.
362      *
363      * <p>The splitter is referenced by its index going from the
364      * front-end splitter to the back-end splitters. For instance, in
365      * the case of a 3 sub-panel window, the 0 index references the
366      * splitter that splits the main window in two, the 1 index, the
367      * one that splits the half-window in two other smaller parts.
368      *
369      * @param splitterId the splitter's index
370      * @param location the position in pixel, regarding to the top/left
371      * component, a negative value means that the splitter should be
372      * set at the preferred sized of the inner components */

373
374     public void setSplitterLocation(int splitterId, float location) {
375         splitters.put(new Integer JavaDoc(splitterId), new Float JavaDoc(location));
376     }
377
378     public Map JavaDoc getSplitters() {
379         return splitters;
380     }
381
382     boolean hasStatusBar=false;
383     MethodItem statusBarMethod=null;
384     String JavaDoc statusPosition = BOTTOM;
385
386     public boolean hasStatusBar() {
387         return hasStatusBar;
388     }
389
390     public String JavaDoc getStatusPosition() {
391         return statusPosition;
392     }
393
394     public MethodItem getStatusBarMethod() {
395         return statusBarMethod;
396     }
397
398     /**
399      * Adds a status bar to the GUI.
400      */

401
402     public void addStatusBar(MethodItem method,String JavaDoc position) {
403         hasStatusBar=true;
404         statusBarMethod = method;
405         statusPosition=position;
406     }
407     
408     /*
409       public void showStatus(String message) {
410       if( statusBar == null ) {
411       System.out.println(message);
412       } else {
413       ((JLabel)statusBar.getComponent(0)).setText(message);
414       }
415       }
416     */

417
418     /**
419      * Creates a new customized GUI.
420      *
421      * <p>When subclassing, a typical implementation of the constructor is:
422      *
423      * <pre class=code>
424      * super()
425      * // initialization calls such as setSubPanesGeometry
426      * // addReferenceToPane, setPosition
427      * ...
428      * show();
429      * </pre>
430      *
431      * @see #setSubPanesGeometry(int,int,boolean[])
432      * @see #addReferenceToPane(MemberItem,String,String[],String)
433      * @see #setPosition(int,int,int,int)
434      * @see #setSplitterLocation(int,float)
435      */

436     public CustomizedGUI(String JavaDoc id) {
437         this.id = id;
438     }
439
440     String JavaDoc id;
441     public String JavaDoc getId() {
442         return id;
443     }
444
445     Hashtable JavaDoc menus = new Hashtable JavaDoc();
446
447     public Hashtable JavaDoc getMenus() {
448         return menus;
449     }
450
451     public Menu getMenus(String JavaDoc name) {
452         Menu menu=(Menu)menus.get(name);
453         if(menu==null) {
454             menu=new Menu();
455             menus.put(name,menu);
456         }
457         return menu;
458     }
459
460     public boolean hasMenuBar() {
461         return menus.size()>0;
462     }
463
464     /**
465      * Add an item in a menu
466      *
467      * @param menuPath the path of the menu where to add an item
468      * @param callback the method to call when this item is selected
469      *
470      * @see #addMenuSeparator(String,String[])
471      */

472     public void addMenuItem(String JavaDoc menuName,
473                             String JavaDoc[] menuPath,
474                             Callback callback) {
475         String JavaDoc[] path = new String JavaDoc[menuPath.length-1];
476         System.arraycopy(menuPath,0,path,0,menuPath.length-1);
477         getMenu(menuName,path).put(
478             menuPath[menuPath.length-1],callback);
479     }
480
481     /**
482      * Add a separator in a menu
483      *
484      * @param menuName the name of the menu
485      * @param menuPath the path of the menu where to add a separator
486      *
487      * @see #addMenuItem(String,String[],Callback)
488      */

489     public void addMenuSeparator(String JavaDoc menuName,String JavaDoc[] menuPath) {
490         getMenu(menuName,menuPath).addSeparator();
491     }
492
493     Vector JavaDoc toolbar = new Vector JavaDoc();
494
495     /**
496      * Add a button in the toolbar
497      *
498      * @param method the static method to call when the button is pressed
499      *
500      * @see #addToolbarSeparator()
501      * @see #addToolbarAction(String,AbstractMethodItem)
502      */

503     public void addToolbarAction(AbstractMethodItem method) {
504         toolbar.add(new Callback(null,method, new Object JavaDoc [0]));
505     }
506
507     /**
508      * Add a button in the toolbar
509      *
510      * @param objectName name of the object on which to invoke the method
511      * @param method the method to call when the button is pressed
512      *
513      * @see #addToolbarSeparator()
514      * @see #addToolbarAction(AbstractMethodItem)
515      */

516     public void addToolbarAction(String JavaDoc objectName, AbstractMethodItem method) {
517         addToolbarAction(objectName,method,new Object JavaDoc[0]);
518     }
519
520     public void addToolbarAction(String JavaDoc objectName, AbstractMethodItem method, Object JavaDoc[] params) {
521         toolbar.add(new Callback(objectName,method,params));
522     }
523
524     /**
525      * Add a separator in the toolbar
526      *
527      * @see #addToolbarAction(AbstractMethodItem)
528      */

529     public void addToolbarSeparator() {
530         toolbar.add(null);
531     }
532
533     /**
534      * Returns the toolbar of the customized GUI.
535      *
536      * @return a collection representing the tool. It contains
537      * AbstractMethodItem objects for buttons, and null for a
538      * separator.
539      */

540     public Collection JavaDoc getToolbar() {
541         return toolbar;
542     }
543
544     public boolean hasToolBar() {
545         return toolbar.size()>0;
546     }
547
548     /**
549      * Find a menu defined by its path
550      */

551     protected Menu getMenu(String JavaDoc menuName,String JavaDoc[] menuPath) {
552         Menu menu = getMenus(menuName);
553         for (int i=0; i<menuPath.length; i++) {
554             if (menu.containsKey(menuPath[i])) {
555                 Object JavaDoc current = menu.get(menuPath[i]);
556                 if (current instanceof Menu) {
557                     menu = (Menu)current;
558                 } else {
559                     logger.warn("overriding menu item "+current);
560                     Menu old = menu;
561                     menu = new Menu();
562                     old.put(menuPath[i],menu);
563                 }
564             } else {
565                 Menu old = menu;
566                 menu = new Menu();
567                 old.put(menuPath[i],menu);
568             }
569         }
570         return menu;
571     }
572
573     public void setMenuIcon(String JavaDoc menuName,
574                             String JavaDoc[] menuPath, String JavaDoc icon) {
575         getMenu(menuName,menuPath).setIcon(icon);
576     }
577    
578     public void setMenuPosition(String JavaDoc menuName,String JavaDoc position) {
579         getMenus(menuName).setPosition(position);
580     }
581    
582     String JavaDoc welcomeTitle = "Welcome";
583     String JavaDoc welcomeMessage = null;
584     String JavaDoc welcomeIcon = null;
585
586     public void setWelcomeMessage(String JavaDoc title,String JavaDoc message,String JavaDoc icon) {
587         this.welcomeTitle = title;
588         this.welcomeMessage = message;
589         this.welcomeIcon = icon;
590     }
591
592     String JavaDoc title;
593     public void setTitle(String JavaDoc title) {
594         this.title=title;
595     }
596     public String JavaDoc getTitle() {
597         return title;
598     }
599
600     Vector JavaDoc cssURLs=new Vector JavaDoc();
601     public void addStyleSheetURL(String JavaDoc url) {
602         cssURLs.add(url);
603     }
604
605     public Vector JavaDoc getStyleSheetURLs() {
606         return cssURLs;
607     }
608
609     AbstractMethodItem onCloseHandler;
610     public void setOnCloseHandler(AbstractMethodItem handler) {
611         onCloseHandler = handler;
612     }
613     public AbstractMethodItem getOnCloseHandler() {
614         return onCloseHandler;
615     }
616
617     String JavaDoc icon;
618     public String JavaDoc getIcon() {
619         return icon;
620     }
621     public void setIcon(String JavaDoc icon) {
622         this.icon = icon;
623     }
624 }
625
Popular Tags