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.web;
019    
020    import org.objectweb.jac.aspects.gui.*;
021    import java.io.PrintWriter;
022    import java.util.HashMap;
023    import java.util.Iterator;
024    import java.util.Vector;
025    
026    public class ToolBar extends AbstractView implements MenuView, HTMLViewer, MenuListener {
027    
028        // key -> [ callback | Menu | null ]
029        HashMap map = new HashMap();
030        // item order
031        Vector keys = new Vector();
032    
033        public ToolBar(ViewFactory factory, DisplayContext context) {
034            super(factory,context);
035        }
036    
037        // MenuView interface
038    
039        public void addSubMenu(String label, String icon, MenuView submenu) {
040        }
041    
042        public void addAction(String label, String icon, Callback callback) {
043            if (!map.containsKey(label)) {
044                keys.add(label);
045                map.put(label,new MenuItem(label,icon,callback));
046            }
047        }
048    
049        public void addSeparator() {
050        }
051    
052        String position;
053       
054        /**
055         * Get the value of position.
056         * @return value of position.
057         */
058        public String getPosition() {
059            return position;
060        }
061       
062        /**
063         * Set the value of position.
064         * @param v  Value to assign to position.
065         */
066        public void setPosition(String  v) {
067            this.position = v;
068        }
069       
070        // HTMLViewer interface
071    
072        public void genHTML(PrintWriter out) {
073            out.println("<div class=\"toolBar\">");
074            Iterator i = keys.iterator();
075            while (i.hasNext()) {
076                String key = (String)i.next();
077                Object item = map.get(key);
078                if (item instanceof MenuItem) {
079                    out.print("<a href=\""+eventURL("onMenuClick")+
080                              "&item="+key+"\">");
081                    if (((MenuItem)item).icon!=null) 
082                        out.print(iconElement(((MenuItem)item).icon,""));
083                    else
084                        out.println(key);
085                    out.println("</a>");
086                }
087            }
088            out.println("</div>");
089        }
090    
091        // MenuListener interface
092    
093        public void onMenuClick(String key) {
094            try {
095                MenuItem item = (MenuItem)map.get(key);
096                if (item.callback!=null)
097                    EventHandler.get().onInvoke(
098                        context,
099                        new InvokeEvent(this,null,item.callback.getMethod()));
100                else 
101                    context.getDisplay().refresh();
102            } catch (Exception e) {
103                context.getDisplay().showError("Menu error",e.toString());
104            }
105        }
106    }