Source code: jcurses/widgets/PopUpMenu.java
1 package jcurses.widgets;
2
3 import jcurses.util.Protocol;
4 import jcurses.event.ItemEvent;
5 import jcurses.event.ItemListener;
6 import jcurses.system.Toolkit;
7
8 /**
9 * This class implements a popup menu window. Such windows can be used
10 * for example to implemene menu bars ( currently not cantained in the library ).
11 * A popup menu window gives a user the possibility to select and ivoke an item from
12 * a list and is than closed. Separator items can be used as by <code>MenuList</code>
13 * described.
14 */
15 public class PopUpMenu implements WidgetsConstants, ItemListener {
16
17 private MenuList _menuList = new MenuList();
18 Dialog _peer = null;
19
20 private int _x = 0;
21 private int _y = 0;
22 String _title = null;
23
24 private int _selectedIndex = -1;
25 private String _selectedItem = null;
26
27 /**
28 * The constructor
29 *
30 * @param x the x coordinate of the dialog window's top left corner
31 * @param y the y coordinate of the dialog window's top left corner
32 * @param title window's title
33 */
34 public PopUpMenu(int x, int y, String title) {
35 _title = title;
36 _x=x;
37 _y=y;
38
39 }
40
41 /**
42 * Makes the window visible. Blocks, until the window is closed.
43 *
44 */
45 public void show() {
46 int width = _menuList.getPreferredSize().getWidth();
47 int height = _menuList.getPreferredSize().getHeight();
48
49 _peer = new Dialog(_x,_y,width,
50 height,
51 false,null);
52 GridLayoutManager manager1 = new GridLayoutManager(1,1);
53 _peer.getRootPanel().setLayoutManager(manager1);
54 manager1.addWidget(_menuList,0,0,1,1,ALIGNMENT_CENTER,ALIGNMENT_CENTER);
55 _menuList.addListener(this);
56 _peer.show();
57 }
58
59
60 /**
61 * Adds a separator item at the specified position
62 *
63 * @param index position
64 */
65 public void addSeparator(int index) {
66 _menuList.addSeparator(index);
67 }
68
69
70 /**
71 * Adds a separator at the end of the list.
72 */
73 public void addSeparator() {
74 _menuList.addSeparator();
75 }
76
77
78 /**
79 * Adds an item at the specified position
80 *
81 * @param item item to add
82 * @param pos position
83 */
84 public void add(int pos, String item) {
85 _menuList.add(pos, item);
86 }
87
88
89 /**
90 * Adds an item at the end of the list.
91 */
92 public void add(String item) {
93 _menuList.add(item);
94 }
95
96
97 /**
98 * @return the number of items
99 */
100 public int getItemsCount() {
101 return _menuList.getItemsCount();
102 }
103
104
105 /**
106 * @return the item at the specified position
107 */
108 public String getItem(int index) {
109 return (String)_menuList.getItem(index);
110 }
111
112
113
114 /**
115 * Removes the item at the specified position
116 *
117 * @param pos position
118 */
119 public void remove(int pos) {
120 _menuList.remove(pos);
121 }
122
123 /**
124 * Removes the first ocuurence of the specified item
125 *
126 * @param item item to be removed
127 *
128 */
129 public void remove(String item) {
130 _menuList.remove(item);
131 }
132
133
134 public void stateChanged(ItemEvent e) {
135 _selectedIndex = e.getId();
136 _selectedItem = (String) e.getItem();
137 _menuList.removeListener(this);
138 _peer.close();
139
140 }
141
142 /**
143 * Returns the last selected index. Should be invoked after the return of the <code>show</code>
144 * to get the result
145 *
146 * @return last selected index
147 */
148 public int getSelectedIndex() {
149 return _selectedIndex;
150 }
151
152 /**
153 * Returns the last selected item. Should be invoked after the return of the <code>show</code>
154 * to get the result
155 *
156 * @return last selected index
157 */
158 public String getSelectedItem() {
159 return _selectedItem;
160 }
161
162
163
164
165 }