Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jcurses/widgets/CheckBox.java


1   
2   package jcurses.widgets;
3   
4   import jcurses.event.ValueChangedListenerManager;
5   import jcurses.event.ValueChangedListener;
6   import jcurses.event.ValueChangedEvent;
7   
8   import jcurses.system.CharColor;
9   import jcurses.system.InputChar;
10  import jcurses.system.Toolkit;
11  
12  import jcurses.util.Protocol;
13  import jcurses.util.Rectangle;
14  
15  import java.util.StringTokenizer;
16  
17  /**
18  *  This class implements a checkbox widget. This checkboxes state is modified
19  * by typing a special char (default 'space'). You can register listeners by this
20  * widget to track state changes.
21  */
22  public class CheckBox extends Widget {
23    
24   
25    private boolean _checked = false; 
26    
27    private ValueChangedListenerManager _listenerManager = new ValueChangedListenerManager();
28    
29    
30    private static CharColor __checkBoxDefaultColors = new CharColor(CharColor.WHITE, CharColor.BLACK);
31    
32    public CharColor getDefaultColors() {
33      return __checkBoxDefaultColors;
34    }
35    
36    
37    
38    private static CharColor __focusedCheckBoxDefaultColors = new CharColor(CharColor.BLUE, CharColor.WHITE, CharColor.REVERSE);
39    private CharColor _focusedCheckboxColors = getFocusedCheckboxDefaultColors();
40    
41    
42    private CharColor getFocusedCheckboxDefaultColors() {
43      return __focusedCheckBoxDefaultColors;
44    }
45    
46    /**
47      *  @return checkboxes colors, if it is focused
48      */
49    public CharColor getFocusedCheckboxColors() {
50      return _focusedCheckboxColors;
51    }
52    
53    
54      /**
55      *  Sets colors of the checkbox in focused state.
56      * 
57      *  @param colors checkboxes colors, if it is focused
58      */
59    public void setFocusedCheckboxColors(CharColor colors) {
60      _focusedCheckboxColors = colors;
61    }
62    
63    
64      /**
65      *  The constructor.
66      * 
67      * @param checked true, if the checkbox is checked at first time, false otherwise
68      */
69    public CheckBox(boolean checked) {
70      _checked = checked;
71    }
72    
73    
74      /**
75      *  The constructor creates an unchecked checkbox
76      */
77    public CheckBox() {
78      this(false);
79      
80    }
81    
82      
83    /**
84    *  return  true, if the checkbox is checked , false otherwise
85    */
86    public boolean getValue() {
87      return _checked;
88    }
89    
90      
91    /**
92    *  Sets checkboxes value
93      * 
94      * @param value if the checkbox becomes checked , false otherwise 
95    */
96    public void setValue(boolean value) {
97      boolean oldValue = _checked;
98      _checked = value;
99      if (oldValue != _checked) {
100       _listenerManager.handleEvent(new ValueChangedEvent(this));
101     }
102     paint();
103   }
104   
105   
106   
107   
108   
109   protected Rectangle getPreferredSize() {
110     return new Rectangle(3,1);
111   }
112   
113   
114   protected void doPaint() {
115     Rectangle rect = (Rectangle)getSize().clone();
116     rect.setLocation(getAbsoluteX(), getAbsoluteY());
117     String text = "["+((_checked)?"X":" ")+"]";
118     CharColor colors = hasFocus()?getFocusedCheckboxColors():getColors();
119     Toolkit.printString(text,rect, colors);
120   }
121   
122   
123   protected boolean isFocusable() {
124     return true;
125   }
126   
127   
128   protected void doRepaint() {
129     doPaint();
130   }
131   
132   
133   private static InputChar __changeStatusChar = new InputChar(' ');
134   
135   
136   protected boolean handleInput(InputChar ch) {
137     if (ch.equals(__changeStatusChar)) {
138       setValue((_checked)?false:true);
139       paint();
140       return true;
141     }
142     
143     return false;
144   }
145   
146   
147   private void changeColors() {
148     CharColor colors = hasFocus()?getFocusedCheckboxColors():getColors();
149     Toolkit.changeColors(getRectangle(),colors);
150   }
151   
152   
153   protected void focus() {
154      changeColors();
155   }
156   
157   
158   protected void unfocus() {
159     changeColors();
160   }
161   
162   
163     /**
164     *  Adds listener to the checkbox to track states changes
165     * 
166     * @param listener listener to add
167     */
168   public void addListener(ValueChangedListener listener) {
169     _listenerManager.addListener(listener);
170   }
171   
172   
173     /**
174     *  Removes listener from the checkbox
175     * 
176     * @param listener to remove
177     */
178   public void removeListener(ValueChangedListener listener) {
179     _listenerManager.removeListener(listener);
180   }
181   
182   
183   
184   
185   
186 
187 }