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