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 java.awt.event.ItemEvent;
019: import java.awt.event.ItemListener;
020: import javax.swing.DefaultComboBoxModel;
021: import javax.swing.event.ListDataEvent;
022: import javax.swing.event.ListDataListener;
023: import org.itsnat.comp.ItsNatComponentManager;
024: import org.itsnat.comp.free.ItsNatFreeComboBox;
025: import org.itsnat.comp.html.ItsNatHTMLInputButton;
026: import org.itsnat.comp.html.ItsNatHTMLInputText;
027: import org.itsnat.core.ItsNatDocument;
028: import org.itsnat.feashow.FeatureTreeNode;
029: import org.w3c.dom.events.Event;
030: import org.w3c.dom.events.EventListener;
031: import org.w3c.dom.events.EventTarget;
032:
033: public class FreeComboBoxTreeNode extends FeatureTreeNode implements
034: EventListener, ListDataListener, ItemListener {
035: protected ItsNatFreeComboBox comboComp;
036: protected ItsNatHTMLInputButton removeButton;
037: protected ItsNatHTMLInputText itemComp;
038: protected ItsNatHTMLInputText posComp;
039: protected ItsNatHTMLInputButton insertButton;
040:
041: public FreeComboBoxTreeNode() {
042: }
043:
044: public void startExamplePanel() {
045: ItsNatDocument itsNatDoc = getItsNatDocument();
046: ItsNatComponentManager componentMgr = itsNatDoc
047: .getItsNatComponentManager();
048:
049: this .comboComp = (ItsNatFreeComboBox) componentMgr
050: .createItsNatComponentById("compId", "freeComboBox",
051: null);
052:
053: DefaultComboBoxModel dataModel = (DefaultComboBoxModel) comboComp
054: .getComboBoxModel();
055: dataModel.addElement("Madrid");
056: dataModel.addElement("Sevilla");
057: dataModel.addElement("Segovia");
058: dataModel.addElement("Barcelona");
059: dataModel.addElement("Oviedo");
060: dataModel.addElement("Valencia");
061:
062: comboComp.addItemListener(new ComboBoxSelectionDecorator(
063: comboComp));
064:
065: dataModel.setSelectedItem("Segovia");
066:
067: comboComp.addEventListener("click", this );
068: dataModel.addListDataListener(this );
069: comboComp.addItemListener(this );
070:
071: this .removeButton = (ItsNatHTMLInputButton) componentMgr
072: .createItsNatComponentById("removeId");
073: removeButton.addEventListener("click", this );
074:
075: this .itemComp = (ItsNatHTMLInputText) componentMgr
076: .createItsNatComponentById("newItemId");
077: itemComp.setText(dataModel.getSelectedItem().toString());
078:
079: this .posComp = (ItsNatHTMLInputText) componentMgr
080: .createItsNatComponentById("posId");
081: posComp.setText(Integer.toString(comboComp.getSelectedIndex()));
082:
083: this .insertButton = (ItsNatHTMLInputButton) componentMgr
084: .createItsNatComponentById("insertId");
085: insertButton.addEventListener("click", this );
086: }
087:
088: public void endExamplePanel() {
089: this .comboComp.dispose();
090: this .comboComp = null;
091:
092: this .removeButton.dispose();
093: this .removeButton = null;
094:
095: this .itemComp.dispose();
096: this .itemComp = null;
097:
098: this .posComp.dispose();
099: this .posComp = null;
100:
101: this .insertButton.dispose();
102: this .insertButton = null;
103: }
104:
105: public void handleEvent(Event evt) {
106: log(evt.getCurrentTarget() + " " + evt.getType());
107:
108: EventTarget currentTarget = evt.getCurrentTarget();
109: if (currentTarget == removeButton.getHTMLInputElement()) {
110: DefaultComboBoxModel dataModel = (DefaultComboBoxModel) comboComp
111: .getComboBoxModel();
112: Object selected = dataModel.getSelectedItem();
113: if (selected != null)
114: dataModel.removeElement(selected);
115: } else if (currentTarget == insertButton.getHTMLInputElement()) {
116: String newItem = itemComp.getText();
117: if (comboComp.indexOf(newItem) != -1)
118: getItsNatDocument().addCodeToSend(
119: "alert('Duplicated Element')");
120: else {
121: int pos;
122: try {
123: pos = Integer.parseInt(posComp.getText());
124: DefaultComboBoxModel dataModel = (DefaultComboBoxModel) comboComp
125: .getComboBoxModel();
126: dataModel.insertElementAt(newItem, pos);
127: comboComp.setSelectedIndex(pos);
128: } catch (NumberFormatException ex) {
129: getItsNatDocument().addCodeToSend(
130: "alert('Bad Position')");
131: } catch (ArrayIndexOutOfBoundsException ex) {
132: getItsNatDocument().addCodeToSend(
133: "alert('Bad Position')");
134: }
135: }
136: }
137: }
138:
139: public void intervalAdded(ListDataEvent e) {
140: listChangedLog(e);
141: }
142:
143: public void intervalRemoved(ListDataEvent e) {
144: listChangedLog(e);
145: }
146:
147: public void contentsChanged(ListDataEvent e) {
148: listChangedLog(e);
149: }
150:
151: public void listChangedLog(ListDataEvent e) {
152: int index0 = e.getIndex0();
153: int index1 = e.getIndex1();
154:
155: String action = "";
156: int type = e.getType();
157: switch (type) {
158: case ListDataEvent.INTERVAL_ADDED:
159: action = "added";
160: break;
161: case ListDataEvent.INTERVAL_REMOVED:
162: action = "removed";
163: break;
164: case ListDataEvent.CONTENTS_CHANGED:
165: action = "changed";
166: break;
167: }
168:
169: String interval = "";
170: if (index0 != -1)
171: interval = "interval " + index0 + "-" + index1;
172:
173: log(e.getClass().toString() + " " + action + " " + interval);
174: }
175:
176: public void itemStateChanged(ItemEvent e) {
177: String fact;
178: int state = e.getStateChange();
179: if (state == ItemEvent.SELECTED)
180: fact = "selected";
181: else
182: fact = "deselected";
183:
184: log(e.getClass().toString() + " " + fact + " " + e.getItem());
185:
186: if (state == ItemEvent.SELECTED) {
187: itemComp.setText(e.getItem().toString());
188: posComp.setText(Integer.toString(comboComp
189: .getSelectedIndex()));
190: }
191: }
192: }
|