001    /*
002      Copyright (C) 2001 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.web;
019    
020    import java.util.HashMap;
021    import java.util.Vector;
022    import org.apache.log4j.Logger;
023    import org.objectweb.jac.aspects.gui.*;
024    
025    public abstract class AbstractMenu extends AbstractView 
026        implements MenuView, HTMLViewer, MenuListener 
027    {
028        static Logger logger = Logger.getLogger("gui.menu");
029        static Logger loggerEvents = Logger.getLogger("gui.events");
030    
031        // key -> [ callback | Menu | null ]
032        HashMap map = new HashMap();
033        // item order
034        Vector keys = new Vector();
035    
036        public AbstractMenu(ViewFactory factory, DisplayContext context) {
037            super(factory,context);
038        }
039    
040        // MenuView interface
041    
042        public void addSubMenu(String label, String icon, MenuView submenu) {
043            if (!map.containsKey(label)) {
044                logger.debug(this+".addSubMenu "+label+" -> "+submenu);
045                keys.add(label);
046                map.put(label,submenu);
047            }
048        }
049    
050        public void addAction(String label, String icon, Callback callback) {
051            if (!map.containsKey(label)) {
052                logger.debug(this+".addAction "+label+" -> "+callback);
053                keys.add(label);
054                map.put(label,new MenuItem(label,icon,callback));
055            }
056        }
057    
058        public void addSeparator() {
059        }
060       
061        String position = org.objectweb.jac.aspects.gui.Menu.LEFT;
062       
063        /**
064         * Get the value of position.
065         * @return value of position.
066         */
067        public String getPosition() {
068            return position;
069        }
070       
071        /**
072         * Set the value of position.
073         * @param v  Value to assign to position.
074         */
075        public void setPosition(String  v) {
076            this.position = v;
077            if (position==null)
078                position = org.objectweb.jac.aspects.gui.Menu.LEFT;
079        }
080    
081        // MenuListener interface
082    
083        public void onMenuClick(String key) {
084            try {
085                loggerEvents.debug(this+".onMenuClick `"+key+"'");
086                MenuItem item = (MenuItem)map.get(key);
087                if (item!=null && item.callback!=null)
088                    EventHandler.get().onInvoke(
089                        context,
090                        new InvokeEvent(this,
091                                        item.callback.getObject(),
092                                        item.callback.getMethod(),
093                                        item.callback.getParameters()),
094                        true,
095                        null,null);
096                else {
097                    loggerEvents.debug("  No item("+item+") or callback("+
098                              (item==null?"":""+item.callback)+") is null");
099                    context.getDisplay().refresh();
100                }
101            } catch (Exception e) {
102                context.getDisplay().showError("Menu error","onMenuClick "+key+": "+
103                                               e.toString()+"<br><pre>"+map+"</pre>");
104                logger.error("onMenuClick "+key,e);
105            }
106        }
107    }