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.itsnat.feashow.features.components.shared.Person;
031: import org.w3c.dom.Element;
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 FreeListCompoundTreeNode extends FeatureTreeNode implements
037: EventListener, ListDataListener, ListSelectionListener {
038: protected ItsNatFreeListMultSel listComp;
039: protected ItsNatHTMLInputButton removeButton;
040: protected ItsNatHTMLInputText personFirstNameComp;
041: protected ItsNatHTMLInputText personLastNameComp;
042: protected ItsNatHTMLInputText posComp;
043: protected ItsNatHTMLInputButton updateButton;
044: protected ItsNatHTMLInputButton insertButton;
045:
046: public FreeListCompoundTreeNode() {
047: }
048:
049: public void startExamplePanel() {
050: ItsNatDocument itsNatDoc = getItsNatDocument();
051: ItsNatComponentManager componentMgr = itsNatDoc
052: .getItsNatComponentManager();
053:
054: this .listComp = (ItsNatFreeListMultSel) componentMgr
055: .createItsNatComponentById("compId", "freeListMultSel",
056: null);
057:
058: listComp.setItsNatListCellRenderer(new PersonRenderer());
059: listComp.setItsNatListCellEditor(null);// disable
060:
061: DefaultListModel dataModel = (DefaultListModel) listComp
062: .getListModel();
063:
064: dataModel.addElement(new Person("Meredith", "Grey"));
065: dataModel.addElement(new Person("Cristina", "Yang"));
066: dataModel.addElement(new Person("Izzie", "Stevens"));
067: dataModel.addElement(new Person("Alex", "Karev"));
068: dataModel.addElement(new Person("George", "O'Malley"));
069: dataModel.addElement(new Person("Derek", "Shepherd"));
070:
071: ListSelectionModel selModel = listComp.getListSelectionModel();
072: selModel
073: .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
074: selModel.setSelectionInterval(2, 3);
075:
076: listComp.addEventListener("click", this );
077: selModel.addListSelectionListener(this );
078: dataModel.addListDataListener(this );
079:
080: this .removeButton = (ItsNatHTMLInputButton) componentMgr
081: .createItsNatComponentById("removeId");
082: removeButton.addEventListener("click", this );
083:
084: Person person = (Person) listComp.getListModel().getElementAt(
085: listComp.getSelectedIndex());
086: this .personFirstNameComp = (ItsNatHTMLInputText) componentMgr
087: .createItsNatComponentById("personFirstNameId");
088: personFirstNameComp.setText(person.getFirstName());
089:
090: this .personLastNameComp = (ItsNatHTMLInputText) componentMgr
091: .createItsNatComponentById("personLastNameId");
092: personLastNameComp.setText(person.getLastName());
093:
094: this .posComp = (ItsNatHTMLInputText) componentMgr
095: .createItsNatComponentById("posId");
096: posComp.setText(Integer.toString(listComp.getSelectedIndex()));
097:
098: this .updateButton = (ItsNatHTMLInputButton) componentMgr
099: .createItsNatComponentById("updateId");
100: updateButton.addEventListener("click", this );
101:
102: this .insertButton = (ItsNatHTMLInputButton) componentMgr
103: .createItsNatComponentById("insertId");
104: insertButton.addEventListener("click", this );
105:
106: for (int i = 0; i < dataModel.getSize(); i++) {
107: decorateSelection(i, selModel.isSelectedIndex(i));
108: }
109: }
110:
111: public void endExamplePanel() {
112: Element parentElem = listComp.getElement();
113: listComp.getItsNatComponentManager().removeItsNatComponents(
114: parentElem, true); // Frees remaining person related components (text boxes)
115:
116: this .listComp.dispose();
117: this .listComp = null;
118:
119: this .removeButton.dispose();
120: this .removeButton = null;
121:
122: this .personFirstNameComp.dispose();
123: this .personFirstNameComp = null;
124:
125: this .personLastNameComp.dispose();
126: this .personLastNameComp = null;
127:
128: this .posComp.dispose();
129: this .posComp = null;
130:
131: this .updateButton.dispose();
132: this .updateButton = null;
133:
134: this .insertButton.dispose();
135: this .insertButton = null;
136: }
137:
138: public void handleEvent(Event evt) {
139: log(evt.getCurrentTarget() + " " + evt.getType());
140:
141: EventTarget currentTarget = evt.getCurrentTarget();
142: if (currentTarget == removeButton.getHTMLInputElement()) {
143: DefaultListModel dataModel = (DefaultListModel) listComp
144: .getListModel();
145: ListSelectionModel selModel = listComp
146: .getListSelectionModel();
147: if (!selModel.isSelectionEmpty()) {
148: // Selection Model is in SINGLE_INTERVAL_SELECTION mode
149: int min = selModel.getMinSelectionIndex();
150: int max = selModel.getMaxSelectionIndex();
151: dataModel.removeRange(min, max);
152: }
153: } else if ((currentTarget == updateButton.getHTMLInputElement())
154: || (currentTarget == insertButton.getHTMLInputElement())) {
155: String firstName = personFirstNameComp.getText();
156: String lastName = personLastNameComp.getText();
157: int pos;
158: try {
159: pos = Integer.parseInt(posComp.getText());
160: DefaultListModel dataModel = (DefaultListModel) listComp
161: .getListModel();
162: if (currentTarget == updateButton.getHTMLInputElement()) {
163: Person person = (Person) dataModel
164: .getElementAt(pos);
165: person.setFirstName(firstName);
166: person.setLastName(lastName);
167: dataModel.setElementAt(person, pos); // To notify the changes (fires an event)
168: } else {
169: Person person = new Person(firstName, lastName);
170: dataModel.insertElementAt(person, pos);
171: }
172: } catch (NumberFormatException ex) {
173: getItsNatDocument().addCodeToSend(
174: "alert('Bad Position')");
175: } catch (ArrayIndexOutOfBoundsException ex) {
176: getItsNatDocument().addCodeToSend(
177: "alert('Bad Position')");
178: }
179: }
180: }
181:
182: public void intervalAdded(ListDataEvent e) {
183: listChangedLog(e);
184: }
185:
186: public void intervalRemoved(ListDataEvent e) {
187: listChangedLog(e);
188: }
189:
190: public void contentsChanged(ListDataEvent e) {
191: listChangedLog(e);
192: }
193:
194: public void listChangedLog(ListDataEvent e) {
195: int index0 = e.getIndex0();
196: int index1 = e.getIndex1();
197:
198: String action = "";
199: int type = e.getType();
200: switch (type) {
201: case ListDataEvent.INTERVAL_ADDED:
202: action = "added";
203: break;
204: case ListDataEvent.INTERVAL_REMOVED:
205: action = "removed";
206: break;
207: case ListDataEvent.CONTENTS_CHANGED:
208: action = "changed";
209: break;
210: }
211:
212: String interval = "";
213: if (index0 != -1)
214: interval = "interval " + index0 + "-" + index1;
215:
216: log(e.getClass().toString() + " " + action + " " + interval);
217: }
218:
219: public void valueChanged(ListSelectionEvent e) {
220: if (e.getValueIsAdjusting())
221: return;
222:
223: int first = e.getFirstIndex();
224: int last = e.getLastIndex();
225:
226: ListSelectionModel selModel = listComp.getListSelectionModel();
227: String fact = "";
228: for (int i = first; i <= last; i++) {
229: boolean selected = selModel.isSelectedIndex(i);
230: if (selected)
231: fact += ", selected ";
232: else
233: fact += ", deselected ";
234: fact += i;
235: }
236:
237: log(e.getClass().toString() + " " + fact);
238:
239: int index = listComp.getSelectedIndex(); // First selected
240: if (index != -1) {
241: Person person = (Person) listComp.getListModel()
242: .getElementAt(index);
243: personFirstNameComp.setText(person.getFirstName());
244: personLastNameComp.setText(person.getLastName());
245: posComp.setText(Integer.toString(index));
246: }
247:
248: for (int i = first; i <= last; i++) {
249: decorateSelection(i, selModel.isSelectedIndex(i));
250: }
251: }
252:
253: public void decorateSelection(int index, boolean selected) {
254: Element elem = listComp.getItsNatListMultSelUI()
255: .getContentElementAt(index);
256: if (elem == null)
257: return;
258:
259: if (selected)
260: elem.setAttribute("style",
261: "background:rgb(0,0,255); color:white;");
262: else
263: elem.removeAttribute("style");
264: }
265:
266: }
|