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 org.objectweb.jac.aspects.gui.Callback;
021    import org.objectweb.jac.aspects.gui.GuiAC;
022    import org.objectweb.jac.aspects.gui.MenuView;
023    import org.objectweb.jac.aspects.gui.MethodUpdate;
024    import org.objectweb.jac.aspects.gui.ResourceManager;
025    import org.objectweb.jac.aspects.gui.Utils;
026    import org.objectweb.jac.aspects.session.SessionAC;
027    import org.objectweb.jac.core.Collaboration;
028    import org.objectweb.jac.core.rtti.MethodItem;
029    import java.awt.event.ActionEvent;
030    import java.awt.event.ActionListener;
031    import java.util.Hashtable;
032    import java.util.Iterator;
033    import javax.swing.JButton;
034    import javax.swing.JToolBar;
035    
036    public class ToolBar extends AbstractCompositeView 
037        implements MenuView, ActionListener, MethodUpdate
038    {
039       
040        JToolBar toolbar;
041    
042        // action command -> AbstractMethodItem
043        Hashtable actions = new Hashtable();
044       
045        // actoin command -> JButton
046        Hashtable buttons = new Hashtable();
047       
048        public ToolBar() {
049            toolbar = new JToolBar();
050            add(toolbar);
051        }
052    
053        // MenuView interface
054    
055        public void addSubMenu(String label, String icon, MenuView submenu) {
056            // do nothing
057        }
058    
059        public void addAction(String label, String icon,
060                              Callback callback) {
061            JButton button = icon!=null? 
062                new JButton(ResourceManager.getIcon(icon)) : 
063                new JButton(label);
064            String actionCommand = callback.toString();
065            button.setActionCommand(actionCommand);
066            button.addActionListener(this);
067            button.setMnemonic(
068                MenuBar.getMnemonic(
069                    toolbar,
070                    GuiAC.getMnemonics(callback.getMethod())+label));
071            button.setToolTipText(GuiAC.getLabel(callback.getMethod()));
072            actions.put(actionCommand,callback);
073            buttons.put(actionCommand,button);
074            updateEnabled(button,callback);
075            toolbar.add(button);
076            MethodItem condition = (MethodItem)callback.getMethod().getAttribute(GuiAC.CONDITION);
077            if (condition!=null)
078                Utils.registerMethod(callback.getObject(),condition,this,actionCommand);
079        }
080       
081    
082        public void addSeparator() {
083            toolbar.addSeparator();
084        }
085    
086        String position;
087       
088        /**
089         * Get the value of position.
090         * @return value of position.
091         */
092        public String getPosition() {
093            return position;
094        }
095       
096        /**
097         * Set the value of position.
098         * @param v  Value to assign to position.
099         */
100        public void setPosition(String  v) {
101            this.position = v;
102        }
103    
104        public void close(boolean validate) {
105            super.close(validate);
106            // Unregister from all events
107            Iterator it = actions.keySet().iterator();
108            while (it.hasNext()) {
109                Callback callback = (Callback)it.next();
110                MethodItem condition = (MethodItem)callback.getMethod().getAttribute(GuiAC.CONDITION);
111                if (condition!=null)
112                    Utils.unregisterMethod(null,condition,this);
113            }
114        }
115       
116        public String toString() {
117            return getClass().getName()+"@"+Integer.toString(hashCode());
118        }
119    
120        // ActionListener interface
121       
122        public void actionPerformed(ActionEvent event) {
123            Collaboration.get().addAttribute(
124                SessionAC.SESSION_ID, GuiAC.getLocalSessionID());
125            Callback callback = (Callback)actions.get(event.getActionCommand());
126            callback.invoke(context,this);
127        }
128    
129        protected void updateEnabled(JButton button, Callback callback) {
130            button.setEnabled(GuiAC.isEnabled(callback.getMethod(), callback.getObject()));      
131        }
132    
133        // MethodUpdate interface
134        public void methodUpdated(Object substance, MethodItem method, Object param) {
135            updateEnabled((JButton)buttons.get((String)param),
136                          (Callback)actions.get((String)param));
137        }
138    }