001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library 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 GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.components;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.awt.GridLayout;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.awt.event.MouseAdapter;
028: import java.awt.event.MouseEvent;
029: import java.util.Collection;
030: import java.util.Iterator;
031: import java.util.LinkedList;
032:
033: import javax.swing.BorderFactory;
034: import javax.swing.ButtonGroup;
035: import javax.swing.ImageIcon;
036: import javax.swing.JComponent;
037: import javax.swing.JLabel;
038: import javax.swing.JPanel;
039: import javax.swing.UIManager;
040: import javax.swing.border.Border;
041:
042: import com.jeta.open.gui.framework.JETAPanel;
043:
044: /**
045: * This class implements a simple button bar
046: *
047: * @author Jeff Tassin
048: */
049: public class TSButtonBar extends JETAPanel {
050:
051: private ButtonGroup m_button_group = new ButtonGroup();
052:
053: /** the current view */
054: private ButtonBarView m_currentview;
055:
056: /** the list of views available on the button bar */
057: private LinkedList m_views = new LinkedList();
058:
059: /**
060: * The selected background color for the button tab.
061: */
062: private Color m_selected_color;
063:
064: private Border m_selected_border;
065: private Border m_unselected_border;
066:
067: /**
068: * A list of action listeners that get invoked when the user clicks on a
069: * button on the button bar.
070: */
071: private LinkedList m_listeners = new LinkedList();
072:
073: private float FACTOR = 0.8f;
074:
075: /**
076: * ctor
077: */
078: public TSButtonBar() {
079: setLayout(new BorderLayout());
080: updateSettings();
081: }
082:
083: /**
084: * Adds a listener that get invoked when the user clicks on a button on the
085: * button bar.
086: */
087: public void addListener(ActionListener listener) {
088: m_listeners.add(listener);
089: }
090:
091: /**
092: * Adds a view to the bar.
093: *
094: * @param caption
095: * the text that appears in the button that will activate this
096: * view
097: * @param view
098: * the view that will be displayed for this button.
099: */
100: public void addView(String caption, JComponent view) {
101: addView(caption, view, null);
102: }
103:
104: /**
105: * Adds a view to the bar.
106: *
107: * @param caption
108: * the text that appears in the button that will activate this
109: * view
110: * @param view
111: * the view that will be displayed for this button.
112: */
113: public void addView(String caption, JComponent view, ImageIcon icon) {
114: JLabel btn = new JLabel(caption);
115: btn.setBorder(m_unselected_border);
116:
117: btn.setOpaque(true);
118: btn.setBackground(UIManager.getColor("control"));
119: if (icon != null)
120: btn.setIcon(icon);
121:
122: btn.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
123: btn.addMouseListener(new MouseAdapter() {
124: public void mousePressed(MouseEvent evt) {
125: JComponent btn = (JComponent) evt.getSource();
126: if (btn != m_currentview.getButton()) {
127: Iterator iter = m_views.iterator();
128: while (iter.hasNext()) {
129: ButtonBarView view = (ButtonBarView) iter
130: .next();
131: if (btn == view.getButton()) {
132: m_currentview = view;
133:
134: /** notify any listeners that a button was pressed. */
135: updateListeners();
136:
137: updateView();
138: btn.setBackground(m_selected_color);
139: btn.setBorder(m_selected_border);
140: } else {
141: view.getButton().setBackground(
142: UIManager.getColor("control"));
143: view.getButton().setBorder(
144: m_unselected_border);
145: }
146: }
147: }
148: }
149: });
150:
151: btn.setFont(javax.swing.UIManager.getFont("Table.font"));
152: ButtonBarView bbview = new ButtonBarView(btn, view);
153:
154: m_views.add(bbview);
155:
156: if (m_currentview == null)
157: m_currentview = bbview;
158:
159: }
160:
161: public JComponent getCurrentView() {
162: if (m_currentview != null)
163: return m_currentview.getView();
164:
165: return null;
166: }
167:
168: /**
169: * @return the views in the button bar
170: */
171: public Collection getViews() {
172: LinkedList list = new LinkedList();
173: Iterator iter = m_views.iterator();
174: while (iter.hasNext()) {
175: ButtonBarView bbview = (ButtonBarView) iter.next();
176: list.add(bbview.getView());
177: }
178: return list;
179: }
180:
181: /**
182: * Removes a listener from the list of listeners that gets invoked when the
183: * user clicks on a button on the button bar.
184: */
185: public void removeListener(ActionListener listener) {
186: m_listeners.remove(listener);
187: }
188:
189: public void setCurrentView(Component view) {
190: if (view == m_currentview.getView())
191: return;
192:
193: Iterator iter = m_views.iterator();
194: while (iter.hasNext()) {
195: ButtonBarView bbview = (ButtonBarView) iter.next();
196: if (view == bbview.getView()) {
197: m_currentview = bbview;
198: updateView();
199: return;
200: }
201: }
202: }
203:
204: /**
205: * Updates all listeners that the view has changed.
206: */
207: private void updateListeners() {
208: ActionEvent evt = new ActionEvent(this ,
209: ActionEvent.ACTION_PERFORMED, "view.changed");
210: try {
211: Iterator iter = m_listeners.iterator();
212: while (iter.hasNext()) {
213: ActionListener listener = (ActionListener) iter.next();
214: listener.actionPerformed(evt);
215: }
216: } catch (Exception e) {
217:
218: }
219: }
220:
221: /**
222: * Updates the button bar
223: */
224: public void updateView() {
225: removeAll();
226:
227: if (m_currentview != null) {
228: add(m_currentview.getView(), BorderLayout.CENTER);
229:
230: m_currentview.getButton().setBackground(m_selected_color);
231: m_currentview.getButton().setBorder(m_selected_border);
232:
233: if (m_views.size() > 1) {
234: JPanel panel = new JPanel(new GridLayout(
235: m_views.size(), 1));
236: Iterator iter = m_views.iterator();
237: while (iter.hasNext()) {
238: ButtonBarView view = (ButtonBarView) iter.next();
239: panel.add(view.getButton());
240: if (view != m_currentview) {
241: view.getButton().setBackground(
242: UIManager.getColor("control"));
243: view.getButton().setBorder(m_unselected_border);
244: }
245: }
246: add(panel, BorderLayout.NORTH);
247: }
248: }
249:
250: revalidate();
251: repaint();
252: }
253:
254: private void updateSettings() {
255: Color c = UIManager.getColor("control");
256: m_selected_color = new Color(Math.max(
257: (int) (c.getRed() * FACTOR), 0), Math.max((int) (c
258: .getGreen() * FACTOR), 0), Math.max(
259: (int) (c.getBlue() * FACTOR), 0));
260:
261: m_unselected_border = BorderFactory.createCompoundBorder(
262: UIManager.getBorder("TableHeader.cellBorder"),
263: BorderFactory.createEmptyBorder(2, 2, 2, 2));
264:
265: m_selected_border = BorderFactory
266: .createCompoundBorder(
267: BorderFactory
268: .createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED),
269: BorderFactory.createEmptyBorder(2, 2, 2, 2));
270: }
271:
272: /**
273: * Override so we can update the components thar are not 'current'
274: */
275: public void updateUI() {
276: super .updateUI();
277:
278: if (m_views != null) {
279: updateSettings();
280:
281: Iterator iter = m_views.iterator();
282: while (iter.hasNext()) {
283: ButtonBarView view = (ButtonBarView) iter.next();
284: if (view != m_currentview) {
285: javax.swing.SwingUtilities
286: .updateComponentTreeUI(view.getView());
287: javax.swing.SwingUtilities
288: .updateComponentTreeUI(view.getButton());
289: view.getButton().setBackground(
290: UIManager.getColor("control"));
291: view.getButton().setBorder(m_unselected_border);
292: } else {
293: view.getButton().setBackground(m_selected_color);
294: view.getButton().setBorder(m_selected_border);
295: }
296: }
297: }
298: }
299:
300: /**
301: * Updates the button bar
302: */
303: public void updateView2() {
304: removeAll();
305:
306: if (m_currentview != null) {
307: add(m_currentview.getButton(), BorderLayout.NORTH);
308: add(m_currentview.getView(), BorderLayout.CENTER);
309: if (m_views.size() > 1) {
310: JPanel panel = new JPanel(new GridLayout(
311: m_views.size() - 1, 1));
312: Iterator iter = m_views.iterator();
313: while (iter.hasNext()) {
314: ButtonBarView view = (ButtonBarView) iter.next();
315: if (view != m_currentview) {
316: panel.add(view.getButton());
317: }
318: }
319: add(panel, BorderLayout.SOUTH);
320: }
321: }
322:
323: revalidate();
324: repaint();
325: }
326:
327: static class ButtonBarView {
328: /** the button that will display the view */
329: private JComponent m_btn;
330: /** the view associated with this button */
331: private JComponent m_view;
332:
333: /**
334: * ctor
335: */
336: public ButtonBarView(JComponent btn, JComponent view) {
337: m_btn = btn;
338: m_view = view;
339: }
340:
341: /**
342: * @return the button associated with this view
343: */
344: public JComponent getButton() {
345: return m_btn;
346: }
347:
348: /**
349: * @return the view that is displayed in the ButtonBar when the view is
350: * selected
351: */
352: public JComponent getView() {
353: return m_view;
354: }
355: }
356:
357: }
|