KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001-2003 Laurent Martelli <laurent@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,
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 License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui.swing;
19
20 import java.awt.Component JavaDoc;
21 import java.awt.Insets JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import javax.swing.JButton JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.JTable JavaDoc;
28 import javax.swing.table.TableCellRenderer JavaDoc;
29 import org.apache.log4j.Logger;
30 import org.objectweb.jac.aspects.gui.Border;
31 import org.objectweb.jac.aspects.gui.DisplayContext;
32 import org.objectweb.jac.aspects.gui.EventHandler;
33 import org.objectweb.jac.aspects.gui.GuiAC;
34 import org.objectweb.jac.aspects.gui.InvokeEvent;
35 import org.objectweb.jac.aspects.gui.Length;
36 import org.objectweb.jac.aspects.gui.MethodUpdate;
37 import org.objectweb.jac.aspects.gui.MethodView;
38 import org.objectweb.jac.aspects.gui.ResourceManager;
39 import org.objectweb.jac.aspects.gui.Utils;
40 import org.objectweb.jac.aspects.gui.View;
41 import org.objectweb.jac.aspects.gui.ViewFactory;
42 import org.objectweb.jac.aspects.gui.ViewIdentity;
43 import org.objectweb.jac.aspects.session.SessionAC;
44 import org.objectweb.jac.core.Collaboration;
45 import org.objectweb.jac.core.rtti.AbstractMethodItem;
46 import org.objectweb.jac.core.rtti.FieldItem;
47 import org.objectweb.jac.core.rtti.MethodItem;
48 import org.objectweb.jac.util.Strings;
49
50 public class SwingMethodView extends JButton JavaDoc
51     implements MethodView, ActionListener JavaDoc, TableCellRenderer JavaDoc, MethodUpdate
52 {
53     static Logger loggerEvents = Logger.getLogger("gui.events");
54
55     DisplayContext context;
56     Length width;
57     Length height;
58     ViewFactory factory;
59     Object JavaDoc[] parameters;
60     String JavaDoc type;
61
62     Object JavaDoc substance;
63     AbstractMethodItem method;
64     MethodItem condition;
65
66
67     // style used to change display (css for web)
68
String JavaDoc style;
69
70     public void setStyle(String JavaDoc style) {
71         this.style = style;
72     }
73
74     public String JavaDoc getStyle() {
75         return style;
76     }
77
78
79     public SwingMethodView(Object JavaDoc substance, AbstractMethodItem method) {
80         this.substance = substance;
81         setMethod(method);
82         setMargin(new Insets JavaDoc(1,1,1,1));
83         addActionListener(this);
84     }
85
86     String JavaDoc description;
87    
88     /**
89      * Get the value of description.
90      * @return value of description.
91      */

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

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

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

118     public void setParentView(View v) {
119         this.parentView = v;
120     }
121
122     public View getRootView() {
123         if (parentView==null)
124             return this;
125         return parentView.getRootView();
126     }
127
128     public boolean isDescendantOf(View ancestor) {
129         if (this==ancestor)
130             return true;
131         else if (parentView==null)
132             return false;
133         else
134             return parentView.isDescendantOf(ancestor);
135     }
136
137     Border viewBorder;
138    
139     /**
140      * Get the value of viewBorder.
141      * @return value of viewBorder.
142      */

143     public Border getViewBorder() {
144         return viewBorder;
145     }
146    
147     /**
148      * Set the value of viewBorder.
149      * @param v Value to assign to viewBorder.
150      */

151     public void setViewBorder(Border v) {
152         this.viewBorder = v;
153     }
154    
155     MethodItem message;
156    
157     /**
158      * Get the value of message.
159      * @return value of message.
160      */

161     public MethodItem getMessage() {
162         return message;
163     }
164    
165     /**
166      * Set the value of message.
167      * @param v Value to assign to message.
168      */

169     public void setMessage(MethodItem v) {
170         this.message = v;
171     }
172
173     public void setContext(DisplayContext context) {
174         this.context = context;
175     }
176
177     public DisplayContext getContext() {
178         return context;
179     }
180
181     public String JavaDoc getLabel() {
182         return getText();
183     }
184
185     public void setLabel(String JavaDoc label) {
186         setText(label);
187         // setToolTipText(label);
188
}
189
190     public String JavaDoc getText() {
191         if (method instanceof MethodItem &&
192             ((MethodItem)method).isSetter() && getIcon()!=null)
193             return "";
194         else
195             return super.getText();
196     }
197
198     public void setIcon(String JavaDoc icon) {
199         setIcon(ResourceManager.getIcon(icon));
200     }
201
202     boolean onlyIcon = false;
203     public void setOnlyIcon(boolean onlyIcon) {
204         this.onlyIcon = onlyIcon;
205     }
206
207     public void setSize(Length width, Length height) {
208         this.width = width;
209         this.height = height;
210         SwingUtils.setSize(this,width,height);
211     }
212
213     public void setMethod(AbstractMethodItem method) {
214         if (condition!=null) {
215             Utils.unregisterMethod(substance,condition,this);
216         }
217         this.method = method;
218         condition = GuiAC.getCondition(method);
219         if (condition!=null) {
220             setEnabled(GuiAC.isEnabled(method,substance));
221             Utils.registerMethod(substance,condition,this);
222         }
223     }
224
225     public boolean equalsView(ViewIdentity view) {
226         return
227             ( ( type!=null &&
228                 type.equals(view.getType()) )
229               || (type==null && view.getType()==null ) )
230             && ( ( parameters!=null &&
231                    Arrays.equals(parameters,view.getParameters()) )
232                  || (parameters==null && view.getParameters()==null) );
233     }
234
235     public boolean equalsView(String JavaDoc type, Object JavaDoc[] parameters) {
236         return this.type.equals(type)
237             && Arrays.equals(this.parameters,parameters);
238     }
239
240     public void setSubstance(Object JavaDoc substance) {
241         this.substance = substance;
242     }
243
244     public void close(boolean validate) {
245         closed = true;
246     }
247
248     boolean closed = false;
249
250     public boolean isClosed() {
251         return closed;
252     }
253
254     public ViewFactory getFactory() {
255         return factory;
256     }
257
258     public void setFactory(ViewFactory factory) {
259         this.factory = factory;
260     }
261
262     public void setType(String JavaDoc type) {
263         this.type = type;
264     }
265
266     public String JavaDoc getType() {
267         return type;
268     }
269
270     public void setParameters(Object JavaDoc[] parameters) {
271         this.parameters = parameters;
272     }
273    
274     public Object JavaDoc[] getParameters() {
275         return parameters;
276     }
277
278     public void setFocus(FieldItem field, Object JavaDoc option) {
279     }
280
281     public String JavaDoc toString() {
282         return Strings.hex(this);
283     }
284
285
286     // ActionListener interface
287

288     public void actionPerformed(ActionEvent JavaDoc action) {
289         loggerEvents.debug("action performed on MethodView "+method.getName());
290         Collaboration collab = Collaboration.get();
291         collab.addAttribute(GuiAC.DISPLAY_CONTEXT,context);
292         collab.addAttribute(SessionAC.SESSION_ID,
293                             GuiAC.getLocalSessionID());
294         EventHandler.get().onInvoke(
295             context,new InvokeEvent(this,substance,method));
296     }
297
298     // TableCellRenderer
299
public Component JavaDoc getTableCellRendererComponent(
300         JTable table, Object JavaDoc value,
301         boolean isSelected, boolean hasFocus,
302         int row, int column)
303     {
304         JComponent JavaDoc component = this;//getComponent();
305

306         if (component!=null) {
307             component.setOpaque(true); // so that the background is really drawn
308
}
309         setOpaque(true); // so that the background is really drawn
310

311         if (isSelected) {
312             if (component!=null) {
313                 component.setForeground(table.getSelectionForeground());
314                 component.setBackground(table.getSelectionBackground());
315             }
316             setForeground(table.getSelectionForeground());
317             setBackground(table.getSelectionBackground());
318         } else {
319             if (component!=null) {
320                 component.setForeground(table.getForeground());
321                 component.setBackground(table.getBackground());
322             }
323             setForeground(table.getForeground());
324             setBackground(table.getBackground());
325         }
326         if (component!=null) {
327             component.setFont(null);
328         }
329         setFont(null);
330
331         //setValue(value);
332

333         return this;
334     }
335
336     // MethodUpdate interface
337
public void methodUpdated(Object JavaDoc substance, MethodItem method, Object JavaDoc param) {
338         setEnabled(GuiAC.isEnabled(this.method,this.substance));
339     }
340 }
341
Popular Tags