KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > TabToolBar


1 /*
2  * Gruntspud
3  *
4  * Copyright (C) 2002 Brett Smith.
5  *
6  * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public License
10  * as published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */

21 package com.sshtools.ui.swing;
22
23 import java.awt.CardLayout JavaDoc;
24 import java.awt.Color JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.GridBagConstraints JavaDoc;
27 import java.awt.GridBagLayout JavaDoc;
28 import java.awt.Insets JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.JViewport JavaDoc;
36 import javax.swing.SwingConstants JavaDoc;
37 import javax.swing.UIManager JavaDoc;
38
39 /**
40  * A TabToolBar creates a vertical tool bar with actions that may be grouped
41  * into contexts, in a style similar to navigation bar used on the left hand
42  * side in Outlook. See the addIcon() method for details on which action
43  * properties are required
44  */

45 public class TabToolBar extends JPanel JavaDoc {
46     // Private instance variables
47
private Vector JavaDoc contextList;
48     private ContextPanel selectedContext;
49     private GridBagConstraints JavaDoc gBC;
50     private CardLayout JavaDoc cardLayout;
51     private JPanel JavaDoc cardPanel;
52     private FolderBar folderBar;
53     private int fixedWidth = -1;
54     private Color JavaDoc toolBarBackground;
55
56     /**
57      * Construct a new TabToolBar
58      */

59     public TabToolBar() {
60         super(new GridBagLayout JavaDoc());
61
62         toolBarBackground = UIManager.getColor("Button.background").darker();
63
64         // Intialise
65
contextList = new Vector JavaDoc();
66         cardLayout = new CardLayout JavaDoc();
67         cardPanel = new JPanel JavaDoc(cardLayout);
68         cardPanel.setOpaque(false);
69         cardPanel.setBackground(toolBarBackground);
70
71         // Create the constraints for use later
72
gBC = new GridBagConstraints JavaDoc();
73         gBC.anchor = GridBagConstraints.NORTH;
74         gBC.fill = GridBagConstraints.HORIZONTAL;
75
76         setBackground(toolBarBackground);
77         setOpaque(true);
78     }
79
80     public void setFixedWidth(int fixedWidth) {
81         this.fixedWidth = fixedWidth;
82         doLayout();
83     }
84
85     public int getFixedWidth() {
86         return fixedWidth;
87     }
88
89     /**
90      * Set the selected context
91      *
92      * @param context context
93      */

94     public void setSelectedContext(String JavaDoc context) {
95         ContextPanel p = null;
96
97         for (Iterator JavaDoc i = contextList.iterator(); i.hasNext();) {
98             ContextPanel z = (ContextPanel) i.next();
99
100             if (context.equals(z.name)) {
101                 p = z;
102             }
103         }
104
105         if (p != null) {
106             selectedContext = p;
107             cardLayout.show(cardPanel, context);
108             makeToolBar();
109         }
110     }
111
112     /**
113      * Sets a FolderBar associated with this tool bar. When actions are invoked,
114      * the FolderBar will change to show the action details for the selected
115      * action.
116      *
117      * @param folder bar
118      */

119     public void setFolderBar(FolderBar folderBar) {
120         this.folderBar = folderBar;
121     }
122
123     /**
124      * Returns the FolderBar that is being changed upon actions..
125      *
126      * @return folder bar
127      */

128     public FolderBar getFolderBar() {
129         return folderBar;
130     }
131
132     /**
133      * Add a new action to the toolbar, this must have a non null
134      * DefaultAction.CONTEXT property or an IllegalArgumentException will be
135      * thrown. The DefaultAction.LARGE_ICON property will be used for the icon,
136      * and the Action.NAME property will be used for the text.
137      * Action.LONG_DESCRIPTION will be used to any tool tip text.
138      *
139      * @param action to build icon from
140      * @throws IllegalArgumentExcption if context not set
141      * @todo Make changes to the action reflect on the button correctly
142      */

143     public void addAction(AppAction action) {
144         // If there is a folder bar in use, and it has no action, then set it
145
// to be this one
146
if ((getFolderBar() != null) && (getFolderBar().getAction() == null)) {
147             getFolderBar().setAction(action);
148         }
149
150         // Get the context and its panel (or create the panel if its new)
151
String JavaDoc context = (String JavaDoc) action.getValue(AppAction.CATEGORY);
152
153         if (context == null) {
154             throw new IllegalArgumentException JavaDoc("AppAction.CONTEXT parameter of action must not be null");
155         }
156
157         ContextPanel contextPanel = null;
158
159         for (int i = 0; (i < contextList.size()) && (contextPanel == null); i++) {
160             ContextPanel p = (ContextPanel) contextList.elementAt(i);
161
162             if (p.name.equals(context)) {
163                 contextPanel = p;
164             }
165         }
166
167         if (contextPanel == null) {
168             contextPanel = new ContextPanel(context);
169             cardPanel.add(contextPanel.name, contextPanel);
170             contextList.addElement(contextPanel);
171         }
172
173         if (selectedContext == null) {
174             selectedContext = contextPanel;
175         }
176
177         // Add the action to the appropriate context panel and layout the bar
178
TabActionButton button = contextPanel.addIcon(action);
179
180         if (getParent() instanceof JViewport JavaDoc && ((JViewport JavaDoc) getParent()).getView() instanceof ScrollingPanel) {
181             ((ScrollingPanel) ((JViewport JavaDoc) getParent()).getView()).setIncrement(button.getPreferredSize().height);
182         }
183
184         makeToolBar();
185     }
186
187     /**
188      *
189      */

190     public void removeAllActions() {
191         contextList.clear();
192         cardPanel.removeAll();
193         selectedContext = null;
194         makeToolBar();
195     }
196
197     private void makeToolBar() {
198         // Rebuild the panels
199
invalidate();
200         removeAll();
201         for (int i = 0; i < contextList.size(); i++) {
202             ContextPanel p = (ContextPanel) contextList.elementAt(i);
203
204             // First add the context button
205
UIUtil.jGridBagAdd(this, new TabButton(p.getContextAction()), gBC, GridBagConstraints.REMAINDER);
206
207             // If this is the selected action, the now add the panel
208
if (p == selectedContext) {
209                 cardLayout.show(cardPanel, p.name);
210
211                 gBC.weighty = 1.0;
212                 UIUtil.jGridBagAdd(this, cardPanel, gBC, GridBagConstraints.REMAINDER);
213                 gBC.weighty = 0.0;
214             }
215         }
216
217         validate();
218         repaint();
219     }
220
221     // Supporting classes
222
public class ContextPanel extends JPanel JavaDoc {
223         String JavaDoc name;
224         AppAction action;
225         GridBagConstraints JavaDoc gBC;
226
227         public ContextPanel(String JavaDoc name) {
228             super(new ListLayout());
229             this.name = name;
230             setOpaque(false);
231             setBackground(toolBarBackground);
232             action = new ContextAction(name, this);
233         }
234
235         public AppAction getContextAction() {
236             return action;
237         }
238
239         public TabActionButton addIcon(AppAction action) {
240             TabActionButton b = new TabActionButton(action);
241             add(b);
242
243             return b;
244         }
245     }
246
247     public class ContextAction extends AppAction {
248         ContextPanel context;
249
250         ContextAction(String JavaDoc name, ContextPanel context) {
251             super();
252             putValue(AppAction.NAME, name);
253             this.context = context;
254         }
255
256         public void actionPerformed(ActionEvent JavaDoc evt) {
257             selectedContext = context;
258             makeToolBar();
259         }
260
261         public boolean checkAvailable() {
262             return true;
263         }
264     }
265
266     public class TabButton extends JButton JavaDoc {
267         public TabButton(AppAction a) {
268             super(a);
269             setFocusPainted(false);
270             setDefaultCapable(false);
271             setMargin(new Insets JavaDoc(1, 1, 1, 1));
272         }
273     }
274
275     public class TabActionButton extends ActionButton {
276
277         public TabActionButton(final AppAction a) {
278             super(a, true, true);
279             setHorizontalTextPosition(SwingConstants.CENTER);
280             setVerticalTextPosition(SwingConstants.BOTTOM);
281         }
282
283         public Dimension JavaDoc getMaximimumSize() {
284             return fixedWidth == -1 ? super.getMaximumSize() : new Dimension JavaDoc(fixedWidth, super.getMaximumSize().height);
285         }
286
287         public Dimension JavaDoc getMinimumSize() {
288             return fixedWidth == -1 ? super.getMinimumSize() : new Dimension JavaDoc(fixedWidth, super.getMinimumSize().height);
289         }
290
291         public Dimension JavaDoc getPreferredSize() {
292             return fixedWidth == -1 ? super.getPreferredSize() : new Dimension JavaDoc(fixedWidth, super.getPreferredSize().height);
293         }
294     }
295 }
296
Popular Tags