001: /*
002: * This file is not part of the ItsNat framework.
003: *
004: * Original source code use and closed source derivatives are authorized
005: * to third parties with no restriction or fee.
006: * The original source code is owned by the author.
007: *
008: * This program is distributed AS IS in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: *
012: * Author: Jose Maria Arranz Santamaria
013: * (C) Innowhere Software Services S.L., Spanish company, year 2007
014: */
015:
016: package org.itsnat.feashow.features.components.lists;
017:
018: import javax.swing.DefaultListModel;
019: import javax.swing.ListSelectionModel;
020: import javax.swing.event.ListDataEvent;
021: import javax.swing.event.ListDataListener;
022: import javax.swing.event.ListSelectionEvent;
023: import javax.swing.event.ListSelectionListener;
024: import org.itsnat.comp.ItsNatComponentManager;
025: import org.itsnat.comp.ItsNatListStructure;
026: import org.itsnat.comp.free.ItsNatFreeListMultSel;
027: import org.itsnat.comp.html.ItsNatHTMLInputButton;
028: import org.itsnat.comp.html.ItsNatHTMLInputText;
029: import org.itsnat.core.ItsNatDocument;
030: import org.itsnat.core.NameValue;
031: import org.itsnat.feashow.FeatureTreeNode;
032: import org.w3c.dom.events.Event;
033: import org.w3c.dom.events.EventListener;
034: import org.w3c.dom.events.EventTarget;
035:
036: public class FreeListCustomStructureTreeNode extends FeatureTreeNode
037: implements EventListener, ListDataListener,
038: ListSelectionListener {
039: protected ItsNatFreeListMultSel listComp;
040: protected ItsNatHTMLInputButton removeButton;
041: protected ItsNatHTMLInputText itemComp;
042: protected ItsNatHTMLInputText posComp;
043: protected ItsNatHTMLInputButton updateButton;
044: protected ItsNatHTMLInputButton insertButton;
045:
046: public FreeListCustomStructureTreeNode() {
047: }
048:
049: public void startExamplePanel() {
050: ItsNatDocument itsNatDoc = getItsNatDocument();
051: ItsNatComponentManager componentMgr = itsNatDoc
052: .getItsNatComponentManager();
053:
054: ItsNatListStructure customStruc = new CityListCustomStructure();
055: NameValue[] artifacts = new NameValue[] { new NameValue(
056: "useStructure", customStruc) };
057:
058: this .listComp = (ItsNatFreeListMultSel) componentMgr
059: .createItsNatComponentById("compId", "freeListMultSel",
060: artifacts);
061:
062: DefaultListModel dataModel = (DefaultListModel) listComp
063: .getListModel();
064: dataModel.addElement("Madrid");
065: dataModel.addElement("Sevilla");
066: dataModel.addElement("Segovia");
067: dataModel.addElement("Barcelona");
068: dataModel.addElement("Oviedo");
069: dataModel.addElement("Valencia");
070:
071: ListSelectionModel selModel = listComp.getListSelectionModel();
072: selModel
073: .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
074:
075: selModel.addListSelectionListener(new ListSelectionDecorator(
076: listComp));
077:
078: selModel.setSelectionInterval(2, 3);
079:
080: listComp.addEventListener("click", this );
081: dataModel.addListDataListener(this );
082: selModel.addListSelectionListener(this );
083:
084: this .removeButton = (ItsNatHTMLInputButton) componentMgr
085: .createItsNatComponentById("removeId");
086: removeButton.addEventListener("click", this );
087:
088: this .itemComp = (ItsNatHTMLInputText) componentMgr
089: .createItsNatComponentById("itemId");
090: itemComp.setText(listComp.getListModel().getElementAt(
091: listComp.getSelectedIndex()).toString());
092:
093: this .posComp = (ItsNatHTMLInputText) componentMgr
094: .createItsNatComponentById("posId");
095: posComp.setText(Integer.toString(listComp.getSelectedIndex()));
096:
097: this .updateButton = (ItsNatHTMLInputButton) componentMgr
098: .createItsNatComponentById("updateId");
099: updateButton.addEventListener("click", this );
100:
101: this .insertButton = (ItsNatHTMLInputButton) componentMgr
102: .createItsNatComponentById("insertId");
103: insertButton.addEventListener("click", this );
104: }
105:
106: public void endExamplePanel() {
107: this .listComp.dispose();
108: this .listComp = null;
109:
110: this .removeButton.dispose();
111: this .removeButton = null;
112:
113: this .itemComp.dispose();
114: this .itemComp = null;
115:
116: this .posComp.dispose();
117: this .posComp = null;
118:
119: this .updateButton.dispose();
120: this .updateButton = null;
121:
122: this .insertButton.dispose();
123: this .insertButton = null;
124: }
125:
126: public void handleEvent(Event evt) {
127: log(evt.getCurrentTarget() + " " + evt.getType());
128:
129: EventTarget currentTarget = evt.getCurrentTarget();
130: if (currentTarget == removeButton.getHTMLInputElement()) {
131: DefaultListModel dataModel = (DefaultListModel) listComp
132: .getListModel();
133: ListSelectionModel selModel = listComp
134: .getListSelectionModel();
135: if (!selModel.isSelectionEmpty()) {
136: // Selection Model is in SINGLE_INTERVAL_SELECTION mode
137: int min = selModel.getMinSelectionIndex();
138: int max = selModel.getMaxSelectionIndex();
139: dataModel.removeRange(min, max);
140: }
141: } else if ((currentTarget == updateButton.getHTMLInputElement())
142: || (currentTarget == insertButton.getHTMLInputElement())) {
143: String newItem = itemComp.getText();
144: int pos;
145: try {
146: pos = Integer.parseInt(posComp.getText());
147: DefaultListModel dataModel = (DefaultListModel) listComp
148: .getListModel();
149: if (currentTarget == updateButton.getHTMLInputElement())
150: dataModel.setElementAt(newItem, pos);
151: else
152: dataModel.insertElementAt(newItem, pos);
153: } catch (NumberFormatException ex) {
154: getItsNatDocument().addCodeToSend(
155: "alert('Bad Position')");
156: } catch (ArrayIndexOutOfBoundsException ex) {
157: getItsNatDocument().addCodeToSend(
158: "alert('Bad Position')");
159: }
160: }
161: }
162:
163: public void intervalAdded(ListDataEvent e) {
164: listChangedLog(e);
165: }
166:
167: public void intervalRemoved(ListDataEvent e) {
168: listChangedLog(e);
169: }
170:
171: public void contentsChanged(ListDataEvent e) {
172: listChangedLog(e);
173: }
174:
175: public void listChangedLog(ListDataEvent e) {
176: int index0 = e.getIndex0();
177: int index1 = e.getIndex1();
178:
179: String action = "";
180: int type = e.getType();
181: switch (type) {
182: case ListDataEvent.INTERVAL_ADDED:
183: action = "added";
184: break;
185: case ListDataEvent.INTERVAL_REMOVED:
186: action = "removed";
187: break;
188: case ListDataEvent.CONTENTS_CHANGED:
189: action = "changed";
190: break;
191: }
192:
193: String interval = "";
194: if (index0 != -1)
195: interval = "interval " + index0 + "-" + index1;
196:
197: log(e.getClass().toString() + " " + action + " " + interval);
198: }
199:
200: public void valueChanged(ListSelectionEvent e) {
201: if (e.getValueIsAdjusting())
202: return;
203:
204: int first = e.getFirstIndex();
205: int last = e.getLastIndex();
206:
207: ListSelectionModel selModel = listComp.getListSelectionModel();
208: String fact = "";
209: for (int i = first; i <= last; i++) {
210: boolean selected = selModel.isSelectedIndex(i);
211: if (selected)
212: fact += ", selected ";
213: else
214: fact += ", deselected ";
215: fact += i;
216: }
217:
218: log(e.getClass().toString() + " " + fact);
219:
220: int index = listComp.getSelectedIndex(); // First selected
221: if (index != -1) {
222: Object value = listComp.getListModel().getElementAt(index);
223: itemComp.setText(value.toString());
224: posComp.setText(Integer.toString(index));
225: }
226: }
227:
228: }
|