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