1 19 package org.lucane.applications.whiteboard.gui; 20 21 import java.awt.Dimension ; 22 23 import javax.swing.ImageIcon ; 24 import javax.swing.JToggleButton ; 25 26 import org.lucane.applications.whiteboard.WhiteBoard; 27 28 public class PenButton extends JToggleButton 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 inactiveIcon; 35 private ImageIcon activeIcon; 36 private ImageIcon queuedIcon; 37 38 private int state; 39 private String user; 40 private int queueSize; 41 42 private Thread 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 (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 () { 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 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 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 getUser() 141 { 142 return this.user; 143 } 144 } | Popular Tags |