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.free;
015:
016: import org.itsnat.comp.ItsNatListCellEditor;
017: import org.itsnat.comp.free.ItsNatFreeListMultSel;
018: import org.itsnat.comp.ui.ItsNatComponentUI;
019: import org.itsnat.comp.ui.ItsNatListMultSelUI;
020: import org.itsnat.impl.comp.*;
021: import org.itsnat.impl.comp.ui.free.ItsNatFreeListMultSelUIImpl;
022: import java.util.List;
023: import javax.swing.DefaultListModel;
024: import javax.swing.DefaultListSelectionModel;
025: import javax.swing.ListModel;
026: import javax.swing.ListSelectionModel;
027: import javax.swing.event.ListDataEvent;
028: import org.itsnat.comp.ItsNatListStructure;
029: import org.itsnat.comp.ui.ItsNatListCellUI;
030: import org.itsnat.comp.ui.ItsNatListUI;
031: import org.itsnat.core.NameValue;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.Node;
034: import org.w3c.dom.events.Event;
035: import org.w3c.dom.events.MouseEvent;
036:
037: /**
038: * Parecido a JList
039: *
040: * @author jmarranz
041: */
042: public class ItsNatFreeListMultSelImpl extends ItsNatFreeListImpl
043: implements ItsNatFreeListMultSel, ItsNatListMultSelInternal {
044: protected ListSelectionModelMgrImpl selModelMgr;
045: protected ListCellEditorProcessor editorProcessor = new ListCellEditorProcessor(
046: this );
047:
048: /**
049: * Creates a new instance of ItsNatFreeListMultSelImpl
050: */
051: public ItsNatFreeListMultSelImpl(Element element,
052: ItsNatListStructure structure, NameValue[] artifacts,
053: ItsNatComponentManagerImpl componentMgr) {
054: super (element, structure, artifacts, componentMgr);
055:
056: // Aunque está diseñado para el caso múltiple admitiría también no multiple
057: // aunque para eso está mejor el componente ComboBox.
058: setItsNatListCellEditor(componentMgr
059: .createDefaultItsNatListCellEditor(null));
060:
061: init();
062: }
063:
064: public void setDefaultModels() {
065: super .setDefaultModels();
066:
067: // Después de la iniciación del data model
068: setListSelectionModel(new DefaultListSelectionModel());
069: }
070:
071: public void unbindModels() {
072: unsetListSelectionModel();
073:
074: super .unbindModels();
075: }
076:
077: public ListCellEditorProcessor getListCellEditorProcessor() {
078: return editorProcessor;
079: }
080:
081: public void processDOMEvent(Event evt) {
082: String type = evt.getType();
083: if (type.equals("click") || type.equals("mouseup")) {
084: Node nodeClicked = (Node) evt.getTarget(); // Puede ser un nodo interior del elemento pulsado
085:
086: ItsNatListUI compUI = getItsNatListUI();
087: ItsNatListCellUI option = compUI
088: .getItsNatListCellUIFromNode(nodeClicked);
089: if (option != null) // por si acaso
090: {
091: MouseEvent mouseEvt = (MouseEvent) evt;
092: int index = option.getIndex();
093: boolean toggle = mouseEvt.getCtrlKey();
094: boolean extend = mouseEvt.getShiftKey();
095: boolean selected = selModelMgr.getListSelectionModel()
096: .isSelectedIndex(index);
097: selModelMgr.changeSelectionModel(index, toggle, extend,
098: selected);
099: }
100: }
101:
102: super .processDOMEvent(evt);
103: }
104:
105: public ItsNatListMultSelUI getItsNatListMultSelUI() {
106: return (ItsNatListMultSelUI) compUI;
107: }
108:
109: public ItsNatListMultSelUI createDefaultItsNatFreeListMultSelUI() {
110: return new ItsNatFreeListMultSelUIImpl(this );
111: }
112:
113: public ItsNatComponentUI createDefaultItsNatComponentUI() {
114: return createDefaultItsNatFreeListMultSelUI();
115: }
116:
117: public ListModel getListModel() {
118: return (ListModel) dataModel;
119: }
120:
121: public void setListModel(ListModel dataModel) {
122: setDataModel(dataModel);
123: }
124:
125: public void setListData(Object[] listData) {
126: ItsNatListMultSelSharedImpl.setListData(this , listData);
127: }
128:
129: public void setListData(List listData) {
130: ItsNatListMultSelSharedImpl.setListData(this , listData);
131: }
132:
133: public ListSelectionModelMgrImpl getListSelectionModelMgr() {
134: return selModelMgr;
135: }
136:
137: public ListSelectionModel getListSelectionModel() {
138: if (selModelMgr == null)
139: return null;
140: return selModelMgr.getListSelectionModel();
141: }
142:
143: public void unsetListSelectionModel() {
144: if (selModelMgr != null)
145: selModelMgr.dispose();
146: // No anulamos el selModelMgr para que se pueda recuperar el ListSelectionModel después de un dispose
147: }
148:
149: public void setListSelectionModel(ListSelectionModel selectionModel) {
150: this .selModelMgr = ItsNatListMultSelSharedImpl
151: .setSelectionModel(this , selectionModel);
152: }
153:
154: public int getSelectedIndex() {
155: return getListSelectionModel().getMinSelectionIndex();
156: }
157:
158: public void setSelectedIndex(int index) {
159: getListSelectionModel().setSelectionInterval(index, index);
160: }
161:
162: public int[] getSelectedIndices() {
163: return selModelMgr.getSelectedIndices();
164: }
165:
166: public void setSelectedIndices(int[] indices) {
167: selModelMgr.setSelectedIndices(indices);
168: }
169:
170: public void setSelectionMode(int selectionMode) {
171: getListSelectionModel().setSelectionMode(selectionMode);
172: }
173:
174: public Object createDefaultModelInternal() {
175: return createDefaultListModel();
176: }
177:
178: public ListModel createDefaultListModel() {
179: return new DefaultListModel();
180: }
181:
182: public void contentsChanged(ListDataEvent e) {
183: super .contentsChanged(e);
184:
185: ItsNatListMultSelSharedImpl.contentsChanged(this , e);
186: }
187:
188: public void syncUIWithDataModel() {
189: super .syncUIWithDataModel();
190:
191: ItsNatListMultSelSharedImpl.syncSelModelWithDataModel(this );
192: }
193:
194: public void insertElementAtInternal(int index, Object anObject) {
195: super .insertElementAtInternal(index, anObject);
196:
197: selModelMgr.insertElementUpdateModel(index);
198: }
199:
200: public void removeElementRangeInternal(int fromIndex, int toIndex) {
201: selModelMgr.removeRangeUpdateModel(fromIndex, toIndex);
202:
203: super .removeElementRangeInternal(fromIndex, toIndex);
204: }
205:
206: public void removeAllElementsInternal() {
207: if (selModelMgr != null) // Es null en el proceso de carga, es normal
208: selModelMgr.removeAllUpdateModel();
209:
210: super .removeAllElementsInternal();
211: }
212:
213: public ItsNatListCellEditor getItsNatListCellEditor() {
214: ListCellEditorProcessor editorProcessor = getListCellEditorProcessor();
215: return editorProcessor.getItsNatListCellEditor();
216: }
217:
218: public void setItsNatListCellEditor(ItsNatListCellEditor cellEditor) {
219: editorProcessor.setItsNatListCellEditor(cellEditor);
220: }
221:
222: public void startEditingAt(int index) {
223: getListCellEditorProcessor().startEdition(index);
224: }
225:
226: public boolean isEditing() {
227: return getListCellEditorProcessor().isEditing();
228: }
229:
230: public int getEditingIndex() {
231: return getListCellEditorProcessor().getIndex();
232: }
233:
234: public String getEditorActivatorEvent() {
235: return getListCellEditorProcessor().getEditorActivatorEvent();
236: }
237:
238: public void setEditorActivatorEvent(String editorActivatorEvent) {
239: getListCellEditorProcessor().setEditorActivatorEvent(
240: editorActivatorEvent);
241: }
242: }
|