001: package org.romaframework.aspect.view.echo2.component;
002:
003: import java.util.HashMap;
004: import java.util.LinkedList;
005: import java.util.List;
006: import java.util.Map;
007:
008: import nextapp.echo2.app.Button;
009: import nextapp.echo2.app.Color;
010: import nextapp.echo2.app.Column;
011: import nextapp.echo2.app.Component;
012: import nextapp.echo2.app.Extent;
013: import nextapp.echo2.app.Insets;
014: import nextapp.echo2.app.event.ActionEvent;
015: import nextapp.echo2.app.event.ActionListener;
016: import nextapp.echo2.app.event.ListDataEvent;
017: import nextapp.echo2.app.event.ListDataListener;
018: import nextapp.echo2.app.list.DefaultListModel;
019: import nextapp.echo2.app.list.ListModel;
020:
021: import org.romaframework.aspect.view.echo2.GUIEventHandler;
022: import org.romaframework.aspect.view.echo2.form.FormUtil;
023:
024: import echopointng.BorderEx;
025: import echopointng.ButtonEx;
026: import echopointng.ContainerEx;
027: import echopointng.DropDown;
028: import echopointng.ExtentEx;
029: import echopointng.LabelEx;
030: import echopointng.able.Scrollable;
031: import echopointng.layout.ScrollableDisplayLayoutData;
032:
033: public class DynaSelect extends DropDown implements SelectComponent {
034: private static final int POPUP_HEIGHT_OVER = 2;
035:
036: private static final int DEF_FIELD_WIDTH = 150;
037:
038: private static final int BUTTON_CONTAINER_OFFSET = 15;
039:
040: private static final Color DEF_ITEM_COLOR = Color.WHITE;
041:
042: private static final String DEF_CELL_SPACING = "0px";
043:
044: private static final int MAX_DISPLAY_ELEMENTS = 6;
045:
046: private static final int ITEM_SIZE = 18;
047:
048: /** The list of items displayed in the select field drop down */
049: private ListModel listModel = null;
050:
051: /** A map from the buttons in the popup box to the items in the select field */
052: private Map<Button, Object> btn2itemMap = new HashMap<Button, Object>();
053:
054: /** The action listeners for this component */
055: private List<ActionListener> lsnrList = new LinkedList<ActionListener>();
056:
057: /** Currently selected item in the list */
058: private Object selectedItem;
059:
060: /** The popeup box for the DropDown component */
061: private ContainerEx popupBox;
062:
063: /** The column containing the list of all items */
064: private Column itemsContainer;
065:
066: /** The target component where the current selection is shown */
067: private LabelEx target;
068:
069: public DynaSelect() {
070: listModel = new DefaultListModel();
071: initComponent();
072: }
073:
074: /**
075: * Create a SelectField from the array of objects passed to this constructor.
076: * The SelectField internally creates a DefaultListModel from the array of
077: * objects.
078: *
079: * @param items
080: * the array of items displayed as options by the select field.
081: */
082: public DynaSelect(Object[] items) {
083: listModel = new DefaultListModel();
084: for (Object item : items) {
085: ((DefaultListModel) listModel).add(item);
086: }
087: initComponent();
088: }
089:
090: public DynaSelect(List items) {
091: listModel = new DefaultListModel();
092: for (Object item : items) {
093: ((DefaultListModel) listModel).add(item);
094: }
095: initComponent();
096: }
097:
098: public DynaSelect(ListModel listModel) {
099: this .listModel = listModel;
100: initComponent();
101: }
102:
103: private void initComponent() {
104: setTarget(createTarget());
105:
106: itemsContainer = new Column();
107: itemsContainer.setCellSpacing(new ExtentEx(DEF_CELL_SPACING));
108: itemsContainer.setInsets(new Insets(0));
109:
110: ScrollableDisplayLayoutData scrollableDisplayLayoutData = new ScrollableDisplayLayoutData();
111: scrollableDisplayLayoutData.setWidth(target.getWidth());
112: scrollableDisplayLayoutData.setScrollBarPolicy(Scrollable.AUTO);
113:
114: itemsContainer.setLayoutData(scrollableDisplayLayoutData);
115:
116: popupBox = new ContainerEx();
117: popupBox.setScrollBarPolicy(Scrollable.AUTO);
118: popupBox.add(itemsContainer);
119: setPopUpAlwaysOnTop(true);
120:
121: setPopUp(popupBox);
122: updateItemsContainer();
123:
124: addListenerToListModel();
125:
126: // Select the first item
127: if (listModel.size() > 0)
128: setSelectedItem(listModel.get(0));
129:
130: setProperty(FormUtil.METADATA_COMPONENT_OWNER, this );
131: }
132:
133: /**
134: * Add listener for list data model
135: */
136: private void addListenerToListModel() {
137: listModel.addListDataListener(new ListDataListener() {
138: public void contentsChanged(ListDataEvent listDataEvent) {
139: updateItemsContainer();
140: }
141:
142: public void intervalAdded(ListDataEvent listDataEvent) {
143: updateItemsContainer();
144: }
145:
146: public void intervalRemoved(ListDataEvent listDataEvent) {
147: updateItemsContainer();
148: }
149: });
150: }
151:
152: private void updateItemsContainer() {
153: btn2itemMap.clear();
154: itemsContainer.removeAll();
155: String itemContent;
156: for (int i = 0; i < listModel.size(); i++) {
157: itemContent = listModel.get(i).toString();
158: final ButtonEx btn = new ButtonEx(itemContent);
159: btn.setBorder(new BorderEx(1, DEF_ITEM_COLOR));
160: btn.setBackground(DEF_ITEM_COLOR);
161: btn.setOutsets(new Insets(0));
162: btn.setWidth(new Extent(target.getWidth().getValue()
163: - BUTTON_CONTAINER_OFFSET));
164: if (itemContent.length() == 0)
165: btn.setHeight(new Extent(ITEM_SIZE));
166:
167: btn.setStyleName("SelectFieldButton");
168: final Object item = listModel.get(i);
169: btn2itemMap.put(btn, listModel.get(i));
170: btn.addActionListener(new ActionListener() {
171: public void actionPerformed(ActionEvent actionEvent) {
172: selectedItem = item;
173: updateTargetFromSelectedItem();
174: setExpanded(false);
175: fireActionEvent(actionEvent);
176: }
177: });
178: itemsContainer.add(btn);
179: }
180:
181: // ADJUST POPUP SIZE
182: Extent popupHeight = new Extent(
183: ((listModel.size() > MAX_DISPLAY_ELEMENTS ? MAX_DISPLAY_ELEMENTS
184: : listModel.size()) + 1)
185: * ITEM_SIZE + POPUP_HEIGHT_OVER);
186: ((ScrollableDisplayLayoutData) itemsContainer.getLayoutData())
187: .setHeight(popupHeight);
188:
189: if (listModel.size() > MAX_DISPLAY_ELEMENTS) {
190: popupBox.setWidth(new Extent(
191: target.getWidth().getValue() + 20));
192: ((ScrollableDisplayLayoutData) itemsContainer
193: .getLayoutData()).setWidth(new Extent(target
194: .getWidth().getValue() + 20));
195: }
196: }
197:
198: public DynaSelect getComponent() {
199: return this ;
200: }
201:
202: private void updateTargetFromSelectedItem() {
203: target.setText(selectedItem != null ? selectedItem.toString()
204: : "");
205: }
206:
207: private Component createTarget() {
208: target = new LabelEx("");
209: target.setWidth(new Extent(DEF_FIELD_WIDTH));
210: return target;
211: }
212:
213: public void addActionListener(ActionListener lsnr) {
214: lsnrList.add(lsnr);
215: }
216:
217: public void removeActionListener(ActionListener lsnr) {
218: lsnrList.remove(lsnr);
219: }
220:
221: private void fireActionEvent(ActionEvent ev) {
222: ActionEvent event = new ActionEvent(this , ev.getActionCommand());
223: GUIEventHandler.getInstance().actionPerformed(event);
224:
225: for (ActionListener lsnr : lsnrList) {
226: lsnr.actionPerformed(event);
227: }
228: }
229:
230: public ListModel getModel() {
231: return listModel;
232: }
233:
234: public void setModel(ListModel iModel) {
235: listModel = iModel;
236: addListenerToListModel();
237: updateItemsContainer();
238: }
239:
240: public Object getSelectedItem() {
241: return selectedItem;
242: }
243:
244: public int getSelectedIndex() {
245: for (int index = 0; index < listModel.size(); ++index) {
246: if (listModel.get(index).equals(selectedItem))
247: return index;
248: }
249: return -1;
250: }
251:
252: public void setSelectedItem(Object item) {
253: if (btn2itemMap.values().contains(item)) {
254: selectedItem = item;
255: updateTargetFromSelectedItem();
256: }
257: }
258:
259: public void setSelectedIndex(int index) {
260: if (index < 0) {
261: setSelectedItem(null);
262: } else {
263: Object item = listModel.get(index);
264: setSelectedItem(item);
265: }
266: }
267:
268: public void setTextFieldWidth(Extent width) {
269: target.setWidth(width);
270: }
271:
272: public void setPopupBoxHeight(Extent height) {
273: popupBox.setHeight(height);
274: }
275: }
|