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