KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > SwingCustomized


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

18
19 package org.objectweb.jac.aspects.gui.swing;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.awt.Toolkit JavaDoc;
26 import java.awt.event.WindowAdapter JavaDoc;
27 import java.awt.event.WindowEvent JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import javax.swing.Box JavaDoc;
33 import javax.swing.ImageIcon JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JFrame JavaDoc;
36 import javax.swing.JMenuBar JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38 import org.apache.log4j.Logger;
39 import org.objectweb.jac.aspects.gui.*;
40 import org.objectweb.jac.aspects.gui.Menu;
41 import org.objectweb.jac.core.rtti.FieldItem;
42 import org.objectweb.jac.core.rtti.MethodItem;
43 import org.objectweb.jac.util.Strings;
44
45
46 public class SwingCustomized extends JFrame JavaDoc implements CustomizedView {
47     static Logger logger = Logger.getLogger("display");
48     static Logger loggerContext = Logger.getLogger("display-context");
49
50     String JavaDoc label;
51     String JavaDoc type;
52     DisplayContext context;
53     ViewFactory factory;
54     Object JavaDoc[] parameters;
55
56     CustomizedGUI customized;
57     JPanel JavaDoc contentPanel;
58     PanelView mainView;
59
60     public SwingCustomized(ViewFactory factory, DisplayContext context,
61                            CustomizedGUI customized) {
62         this.factory = factory;
63         this.customized = customized;
64         this.context = context;
65         context.setCustomizedView(this);
66         contentPanel = new JPanel JavaDoc();
67         contentPanel.setLayout(new BorderLayout JavaDoc());
68         setContentPane(contentPanel);
69         try {
70             mainView = (PanelView)factory.createCompositeView(
71                 "main",
72                 "Panel",
73                 new Object JavaDoc[] {
74                     new Integer JavaDoc(customized.getSubPanesCount()),
75                     new Integer JavaDoc(customized.getGeometry()),
76                     customized.getPaneContainers(),
77                     customized.getScrollings(),
78                     customized.getSplitters() },
79                 context
80             );
81             contentPanel.add((JComponent JavaDoc)mainView,BorderLayout.CENTER);
82         } catch (ViewFactory.UnhandledViewTypeException e) {
83             e.printStackTrace();
84         }
85
86         GenericFactory.initCustomized(factory, context, mainView, customized, null);
87         GenericFactory.setMenuBars(factory,context,this,customized.getMenus());
88         GenericFactory.setToolBar(factory,context,this,customized.getToolbar());
89
90         if (customized.hasStatusBar())
91             GenericFactory.setStatusBar(factory, context,
92                                         this, customized.getStatusBarMethod(),
93                                         customized.getStatusPosition());
94
95         addMouseListener(CollaborationInitializer.get());
96
97         this.addWindowListener(
98             new WindowAdapter JavaDoc() {
99                     public void windowClosing(WindowEvent JavaDoc e) {
100                         close(true);
101                         if(getCustomized().getOnCloseHandler()!=null) {
102                             EventHandler.get().onInvoke(
103                                 getContext(),
104                                 new InvokeEvent(
105                                     SwingCustomized.this,
106                                     null,
107                                     getCustomized().getOnCloseHandler()),
108                                 false,
109                                 null,null);
110                         }
111                         GuiAC.removeDisplay(
112                             GuiAC.getDisplay(getCustomized().getId()));
113                   
114                         //((GuiAC)ACManager.get().getAC("gui"))
115

116                     }
117                 }
118         );
119
120         if (customized.getIcon()!=null) {
121             ImageIcon JavaDoc icon = ResourceManager.getIconResource(customized.getIcon());
122             if (icon!=null)
123                 setIconImage(icon.getImage());
124         }
125
126         pack();
127
128         if (customized.isGeometrySet()) {
129             setPosition(customized.getLeft(),customized.getUp(),
130                         customized.getWidth(),customized.getHeight());
131         }
132     }
133
134     public void addHorizontalStrut(int width) {}
135     public void addVerticalStrut(int height) {}
136
137     public void setSplitters() {
138         Iterator JavaDoc i = customized.getSplitters().entrySet().iterator();
139         while(i.hasNext()) {
140             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
141             mainView.setSplitterLocation(((Integer JavaDoc)entry.getKey()).intValue(),
142                                          ((Float JavaDoc)entry.getValue()).floatValue());
143         }
144     }
145
146     /**
147      * Sets the dimensions and position of the window regarding to the
148      * main screen.
149      *
150      * @param left left-border pixel
151      * @param up upper-border pixel
152      * @param width in percentage regarding the screen
153      * @param height in percentage regarding the screen */

154
155     public void setPosition(int left, int up, int width, int height) {
156         logger.debug("setPosition("+left+","+up+","+width+","+height+")");
157         Dimension JavaDoc screenDim = Toolkit.getDefaultToolkit().getScreenSize();
158         int w = (int) ((float)screenDim.getWidth()*width)/100;
159         int h = (int) ((float)screenDim.getHeight()*height)/100;
160         logger.debug("dimension = "+w+"x"+h);
161         setBounds(new Rectangle JavaDoc(
162             left,up,
163             (int)(((float)screenDim.getWidth()*width)/100),
164             (int)(((float)screenDim.getHeight()*height)/100)));
165     }
166
167
168     public void close(boolean validate) {
169         mainView.close(validate);
170         closed = true;
171         dispose();
172     }
173
174     boolean closed = false;
175
176     public boolean isClosed() {
177         return closed;
178     }
179
180     // View interface
181

182     Border viewBorder;
183    
184     /**
185      * Get the value of viewBorder.
186      * @return value of viewBorder.
187      */

188     public Border getViewBorder() {
189         return viewBorder;
190     }
191    
192     /**
193      * Set the value of viewBorder.
194      * @param v Value to assign to viewBorder.
195      */

196     public void setViewBorder(Border v) {
197         this.viewBorder = v;
198     }
199    
200     // style used to change display (css for web)
201
String JavaDoc style;
202
203     public void setStyle(String JavaDoc style) {
204         this.style = style;
205     }
206
207     public String JavaDoc getStyle() {
208         return style;
209     }
210
211
212     String JavaDoc description;
213    
214     /**
215      * Get the value of description.
216      * @return value of description.
217      */

218     public String JavaDoc getDescription() {
219         return description;
220     }
221    
222     /**
223      * Set the value of description.
224      * @param v Value to assign to description.
225      */

226     public void setDescription(String JavaDoc v) {
227         this.description = v;
228     }
229    
230     View parentView;
231    
232     /**
233      * Get the value of parentView.
234      * @return value of parentView.
235      */

236     public View getParentView() {
237         return parentView;
238     }
239    
240     /**
241      * Set the value of parentView.
242      * @param v Value to assign to parentView.
243      */

244     public void setParentView(View v) {
245         this.parentView = v;
246     }
247
248     public View getRootView() {
249         if (parentView==null)
250             return this;
251         return parentView.getRootView();
252     }
253
254     public boolean isDescendantOf(View ancestor) {
255         if (this==ancestor)
256             return true;
257         else if (parentView==null)
258             return false;
259         else
260             return parentView.isDescendantOf(ancestor);
261     }
262
263     MethodItem message;
264    
265     /**
266      * Get the value of message.
267      * @return value of message.
268      */

269     public MethodItem getMessage() {
270         return message;
271     }
272    
273     /**
274      * Set the value of message.
275      * @param v Value to assign to message.
276      */

277     public void setMessage(MethodItem v) {
278         this.message = v;
279     }
280
281     public void setLabel(String JavaDoc label) {
282         setTitle(label);
283     }
284
285     public String JavaDoc getLabel() {
286         return getTitle();
287     }
288
289     public void setFactory(ViewFactory factory) {
290         this.factory = factory;
291     }
292
293     public ViewFactory getFactory() {
294         return factory;
295     }
296
297     public void setContext(DisplayContext context) {
298         loggerContext.debug("setContext on "+getClass().getName());
299         this.context = context;
300         // recursively set the display of inner components
301
Iterator JavaDoc i = mainView.getViews().iterator();
302         while (i.hasNext()) {
303             View view = (View)i.next();
304             loggerContext.debug("set context on subView "+view);
305             view.setContext(context);
306         }
307     }
308
309     public void setSize(Length width, Length height) {
310         //SwingUtils.setSize(this,width,height);
311
}
312
313     public void setParameters(Object JavaDoc[] parameters) {
314         this.parameters = parameters;
315     }
316    
317     public Object JavaDoc[] getParameters() {
318         return parameters;
319     }
320
321     public void setType(String JavaDoc type) {
322         this.type = type;
323     }
324
325     public String JavaDoc getType() {
326         return type;
327     }
328
329     public boolean equalsView(ViewIdentity view) {
330         return
331             ( ( type!=null &&
332                 type.equals(view.getType()) )
333               || (type==null && view.getType()==null ) )
334             && ( ( parameters!=null &&
335                    Arrays.equals(parameters,view.getParameters()) )
336                  || (parameters==null && view.getParameters()==null) );
337     }
338
339     public boolean equalsView(String JavaDoc type, Object JavaDoc[] parameters) {
340         return this.type.equals(type)
341             && Arrays.equals(this.parameters,parameters);
342     }
343
344     public void addView(View view, Object JavaDoc extraInfos) {
345         view.setContext(context);
346         mainView.addView(view,extraInfos);
347     }
348
349     public void addView(View view) {
350         addView(view,null);
351     }
352
353     public Collection JavaDoc getViews() {
354         return mainView.getViews();
355     }
356
357     public View getView(Object JavaDoc id) {
358         return mainView.getView(id);
359     }
360
361     public void removeView(View component, boolean validate)
362     {
363         mainView.removeView(component, validate);
364     }
365
366     public void removeAllViews(boolean validate) {
367         mainView.removeAllViews(validate);
368     }
369
370     public boolean containsView(String JavaDoc viewType, Object JavaDoc[] parameters) {
371         Iterator JavaDoc it = getViews().iterator();
372         while (it.hasNext()) {
373             View view = (View)it.next();
374             if (view.equalsView(viewType,parameters))
375                 return true;
376         }
377         return false;
378     }
379
380     public void setFocus(FieldItem field, Object JavaDoc option) {
381     }
382
383     // CustomizedView interface
384

385     public CustomizedGUI getCustomizedGUI() {
386         return customized;
387     }
388
389     public void setMenuBar(MenuView menuBar,String JavaDoc position) {
390         if (position==null) {
391             position = Menu.TOP;
392         }
393         menuBar.setPosition(position);
394         if(position.equals(Menu.TOP)) {
395             setJMenuBar((JMenuBar JavaDoc)menuBar);
396         } else {
397             if(position.equals(Menu.BOTTOM)) {
398                 getContentPane().add((JMenuBar JavaDoc)menuBar,BorderLayout.SOUTH);
399             } else if(position.equals(Menu.LEFT)) {
400                 getContentPane().add((JMenuBar JavaDoc)menuBar,BorderLayout.WEST);
401             } else if(position.equals(Menu.RIGHT)) {
402                 getContentPane().add((JMenuBar JavaDoc)menuBar,BorderLayout.EAST);
403             }
404         }
405         if(position.equals(Menu.LEFT)||position.equals(Menu.BOTTOM)) {
406             // What the hell is this ???
407
for(int i=0;i<50;i++) {
408                 ((JMenuBar JavaDoc)menuBar).add(Box.createVerticalGlue());
409             }
410         }
411     }
412
413     public void setToolBar(MenuView toolBar) {
414         contentPanel.add((Component JavaDoc)toolBar,BorderLayout.NORTH);
415     }
416
417     StatusView statusView;
418
419     public void setStatusBar(StatusView view, String JavaDoc position) {
420         statusView=view;
421         contentPanel.add((Component JavaDoc)view,BorderLayout.SOUTH);
422     }
423
424     public void showStatus(String JavaDoc message) {
425         statusView.showMessage(message);
426     }
427
428     public PanelView getPanelView() {
429         return mainView;
430     }
431
432     public DisplayContext getContext() {
433         return context;
434     }
435
436     CustomizedGUI getCustomized() {
437         return customized;
438     }
439
440     public void requestFocus() {
441         show();
442         super.requestFocus();
443     }
444
445     public String JavaDoc toString() {
446         return Strings.hex(this);
447     }
448 }
449  
450
Popular Tags