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.ledder;
21  import java.awt.Color;
22  import java.awt.Font;
23  import java.awt.event.MouseEvent;
24  import java.awt.event.MouseListener;
25  
26  import javax.swing.BorderFactory;
27  import javax.swing.Icon;
28  import javax.swing.JLabel;
29  import javax.swing.SwingConstants;
30  import javax.swing.SwingUtilities;
31  import javax.swing.border.BevelBorder;
32  
33  import javax.swing.border.Border;
34  import javax.swing.border.CompoundBorder;
35  import javax.swing.border.LineBorder;
36  
37  import com.cosylab.gui.components.util.PopupManager;
38  import com.cosylab.gui.components.AbstractDisplayerPanel;
39  
40  /**
41   * Visual representation of LED status. A LED is represented by icon indicating 
42   * current status and textual description. Status is defined by binary value of 
43   * true or false and each of the two states has user defined color associated.
44   * <p>
45   * This implementation attempts to create the icons using the LedFactory class.
46   * If icon for the desired color cannot be found, simple color representation
47   * will be used.
48   * 
49   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
50   * @version $id$
51  
52   * @see com.cosylab.gui.components.ledder.LedIconFactory
53   * @see com.cosylab.gui.components.ledder.DefaultLedIcon
54   */
55  public class Led extends JLabel {
56  
57  	// Icon representing the OFF state.
58  	private Icon iconWhenOff = null;
59  	// Icon representing the ON state.
60  	private Icon iconWhenOn = null;
61  
62  	// Flag indicating current state of the led.
63  	private boolean on = false;
64  	
65  	private CustomBorder border;
66  	private boolean editable = false;
67  
68  	
69  	
70  	/**
71  	 * Default constructor for Led component. 
72  	 * Creates a green/red led icon and "No description" label.
73  	 */
74  	public Led() {
75  		this(Color.GREEN, Color.RED);
76  		
77  	}
78  
79  	/**
80  	 * Switches between editable and read only mode. When editable, the led
81  	 * responds to mouse clicks.
82  	 * 
83  	 * @param editable
84  	 */
85  	public void setEditable(boolean editable){
86  	    this.editable=editable;
87  	    if (editable) {
88  	    	setBorder(getCustomBorder());
89  	    } else {
90  	    	setBorder(null);
91  	    }
92  	}
93  	/**
94  	 * Constructs new Led with user defined colors. 
95  	 * Description will be set to "No description".
96  	 * 
97  	 * @param colorWhenOn Color
98  	 * @param colorWhenOff Color
99  	 */
100 	public Led(Color colorWhenOn, Color colorWhenOff) {
101 	    
102 		this("< No description >", colorWhenOn, colorWhenOff);
103 	
104 	}
105 
106 	/**
107 	 * Constructs new Led with user defined description and colors.
108 	 * 
109 	 * @param description String
110 	 * @param colorWhenOn Color
111 	 * @param colorWhenOff Color
112 	 */
113 	public Led(String description, Color colorWhenOn, Color colorWhenOff) {
114 		super();
115 
116 		setInheritsPopupMenu(true);
117 		
118 		setFont(getFont().deriveFont(Font.PLAIN,12f));
119 
120 		setText(description);
121 		setVerticalAlignment(SwingConstants.CENTER);
122 		setVerticalTextPosition(SwingConstants.CENTER);
123 
124 		iconWhenOff = LedIconFactory.createIcon(colorWhenOff);
125 		iconWhenOn = LedIconFactory.createIcon(colorWhenOn);
126 
127 		updateIcon();
128 		setOpaque(false);		
129 	    
130 		addMouseListener(new MouseListener() {
131 		        
132 	        public void mouseClicked(MouseEvent e) {
133 	        	
134 	      
135 	        		
136 	        }
137 
138             public void mouseEntered(MouseEvent e) {
139             	if(editable){
140                 	getCustomBorder().setVisible(true);
141                 	repaint();
142                  }
143              }
144 
145             public void mouseExited(MouseEvent e) {
146             	if(editable){
147                 	getCustomBorder().setVisible(false);
148                 	repaint();
149                  }
150             }
151 
152             public void mousePressed(MouseEvent e) {
153             	if(SwingUtilities.isLeftMouseButton(e)){
154 	        		if(editable){
155 	        			if(isOn()){
156 	        				setOn(false);
157 	                
158 	        			}else {
159 	        				setOn(true);
160 	        			}     
161 	        		
162 	        		}
163             	}
164             }
165 
166             public void mouseReleased(MouseEvent e) {
167                 // TODO Auto-generated method stub
168                 
169             }
170 
171             
172           	});
173 		
174 		
175 	}
176 
177 	/**
178 	 * Returns current state of the led, true or false. Visual representation 
179 	 * of this state depends on the specified colors.
180 	 * 
181 	 * @return boolean
182 	 */
183 	public boolean isOn() {
184 		return on;
185 	}
186 
187 	/**
188 	 * Sets the state of the led. Visual representation of this state depends 
189 	 * on the specified colors.
190 	 * 
191 	 * @param value boolean
192 	 */
193 	public void setOn(boolean value) {
194 		on = value;
195 		updateIcon();
196 	}
197 
198 	/**
199 	 * Sets the apropriate icon based on the state of the led.
200 	 */
201 	private void updateIcon() {
202 		setIcon(on ? iconWhenOn : iconWhenOff);
203 	}
204 
205 	/**
206 	 * Sets the text of led and the tool tip to be displayed when the user
207 	 * holds the mouse above the led.
208 	 * 
209 	 * @param s String
210 	 */
211 	public void setText(String s) {
212 		super.setText(s);
213 		setToolTipText(s);
214 	}
215 
216 	/**
217 	 * Simple test method.
218 	 * 
219 	 * @param args String[]
220 	 */
221 	public static void main(String[] args) {
222 		javax.swing.JDialog dialog = new javax.swing.JDialog();
223 		dialog.setModal(true);
224 		dialog.setSize(200, 200);
225 
226 		dialog.getContentPane().setLayout(new java.awt.FlowLayout());
227 
228 		Led l = new Led();
229 		l.setText("Transmit");
230 		dialog.getContentPane().add(l);
231 
232 		l = new Led(Color.YELLOW, Color.BLUE);
233 		l.setText("Receive");
234 		dialog.getContentPane().add(l);
235 
236 		l = new Led("Status", Color.RED, Color.GREEN);
237 		l.setOn(false);
238 		dialog.getContentPane().add(l);
239 
240 		l = new Led(Color.blue, Color.blue);
241 		l.setText("On");
242 		dialog.getContentPane().add(l);
243 
244 		l = new Led(Color.GREEN, Color.RED);
245 		l.setText("Off");
246 		dialog.getContentPane().add(l);
247 
248 		l = new Led(Color.yellow, Color.blue);
249 		l.setText("Power");
250 		dialog.getContentPane().add(l);
251 
252 		dialog.show();
253 
254 		System.exit(0);
255 	}
256 
257 	/**
258 	 * @return Returns the border.
259 	 */
260 	private CustomBorder getCustomBorder() {
261 		if (border == null) {
262 			border = new CustomBorder();
263 			border.setVisible(false);
264 		}
265 
266 		return border;
267 	}
268 	
269 	/**
270 	 * Returns true if this led is editable. When editable, the led responds to
271 	 * the mouse clicks.
272 	 * @return
273 	 */
274 	public boolean isEditable() {
275 		return editable;
276 	}
277 	
278 	
279 }
280