001    /*
002      Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com>
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.gui.swing;
019    
020    import java.awt.Component;
021    import java.awt.Insets;
022    import java.awt.event.ActionEvent;
023    import java.awt.event.ActionListener;
024    import java.util.Arrays;
025    import javax.swing.JButton;
026    import javax.swing.JComponent;
027    import javax.swing.JTable;
028    import javax.swing.table.TableCellRenderer;
029    import org.apache.log4j.Logger;
030    import org.objectweb.jac.aspects.gui.Border;
031    import org.objectweb.jac.aspects.gui.DisplayContext;
032    import org.objectweb.jac.aspects.gui.EventHandler;
033    import org.objectweb.jac.aspects.gui.GuiAC;
034    import org.objectweb.jac.aspects.gui.InvokeEvent;
035    import org.objectweb.jac.aspects.gui.MethodUpdate;
036    import org.objectweb.jac.aspects.gui.MethodView;
037    import org.objectweb.jac.aspects.gui.ResourceManager;
038    import org.objectweb.jac.aspects.gui.Utils;
039    import org.objectweb.jac.aspects.gui.View;
040    import org.objectweb.jac.aspects.gui.ViewFactory;
041    import org.objectweb.jac.aspects.gui.ViewIdentity;
042    import org.objectweb.jac.aspects.session.SessionAC;
043    import org.objectweb.jac.core.Collaboration;
044    import org.objectweb.jac.core.rtti.AbstractMethodItem;
045    import org.objectweb.jac.core.rtti.FieldItem;
046    import org.objectweb.jac.core.rtti.MethodItem;
047    import org.objectweb.jac.util.Strings;
048    
049    public class SwingMethodView extends JButton 
050        implements MethodView, ActionListener, TableCellRenderer, MethodUpdate
051    {
052        static Logger loggerEvents = Logger.getLogger("gui.events");
053    
054        DisplayContext context;
055        int width;
056        int height;
057        ViewFactory factory;
058        Object[] parameters;
059        String type;
060    
061        Object substance;
062        AbstractMethodItem method;
063        MethodItem condition;
064    
065    
066        // style used to change display (css for web)
067        String style;
068    
069        public void setStyle(String style) {
070            this.style = style;
071        }
072    
073        public String getStyle() {
074            return style;
075        }
076    
077    
078        public SwingMethodView(Object substance, AbstractMethodItem method) {
079            this.substance = substance;
080            setMethod(method);
081            setMargin(new Insets(1,1,1,1));
082            addActionListener(this);
083        }
084    
085        String description;
086       
087        /**
088         * Get the value of description.
089         * @return value of description.
090         */
091        public String getDescription() {
092            return description;
093        }
094       
095        /**
096         * Set the value of description.
097         * @param v  Value to assign to description.
098         */
099        public void setDescription(String  v) {
100            this.description = v;
101        }
102       
103        View parentView;
104       
105        /**
106         * Get the value of parentView.
107         * @return value of parentView.
108         */
109        public View getParentView() {
110            return parentView;
111        }
112       
113        /**
114         * Set the value of parentView.
115         * @param v  Value to assign to parentView.
116         */
117        public void setParentView(View  v) {
118            this.parentView = v;
119        }
120    
121        public View getRootView() {
122            if (parentView==null)
123                return this;
124            return parentView.getRootView();
125        }
126    
127        public boolean isDescendantOf(View ancestor) {
128            if (this==ancestor)
129                return true;
130            else if (parentView==null)
131                return false;
132            else
133                return parentView.isDescendantOf(ancestor);
134        }
135    
136        Border viewBorder;
137       
138        /**
139         * Get the value of viewBorder.
140         * @return value of viewBorder.
141         */
142        public Border getViewBorder() {
143            return viewBorder;
144        }
145       
146        /**
147         * Set the value of viewBorder.
148         * @param v  Value to assign to viewBorder.
149         */
150        public void setViewBorder(Border  v) {
151            this.viewBorder = v;
152        }
153       
154        MethodItem message;
155       
156        /**
157         * Get the value of message.
158         * @return value of message.
159         */
160        public MethodItem getMessage() {
161            return message;
162        }
163       
164        /**
165         * Set the value of message.
166         * @param v  Value to assign to message.
167         */
168        public void setMessage(MethodItem  v) {
169            this.message = v;
170        }
171    
172        public void setContext(DisplayContext context) {
173            this.context = context;
174        }
175    
176        public DisplayContext getContext() {
177            return context;
178        }
179    
180        public void setLabel(String label) {
181            setText(label);
182            //      setToolTipText(label);
183        }
184    
185        public String getText() {
186            if (method instanceof MethodItem && 
187                ((MethodItem)method).isSetter() && getIcon()!=null)
188                return "";
189            else
190                return super.getText();
191        }
192    
193        public void setIcon(String icon) {
194            setIcon(ResourceManager.getIcon(icon));
195        }
196    
197        boolean onlyIcon = false;
198        public void setOnlyIcon(boolean onlyIcon) {
199            this.onlyIcon = onlyIcon;
200        }
201    
202        public void setWidth(int width) {
203            this.width = width;
204        }
205    
206        public void setHeight(int height) {
207            this.height = height;
208        }
209    
210        public void setMethod(AbstractMethodItem method) {
211            if (condition!=null) {
212                Utils.unregisterMethod(substance,condition,this);
213            }
214            this.method = method;
215            condition = GuiAC.getCondition(method);
216            if (condition!=null) {
217                setEnabled(GuiAC.isEnabled(method,substance));
218                Utils.registerMethod(substance,condition,this);
219            }
220        }
221    
222        public boolean equalsView(ViewIdentity view) {
223            return 
224                ( ( type!=null && 
225                    type.equals(view.getType()) )
226                  || (type==null && view.getType()==null ) )
227                && ( ( parameters!=null && 
228                       Arrays.equals(parameters,view.getParameters()) ) 
229                     || (parameters==null && view.getParameters()==null) );
230        }
231    
232        public boolean equalsView(String type, Object[] parameters) {
233            return this.type.equals(type)
234                && Arrays.equals(this.parameters,parameters);
235        }
236    
237        public void setSubstance(Object substance) {
238            this.substance = substance;
239        }
240    
241        public void close(boolean validate) {
242            closed = true;
243        }
244    
245        boolean closed = false;
246    
247        public boolean isClosed() {
248            return closed;
249        }
250    
251        public ViewFactory getFactory() {
252            return factory;
253        }
254    
255        public void setFactory(ViewFactory factory) {
256            this.factory = factory;
257        }
258    
259        public void setType(String type) {
260            this.type = type;
261        }
262    
263        public String getType() {
264            return type;
265        }
266    
267        public void setParameters(Object[] parameters) {
268            this.parameters = parameters;
269        }
270       
271        public Object[] getParameters() {
272            return parameters;
273        }
274    
275        public void setFocus(FieldItem field, Object option) {
276        }
277    
278        public String toString() {
279            return Strings.hex(this);
280        }
281    
282    
283        // ActionListener interface
284    
285        public void actionPerformed(ActionEvent action) {
286            loggerEvents.debug("action performed on MethodView "+method.getName());
287            Collaboration collab = Collaboration.get();
288            collab.addAttribute(GuiAC.DISPLAY_CONTEXT,context);
289            collab.addAttribute(SessionAC.SESSION_ID,
290                                GuiAC.getLocalSessionID());
291            EventHandler.get().onInvoke(
292                context,new InvokeEvent(this,substance,method));
293        }
294    
295        // TableCellRenderer
296        public Component getTableCellRendererComponent(
297            JTable table, Object value, 
298            boolean isSelected, boolean hasFocus, 
299            int row, int column) 
300        {
301            JComponent component = this;//getComponent();
302          
303            if (component!=null) {
304                component.setOpaque(true); // so that the background is really drawn
305            }
306            setOpaque(true); // so that the background is really drawn
307             
308            if (isSelected) {
309                if (component!=null) {
310                    component.setForeground(table.getSelectionForeground());
311                    component.setBackground(table.getSelectionBackground());
312                }
313                setForeground(table.getSelectionForeground());
314                setBackground(table.getSelectionBackground());
315            } else {
316                if (component!=null) {
317                    component.setForeground(table.getForeground());
318                    component.setBackground(table.getBackground());
319                }
320                setForeground(table.getForeground());
321                setBackground(table.getBackground());
322            }
323            if (component!=null) {
324                component.setFont(null);
325            }
326            setFont(null);
327    
328            //setValue(value); 
329    
330            return this;
331        }
332    
333        // MethodUpdate interface
334        public void methodUpdated(Object substance, MethodItem method, Object param) {
335            setEnabled(GuiAC.isEnabled(this.method,this.substance));
336        }
337    }