KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > ToolBar


1 /*
2   Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui.swing;
19
20 import org.objectweb.jac.aspects.gui.Callback;
21 import org.objectweb.jac.aspects.gui.GuiAC;
22 import org.objectweb.jac.aspects.gui.MenuView;
23 import org.objectweb.jac.aspects.gui.MethodUpdate;
24 import org.objectweb.jac.aspects.gui.ResourceManager;
25 import org.objectweb.jac.aspects.gui.Utils;
26 import org.objectweb.jac.aspects.session.SessionAC;
27 import org.objectweb.jac.core.Collaboration;
28 import org.objectweb.jac.core.rtti.MethodItem;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JToolBar JavaDoc;
35
36 public class ToolBar extends AbstractCompositeView
37     implements MenuView, ActionListener JavaDoc, MethodUpdate
38 {
39    
40     JToolBar JavaDoc toolbar;
41
42     // action command -> AbstractMethodItem
43
Hashtable JavaDoc actions = new Hashtable JavaDoc();
44    
45     // actoin command -> JButton
46
Hashtable JavaDoc buttons = new Hashtable JavaDoc();
47    
48     public ToolBar() {
49         toolbar = new JToolBar JavaDoc();
50         add(toolbar);
51     }
52
53     // MenuView interface
54

55     public void addSubMenu(String JavaDoc label, String JavaDoc icon, MenuView submenu) {
56         // do nothing
57
}
58
59     public void addAction(String JavaDoc label, String JavaDoc icon,
60                           Callback callback) {
61         JButton JavaDoc button = icon!=null?
62             new JButton JavaDoc(ResourceManager.getIcon(icon)) :
63             new JButton JavaDoc(label);
64         String JavaDoc actionCommand = callback.toString();
65         button.setActionCommand(actionCommand);
66         button.addActionListener(this);
67         button.setMnemonic(
68             MenuBar.getMnemonic(
69                 toolbar,
70                 GuiAC.getMnemonics(callback.getMethod())+label));
71         button.setToolTipText(GuiAC.getLabel(callback.getMethod()));
72         actions.put(actionCommand,callback);
73         buttons.put(actionCommand,button);
74         updateEnabled(button,callback);
75         toolbar.add(button);
76         MethodItem condition = (MethodItem)callback.getMethod().getAttribute(GuiAC.CONDITION);
77         if (condition!=null)
78             Utils.registerMethod(callback.getObject(),condition,this,actionCommand);
79     }
80    
81
82     public void addSeparator() {
83         toolbar.addSeparator();
84     }
85
86     String JavaDoc position;
87    
88     /**
89      * Get the value of position.
90      * @return value of position.
91      */

92     public String JavaDoc getPosition() {
93         return position;
94     }
95    
96     /**
97      * Set the value of position.
98      * @param v Value to assign to position.
99      */

100     public void setPosition(String JavaDoc v) {
101         this.position = v;
102     }
103
104     public void close(boolean validate) {
105         super.close(validate);
106         // Unregister from all events
107
Iterator JavaDoc 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 JavaDoc toString() {
117         return getClass().getName()+"@"+Integer.toString(hashCode());
118     }
119
120     // ActionListener interface
121

122     public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc button, Callback callback) {
130         button.setEnabled(GuiAC.isEnabled(callback.getMethod(), callback.getObject()));
131     }
132
133     // MethodUpdate interface
134
public void methodUpdated(Object JavaDoc substance, MethodItem method, Object JavaDoc param) {
135         updateEnabled((JButton JavaDoc)buttons.get((String JavaDoc)param),
136                       (Callback)actions.get((String JavaDoc)param));
137     }
138 }
139
Popular Tags