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.functest;
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.itsnat.feashow.features.components.lists.ListSelectionDecorator;
031: import org.w3c.dom.events.Event;
032: import org.w3c.dom.events.EventListener;
033: import org.w3c.dom.events.EventTarget;
034:
035: public class FuncTestUsingBrowserTreeNode extends FeatureTreeNode
036: implements EventListener, ListDataListener,
037: ListSelectionListener {
038: protected ItsNatHTMLInputButton startButton;
039:
040: protected ItsNatFreeListMultSel listComp;
041: protected ItsNatHTMLInputButton removeButton;
042: protected ItsNatHTMLInputText itemComp;
043: protected ItsNatHTMLInputText posComp;
044: protected ItsNatHTMLInputButton updateButton;
045: protected ItsNatHTMLInputButton insertButton;
046:
047: public FuncTestUsingBrowserTreeNode() {
048: }
049:
050: public ItsNatFreeListMultSel getList() {
051: return listComp;
052: }
053:
054: public ItsNatHTMLInputButton getRemoveButton() {
055: return removeButton;
056: }
057:
058: public ItsNatHTMLInputText getItemInput() {
059: return itemComp;
060: }
061:
062: public ItsNatHTMLInputText getPosInput() {
063: return posComp;
064: }
065:
066: public ItsNatHTMLInputButton getUpdateButton() {
067: return updateButton;
068: }
069:
070: public ItsNatHTMLInputButton getInsertButton() {
071: return insertButton;
072: }
073:
074: public void startExamplePanel() {
075: ItsNatDocument itsNatDoc = getItsNatDocument();
076: ItsNatComponentManager componentMgr = itsNatDoc
077: .getItsNatComponentManager();
078:
079: this .startButton = (ItsNatHTMLInputButton) componentMgr
080: .createItsNatComponentById("startId");
081: startButton.addEventListener("click", this );
082:
083: this .listComp = (ItsNatFreeListMultSel) componentMgr
084: .createItsNatComponentById("compId", "freeListMultSel",
085: null);
086:
087: DefaultListModel dataModel = (DefaultListModel) listComp
088: .getListModel();
089:
090: ListSelectionModel selModel = listComp.getListSelectionModel();
091: selModel
092: .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
093:
094: selModel.addListSelectionListener(new ListSelectionDecorator(
095: listComp));
096:
097: listComp.addEventListener("click", this );
098: dataModel.addListDataListener(this );
099: selModel.addListSelectionListener(this );
100:
101: this .removeButton = (ItsNatHTMLInputButton) componentMgr
102: .createItsNatComponentById("removeId");
103: removeButton.addEventListener("click", this );
104:
105: this .itemComp = (ItsNatHTMLInputText) componentMgr
106: .createItsNatComponentById("itemId");
107:
108: this .posComp = (ItsNatHTMLInputText) componentMgr
109: .createItsNatComponentById("posId");
110:
111: this .updateButton = (ItsNatHTMLInputButton) componentMgr
112: .createItsNatComponentById("updateId");
113: updateButton.addEventListener("click", this );
114:
115: this .insertButton = (ItsNatHTMLInputButton) componentMgr
116: .createItsNatComponentById("insertId");
117: insertButton.addEventListener("click", this );
118: }
119:
120: public void endExamplePanel() {
121: this .startButton.dispose();
122: this .startButton = null;
123:
124: this .listComp.dispose();
125: this .listComp = null;
126:
127: this .removeButton.dispose();
128: this .removeButton = null;
129:
130: this .itemComp.dispose();
131: this .itemComp = null;
132:
133: this .posComp.dispose();
134: this .posComp = null;
135:
136: this .updateButton.dispose();
137: this .updateButton = null;
138:
139: this .insertButton.dispose();
140: this .insertButton = null;
141: }
142:
143: public void handleEvent(Event evt) {
144: EventTarget currentTarget = evt.getCurrentTarget();
145: if (currentTarget == startButton.getHTMLInputElement()) {
146: new FuncTestUsingBrowserExample(this ).startTest();
147: } else
148: normalBehavior(evt);
149: }
150:
151: public void normalBehavior(Event evt) {
152: EventTarget currentTarget = evt.getCurrentTarget();
153: if (currentTarget == removeButton.getHTMLInputElement()) {
154: DefaultListModel dataModel = (DefaultListModel) listComp
155: .getListModel();
156: ListSelectionModel selModel = listComp
157: .getListSelectionModel();
158: if (!selModel.isSelectionEmpty()) {
159: // Selection Model is in SINGLE_INTERVAL_SELECTION mode
160: int min = selModel.getMinSelectionIndex();
161: int max = selModel.getMaxSelectionIndex();
162: dataModel.removeRange(min, max);
163: }
164: } else if ((currentTarget == updateButton.getHTMLInputElement())
165: || (currentTarget == insertButton.getHTMLInputElement())) {
166: String newItem = itemComp.getText();
167: int pos;
168: try {
169: pos = Integer.parseInt(posComp.getText());
170: DefaultListModel dataModel = (DefaultListModel) listComp
171: .getListModel();
172: if (currentTarget == updateButton.getHTMLInputElement())
173: dataModel.setElementAt(newItem, pos);
174: else
175: dataModel.insertElementAt(newItem, pos);
176: } catch (NumberFormatException ex) {
177: getItsNatDocument().addCodeToSend(
178: "alert('Bad Position')");
179: } catch (ArrayIndexOutOfBoundsException ex) {
180: getItsNatDocument().addCodeToSend(
181: "alert('Bad Position')");
182: }
183: }
184: }
185:
186: public void intervalAdded(ListDataEvent e) {
187: }
188:
189: public void intervalRemoved(ListDataEvent e) {
190: }
191:
192: public void contentsChanged(ListDataEvent e) {
193: }
194:
195: public void valueChanged(ListSelectionEvent e) {
196: if (e.getValueIsAdjusting())
197: return;
198:
199: int index = listComp.getSelectedIndex(); // First selected
200: if (index != -1) {
201: Object value = listComp.getListModel().getElementAt(index);
202: itemComp.setText(value.toString());
203: posComp.setText(Integer.toString(index));
204: }
205: }
206:
207: }
|