KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > whiteboard > gui > PenButton


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.whiteboard.gui;
20
21 import java.awt.Dimension JavaDoc;
22
23 import javax.swing.ImageIcon JavaDoc;
24 import javax.swing.JToggleButton JavaDoc;
25
26 import org.lucane.applications.whiteboard.WhiteBoard;
27
28 public class PenButton extends JToggleButton JavaDoc
29 {
30     public static final int STATE_INACTIVE = 0;
31     public static final int STATE_ACTIVE = 1;
32     public static final int STATE_QUEUED = 2;
33     
34     private ImageIcon JavaDoc inactiveIcon;
35     private ImageIcon JavaDoc activeIcon;
36     private ImageIcon JavaDoc queuedIcon;
37     
38     private int state;
39     private String JavaDoc user;
40     private int queueSize;
41     
42     private Thread JavaDoc blinker = null;
43     
44     public PenButton(WhiteBoard plugin)
45     {
46         this.inactiveIcon = plugin.getImageIcon("pen_inactive.png");
47         this.activeIcon = plugin.getImageIcon("pen_active.png");
48         this.queuedIcon = plugin.getImageIcon("pen_queued.png");
49         
50         this.setPreferredSize(new Dimension JavaDoc(100, 100));
51         setState(STATE_INACTIVE);
52     }
53     
54     private void startBlinking()
55     {
56         final PenButton button = this;
57         
58         if(blinker == null)
59         {
60             blinker = new Thread JavaDoc() {
61                 
62                 public void run() {
63                     while(queueSize > 1)
64                     {
65                         button.setSelected(!button.isSelected());
66                         try {
67                             Thread.sleep(1000/queueSize);
68                         } catch (InterruptedException JavaDoc e) {
69                             break;
70                         }
71                     }
72                 }
73             };
74             blinker.start();
75         }
76     }
77     
78     private void stopBlinking()
79     {
80         if(this.blinker != null)
81         {
82             this.blinker.interrupt();
83             this.blinker = null;
84         }
85         setSelected(state != STATE_INACTIVE);
86     }
87     
88     public void setUser(String JavaDoc user)
89     {
90         this.user = user;
91         this.setText(user);
92     }
93     
94     public void setQueueSize(int queueSize)
95     {
96         this.queueSize = queueSize;
97         if(queueSize > 1)
98             startBlinking();
99         else
100             stopBlinking();
101     }
102     
103     public void setState(int state)
104     {
105         setQueueSize(0);
106         
107         this.state = state;
108         if(isInactive())
109         {
110             this.setSelected(false);
111             this.setIcon(this.inactiveIcon);
112         }
113         else if(isActive())
114         {
115             this.setSelected(true);
116             this.setIcon(this.activeIcon);
117         }
118         else if(isQueued())
119         {
120             this.setSelected(true);
121             this.setIcon(this.queuedIcon);
122         }
123     }
124     
125     public boolean isActive()
126     {
127         return this.state == STATE_ACTIVE;
128     }
129     
130     public boolean isInactive()
131     {
132         return this.state == STATE_INACTIVE;
133     }
134     
135     public boolean isQueued()
136     {
137         return this.state == STATE_QUEUED;
138     }
139     
140     public String JavaDoc getUser()
141     {
142         return this.user;
143     }
144 }
Popular Tags