001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: ScrollButtonBox.java,v 1.17 2005/12/04 13:46:04 jesper Exp $
023: package net.infonode.gui;
024:
025: import net.infonode.gui.icon.button.ArrowIcon;
026: import net.infonode.gui.layout.DirectionLayout;
027: import net.infonode.gui.panel.SimplePanel;
028: import net.infonode.util.Direction;
029:
030: import javax.swing.*;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.awt.event.MouseWheelEvent;
034: import java.awt.event.MouseWheelListener;
035: import java.util.ArrayList;
036:
037: public class ScrollButtonBox extends SimplePanel {
038: private AbstractButton upButton;
039: private AbstractButton downButton;
040: private AbstractButton leftButton;
041: private AbstractButton rightButton;
042:
043: private boolean button1Enabled;
044: private boolean button2Enabled;
045:
046: private boolean vertical;
047:
048: private ArrayList listeners;
049:
050: private ActionListener button1Listener = new ActionListener() {
051: public void actionPerformed(ActionEvent e) {
052: fireButton1();
053: }
054: };
055:
056: private ActionListener button2Listener = new ActionListener() {
057: public void actionPerformed(ActionEvent e) {
058: fireButton2();
059: }
060: };
061:
062: public ScrollButtonBox(boolean vertical, int iconSize) {
063: this (vertical, ButtonFactory.createFlatHighlightButton(
064: new ArrowIcon(iconSize, Direction.UP), "", 0, null),
065: ButtonFactory.createFlatHighlightButton(new ArrowIcon(
066: iconSize, Direction.DOWN), "", 0, null),
067: ButtonFactory.createFlatHighlightButton(new ArrowIcon(
068: iconSize, Direction.LEFT), "", 0, null),
069: ButtonFactory.createFlatHighlightButton(new ArrowIcon(
070: iconSize, Direction.RIGHT), "", 0, null));
071: }
072:
073: public ScrollButtonBox(final boolean vertical,
074: AbstractButton upButton, AbstractButton downButton,
075: AbstractButton leftButton, AbstractButton rightButton) {
076: this .vertical = vertical;
077: setLayout(new DirectionLayout(vertical ? Direction.DOWN
078: : Direction.RIGHT));
079:
080: addMouseWheelListener(new MouseWheelListener() {
081: public void mouseWheelMoved(MouseWheelEvent e) {
082: if (e.getWheelRotation() < 0)
083: fireButton1();
084: else
085: fireButton2();
086: }
087: });
088:
089: setButtons(upButton, downButton, leftButton, rightButton);
090: }
091:
092: public void setButton1Enabled(boolean enabled) {
093: this .button1Enabled = enabled;
094: if (getComponentCount() > 0)
095: ((AbstractButton) getComponent(0)).setEnabled(enabled);
096: }
097:
098: public void setButton2Enabled(boolean enabled) {
099: this .button2Enabled = enabled;
100: if (getComponentCount() > 0)
101: ((AbstractButton) getComponent(1)).setEnabled(enabled);
102: }
103:
104: public boolean isButton1Enabled() {
105: return button1Enabled;
106: }
107:
108: public boolean isButton2Enabled() {
109: return button2Enabled;
110: }
111:
112: public void addListener(ScrollButtonBoxListener listener) {
113: if (listeners == null)
114: listeners = new ArrayList(2);
115:
116: listeners.add(listener);
117: }
118:
119: public void removeListener(ScrollButtonBoxListener listener) {
120: if (listeners != null) {
121: listeners.remove(listener);
122:
123: if (listeners.size() == 0)
124: listeners = null;
125: }
126: }
127:
128: public boolean isVertical() {
129: return vertical;
130: }
131:
132: public void setVertical(boolean vertical) {
133: if (vertical != this .vertical) {
134: this .vertical = vertical;
135: initialize();
136: //update();
137: }
138: }
139:
140: public void setButtons(AbstractButton upButton,
141: AbstractButton downButton, AbstractButton leftButton,
142: AbstractButton rightButton) {
143: if (upButton != this .upButton || downButton != this .downButton
144: || leftButton != this .leftButton
145: || rightButton != this .rightButton) {
146: this .upButton = upButton;
147: this .downButton = downButton;
148: this .leftButton = leftButton;
149: this .rightButton = rightButton;
150:
151: initialize();
152: }
153: }
154:
155: public AbstractButton getUpButton() {
156: return upButton;
157: }
158:
159: public AbstractButton getDownButton() {
160: return downButton;
161: }
162:
163: public AbstractButton getLeftButton() {
164: return leftButton;
165: }
166:
167: public AbstractButton getRightButton() {
168: return rightButton;
169: }
170:
171: /* public void updateUI() {
172: super.updateUI();
173:
174: if (button1 != null) {
175: update();
176: }
177: }
178: */
179: private void fireButton1() {
180: if (listeners != null) {
181: Object[] l = listeners.toArray();
182:
183: for (int i = 0; i < l.length; i++)
184: ((ScrollButtonBoxListener) l[i]).scrollButton1();
185: }
186: }
187:
188: private void fireButton2() {
189: if (listeners != null) {
190: Object[] l = listeners.toArray();
191:
192: for (int i = 0; i < l.length; i++)
193: ((ScrollButtonBoxListener) l[i]).scrollButton2();
194: }
195: }
196:
197: private void initialize() {
198: if (getComponentCount() > 0) {
199: ((AbstractButton) getComponent(0))
200: .removeActionListener(button1Listener);
201: ((AbstractButton) getComponent(1))
202: .removeActionListener(button2Listener);
203: removeAll();
204: }
205:
206: ((DirectionLayout) getLayout())
207: .setDirection(vertical ? Direction.DOWN
208: : Direction.RIGHT);
209:
210: AbstractButton button1;
211: AbstractButton button2;
212:
213: if (vertical) {
214: button1 = upButton;
215: button2 = downButton;
216: } else {
217: button1 = leftButton;
218: button2 = rightButton;
219: }
220:
221: if (button1 != null && button2 != null) {
222: add(button1);
223: add(button2);
224:
225: button1.setFocusable(false);
226: button2.setFocusable(false);
227:
228: button1.setEnabled(button1Enabled);
229: button2.setEnabled(button2Enabled);
230:
231: button1.addActionListener(button1Listener);
232: button2.addActionListener(button2Listener);
233: }
234:
235: if (getParent() != null)
236: ComponentUtil.validate(getParent());
237:
238: //update();
239: }
240:
241: /* private void update() {
242: Color c1 = UIManager.getColor("Button.foreground");
243: Color c2 = UIManager.getColor("Button.disabledForeground");
244:
245: if (c2 == null)
246: c2 = ColorUtil.blend(c1, UIManager.getColor("Panel.background"), 0.7f);
247:
248: button1.setIcon(new ArrowIcon(c1, iconSize, vertical ? Direction.UP : Direction.LEFT));
249: button2.setIcon(new ArrowIcon(c1, iconSize, vertical ? Direction.DOWN : Direction.RIGHT));
250:
251: ArrowIcon icon = new ArrowIcon(c2, iconSize - 2, vertical ? Direction.UP : Direction.LEFT);
252: icon.setShadowEnabled(false);
253: button1.setDisabledIcon(new BorderIcon(icon, 1));
254:
255: icon = new ArrowIcon(c2, iconSize - 2, vertical ? Direction.DOWN : Direction.RIGHT);
256: icon.setShadowEnabled(false);
257: button2.setDisabledIcon(new BorderIcon(icon, 1));
258: }*/
259: }
|