01: /*
02: ItsNat Java Web Application Framework
03: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
04: Author: Jose Maria Arranz Santamaria
05:
06: This program is free software: you can redistribute it and/or modify
07: it under the terms of the GNU Affero General Public License as published by
08: the Free Software Foundation, either version 3 of the License, or
09: (at your option) any later version. See the GNU Affero General Public
10: License for more details. See the copy of the GNU Affero General Public License
11: included in this program. If not, see <http://www.gnu.org/licenses/>.
12: */
13:
14: package org.itsnat.impl.comp;
15:
16: import javax.swing.DefaultComboBoxModel;
17: import javax.swing.DefaultListModel;
18: import javax.swing.ListModel;
19: import javax.swing.event.ListDataEvent;
20: import org.itsnat.comp.ui.ItsNatListUI;
21:
22: /**
23: *
24: * @author jmarranz
25: */
26: public class ItsNatListSharedImpl {
27:
28: /**
29: * Creates a new instance of ItsNatListSharedImpl
30: */
31: public ItsNatListSharedImpl() {
32: }
33:
34: public static int indexOf(Object obj, ListModel dataModel) {
35: if (obj == null)
36: return -1;
37:
38: if (dataModel instanceof DefaultListModel)
39: return ((DefaultListModel) dataModel).indexOf(obj);
40: else if (dataModel instanceof DefaultComboBoxModel)
41: return ((DefaultComboBoxModel) dataModel).getIndexOf(obj);
42: else
43: return ListDataModelUtil.indexOf(obj, dataModel);
44: }
45:
46: public static void bindDataModel(ItsNatListInternal comp) {
47: // A partir de ahora los cambios los repercutimos en el DOM por eventos
48: // No se debe cambiar el DOM select por otra vía que por el objeto dataModel del usuario (si es mutable)
49:
50: ListModel dataModel = comp.getListModel();
51: dataModel.addListDataListener(comp);
52: }
53:
54: public static void unbindDataModel(ItsNatListInternal comp) {
55: ListModel dataModel = comp.getListModel();
56: dataModel.removeListDataListener(comp);
57: }
58:
59: public static void syncUIWithDataModel(ItsNatListInternal comp) {
60: ListModel dataModel = comp.getListModel();
61: ItsNatListUI compUI = comp.getItsNatListUI();
62:
63: compUI.removeAllElements();
64: int size = dataModel.getSize();
65: for (int i = 0; i < size; i++) {
66: Object item = dataModel.getElementAt(i);
67: compUI.insertElementAt(i, item);
68: }
69: }
70:
71: public static void intervalAdded(ItsNatListInternal comp,
72: ListDataEvent e) {
73: // Sincronizamos con el DOM
74: ListModel dataModel = (ListModel) e.getSource();
75: int index0 = e.getIndex0();
76: int index1 = e.getIndex1();
77: for (int i = index0; i <= index1; i++) {
78: Object item = dataModel.getElementAt(i);
79: comp.insertElementAtInternal(i, item);
80: }
81: }
82:
83: public static void intervalRemoved(ItsNatListInternal comp,
84: ListDataEvent e) {
85: // Sincronizamos con el DOM
86: int index0 = e.getIndex0();
87: int index1 = e.getIndex1();
88: comp.removeElementRangeInternal(index0, index1);
89: }
90:
91: public static void contentsChanged(ItsNatListInternal comp,
92: ListDataEvent e) {
93: // Nada que hacer en este nivel
94: }
95:
96: }
|