001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.designstudio.editors.actionsequence.pages.parameters;
014:
015: import java.util.Arrays;
016: import java.util.Iterator;
017: import java.util.Vector;
018:
019: import javax.swing.table.DefaultTableModel;
020: import javax.swing.table.TableModel;
021:
022: import org.eclipse.swt.graphics.Point;
023: import org.eclipse.swt.graphics.Rectangle;
024: import org.pentaho.actionsequence.dom.ActionSequenceInput;
025:
026: import de.kupzog.ktable.KTable;
027: import de.kupzog.ktable.KTableCellEditor;
028: import de.kupzog.ktable.KTableCellRenderer;
029: import de.kupzog.ktable.KTableDefaultModel;
030: import de.kupzog.ktable.editors.KTableCellEditorText;
031: import de.kupzog.ktable.renderers.FixedCellRenderer;
032: import de.kupzog.ktable.renderers.TextCellRenderer;
033:
034: public class InputTableModel extends KTableDefaultModel {
035:
036: private static final String COLUMN_HDR_0 = ""; //$NON-NLS-1$
037: private static final DefaultTableModel EMPTY_TABLE_MODEL = new DefaultTableModel(
038: new String[] { COLUMN_HDR_0 }, 0);
039: static final String NEW_ROW_FILLER = new String(""); //$NON-NLS-1$
040:
041: ActionSequenceInput actionSequenceInput = null;
042: boolean saveAsPropertyMapList;
043: final FixedCellRenderer headerRenderer = new FixedCellRenderer(
044: FixedCellRenderer.STYLE_FLAT
045: | FixedCellRenderer.INDICATION_CLICKED
046: | FixedCellRenderer.INDICATION_FOCUS);
047:
048: final TextCellRenderer cellRenderer = new TextCellRenderer(
049: TextCellRenderer.INDICATION_FOCUS);
050:
051: final DataCellEditor cellEditor = new DataCellEditor();
052:
053: final HeaderCellEditor headerEditor = new HeaderCellEditor();
054:
055: class HeaderCellEditor extends KTableCellEditorText {
056:
057: public void close(boolean save) {
058: if (save && (m_Text != null)) {
059: String text = m_Text.getText().trim();
060: save = (text.length() > 0) && (text.indexOf(" ") == -1); //$NON-NLS-1$
061: }
062: super .close(save);
063: }
064:
065: public int getActivationSignals() {
066: return DOUBLECLICK;
067: }
068: }
069:
070: class DataCellEditor extends KTableCellEditorText {
071: boolean isOpen = false;
072:
073: public void open(KTable table, int col, int row, Rectangle rect) {
074: isOpen = true;
075: boolean openComplete = false;
076: try {
077: super .open(table, col, row, rect);
078: openComplete = true;
079: } finally {
080: if (!openComplete) {
081: isOpen = false;
082: }
083: }
084: }
085:
086: public void close(boolean save) {
087: isOpen = false;
088: super .close(save);
089: if (save) {
090: if ((m_Row == getRowCount() - 1)
091: && (m_Col == getColumnCount() - 1)) {
092: String[] newRow = new String[tableModel
093: .getColumnCount()];
094: Arrays.fill(newRow, NEW_ROW_FILLER);
095: tableModel.addRow(newRow);
096: }
097: if (m_Col == getColumnCount() - 1) {
098: m_Table.setSelection(getFixedColumnCount(),
099: m_Row + 1, true);
100: } else {
101: m_Table.setSelection(m_Col + 1, m_Row, true);
102: }
103: }
104: }
105: }
106:
107: DefaultTableModel tableModel = EMPTY_TABLE_MODEL;
108:
109: public InputTableModel(boolean saveAsPropertyMapList) {
110: this .saveAsPropertyMapList = saveAsPropertyMapList;
111: initialize();
112: }
113:
114: public Object doGetContentAt(int col, int row) {
115: Object value = ""; //$NON-NLS-1$
116: if ((row >= 0) && (row < getRowCount()) && (col >= 0)
117: && (col < getColumnCount())) {
118: if (row < getFixedHeaderRowCount()) {
119: value = tableModel.getColumnName(col
120: - getFixedHeaderColumnCount());
121: } else {
122: value = tableModel.getValueAt(row
123: - getFixedHeaderRowCount(), col
124: - getFixedHeaderColumnCount());
125: }
126: }
127: return value == null ? "" : value.toString(); //$NON-NLS-1$
128: }
129:
130: public KTableCellEditor doGetCellEditor(int col, int row) {
131: KTableCellEditor editor = null;
132: if (col >= getFixedColumnCount() && row >= getFixedRowCount()) {
133: editor = cellEditor;
134: } else {
135: editor = headerEditor;
136: }
137: return editor;
138: }
139:
140: public void doSetContentAt(int col, int row, Object value) {
141: if ((row >= 0) && (row < getRowCount()) && (col >= 0)
142: && (col < getColumnCount())) {
143: if (row < getFixedHeaderRowCount()) {
144: if (value.toString().length() > 0) {
145: if (tableModel == EMPTY_TABLE_MODEL) {
146: tableModel = new DefaultTableModel(
147: new String[] { value.toString() }, 0);
148: } else {
149: String[] columnNames = new String[getColumnCount()
150: - getFixedHeaderColumnCount()];
151: for (int i = 0; i < columnNames.length; i++) {
152: columnNames[i] = tableModel
153: .getColumnName(i);
154: }
155: columnNames[col - getFixedHeaderColumnCount()] = value
156: .toString();
157: tableModel.setColumnIdentifiers(columnNames);
158: }
159: updateActionSequence();
160: }
161: } else if (row < getRowCount()) {
162: tableModel.setValueAt(value, row
163: - getFixedHeaderRowCount(), col
164: - getFixedHeaderColumnCount());
165: updateActionSequence();
166: }
167: }
168: }
169:
170: public int doGetRowCount() {
171: return getFixedRowCount() + tableModel.getRowCount();
172: }
173:
174: public int getFixedHeaderRowCount() {
175: return 1;
176: }
177:
178: public int doGetColumnCount() {
179: return tableModel.getColumnCount()
180: + getFixedHeaderColumnCount();
181: }
182:
183: public int getFixedHeaderColumnCount() {
184: return 0;
185: }
186:
187: public int getFixedSelectableRowCount() {
188: return 0;
189: }
190:
191: public int getFixedSelectableColumnCount() {
192: return 0;
193: }
194:
195: public boolean isColumnResizable(int col) {
196: return true;
197: }
198:
199: public boolean isRowResizable(int row) {
200: return false;
201: }
202:
203: public int getRowHeightMinimum() {
204: return 22;
205: }
206:
207: public KTableCellRenderer doGetCellRenderer(int col, int row) {
208: if (isFixedCell(col, row))
209: return headerRenderer;
210:
211: return cellRenderer;
212: }
213:
214: public Point doBelongsToCell(int col, int row) {
215: return null;
216: }
217:
218: public int getInitialColumnWidth(int column) {
219: return 100;
220: }
221:
222: public int getInitialRowHeight(int row) {
223: return 22;
224: }
225:
226: void setInput(ActionSequenceInput actionSequenceInput) {
227: this .actionSequenceInput = actionSequenceInput;
228: refresh();
229: }
230:
231: void refresh() {
232: if (actionSequenceInput != null) {
233: Object defaultValue = actionSequenceInput.getDefaultValue();
234: if (defaultValue instanceof TableModel) {
235: tableModel = (DefaultTableModel) defaultValue;
236: if (tableModel.getColumnCount() == 0) {
237: tableModel = EMPTY_TABLE_MODEL;
238: }
239: } else {
240: tableModel = EMPTY_TABLE_MODEL;
241: }
242: }
243: }
244:
245: protected void updateActionSequence() {
246: if (tableModel == EMPTY_TABLE_MODEL) {
247: actionSequenceInput.setDefaultValue(
248: new DefaultTableModel(), saveAsPropertyMapList);
249: } else {
250: actionSequenceInput.setDefaultValue(tableModel,
251: saveAsPropertyMapList);
252: }
253: }
254:
255: public void moveRow(int from, int to) {
256: if (from != to) {
257: if ((from < getFixedHeaderRowCount())
258: || (to < getFixedHeaderRowCount())
259: || (from >= getRowCount()) || (to >= getRowCount())) {
260: throw new IndexOutOfBoundsException();
261: }
262:
263: Vector rowVector = (Vector) tableModel.getDataVector().get(
264: from - getFixedHeaderRowCount());
265: tableModel.removeRow(from - getFixedHeaderRowCount());
266: tableModel.insertRow(to - getFixedHeaderRowCount(),
267: rowVector);
268: }
269: }
270:
271: public void insertRow(int row) {
272: if (row >= getRowCount()) {
273: throw new IndexOutOfBoundsException();
274: }
275: String[] rowData = new String[getColumnCount()
276: - getFixedHeaderColumnCount()];
277: Arrays.fill(rowData, ""); //$NON-NLS-1$
278: tableModel.insertRow(row - getFixedHeaderRowCount(), rowData);
279: updateActionSequence();
280: }
281:
282: public void addRow() {
283: if (tableModel == EMPTY_TABLE_MODEL) {
284: tableModel = new DefaultTableModel(
285: new String[] { "New_Column" }, 0); //$NON-NLS-1$
286: }
287: String[] rowData = new String[getColumnCount()
288: - getFixedHeaderColumnCount()];
289: Arrays.fill(rowData, ""); //$NON-NLS-1$
290: tableModel.addRow(rowData);
291: updateActionSequence();
292: }
293:
294: public void addColumn() {
295: if (tableModel == EMPTY_TABLE_MODEL) {
296: tableModel = new DefaultTableModel(
297: new String[] { "New_Column" }, 0); //$NON-NLS-1$
298: } else {
299: Vector columnsVector = new Vector();
300: for (int i = 0; i < tableModel.getColumnCount(); i++) {
301: columnsVector.add(tableModel.getColumnName(i));
302: }
303: String newName = "New_Column"; //$NON-NLS-1$
304: int index = 1;
305: while (columnsVector.contains(newName)) {
306: newName = "New_Column" + index++; //$NON-NLS-1$
307: }
308: columnsVector.add(newName);
309:
310: Vector dataVector = tableModel.getDataVector();
311: for (Iterator iter = dataVector.iterator(); iter.hasNext();) {
312: Vector rowVector = (Vector) iter.next();
313: rowVector.add(""); //$NON-NLS-1$
314: }
315: tableModel.setDataVector(dataVector, columnsVector);
316: }
317: updateActionSequence();
318: }
319:
320: public void insertColumn(int column) {
321: if (column >= getColumnCount()) {
322: throw new IndexOutOfBoundsException();
323: }
324:
325: Vector columnsVector = new Vector();
326: for (int i = 0; i < tableModel.getColumnCount(); i++) {
327: columnsVector.add(tableModel.getColumnName(i));
328: }
329: String newName = "New_Column"; //$NON-NLS-1$
330: int index = 1;
331: while (columnsVector.contains(newName)) {
332: newName = "New_Column" + index++; //$NON-NLS-1$
333: }
334: columnsVector
335: .add(column - getFixedHeaderColumnCount(), newName);
336:
337: Vector newDataVector = new Vector();
338: Vector dataVector = tableModel.getDataVector();
339: for (Iterator iter = dataVector.iterator(); iter.hasNext();) {
340: Vector newRowVector = new Vector((Vector) iter.next());
341: newRowVector.insertElementAt(
342: "", column - getFixedHeaderColumnCount()); //$NON-NLS-1$
343: newDataVector.add(newRowVector);
344: }
345: tableModel.setDataVector(newDataVector, columnsVector);
346: updateActionSequence();
347: }
348:
349: public void deleteRow(int row) {
350: if (row >= getRowCount()) {
351: throw new IndexOutOfBoundsException();
352: }
353: tableModel.removeRow(row - getFixedHeaderRowCount());
354: updateActionSequence();
355: }
356:
357: public void deleteColumn(int column) {
358: if (column >= getColumnCount()) {
359: throw new IndexOutOfBoundsException();
360: }
361:
362: Vector columnsVector = new Vector();
363: for (int i = 0; i < tableModel.getColumnCount(); i++) {
364: columnsVector.add(tableModel.getColumnName(i));
365: }
366:
367: columnsVector.remove(column - getFixedHeaderColumnCount());
368:
369: Vector dataVector = tableModel.getDataVector();
370: for (Iterator iter = dataVector.iterator(); iter.hasNext();) {
371: Vector rowVector = (Vector) iter.next();
372: rowVector.remove(column - getFixedHeaderColumnCount());
373: }
374: tableModel.setDataVector(dataVector, columnsVector);
375: if (tableModel.getColumnCount() == 0) {
376: tableModel = EMPTY_TABLE_MODEL;
377: }
378:
379: updateActionSequence();
380: }
381: }
|