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.ItsNatListMultSel;
017: import org.itsnat.comp.ui.ItsNatListMultSelUI;
018: import java.util.List;
019: import javax.swing.AbstractListModel;
020: import javax.swing.ListModel;
021: import javax.swing.ListSelectionModel;
022: import javax.swing.event.ListDataEvent;
023:
024: /**
025: *
026: * @author jmarranz
027: */
028: public class ItsNatListMultSelSharedImpl {
029:
030: /**
031: * Creates a new instance of ItsNatListMultSelSharedImpl
032: */
033: public ItsNatListMultSelSharedImpl() {
034: }
035:
036: public static ListSelectionModelMgrImpl setSelectionModel(
037: ItsNatListMultSelInternal comp,
038: ListSelectionModel selectionModel) {
039: int size = comp.getListModel().getSize();
040: return ListSelectionModelMgrImpl.newListSelectionModelMgr(
041: selectionModel, size);
042: }
043:
044: public static void syncSelModelWithDataModel(
045: ItsNatListMultSelInternal comp) {
046: ListSelectionModelMgrImpl selModelMgr = comp
047: .getListSelectionModelMgr();
048: if (selModelMgr != null) {
049: ListModel dataModel = comp.getListModel();
050: selModelMgr.syncWithDataModel(dataModel.getSize());
051: }
052: }
053:
054: public static void contentsChanged(ItsNatListMultSel comp,
055: ListDataEvent e) {
056: // Es lanzado por el dataModel del usuario
057: // El ListModel y concretamente el DefaultListModel sí soportan
058: // el cambio de valor de un elemento, y sin embargo al contrario
059: // que en el caso ComboBox este evento no debe ser debido a cambios
060: // en la selección pues para eso está el SelectionModel.
061:
062: ItsNatListMultSelUI compUI = comp.getItsNatListMultSelUI();
063:
064: int index0 = e.getIndex0();
065: int index1 = e.getIndex1();
066:
067: ListModel dataModel = (ListModel) e.getSource();
068: ListSelectionModel selModel = comp.getListSelectionModel();
069: // Necesariamente es index0 <= index1
070: for (int i = index0; i <= index1; i++) {
071: Object obj = dataModel.getElementAt(i);
072: boolean isSelected = selModel.isSelectedIndex(i);
073: compUI.setElementValueAt(i, obj, isSelected, false);
074: }
075: }
076:
077: public static void setListData(ItsNatListMultSel comp,
078: final Object[] listData) {
079: // Similar a JList
080: AbstractListModel dataModel = new AbstractListModel() {
081: public int getSize() {
082: return listData.length;
083: }
084:
085: public Object getElementAt(int i) {
086: return listData[i];
087: }
088: };
089: comp.setListModel(dataModel);
090: }
091:
092: public static void setListData(ItsNatListMultSel comp,
093: final List listData) {
094: // Similar a JList
095: AbstractListModel dataModel = new AbstractListModel() {
096: public int getSize() {
097: return listData.size();
098: }
099:
100: public Object getElementAt(int i) {
101: return listData.get(i);
102: }
103: };
104: comp.setListModel(dataModel);
105: }
106:
107: }
|