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.free.ItsNatFreeListMultSel;
026: import org.itsnat.comp.html.ItsNatHTMLInputButton;
027: import org.itsnat.comp.html.ItsNatHTMLInputText;
028: import org.itsnat.core.ItsNatDocument;
029: import org.itsnat.feashow.FeatureTreeNode;
030: import org.w3c.dom.events.Event;
031: import org.w3c.dom.events.EventListener;
032: import org.w3c.dom.events.EventTarget;
033:
034: public class FreeListTreeNode extends FeatureTreeNode implements
035: EventListener, ListDataListener, ListSelectionListener {
036: protected ItsNatFreeListMultSel listComp;
037: protected ItsNatHTMLInputButton removeButton;
038: protected ItsNatHTMLInputText itemComp;
039: protected ItsNatHTMLInputText posComp;
040: protected ItsNatHTMLInputButton updateButton;
041: protected ItsNatHTMLInputButton insertButton;
042:
043: public FreeListTreeNode() {
044: }
045:
046: public void startExamplePanel() {
047: ItsNatDocument itsNatDoc = getItsNatDocument();
048: ItsNatComponentManager componentMgr = itsNatDoc
049: .getItsNatComponentManager();
050:
051: this .listComp = (ItsNatFreeListMultSel) componentMgr
052: .createItsNatComponentById("compId", "freeListMultSel",
053: null);
054:
055: DefaultListModel dataModel = (DefaultListModel) listComp
056: .getListModel();
057: dataModel.addElement("Madrid");
058: dataModel.addElement("Sevilla");
059: dataModel.addElement("Segovia");
060: dataModel.addElement("Barcelona");
061: dataModel.addElement("Oviedo");
062: dataModel.addElement("Valencia");
063:
064: ListSelectionModel selModel = listComp.getListSelectionModel();
065: selModel
066: .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
067:
068: selModel.addListSelectionListener(new ListSelectionDecorator(
069: listComp));
070:
071: selModel.setSelectionInterval(2, 3);
072:
073: listComp.addEventListener("click", this );
074: dataModel.addListDataListener(this );
075: selModel.addListSelectionListener(this );
076:
077: this .removeButton = (ItsNatHTMLInputButton) componentMgr
078: .createItsNatComponentById("removeId");
079: removeButton.addEventListener("click", this );
080:
081: this .itemComp = (ItsNatHTMLInputText) componentMgr
082: .createItsNatComponentById("itemId");
083: itemComp.setText(listComp.getListModel().getElementAt(
084: listComp.getSelectedIndex()).toString());
085:
086: this .posComp = (ItsNatHTMLInputText) componentMgr
087: .createItsNatComponentById("posId");
088: posComp.setText(Integer.toString(listComp.getSelectedIndex()));
089:
090: this .updateButton = (ItsNatHTMLInputButton) componentMgr
091: .createItsNatComponentById("updateId");
092: updateButton.addEventListener("click", this );
093:
094: this .insertButton = (ItsNatHTMLInputButton) componentMgr
095: .createItsNatComponentById("insertId");
096: insertButton.addEventListener("click", this );
097: }
098:
099: public void endExamplePanel() {
100: this .listComp.dispose();
101: this .listComp = null;
102:
103: this .removeButton.dispose();
104: this .removeButton = null;
105:
106: this .itemComp.dispose();
107: this .itemComp = null;
108:
109: this .posComp.dispose();
110: this .posComp = null;
111:
112: this .updateButton.dispose();
113: this .updateButton = null;
114:
115: this .insertButton.dispose();
116: this .insertButton = null;
117: }
118:
119: public void handleEvent(Event evt) {
120: log(evt.getCurrentTarget() + " " + evt.getType());
121:
122: EventTarget currentTarget = evt.getCurrentTarget();
123: if (currentTarget == removeButton.getHTMLInputElement()) {
124: DefaultListModel dataModel = (DefaultListModel) listComp
125: .getListModel();
126: ListSelectionModel selModel = listComp
127: .getListSelectionModel();
128: if (!selModel.isSelectionEmpty()) {
129: // Selection Model is in SINGLE_INTERVAL_SELECTION mode
130: int min = selModel.getMinSelectionIndex();
131: int max = selModel.getMaxSelectionIndex();
132: dataModel.removeRange(min, max);
133: }
134: } else if ((currentTarget == updateButton.getHTMLInputElement())
135: || (currentTarget == insertButton.getHTMLInputElement())) {
136: String newItem = itemComp.getText();
137: int pos;
138: try {
139: pos = Integer.parseInt(posComp.getText());
140: DefaultListModel dataModel = (DefaultListModel) listComp
141: .getListModel();
142: if (currentTarget == updateButton.getHTMLInputElement())
143: dataModel.setElementAt(newItem, pos);
144: else
145: dataModel.insertElementAt(newItem, pos);
146: } catch (NumberFormatException ex) {
147: getItsNatDocument().addCodeToSend(
148: "alert('Bad Position')");
149: } catch (ArrayIndexOutOfBoundsException ex) {
150: getItsNatDocument().addCodeToSend(
151: "alert('Bad Position')");
152: }
153: }
154: }
155:
156: public void intervalAdded(ListDataEvent e) {
157: listChangedLog(e);
158: }
159:
160: public void intervalRemoved(ListDataEvent e) {
161: listChangedLog(e);
162: }
163:
164: public void contentsChanged(ListDataEvent e) {
165: listChangedLog(e);
166: }
167:
168: public void listChangedLog(ListDataEvent e) {
169: int index0 = e.getIndex0();
170: int index1 = e.getIndex1();
171:
172: String action = "";
173: int type = e.getType();
174: switch (type) {
175: case ListDataEvent.INTERVAL_ADDED:
176: action = "added";
177: break;
178: case ListDataEvent.INTERVAL_REMOVED:
179: action = "removed";
180: break;
181: case ListDataEvent.CONTENTS_CHANGED:
182: action = "changed";
183: break;
184: }
185:
186: String interval = "";
187: if (index0 != -1)
188: interval = "interval " + index0 + "-" + index1;
189:
190: log(e.getClass().toString() + " " + action + " " + interval);
191: }
192:
193: public void valueChanged(ListSelectionEvent e) {
194: if (e.getValueIsAdjusting())
195: return;
196:
197: int first = e.getFirstIndex();
198: int last = e.getLastIndex();
199:
200: ListSelectionModel selModel = listComp.getListSelectionModel();
201: String fact = "";
202: for (int i = first; i <= last; i++) {
203: boolean selected = selModel.isSelectedIndex(i);
204: if (selected)
205: fact += ", selected ";
206: else
207: fact += ", deselected ";
208: fact += i;
209: }
210:
211: log(e.getClass().toString() + " " + fact);
212:
213: int index = listComp.getSelectedIndex(); // First selected
214: if (index != -1) {
215: Object value = listComp.getListModel().getElementAt(index);
216: itemComp.setText(value.toString());
217: posComp.setText(Integer.toString(index));
218: }
219: }
220:
221: }
|