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: package org.itsnat.feashow.features.components.tables;
016:
017: import java.util.Collections;
018: import java.util.Comparator;
019: import java.util.Vector;
020: import javax.swing.ListSelectionModel;
021: import javax.swing.event.ListSelectionEvent;
022: import javax.swing.event.ListSelectionListener;
023: import javax.swing.event.TableModelEvent;
024: import javax.swing.event.TableModelListener;
025: import javax.swing.table.DefaultTableModel;
026: import org.itsnat.comp.ItsNatComponentManager;
027: import org.itsnat.comp.html.ItsNatHTMLInputButton;
028: import org.itsnat.comp.html.ItsNatHTMLInputText;
029: import org.itsnat.comp.html.ItsNatHTMLTable;
030: import org.itsnat.core.ItsNatDocument;
031: import org.itsnat.feashow.FeatureTreeNode;
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 TableTreeNode extends FeatureTreeNode implements
037: EventListener, TableModelListener, ListSelectionListener {
038: protected ItsNatHTMLTable tableComp;
039: protected ItsNatHTMLInputButton removeButton;
040: protected ItsNatHTMLInputText[] newItemComp;
041: protected ItsNatHTMLInputText posComp;
042: protected ItsNatHTMLInputButton updateButton;
043: protected ItsNatHTMLInputButton insertButton;
044:
045: public TableTreeNode() {
046: }
047:
048: public void startExamplePanel() {
049: ItsNatDocument itsNatDoc = getItsNatDocument();
050: ItsNatComponentManager componentMgr = itsNatDoc
051: .getItsNatComponentManager();
052:
053: this .tableComp = (ItsNatHTMLTable) componentMgr
054: .createItsNatComponentById("compId");
055:
056: DefaultTableModel dataModel = (DefaultTableModel) tableComp
057: .getTableModel();
058: dataModel.addColumn("City");
059: dataModel.addColumn("Public square");
060: dataModel.addColumn("Monument");
061: dataModel.addRow(new String[] { "Madrid", "Plaza Mayor",
062: "Palacio Real" });
063: dataModel.addRow(new String[] { "Sevilla", "Plaza de Espaņa",
064: "Giralda" });
065: dataModel.addRow(new String[] { "Segovia",
066: "Plaza del Azoguejo", "Acueducto Romano" });
067:
068: ListSelectionModel rowSelModel = tableComp
069: .getRowSelectionModel();
070: // ListSelectionModel columnSelModel = tableComp.getColumnSelectionModel();
071:
072: rowSelModel
073: .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
074: // columnSelModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
075:
076: tableComp.setRowSelectionAllowed(true);
077: tableComp.setColumnSelectionAllowed(false);
078:
079: rowSelModel
080: .addListSelectionListener(new TableRowSelectionDecoration(
081: tableComp));
082:
083: rowSelModel.setSelectionInterval(1, 1);
084:
085: dataModel.addTableModelListener(this );
086: rowSelModel.addListSelectionListener(this );
087:
088: ListSelectionModel headSelModel = tableComp
089: .getItsNatTableHeader().getListSelectionModel();
090: headSelModel.addListSelectionListener(this );
091:
092: this .removeButton = (ItsNatHTMLInputButton) componentMgr
093: .createItsNatComponentById("removeId");
094: removeButton.addEventListener("click", this );
095:
096: this .newItemComp = new ItsNatHTMLInputText[dataModel
097: .getColumnCount()];
098: for (int i = 0; i < newItemComp.length; i++) {
099: this .newItemComp[i] = (ItsNatHTMLInputText) componentMgr
100: .createItsNatComponentById("itemId" + i);
101: newItemComp[i].setText(dataModel.getValueAt(
102: rowSelModel.getMinSelectionIndex(), i).toString());
103: }
104:
105: this .posComp = (ItsNatHTMLInputText) componentMgr
106: .createItsNatComponentById("posId");
107: posComp.setText(Integer.toString(rowSelModel
108: .getMinSelectionIndex()));
109:
110: this .updateButton = (ItsNatHTMLInputButton) componentMgr
111: .createItsNatComponentById("updateId");
112: updateButton.addEventListener("click", this );
113:
114: this .insertButton = (ItsNatHTMLInputButton) componentMgr
115: .createItsNatComponentById("insertId");
116: insertButton.addEventListener("click", this );
117: }
118:
119: public void endExamplePanel() {
120: this .tableComp.dispose();
121: this .tableComp = null;
122:
123: this .removeButton.dispose();
124: this .removeButton = null;
125:
126: for (int i = 0; i < newItemComp.length; i++) {
127: this .newItemComp[i].dispose();
128: this .newItemComp[i] = null;
129: }
130:
131: this .posComp.dispose();
132: this .posComp = null;
133:
134: this .updateButton.dispose();
135: this .updateButton = null;
136:
137: this .insertButton.dispose();
138: this .insertButton = null;
139: }
140:
141: public void handleEvent(Event evt) {
142: log(evt.getCurrentTarget() + " " + evt.getType());
143:
144: EventTarget currentTarget = evt.getCurrentTarget();
145: if (currentTarget == removeButton.getHTMLInputElement()) {
146: DefaultTableModel dataModel = (DefaultTableModel) tableComp
147: .getTableModel();
148: ListSelectionModel rowSelModel = tableComp
149: .getRowSelectionModel();
150: if (!rowSelModel.isSelectionEmpty()) {
151: // Selection Model is in SINGLE_INTERVAL_SELECTION mode
152: int min = rowSelModel.getMinSelectionIndex();
153: int max = rowSelModel.getMaxSelectionIndex();
154: for (int i = 0; i <= max - min; i++)
155: dataModel.removeRow(min);
156: }
157: } else if ((currentTarget == updateButton.getHTMLInputElement())
158: || (currentTarget == insertButton.getHTMLInputElement())) {
159: String[] newItem = new String[newItemComp.length];
160: for (int i = 0; i < newItemComp.length; i++)
161: newItem[i] = newItemComp[i].getText();
162:
163: int row;
164: try {
165: row = Integer.parseInt(posComp.getText());
166: DefaultTableModel dataModel = (DefaultTableModel) tableComp
167: .getTableModel();
168: if (currentTarget == updateButton.getHTMLInputElement()) {
169: for (int i = 0; i < newItemComp.length; i++)
170: dataModel.setValueAt(newItem[i], row, i);
171: } else
172: dataModel.insertRow(row, newItem);
173: } catch (NumberFormatException ex) {
174: getItsNatDocument().addCodeToSend(
175: "alert('Bad Position')");
176: } catch (ArrayIndexOutOfBoundsException ex) {
177: getItsNatDocument().addCodeToSend(
178: "alert('Bad Position')");
179: }
180: }
181: }
182:
183: public void tableChanged(TableModelEvent e) {
184: int firstRow = e.getFirstRow();
185: int lastRow = e.getLastRow();
186:
187: String action = "";
188: int type = e.getType();
189: switch (type) {
190: case TableModelEvent.INSERT:
191: action = "added";
192: break;
193: case TableModelEvent.DELETE:
194: action = "removed";
195: break;
196: case TableModelEvent.UPDATE:
197: action = "changed";
198: break;
199: }
200:
201: String interval = "";
202: if (firstRow != -1)
203: interval = "interval " + firstRow + "-" + lastRow;
204:
205: log(e.getClass().toString() + " " + action + " " + interval);
206: }
207:
208: public void valueChanged(ListSelectionEvent e) {
209: if (e.getValueIsAdjusting())
210: return;
211:
212: int first = e.getFirstIndex();
213: int last = e.getLastIndex();
214:
215: ListSelectionModel selModel = (ListSelectionModel) e
216: .getSource();
217: if (selModel == tableComp.getRowSelectionModel()) {
218: String fact = "";
219: for (int i = first; i <= last; i++) {
220: boolean selected = selModel.isSelectedIndex(i);
221: if (selected)
222: fact += ", selected ";
223: else
224: fact += ", deselected ";
225: fact += i;
226: }
227:
228: log(e.getClass().toString() + " " + fact);
229:
230: int row = selModel.getMinSelectionIndex(); // First selected
231: if (row != -1) {
232: for (int i = 0; i < newItemComp.length; i++) {
233: Object value = tableComp.getTableModel()
234: .getValueAt(row, i);
235: newItemComp[i].setText(value.toString());
236: }
237: posComp.setText(Integer.toString(row));
238: }
239: } else if (selModel == tableComp.getItsNatTableHeader()
240: .getListSelectionModel()) {
241: int selected = -1;
242: for (int i = first; i <= last; i++) {
243: if (selModel.isSelectedIndex(i))
244: selected = i;
245: }
246: final int selectedCol = selected;
247: DefaultTableModel dataModel = (DefaultTableModel) tableComp
248: .getTableModel();
249: Vector vector = dataModel.getDataVector();
250: Comparator comp = new Comparator() {
251: public int compare(Object o1, Object o2) {
252: Vector row1 = (Vector) o1;
253: Vector row2 = (Vector) o2;
254: String item1 = (String) row1.get(selectedCol);
255: String item2 = (String) row2.get(selectedCol);
256: return item1.compareTo(item2);
257: }
258: };
259: Collections.sort(vector, comp);
260: dataModel.fireTableStructureChanged();
261: }
262: }
263:
264: }
|