001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.comp.html;
015:
016: import org.itsnat.core.event.CustomParamTransport;
017: import org.itsnat.comp.html.ItsNatHTMLSelectComboBox;
018: import org.itsnat.comp.ui.ItsNatComboBoxUI;
019: import org.itsnat.comp.ui.ItsNatComponentUI;
020: import org.itsnat.impl.comp.ItsNatComboBoxInternal;
021: import org.itsnat.impl.comp.ItsNatComboBoxSharedImpl;
022: import org.itsnat.impl.comp.html.ui.ItsNatHTMLSelectComboBoxUIImpl;
023: import java.awt.event.ItemEvent;
024: import java.awt.event.ItemListener;
025: import javax.swing.ComboBoxModel;
026: import javax.swing.DefaultComboBoxModel;
027: import javax.swing.ListModel;
028: import javax.swing.MutableComboBoxModel;
029: import javax.swing.event.EventListenerList;
030: import javax.swing.event.ListDataEvent;
031: import org.itsnat.core.event.ParamTransport;
032: import org.itsnat.core.NameValue;
033: import org.itsnat.core.event.DOMEvent;
034: import org.w3c.dom.events.Event;
035: import org.w3c.dom.html.HTMLSelectElement;
036:
037: /**
038: * Similar a JComboBox
039: * @author jmarranz
040: */
041: public class ItsNatHTMLSelectComboBoxImpl extends ItsNatHTMLSelectImpl
042: implements ItsNatHTMLSelectComboBox, ItsNatComboBoxInternal {
043: protected Object selectedItemReminder;
044: protected EventListenerList listenerList = new EventListenerList();
045:
046: /**
047: * Creates a new instance of ItsNatHTMLSelectComboBoxImpl
048: */
049: public ItsNatHTMLSelectComboBoxImpl(HTMLSelectElement element,
050: NameValue[] artifacts,
051: ItsNatHTMLComponentManagerImpl componentMgr) {
052: super (element, artifacts, componentMgr);
053:
054: // No imponemos así podríamos en teoría asociar al tipo de select que queramos
055: //getHTMLSelectElement().setMultiple(false); // Para asegurarnos
056:
057: init();
058: }
059:
060: public Object getSelectedItemReminder() {
061: return selectedItemReminder;
062: }
063:
064: public void setSelectedItemReminder(Object obj) {
065: this .selectedItemReminder = obj;
066: }
067:
068: protected ParamTransport[] getParamTransports(String type) {
069: if (type.equals("change")) {
070: // Redefinimos porque es un poco más complicado que el código por defecto
071: CustomParamTransport selectedIndex = new CustomParamTransport(
072: "selectedIndex",
073: "this.getCurrentTarget().selectedIndex");
074: return new ParamTransport[] { selectedIndex };
075: } else
076: return null;
077: }
078:
079: public void handleEventOnChange(Event evt) {
080: // Ejecutado como respuesta al evento "change" en el SELECT en el navegador
081: DOMEvent itsNatEvent = (DOMEvent) evt;
082: int selectedIndex = Integer.parseInt((String) itsNatEvent
083: .getExtraParam("selectedIndex"));
084:
085: setServerUpdatingFromClient(itsNatEvent); // Pues el evento viene del navegador y no se necesita enviar actualizaciones (salvo observers para que vean el cambio del cliente)
086:
087: try {
088: setSelectedIndex(selectedIndex);
089: } finally {
090: setServerUpdatingFromClient(null);
091: }
092: }
093:
094: public ItsNatComboBoxUI getItsNatComboBoxUI() {
095: return (ItsNatComboBoxUI) compUI;
096: }
097:
098: public ItsNatComboBoxUI createDefaultItsNatHTMLSelectComboBoxUI() {
099: return new ItsNatHTMLSelectComboBoxUIImpl(this );
100: }
101:
102: public ItsNatComponentUI createDefaultItsNatComponentUI() {
103: // Por defecto es mutable
104: return createDefaultItsNatHTMLSelectComboBoxUI();
105: }
106:
107: public void syncUIWithDataModel() {
108: super .syncUIWithDataModel();
109:
110: ItsNatComboBoxSharedImpl.syncUIWithDataModel(this );
111: }
112:
113: public ComboBoxModel getComboBoxModel() {
114: return (ComboBoxModel) dataModel;
115: }
116:
117: public void setComboBoxModel(ComboBoxModel dataModel) {
118: setDataModel(dataModel);
119: }
120:
121: public ListModel getListModel() {
122: return getComboBoxModel();
123: }
124:
125: public void setListModel(ListModel dataModel) {
126: setComboBoxModel((ComboBoxModel) dataModel);
127: }
128:
129: public void checkMutableComboBoxModel() {
130: ItsNatComboBoxSharedImpl
131: .checkMutableComboBoxModel(getComboBoxModel());
132: }
133:
134: public void addElement(Object anObject) {
135: checkMutableComboBoxModel();
136: MutableComboBoxModel model = (MutableComboBoxModel) getComboBoxModel();
137: model.addElement(anObject);
138: }
139:
140: public void insertElementAt(int index, Object anObject) {
141: checkMutableComboBoxModel();
142: MutableComboBoxModel model = (MutableComboBoxModel) getComboBoxModel();
143: model.insertElementAt(anObject, index);
144: }
145:
146: public void removeElementAt(int anIndex) {
147: checkMutableComboBoxModel();
148: MutableComboBoxModel model = (MutableComboBoxModel) getComboBoxModel();
149: model.removeElementAt(anIndex);
150: }
151:
152: public void removeAllElements() {
153: checkMutableComboBoxModel();
154: ItsNatComboBoxSharedImpl.removeAllElements(this );
155: }
156:
157: public void removeElement(Object anObject) {
158: checkMutableComboBoxModel();
159: MutableComboBoxModel model = (MutableComboBoxModel) getComboBoxModel();
160: model.removeElement(anObject);
161: }
162:
163: public int getSelectedIndex() {
164: return ItsNatComboBoxSharedImpl.getSelectedIndex(this );
165: }
166:
167: public void setSelectedIndex(int index) {
168: ItsNatComboBoxSharedImpl.setSelectedIndex(this , index);
169: }
170:
171: public Object getSelectedItem() {
172: return ItsNatComboBoxSharedImpl.getSelectedItem(this );
173: }
174:
175: public void setSelectedItem(Object anObject) {
176: ItsNatComboBoxSharedImpl.setSelectedItem(this , anObject);
177: }
178:
179: public Object[] getSelectedObjects() {
180: return ItsNatComboBoxSharedImpl.getSelectedObjects(this );
181: }
182:
183: public void contentsChanged(ListDataEvent e) {
184: super .contentsChanged(e);
185:
186: ItsNatComboBoxSharedImpl.contentsChanged(this , e);
187: }
188:
189: public void intervalAdded(ListDataEvent e) {
190: super .intervalAdded(e);
191:
192: ItsNatComboBoxSharedImpl.intervalAdded(this , e);
193: }
194:
195: public void intervalRemoved(ListDataEvent e) {
196: super .intervalRemoved(e);
197:
198: ItsNatComboBoxSharedImpl.intervalRemoved(this , e);
199: }
200:
201: public void addItemListener(ItemListener l) {
202: listenerList.add(ItemListener.class, l);
203: }
204:
205: public void removeItemListener(ItemListener l) {
206: listenerList.remove(ItemListener.class, l);
207: }
208:
209: public boolean hasItemListeners() {
210: return listenerList.getListenerCount() > 0;
211: }
212:
213: public ItemListener[] getItemListeners() {
214: return (ItemListener[]) listenerList
215: .getListeners(ItemListener.class);
216: }
217:
218: public void fireItemStateChanged(ItemEvent e) {
219: ItsNatComboBoxSharedImpl.fireItemStateChanged(this , e);
220: }
221:
222: public Object createDefaultModelInternal() {
223: return createDefaultListModel();
224: }
225:
226: public ListModel createDefaultListModel() {
227: return new DefaultComboBoxModel();
228: }
229:
230: public boolean isCombo() {
231: return true;
232: }
233:
234: public void setUISelectedIndex(int index) {
235: ItsNatComboBoxUI compUI = getItsNatComboBoxUI();
236:
237: boolean wasDisabled = disableSendCodeToRequesterIfServerUpdating(); // Evitamos así que llegue al requester si no debe llegar (pero sí a los observadores)
238: try {
239: compUI.setSelectedIndex(index);
240: } finally {
241: if (wasDisabled)
242: enableSendCodeToRequester();
243: }
244: }
245: }
|