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

Quick Search    Search Deep

Source code: jcurses/widgets/Button.java


1   package jcurses.widgets;
2   
3   import jcurses.event.ActionListenerManager;
4   import jcurses.event.ActionListener;
5   import jcurses.event.ActionEvent;
6   
7   import jcurses.system.CharColor;
8   import jcurses.system.InputChar;
9   import jcurses.system.Toolkit;
10  
11  import jcurses.util.Protocol;
12  import jcurses.util.Rectangle;
13  
14  import java.util.Vector;
15  
16  /**
17  *  This class implements a buttton-widget. 
18  *  Such button has a label and is 'clicked' by user typing a special 
19  *  character (default 'enter'). If  it is 'clicked', it generates an
20  *  <code>ActionEvent</code>, that is delegetated to registered listeners.  
21  */
22  public class Button extends Widget {
23    
24   
25    
26    private ActionListenerManager _listenerManager = new ActionListenerManager();
27    
28    
29    private static CharColor __buttonDefaultColors = new CharColor(CharColor.WHITE, CharColor.BLACK);
30    
31    public CharColor getDefaultColors() {
32      return __buttonDefaultColors;
33    }
34    
35    
36    private String _label = null;
37    
38      /**
39      *  Sets button's label
40      * 
41      * @param label buttton's label
42      */
43    public void setLabel(String label) {
44      _label = label;
45      
46    }
47    
48    
49      /**
50      *  @return button's label
51      */
52    public String getLabel() {
53      return _label;
54    }
55    
56    
57    private static CharColor __focusedButtonDefaultColors = new CharColor(CharColor.BLUE, CharColor.WHITE, CharColor.REVERSE);
58    private CharColor _focusedButtonColors = getFocusedButtonDefaultColors();
59    
60    
61     
62    private CharColor getFocusedButtonDefaultColors() {
63      return __focusedButtonDefaultColors;
64    }
65    
66    /**
67      *  @return button's colors, if it is focused
68      */
69    public CharColor getFocusedButtonColors() {
70      return _focusedButtonColors;
71    }
72    
73    /**
74    *  Sets button's colors in focused state
75      * 
76      * @param colors  button's colors, if it is focused
77    */
78    public void setFocusedButtonColors(CharColor colors) {
79      _focusedButtonColors = colors;
80    }
81    
82    
83    private static CharColor __shortCutDefaultColors = new CharColor(CharColor.WHITE, CharColor.RED);
84    private CharColor _shortCutColors = getShortCutDefaultColors();
85    
86    
87    private CharColor getShortCutDefaultColors() {
88      return __shortCutDefaultColors;
89    }
90    
91    
92      /**
93      * @return colors button's shortcut char's colors
94      */
95    public CharColor getShortCutColors() {
96      return _shortCutColors;
97    }
98    
99    
100     /**
101     *  Sets button's shortcut char's colors. If the button has a shortcut char
102     * and this char is contained by the label, than the char within the label will
103     * be painted in different colors, set by this method
104     * 
105     * @param colors button's shortcut char's colors
106     */
107   public void setShortCutColors(CharColor colors) {
108     _shortCutColors = colors;
109   }
110   
111   /**
112   *  The constructor
113     * 
114     * @param button's label
115   */
116   public Button(String label) {
117     _label = label;
118   }
119   
120   
121   
122   protected Rectangle getPreferredSize() {
123     return new Rectangle(_label.length()+4,1);
124   }
125   
126   
127   protected void doPaint() {
128     Rectangle rect = getRectangle();
129     String text = "< "+_label+" >";
130     CharColor colors = hasFocus()?getFocusedButtonColors():getColors();
131     Toolkit.printString(text,rect, colors);
132     if (!hasFocus()) {
133       drawShortCutIfNeeded();
134     }
135   }
136   
137   
138   private void drawShortCutIfNeeded() {
139     InputChar shortCut = getShortCut();
140     if (shortCut != null) {
141       String c = shortCut.toString();
142       if (_label != null) {
143         int index = _label.toLowerCase().indexOf(c.toLowerCase());
144         if (index!=-1) {
145           String c1 = _label.substring(index,index+1);
146           Toolkit.printString(c1, getAbsoluteX()+index+2, getAbsoluteY(), getShortCutColors());
147         }
148       }
149     }
150   }
151   
152   
153   protected  Vector getShortCutsList() {
154     if (getShortCut() == null) {
155       return null;
156     }
157     Vector result = new Vector();
158     result.add(getShortCut());
159     return result;
160   }
161   
162   
163   protected boolean isFocusable() {
164     return true;
165   }
166   
167   
168   protected void doRepaint() {
169     doPaint();
170   }
171   
172   
173   private static InputChar __actionChar = new InputChar('\n');
174   
175   
176   protected boolean handleInput(InputChar ch) {
177     if ((ch.equals(__actionChar))||
178         ((getShortCut()!=null) && (getShortCut().equals(ch)))) {
179       doAction();
180       return true;
181     } 
182     
183     return false;
184   }
185   
186   
187   private void changeColors() {
188     CharColor colors = hasFocus()?getFocusedButtonColors():getColors();
189     Toolkit.changeColors(getRectangle(),colors);
190   }
191   
192   
193   protected void focus() {
194      changeColors();
195   }
196   
197   
198   protected void unfocus() {
199     changeColors();
200   }
201   
202   
203     /**
204     *  Adds a listener to the button.
205     * 
206     * @param listener listener to add
207     */
208   public void addListener(ActionListener listener) {
209     _listenerManager.addListener(listener);
210   }
211   
212   
213     /**
214     *   Removes a listener from the button.
215     * 
216     * @param listener listener to remove
217     */
218   public void removeListener(ActionListener listener) {
219     _listenerManager.removeListener(listener);
220   }
221   
222   
223   private void doAction() {
224     _listenerManager.handleEvent(new ActionEvent(this));
225   }
226   
227   
228   //Shortcut
229   
230   private InputChar _shortCut = null;
231   
232     /**
233     *  Set's button's shortcut char. If this shortcut is typed, than 
234     * the button will handle the char, as described by <code>Widget</code>, and generate
235     * an Event as whether the button would be 'clicked'.
236     */
237   public void setShortCut(char c) {
238     _shortCut = new InputChar(c);
239   }
240   
241   
242   private InputChar getShortCut() {
243     return _shortCut;
244   }
245   
246   
247   
248   
249   
250 
251 }