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;
015:
016: import org.itsnat.comp.ui.ItsNatComboBoxUI;
017: import org.itsnat.core.ItsNatException;
018: import java.awt.event.ItemEvent;
019: import java.awt.event.ItemListener;
020: import javax.swing.ComboBoxModel;
021: import javax.swing.DefaultComboBoxModel;
022: import javax.swing.MutableComboBoxModel;
023: import javax.swing.event.ListDataEvent;
024:
025: /**
026: *
027: * @author jmarranz
028: */
029: public class ItsNatComboBoxSharedImpl extends ItsNatListSharedImpl {
030:
031: /**
032: * Creates a new instance of ItsNatComboBoxSharedImpl
033: */
034: public ItsNatComboBoxSharedImpl() {
035: }
036:
037: public static void syncUIWithDataModel(ItsNatComboBoxInternal comp) {
038: copyCurrentSelectionToUI(comp);
039: }
040:
041: public static void checkMutableComboBoxModel(ComboBoxModel model) {
042: if (!(model instanceof MutableComboBoxModel))
043: throw new ItsNatException("Expected a MutableComboBoxModel");
044: }
045:
046: public static void removeAllElements(ItsNatComboBoxInternal comp) {
047: MutableComboBoxModel dataModel = (MutableComboBoxModel) comp
048: .getComboBoxModel();
049:
050: if (dataModel instanceof DefaultComboBoxModel) {
051: ((DefaultComboBoxModel) dataModel).removeAllElements();
052: } else {
053: while (dataModel.getSize() > 0) {
054: Object element = dataModel.getElementAt(0);
055: dataModel.removeElement(element);
056: }
057: }
058: }
059:
060: public static int getSelectedIndex(ItsNatComboBoxInternal comp) {
061: ComboBoxModel dataModel = comp.getComboBoxModel();
062: Object selected = dataModel.getSelectedItem();
063: return indexOf(selected, dataModel);
064: }
065:
066: public static void setSelectedIndex(ItsNatComboBoxInternal comp,
067: int index) {
068: // Se propagará el cambio al DOM via evento
069: ComboBoxModel dataModel = comp.getComboBoxModel();
070: Object selected = dataModel.getElementAt(index);
071: dataModel.setSelectedItem(selected); // Notar que como haya elementos repetidos puede fallar
072: }
073:
074: public static Object getSelectedItem(ItsNatComboBoxInternal comp) {
075: return comp.getComboBoxModel().getSelectedItem();
076: }
077:
078: public static void setSelectedItem(ItsNatComboBoxInternal comp,
079: Object anObject) {
080: comp.getComboBoxModel().setSelectedItem(anObject);
081: }
082:
083: public static Object[] getSelectedObjects(
084: ItsNatComboBoxInternal comp) {
085: ComboBoxModel dataModel = comp.getComboBoxModel();
086: Object selectedObject = dataModel.getSelectedItem();
087: if (selectedObject == null)
088: return new Object[0];
089: else
090: return new Object[] { selectedObject };
091: }
092:
093: public static void fireItemStateChanged(
094: ItsNatComboBoxInternal comp, ItemEvent e) {
095: if (comp.hasItemListeners()) {
096: ItemListener[] listeners = comp.getItemListeners();
097: for (int i = 0; i < listeners.length; i++) {
098: listeners[i].itemStateChanged(e);
099: }
100: }
101: }
102:
103: public static void contentsChanged(ItsNatComboBoxInternal comp,
104: ListDataEvent e) {
105: // No necesitamos el objeto evento
106:
107: copyCurrentSelectionToUI(comp);
108:
109: // Lanzamos el evento a los ItemListener del cambio de selección
110: selectedItemChanged(comp);
111: }
112:
113: public static void intervalAdded(ItsNatComboBoxInternal comp,
114: ListDataEvent e) {
115: // Lanzamos el evento a los ItemListener del cambio de selección (posible)
116: selectedItemChanged(comp);
117: }
118:
119: public static void intervalRemoved(ItsNatComboBoxInternal comp,
120: ListDataEvent e) {
121: // Lanzamos el evento a los ItemListener del cambio de selección (posible)
122: selectedItemChanged(comp);
123: }
124:
125: public static void copyCurrentSelectionToUI(
126: ItsNatComboBoxInternal comp) {
127: ComboBoxModel dataModel = comp.getComboBoxModel();
128:
129: // Copiamos la selección actual
130: Object selectedObj = dataModel.getSelectedItem();
131: int index = indexOf(selectedObj, dataModel);
132:
133: comp.setUISelectedIndex(index);
134: }
135:
136: public static void selectedItemChanged(ItsNatComboBoxInternal comp) {
137: Object oldSelection = comp.getSelectedItemReminder();
138: Object newSelection = comp.getComboBoxModel().getSelectedItem();
139: if (oldSelection == newSelection)
140: return;
141:
142: if (oldSelection != null)
143: comp.fireItemStateChanged(new ItemEvent(comp,
144: ItemEvent.ITEM_STATE_CHANGED, oldSelection,
145: ItemEvent.DESELECTED));
146:
147: // set the new selected item.
148: comp.setSelectedItemReminder(newSelection);
149:
150: if (newSelection != null)
151: comp.fireItemStateChanged(new ItemEvent(comp,
152: ItemEvent.ITEM_STATE_CHANGED, newSelection,
153: ItemEvent.SELECTED));
154: }
155:
156: }
|