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