001: /*
002: * Created on 07.11.2004
003: */
004: package de.jwic.controls;
005:
006: import java.util.ArrayList;
007: import java.util.Iterator;
008: import java.util.List;
009:
010: import de.jwic.base.IControlContainer;
011: import de.jwic.events.SelectionEvent;
012: import de.jwic.events.SelectionListener;
013:
014: /**
015: * This control renders a listbox using the HTML select tag.
016: *
017: * @author Florian Lippisch
018: * @version $Revision: 1.3 $
019: */
020: public class ListBoxControl extends ListControl {
021:
022: public final static int NO_SELECTION = 0;
023: public final static int CLICK_SELECTION = 1;
024: public final static int DBLCLICK_SELECTION = 2;
025:
026: private static final long serialVersionUID = 2L;
027:
028: private boolean multiple = false;
029: private String confirmMsg = "";
030: protected int size = 1;
031:
032: protected int selectionMode = NO_SELECTION;
033: protected List listeners = null;
034:
035: /**
036: * @param container
037: */
038: public ListBoxControl(IControlContainer container) {
039: super (container, null);
040: }
041:
042: /**
043: * @param container
044: * @param name
045: */
046: public ListBoxControl(IControlContainer container, String name) {
047: super (container, name);
048: }
049:
050: /**
051: * Register a listener that will be notified when the listbox has been
052: * selected (i.e. doubleClick). The event is only triggered if the
053: * selectionMode property is not NO_SELECTION.
054: * @param listener
055: */
056: public void addSelectionListener(SelectionListener listener) {
057: if (listeners == null) {
058: listeners = new ArrayList();
059: }
060: listeners.add(listener);
061: }
062:
063: /**
064: * Removes the specified listener.
065: * @param listener
066: */
067: public void removeSelectionListener(SelectionListener listener) {
068: if (listeners != null) {
069: listeners.remove(listener);
070: }
071: }
072:
073: /**
074: * Send the element selected event to the registerd listeners.
075: */
076: protected void sendSelectionEvent() {
077:
078: if (listeners != null) {
079: SelectionEvent e = new SelectionEvent(this );
080: for (Iterator it = listeners.iterator(); it.hasNext();) {
081: SelectionListener osl = (SelectionListener) it.next();
082: osl.objectSelected(e);
083: }
084: }
085:
086: }
087:
088: /**
089: * The action method that handles if the user selects the control, depending on
090: * how the selectionMode is set.
091: */
092: public void actionSelected() {
093: sendSelectionEvent();
094: }
095:
096: /**
097: * @return Returns the multiple.
098: */
099: public boolean isMultiple() {
100: return multiple;
101: }
102:
103: /**
104: * @param multiple The multiple to set.
105: */
106: public void setMultiple(boolean multiple) {
107: if (this .multiple && !multiple) {
108: // changed from multi-selection to single selection
109: if (field.getValues().length > 1) {
110: // if more then one entry is selected, select the first one
111: field.setValue(field.getValues()[0]);
112: }
113: }
114: this .multiple = multiple;
115:
116: requireRedraw();
117: }
118:
119: /**
120: * @return Returns the confirmMsg.
121: */
122: public String getConfirmMsg() {
123: return confirmMsg;
124: }
125:
126: /**
127: * If the confirm message is not an empty string, the user will be prompted with a
128: * message box and the specified message. He may then answer 'ok' or 'cancel'. If the
129: * user presses ok, the change will be done. Otherwise the old value is restored
130: * and no event happens.
131: *
132: * @param confirmMsg The confirmMsg to set.
133: */
134: public void setConfirmMsg(String confirmMsg) {
135: this .confirmMsg = confirmMsg;
136: requireRedraw();
137: }
138:
139: /**
140: * @return Returns the size.
141: */
142: public int getSize() {
143: return size;
144: }
145:
146: /**
147: * Set the size of the control. A value of 1 will display a combo-box, other
148: * values display a list.
149: *
150: * @param size The size to set.
151: */
152: public void setSize(int size) {
153: this .size = size;
154: requireRedraw();
155: }
156:
157: /**
158: * @return Returns the selectionMode.
159: */
160: public int getSelectionMode() {
161: return selectionMode;
162: }
163:
164: /**
165: * @param selectionMode The selectionMode to set.
166: */
167: public void setSelectionMode(int selectionMode) {
168: if (selectionMode != NO_SELECTION && isChangeNotification()) {
169: // the submit initiated by the onChange event would conflict with the following onDblClick event,
170: // therefor it can not be allowed to enable both.
171: throw new IllegalArgumentException(
172: "You can not activate selectionMode if the changeNotification is on");
173: }
174: this .selectionMode = selectionMode;
175: requireRedraw();
176: }
177:
178: /* (non-Javadoc)
179: * @see de.jwic.controls.ListControl#setChangeNotification(boolean)
180: */
181: public void setChangeNotification(boolean changeNotification) {
182: if (selectionMode != NO_SELECTION && changeNotification) {
183: // the submit initiated by the onChange event would conflict with the following onDblClick event,
184: // therefor it can not be allowed to enable both.
185: throw new IllegalArgumentException(
186: "You can not activate changeNotifaction if the selectionMode is activated");
187: }
188: super.setChangeNotification(changeNotification);
189: }
190:
191: }
|