| ToolBar.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: ToolBar.java 2104 2011-03-07 18:54:34Z dmsmith $
21 */
22 package org.crosswire.common.swing.desktop;
23
24 import java.awt.BorderLayout;
25 import java.awt.Component;
26 import java.awt.Container;
27 import java.awt.event.ActionEvent;
28 import java.io.IOException;
29 import java.io.ObjectInputStream;
30
31 import javax.swing.Action;
32 import javax.swing.Icon;
33 import javax.swing.JButton;
34 import javax.swing.JCheckBoxMenuItem;
35 import javax.swing.JFrame;
36 import javax.swing.JMenuItem;
37 import javax.swing.JToolBar;
38 import javax.swing.SwingConstants;
39
40 import org.crosswire.common.swing.ActionFactory;
41 import org.crosswire.common.swing.CWAction;
42 import org.crosswire.common.swing.GuiUtil;
43 import org.crosswire.common.swing.CWMsg;
44 import org.crosswire.common.util.OSType;
45
46 /**
47 * This toolbar allows for manipulating how it looks. That is it allows for:
48 * <ul>
49 * <li>showing/hiding labels</li>
50 * <li>small/large icons</li>
51 * <li>showing/hiding toolbar</li>
52 * </ul>
53 * It starts with large icons.
54 *
55 * @see gnu.lgpl.License for license details.<br>
56 * The copyright to this program is held by it's authors.
57 * @author DM Smith [ dmsmith555 at yahoo dot com]
58 */
59 public class ToolBar extends JToolBar {
60 /**
61 * ToolBar constructor.
62 */
63 public ToolBar(JFrame frame) {
64 this.frame = frame;
65 actions = new ActionFactory(this);
66
67 setRollover(true);
68
69 // Floating is not appropriate on a Mac
70 // It is the default on all others
71 if (OSType.MAC.equals(OSType.getOSType())) {
72 setFloatable(false);
73 }
74 GuiUtil.applyDefaultOrientation(this);
75 }
76
77 /*
78 * (non-Javadoc)
79 *
80 * @see javax.swing.JToolBar#add(javax.swing.Action)
81 */
82 @Override
83 public JButton add(Action a) {
84 JButton button = super.add(a);
85 button.setIcon((Icon) a.getValue(CWAction.LARGE_ICON));
86 return button;
87 }
88
89 /**
90 * Show or hide the tool bar.
91 *
92 * @param show
93 * indicates whether the toolbar is visible
94 */
95 public void showToolBar(boolean show) {
96 Container contentPane = frame.getContentPane();
97
98 if (show) {
99 // Honor the previous orientation
100 // Don't know how to honor the last location
101 if (getOrientation() == SwingConstants.HORIZONTAL) {
102 contentPane.add(this, BorderLayout.NORTH);
103 } else {
104 contentPane.add(this, BorderLayout.LINE_START);
105 }
106 } else {
107 contentPane.remove(this);
108 }
109 frame.validate();
110 }
111
112 /**
113 * Set the tool tip text for the buttons on the tool bar.
114 *
115 * @param show
116 * indicates whether the buttons should be labeled
117 */
118 public void showText(boolean show) {
119 int i = 0;
120 Component c = getComponentAtIndex(0);
121 while (c != null) {
122 if (c instanceof JButton) {
123 JButton button = (JButton) c;
124 if (show) {
125 Action action = button.getAction();
126 button.setText((String) action.getValue(Action.NAME));
127 } else {
128 button.setText(null);
129 }
130 }
131 i++;
132 c = getComponentAtIndex(i);
133 }
134 }
135
136 /**
137 * Sets the size of the tool bar button images.
138 *
139 * @param large
140 * indicates whether large buttons should be used
141 */
142 public void showLargeIcons(boolean large) {
143 int i = 0;
144 Component c = getComponentAtIndex(0);
145 while (c != null) {
146 if (c instanceof JButton) {
147 JButton button = (JButton) c;
148 Action action = button.getAction();
149 if (action instanceof CWAction) {
150 // Clear the button's computed disabled icon
151 // so the button can get it again.
152 button.setDisabledIcon(null);
153 if (large) {
154 button.setIcon((Icon) action.getValue(CWAction.LARGE_ICON));
155 } else {
156 button.setIcon((Icon) action.getValue(Action.SMALL_ICON));
157 }
158 }
159 }
160 i++;
161 c = getComponentAtIndex(i);
162 }
163 }
164
165 /**
166 * Build a menu item that an end user can use to toggle visibility of the
167 * toolbar
168 *
169 * @return a check box that can be used to toggle the visibility of the
170 * toolbar
171 */
172 public JMenuItem getShowToggle() {
173 // TRANSLATOR: This is the label of a view option allowing a user to show/hide the tool bar
174 CWAction action = actions.addAction("ToolBarToggle", CWMsg.gettext("Show Tool Bar"));
175 // TRANSLATOR: This is the tooltip for a view option allowing a user to show/hide the tool bar
176 action.setTooltip(CWMsg.gettext("Toggle the display of the tool bar"));
177 action.setAccelerator("B,ctrl");
178 JCheckBoxMenuItem toggle = new JCheckBoxMenuItem(action);
179 toggle.setSelected(true);
180 return toggle;
181 }
182
183 /**
184 * Build a menu item that an end user can use to toggle the text
185 *
186 * @return a check box that can be used to toggle the text
187 */
188 public JMenuItem getTextToggle() {
189 // TRANSLATOR: This is the label of a view option allowing a user to show/hide the text for icons on the tool bar
190 CWAction action = actions.addAction("ToolBarText", CWMsg.gettext("Show Tool Bar Text"));
191 // TRANSLATOR: This is the tooltip for a view option allowing a user to show/hide the text for icons on the tool bar
192 action.setTooltip(CWMsg.gettext("Toggle the display of the tool bar text"));
193 return new JCheckBoxMenuItem(action);
194 }
195
196 /**
197 * Build a menu item that an end user can use to toggle the size of the
198 * icons
199 *
200 * @return a check box that can be used to toggle the size of the icons
201 */
202 public JMenuItem getIconSizeToggle() {
203 // TRANSLATOR: This is the label of a view option allowing a user to toggle between large and small icons on the tool bar
204 CWAction action = actions.addAction("ToolBarSize", CWMsg.gettext("Large Tool Bar"));
205 // TRANSLATOR: This is the tooltip for a view option allowing a user to toggle between large and small icons on the tool bar
206 action.setTooltip(CWMsg.gettext("Toggle size of the tool bar icons"));
207 JCheckBoxMenuItem toggle = new JCheckBoxMenuItem(action);
208 toggle.setSelected(true);
209 return toggle;
210 }
211
212 /**
213 * Show or hide the tool bar.
214 */
215 public void doToolBarToggle(ActionEvent ev) {
216 JCheckBoxMenuItem toggle = (JCheckBoxMenuItem) ev.getSource();
217 showToolBar(toggle.isSelected());
218 }
219
220 /**
221 * Show or hide the tool bar text.
222 */
223 public void doToolBarText(ActionEvent ev) {
224 JCheckBoxMenuItem toggle = (JCheckBoxMenuItem) ev.getSource();
225 showText(toggle.isSelected());
226 }
227
228 /**
229 * Show large or small tool bar icons.
230 */
231 public void doToolBarLarge(ActionEvent ev) {
232 JCheckBoxMenuItem toggle = (JCheckBoxMenuItem) ev.getSource();
233 showLargeIcons(toggle.isSelected());
234 }
235
236 /**
237 * Serialization support.
238 *
239 * @param is
240 * @throws IOException
241 * @throws ClassNotFoundException
242 */
243 private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
244 actions = new ActionFactory(this);
245 is.defaultReadObject();
246 }
247
248 /**
249 * The frame in which the toolbar is shown. It must be border layout with
250 * the only other component being centered.
251 */
252 private JFrame frame;
253 private transient ActionFactory actions;
254
255 /**
256 * Serialization ID
257 */
258 private static final long serialVersionUID = 3544669594414690871L;
259 }
260