KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import javax.swing.BorderFactory JavaDoc;
24 import javax.swing.BoxLayout JavaDoc;
25 import javax.swing.JPanel JavaDoc;
26 import javax.swing.border.TitledBorder JavaDoc;
27 import org.apache.log4j.Logger;
28 import org.objectweb.jac.aspects.gui.Border;
29 import org.objectweb.jac.aspects.gui.DialogView;
30 import org.objectweb.jac.aspects.gui.DisplayContext;
31 import org.objectweb.jac.aspects.gui.GuiAC;
32 import org.objectweb.jac.aspects.gui.InvokeEvent;
33 import org.objectweb.jac.aspects.gui.InvokeThread;
34 import org.objectweb.jac.aspects.gui.Length;
35 import org.objectweb.jac.aspects.gui.View;
36 import org.objectweb.jac.aspects.gui.ViewFactory;
37 import org.objectweb.jac.aspects.gui.ViewIdentity;
38 import org.objectweb.jac.aspects.session.SessionAC;
39 import org.objectweb.jac.core.Collaboration;
40 import org.objectweb.jac.core.rtti.ClassRepository;
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 public abstract class AbstractView extends JPanel JavaDoc implements View {
46     static Logger loggerEvents = Logger.getLogger("gui.events");
47     static Logger loggerClose = Logger.getLogger("gui.close");
48     static Logger loggerContext = Logger.getLogger("display-context");
49     static Logger loggerDnd = Logger.getLogger("gui.dnd");
50
51     String JavaDoc label;
52     DisplayContext context;
53     protected Length width;
54     protected Length height;
55     ViewFactory factory;
56
57     Object JavaDoc[] parameters;
58     String JavaDoc type;
59
60     public AbstractView() {
61         setLayout(new BoxLayout JavaDoc(this,BoxLayout.X_AXIS));
62     }
63
64     public AbstractView(ViewFactory factory, DisplayContext context) {
65         this.factory = factory;
66         this.context = context;
67     }
68
69     MethodItem message;
70    
71     /**
72      * Get the value of message.
73      * @return value of message.
74      */

75     public MethodItem getMessage() {
76         return message;
77     }
78    
79     /**
80      * Set the value of message.
81      * @param v Value to assign to message.
82      */

83     public void setMessage(MethodItem v) {
84         this.message = v;
85     }
86
87     String JavaDoc description;
88    
89     /**
90      * Get the value of description.
91      * @return value of description.
92      */

93     public String JavaDoc getDescription() {
94         return description;
95     }
96    
97     /**
98      * Set the value of description.
99      * @param v Value to assign to description.
100      */

101     public void setDescription(String JavaDoc v) {
102         this.description = v;
103     }
104    
105     View parentView;
106    
107     /**
108      * Get the value of parentView.
109      * @return value of parentView.
110      */

111     public View getParentView() {
112         return parentView;
113     }
114    
115     /**
116      * Set the value of parentView.
117      * @param v Value to assign to parentView.
118      */

119     public void setParentView(View v) {
120         this.parentView = v;
121     }
122
123     public View getRootView() {
124         if (parentView==null)
125             return this;
126         return parentView.getRootView();
127     }
128
129     public boolean isDescendantOf(View ancestor) {
130         if (this==ancestor)
131             return true;
132         else if (parentView==null)
133             return false;
134         else
135             return parentView.isDescendantOf(ancestor);
136     }
137
138     public void setContext(DisplayContext context) {
139         loggerContext.debug("setContext on "+this);
140         this.context = context;
141     }
142
143     public DisplayContext getContext() {
144         return context;
145     }
146
147     // style used to change display (css for web)
148
String JavaDoc style;
149
150     public void setStyle(String JavaDoc style) {
151         this.style = style;
152     }
153
154     public String JavaDoc getStyle() {
155         return style;
156     }
157
158     public void setFactory(ViewFactory factory) {
159         this.factory = factory;
160     }
161
162     public ViewFactory getFactory() {
163         return factory;
164     }
165
166     public void setLabel(String JavaDoc label) {
167         this.label = label;
168     }
169
170     public String JavaDoc getLabel() {
171         return label;
172     }
173
174     public void setSize(Length width, Length height) {
175         this.width = width;
176         this.height = height;
177         SwingUtils.setSize(this,width,height);
178     }
179
180     public void setType(String JavaDoc type) {
181         this.type = type;
182     }
183
184     public String JavaDoc getType() {
185         return type;
186     }
187
188     public void setParameters(Object JavaDoc[] parameters) {
189         this.parameters = parameters;
190     }
191    
192     public Object JavaDoc[] getParameters() {
193         return parameters;
194     }
195
196     public boolean equalsView(ViewIdentity view) {
197         return
198             ( ( type!=null &&
199                 type.equals(view.getType()) )
200               || (type==null && view.getType()==null ) )
201             && ( ( parameters!=null &&
202                    Arrays.equals(parameters,view.getParameters()) )
203                  || (parameters==null && view.getParameters()==null) );
204     }
205
206     public boolean equalsView(String JavaDoc type, Object JavaDoc[] parameters) {
207         return this.type.equals(type)
208             && Arrays.equals(this.parameters,parameters);
209     }
210
211     protected boolean closed = false;
212
213     public void close(boolean validate) {
214         closed = true;
215         parameters = null;
216     }
217
218     public boolean isClosed() {
219         return closed;
220     }
221
222     Border JavaDoc viewBorder;
223    
224     /**
225      * Get the value of viewBorder.
226      * @return value of viewBorder.
227      */

228     public Border JavaDoc getViewBorder() {
229         return viewBorder;
230     }
231    
232     /**
233      * Set the value of viewBorder.
234      * @param border Value to assign to viewBorder.
235      */

236     public void setViewBorder(Border JavaDoc border) {
237         javax.swing.border.Border JavaDoc swingBorder;
238         this.viewBorder = border;
239         switch(border.getStyle()) {
240             case Border.LINE:
241                 swingBorder=BorderFactory.createLineBorder(Color.black);
242                 break;
243             case Border.ETCHED:
244                 swingBorder=BorderFactory.createEtchedBorder();
245                 break;
246             case Border.RAISED:
247                 swingBorder=BorderFactory.createRaisedBevelBorder();
248                 break;
249             case Border.LOWERED:
250                 swingBorder=BorderFactory.createLoweredBevelBorder();
251                 break;
252             default:
253                 swingBorder=BorderFactory.createLineBorder(Color.black);
254         }
255         if(border.hasTitle()) {
256             swingBorder=BorderFactory.createTitledBorder(
257                 swingBorder,border.getTitle());
258             switch(border.getAlignment()) {
259                 case Border.RIGHT:
260                     ((TitledBorder JavaDoc)swingBorder).setTitleJustification(
261                         TitledBorder.RIGHT);
262                     break;
263                 case Border.CENTER:
264                     ((TitledBorder JavaDoc)swingBorder).setTitleJustification(
265                         TitledBorder.CENTER);
266             }
267         }
268         this.setBorder(swingBorder);
269     }
270    
271     public String JavaDoc toString() {
272         return Strings.hex(this);
273     }
274
275     void setContext() {
276         Collaboration.get().addAttribute(
277             GuiAC.DISPLAY_CONTEXT,context);
278         Collaboration.get().addAttribute(
279             SessionAC.SESSION_ID,GuiAC.getLocalSessionID());
280     }
281
282     public void setFocus(FieldItem field, Object JavaDoc option) {
283     }
284
285     /**
286      * Invoke a method with correct attributes
287      * (DISPLAY_CONTEXT,SESSION_ID and all attributes stored by the
288      * current window) with InvokeThread
289      *
290      * @param substance the object on which to invoke the method
291      * @param methodName the method to invoke
292      * @param parameters parameters to pass to the method
293      */

294     protected void invokeInContext(Object JavaDoc substance,String JavaDoc methodName,
295                                    Object JavaDoc[] parameters)
296     {
297         MethodItem method = ClassRepository.get().getClass(substance)
298             .getMethod(methodName);
299         String JavaDoc[] names = new String JavaDoc[2];
300         Object JavaDoc[] values = new Object JavaDoc[2];
301         names[0] = GuiAC.DISPLAY_CONTEXT;
302         values[0] = context;
303         names[1] = SessionAC.SESSION_ID;
304         values[1] = GuiAC.getLocalSessionID();
305         //names[2] = "Gui.askForParameters";
306
//values[2] = method;
307
Object JavaDoc window = context.getWindow();
308
309         if (window instanceof DialogView) {
310             ((DialogView)window).restoreContext();
311         } else if (window instanceof ObjectViewDialog) {
312             ((ObjectViewDialog)window).restoreContext();
313         }
314         InvokeThread.run(new InvokeEvent(this,substance, method, parameters),
315                          null, null, names, values);
316     }
317 }
318
Popular Tags