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

Quick Search    Search Deep

Source code: jcurses/widgets/PopUpList.java


1   package jcurses.widgets;
2   
3   import jcurses.event.ValueChangedListenerManager;
4   import jcurses.event.ValueChangedListener;
5   import jcurses.event.ValueChangedEvent;
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 popup list. 
18  * Such list has always one of the items selected and gives the possibility to change
19  * this selection ( througth an popup menu that is shown, if the user typed 'enter')
20  * 
21  */
22  public class PopUpList extends Widget {
23    
24   
25    private int _selectedIndex = -1;
26    Vector _items = new Vector();
27    
28    private ValueChangedListenerManager _listenerManager = new ValueChangedListenerManager();
29    
30    
31    private static CharColor __popUpDefaultColors = new CharColor(CharColor.WHITE, CharColor.BLACK);
32    
33    public CharColor getDefaultColors() {
34      return __popUpDefaultColors;
35    }
36    
37    
38    
39    private static CharColor __focusedPopUpDefaultColors = new CharColor(CharColor.BLUE, CharColor.WHITE, CharColor.REVERSE);
40    private CharColor _focusedPopUpColors = getFocusedPopUpDefaultColors();
41    
42      
43    private CharColor getFocusedPopUpDefaultColors() {
44      return __focusedPopUpDefaultColors;
45    }
46    
47    
48      /**
49    *  @raturn colors used to paint the widget, if it has focus
50      * 
51    */
52    public CharColor getFocusedPopUpColors() {
53      return _focusedPopUpColors;
54    }
55    
56    /**
57    *  Sets colors used to paint the widget, if it has focus
58      * 
59      * @param colors colors used to paint the widget, if it has focus
60    */
61    public void setFocusedPopUpColors(CharColor colors) {
62      _focusedPopUpColors = colors;
63    }
64    
65    
66    public PopUpList() {
67    }
68    
69    /**
70    *  Adds an item
71      * 
72      * @param item the item to add
73    */
74    public void add(String item) {
75      _items.add(item);
76    }
77    
78    
79      /**
80    *  Adds an item at the specified position
81      * 
82      * @param item the item to add
83      * @param pos position 
84    */
85    public void add(int pos, String item) {
86      _items.add(pos, item);
87    }
88    
89    
90      /**
91      * Removes the first ocuurence of the specified item
92      * 
93      * @param item item to be removed
94      * 
95      */
96    public void remove(String item) {
97      _items.remove(item);
98    }
99    
100   
101     /**
102     *  Removes the item at the specified position
103     * 
104     * @param pos position
105     */
106   public void remove(int pos) {
107     _items.remove(pos);
108   }
109   
110   
111     /**
112     *  Clears the item list
113     */
114   public void clear() {
115     _items.clear();
116   }
117   
118   
119     /**
120     *  Returns the currently selected index
121     * 
122     * @return currently selected index
123     */
124   public int getSelectedIndex() {
125     if (_selectedIndex!=-1) {
126       return _selectedIndex;
127     } else {
128       if (_items.size()>0) {
129         return 0;
130       } else {
131         return -1;
132       }
133     }
134   }
135   
136     /**
137     *  Returns the currently selected item
138     * 
139     * @return currently selected item
140     */
141   public String getSelectedItem() {
142     if (getSelectedIndex()>=0) {
143       return (String)_items.elementAt(getSelectedIndex());
144     } else {
145       return null;
146     }
147   }
148   
149   
150   protected Rectangle getPreferredSize() {
151     return new Rectangle(2+getMaxLength(),1);
152   }
153   
154   
155   private int getMaxLength() {
156     int result = 0;
157     for (int i=0; i<_items.size(); i++) {
158       String item = (String)_items.elementAt(i);
159       if (item.length() > result) {
160         result = item.length();
161       }
162     }
163     
164     return result;
165   }
166   
167   
168   private String getText() {
169     String result = null;
170     int length = getSize().getWidth()-2;
171     String item = (getSelectedItem()==null)?"":getSelectedItem();
172     if (item.length()>length) {
173       result = item.substring(0,length);
174     } else {
175       StringBuffer buf = new StringBuffer();
176       buf.append(item);
177       for (int i=0; i<(length-item.length()); i++) {
178         buf.append(' ');
179       }
180       result = buf.toString();
181     }
182     
183     
184     return result;
185   }
186   
187   
188   protected void doPaint() {
189     Rectangle rect = (Rectangle)getSize().clone();
190     rect.setLocation(getAbsoluteX(), getAbsoluteY());
191     String text = "["+getText()+"]";
192     CharColor colors = hasFocus()?getFocusedPopUpColors():getColors();
193     Toolkit.printString(text,rect, colors);
194   }
195   
196   
197   protected boolean isFocusable() {
198     return true;
199   }
200   
201   
202   protected void doRepaint() {
203     doPaint();
204   }
205   
206   
207   private static InputChar __changeValueChar = new InputChar('\n');
208   
209   
210   protected boolean handleInput(InputChar ch) {
211     if (ch.equals(__changeValueChar)) {
212       if (_items.size()>2) {
213         PopUpMenu menu = new PopUpMenu(getAbsoluteX(),getAbsoluteY(),null);
214         for (int i=0; i<_items.size(); i++) {
215           menu.add((String)_items.elementAt(i));
216         }
217         menu.show();
218         if ((menu.getSelectedIndex()!=-1) && (menu.getSelectedIndex()!=getSelectedIndex())) {
219           _selectedIndex = menu.getSelectedIndex();
220           paint();
221           _listenerManager.handleEvent(new ValueChangedEvent(this));
222           
223         }
224       }
225         
226       return true;
227     }
228     
229     return false;
230   }
231   
232   
233   protected void focus() {
234      paint();
235   }
236   
237   
238   protected void unfocus() {
239     paint();
240   }
241   
242   
243     /**
244     * Adds a listener to register selected value changes
245     * 
246     */
247   public void addListener(ValueChangedListener listener) {
248     _listenerManager.addListener(listener);
249   }
250   
251   
252     /**
253     * Removes a listener
254     * 
255     */
256   public void removeListener(ValueChangedListener listener) {
257     _listenerManager.removeListener(listener);
258   }
259   
260   
261   
262   
263   
264 
265 }