View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.wheelswitch;
21  
22  import java.awt.Color;
23  import java.awt.Dimension;
24  import java.awt.event.MouseListener;
25  
26  import javax.swing.JComponent;
27  import javax.swing.event.EventListenerList;
28  
29  import com.cosylab.gui.components.ArrowButton;
30  import com.cosylab.gui.components.util.ColorHelper;
31  
32  /**
33   * Descedant of <code>javax.swing.JComponent</code> that contains two 
34   * <code>ArrowButton</code>s acting as a two-way (up/down) control. 
35   * 
36   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
37   * @version $id$
38   */
39  public class UpDownButton extends JComponent {
40  
41  	private static final long serialVersionUID = 1L;
42  
43  	private ArrowButton down;
44  
45  	private ArrowButton up;
46  	
47  	private Color foregroundColor = Color.BLACK;
48  	
49  	private int outOfBounds = 0;
50  
51  	/**
52  	 * Constructor for UpDownButton.
53  	 */			
54  	public UpDownButton() {
55  		super();
56  		listenerList = new EventListenerList();	
57  		setLayout(new UpDownLayout());
58  		add(getUpButton());
59  		add(getDownButton());
60  	}		
61  
62    /**
63     * This method was overridden to implement correct 
64     * <code>UpDownButton<code> placement inside the 
65     * <code>Wheelswitch</code> container.
66     * 
67     * @see JComponent#getPreferredSize()
68     */
69  	public Dimension getPreferredSize() {
70  		return new Dimension(12,24);
71  	}
72  	
73    /**
74     * This method was overridden to implement correct 
75     * <code>UpDownButton<code> placement inside the 
76     * <code>Wheelswitch</code> container.
77     * 
78     * @see JComponent#getMinimumSize()
79     */
80  	public Dimension getMinimumSize() {
81  		return new Dimension(12,24);
82  	}	
83  	/* (non-Javadoc)
84  	 * @see java.awt.Component#setEnabled(boolean)
85  	 */
86  	public void setEnabled(boolean arg0) {
87  		super.setEnabled(arg0);
88  		getDownButton().setEnabled(arg0);
89  		getUpButton().setEnabled(arg0);
90  	}
91  
92  	/**
93  	 * Sets this button to out of bounds state. When in such state the button
94  	 * will be painted with alarm color foreground.
95  	 * 
96  	 * @param i
97  	 */
98  	public void setOutOfBounds(int i) {
99  		outOfBounds = i;
100 		if (i>0) {
101 			getUpButton().setEnabled(false);
102 			if (isEnabled())getDownButton().setEnabled(true);
103 			getUpButton().setForeground(foregroundColor);
104 			getDownButton().setForeground(ColorHelper.getAlarmOutline());
105 		} else if (i<0) {
106 			if (isEnabled())getUpButton().setEnabled(true);
107 			getDownButton().setEnabled(false);
108 			getUpButton().setForeground(ColorHelper.getAlarmOutline());
109 			getDownButton().setForeground(foregroundColor);
110 		} else {
111 			if (isEnabled()) {
112 				getUpButton().setEnabled(true);
113 				getDownButton().setEnabled(true);
114 			}
115 			getUpButton().setForeground(foregroundColor);
116 			getDownButton().setForeground(foregroundColor);
117 		}
118 	}
119 	
120 	/**
121 	 * Laziliy initializes the up arrow button and returns it.
122 	 * 
123 	 * @return up button
124 	 */
125 	public ArrowButton getUpButton() {
126 		if (up==null) {
127 			up = new ArrowButton(ArrowButton.UP);
128 			up.setFocusable(false);
129 			up.setBackground(ColorHelper.getCosyControl());
130 		}
131 
132 		return up;
133 	}
134 	
135 	/**
136 	 * Laziliy initializes the down arrow button and returns it.
137 	 * 
138 	 * @return down button
139 	 */
140 	public ArrowButton getDownButton() {
141 		if (down==null) {
142 			down = new ArrowButton(ArrowButton.DOWN);
143 			down.setFocusable(false);
144 			down.setBackground(ColorHelper.getCosyControl());
145 		}
146 		
147 		return down;
148 	}
149 	
150 	/*
151 	 * (non-Javadoc)
152 	 * @see javax.swing.JComponent#setBackground(java.awt.Color)
153 	 */
154 	@Override
155 	public void setBackground(Color bg) {
156 		super.setBackground(bg);
157 		getUpButton().setBackground(bg);
158 		getDownButton().setBackground(bg);
159 	}
160 	
161 	/*
162 	 * (non-Javadoc)
163 	 * @see javax.swing.JComponent#setForeground(java.awt.Color)
164 	 */
165 	@Override
166 	public void setForeground(Color fg) {
167 		super.setForeground(fg);
168 		foregroundColor = fg;
169 		setOutOfBounds(outOfBounds);
170 	}
171 	
172 	@Override
173 	public synchronized void addMouseListener(MouseListener l) {
174 		super.addMouseListener(l);
175 		getUpButton().addMouseListener(l);
176 		getDownButton().addMouseListener(l);
177 	}
178 
179 }
180