KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com>
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, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   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.swing;
20
21 import java.util.Arrays JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.JDesktopPane JavaDoc;
27 import javax.swing.JInternalFrame JavaDoc;
28 import org.apache.log4j.Logger;
29 import org.objectweb.jac.aspects.gui.Border;
30 import org.objectweb.jac.aspects.gui.CompositeView;
31 import org.objectweb.jac.aspects.gui.DisplayContext;
32 import org.objectweb.jac.aspects.gui.Length;
33 import org.objectweb.jac.aspects.gui.View;
34 import org.objectweb.jac.aspects.gui.ViewFactory;
35 import org.objectweb.jac.aspects.gui.ViewIdentity;
36 import org.objectweb.jac.core.rtti.FieldItem;
37 import org.objectweb.jac.core.rtti.MethodItem;
38
39 /**
40  * This class defines a Swing component tree view for objects that are
41  * related to a root object through relations or collections.
42  */

43
44 public class DesktopView extends JDesktopPane JavaDoc implements CompositeView {
45     static Logger logger = Logger.getLogger("gui.swing");
46    
47     Hashtable JavaDoc views = new Hashtable JavaDoc();
48
49     DisplayContext context;
50     Length width;
51     Length height;
52     ViewFactory factory;
53     Object JavaDoc[] parameters;
54     String JavaDoc type;
55     String JavaDoc label;
56    
57     public DesktopView() {
58     }
59
60     public void addHorizontalStrut(int width) {}
61     public void addVerticalStrut(int height) {}
62
63     // style used to change display (css for web)
64
String JavaDoc style;
65
66     public void setStyle(String JavaDoc style) {
67         this.style = style;
68     }
69
70     public String JavaDoc getStyle() {
71         return style;
72     }
73
74     Border viewBorder;
75    
76     /**
77      * Get the value of viewBorder.
78      * @return value of viewBorder.
79      */

80     public Border getViewBorder() {
81         return viewBorder;
82     }
83    
84     /**
85      * Set the value of viewBorder.
86      * @param v Value to assign to viewBorder.
87      */

88     public void setViewBorder(Border v) {
89         this.viewBorder = v;
90     }
91    
92     String JavaDoc description;
93    
94     /**
95      * Get the value of description.
96      * @return value of description.
97      */

98     public String JavaDoc getDescription() {
99         return description;
100     }
101    
102     /**
103      * Set the value of description.
104      * @param v Value to assign to description.
105      */

106     public void setDescription(String JavaDoc v) {
107         this.description = v;
108     }
109    
110     View parentView;
111    
112     /**
113      * Get the value of parentView.
114      * @return value of parentView.
115      */

116     public View getParentView() {
117         return parentView;
118     }
119    
120     /**
121      * Set the value of parentView.
122      * @param v Value to assign to parentView.
123      */

124     public void setParentView(View v) {
125         this.parentView = v;
126     }
127
128     public View getRootView() {
129         if (parentView==null)
130             return this;
131         return parentView.getRootView();
132     }
133
134     public boolean isDescendantOf(View ancestor) {
135         if (this==ancestor)
136             return true;
137         else if (parentView==null)
138             return false;
139         else
140             return parentView.isDescendantOf(ancestor);
141     }
142
143     MethodItem message;
144    
145     /**
146      * Get the value of message.
147      * @return value of message.
148      */

149     public MethodItem getMessage() {
150         return message;
151     }
152
153     /**
154      * Set the value of message.
155      * @param v Value to assign to message.
156      */

157     public void setMessage(MethodItem v) {
158         this.message = v;
159     }
160
161
162     // CompositeView interface
163

164     public void addView(View view, Object JavaDoc extraInfos) {
165         logger.debug("Adding view in desktop");
166         JInternalFrame JavaDoc frame=new JInternalFrame JavaDoc();
167         frame.getContentPane().add((JComponent JavaDoc)view);
168         frame.setVisible(true); //necessary as of kestrel
169
frame.setTitle((String JavaDoc)extraInfos);
170         frame.setResizable(true);
171         frame.setClosable(true);
172         frame.setIconifiable(true);
173         frame.setMaximizable(true);
174         add(frame);
175         try {
176             frame.setSelected(true);
177         } catch (java.beans.PropertyVetoException JavaDoc e) {
178         }
179         views.put(extraInfos,view);
180         frame.pack();
181         frame.show();
182     }
183
184     public void addView(View view) {
185         addView(view,view.getLabel());
186     }
187
188     public void removeView(View component, boolean validate) {
189         for (int i=0; i<getComponentCount(); i++) {
190             JInternalFrame JavaDoc frame = (JInternalFrame JavaDoc)getComponent(i);
191             if (frame.getContentPane().getComponent(0).equals(component)) {
192                 component.close(validate);
193                 remove(frame);
194             }
195         }
196     }
197
198     public View getView(Object JavaDoc id) {
199         return (View)views.get(id);
200     }
201
202     public Collection JavaDoc getViews() {
203         return views.values();
204     }
205
206     public void removeAllViews(boolean validate) {
207         Iterator JavaDoc i = views.values().iterator();
208         while (i.hasNext()) {
209             ((View)i.next()).close(validate);
210         }
211         removeAll();
212     }
213
214     public boolean containsView(String JavaDoc viewType, Object JavaDoc[] parameters) {
215         Iterator JavaDoc it = getViews().iterator();
216         while (it.hasNext()) {
217             View view = (View)it.next();
218             if (view.equalsView(viewType,parameters))
219                 return true;
220         }
221         return false;
222     }
223
224     // View interface
225

226     public void setContext(DisplayContext context) {
227         this.context = context;
228     }
229
230     public DisplayContext getContext() {
231         return context;
232     }
233
234     public void setLabel(String JavaDoc label) {
235         this.label = label;
236     }
237
238     public String JavaDoc getLabel() {
239         return label;
240     }
241
242
243     public void setSize(Length width, Length height) {
244         this.width = width;
245         this.height = height;
246         SwingUtils.setSize(this,width,height);
247     }
248
249     public void close(boolean validate) {
250         closed = true;
251     }
252
253     boolean closed = false;
254
255     public boolean isClosed() {
256         return closed;
257     }
258
259     public ViewFactory getFactory() {
260         return factory;
261     }
262
263     public void setFactory(ViewFactory factory) {
264         this.factory = factory;
265     }
266
267     public void setType(String JavaDoc type) {
268         this.type = type;
269     }
270
271     public String JavaDoc getType() {
272         return type;
273     }
274
275     public void setParameters(Object JavaDoc[] parameters) {
276         this.parameters = parameters;
277     }
278    
279     public Object JavaDoc[] getParameters() {
280         return parameters;
281     }
282
283     public boolean equalsView(ViewIdentity view) {
284         return
285             ( ( type!=null &&
286                 type.equals(view.getType()) )
287               || (type==null && view.getType()==null ) )
288             && ( ( parameters!=null &&
289                    Arrays.equals(parameters,view.getParameters()) )
290                  || (parameters==null && view.getParameters()==null) );
291     }
292
293     public boolean equalsView(String JavaDoc type, Object JavaDoc[] parameters) {
294         return this.type.equals(type)
295             && Arrays.equals(this.parameters,parameters);
296     }
297
298     public void setFocus(FieldItem field, Object JavaDoc option) {
299     }
300
301     public String JavaDoc toString() {
302         return getClass().getName()+"@"+Integer.toString(hashCode());
303     }
304
305 }
306
Popular Tags