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.ArrayList;
016: import java.util.Iterator;
017: import java.util.LinkedHashMap;
018: import java.util.Map;
019:
020: import org.eclipse.swt.graphics.Point;
021: import org.pentaho.actionsequence.dom.ActionSequenceInput;
022: import org.pentaho.designstudio.messages.Messages;
023:
024: import de.kupzog.ktable.KTableCellEditor;
025: import de.kupzog.ktable.KTableCellRenderer;
026: import de.kupzog.ktable.KTableDefaultModel;
027: import de.kupzog.ktable.editors.KTableCellEditorText;
028: import de.kupzog.ktable.renderers.FixedCellRenderer;
029: import de.kupzog.ktable.renderers.TextCellRenderer;
030:
031: public class InputMapModel extends KTableDefaultModel {
032:
033: static final String NEW_ROW_FILLER = new String(""); //$NON-NLS-1$
034: private static final String KEY_HEADER = Messages
035: .getString("InputMapModel.KEYS_COLUMN_HEADER"); //$NON-NLS-1$
036: private static final String VALUE_HEADER = Messages
037: .getString("InputMapModel.VALUES_COLUMN_HEADER"); //$NON-NLS-1$
038:
039: ActionSequenceInput actionSequenceInput = null;
040: LinkedHashMap map = new LinkedHashMap();
041: ArrayList keys = new ArrayList();
042:
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: class DataCellEditor extends KTableCellEditorText {
054:
055: public void close(boolean save) {
056: if (save && (m_Col == 0)) {
057: if (m_Text != null) {
058: save = m_Text.getText().trim().length() > 0;
059: }
060: }
061: super .close(save);
062: if (save) {
063: if ((m_Row == getRowCount() - 1)
064: && (m_Col == getColumnCount() - 1)) {
065: map.put(NEW_ROW_FILLER, NEW_ROW_FILLER);
066: keys.add(NEW_ROW_FILLER);
067: }
068: if (m_Col == getColumnCount() - 1) {
069: m_Table.setSelection(getFixedColumnCount(),
070: m_Row + 1, true);
071: } else {
072: m_Table.setSelection(m_Col + 1, m_Row, true);
073: }
074: }
075: }
076:
077: }
078:
079: public InputMapModel() {
080: initialize();
081: }
082:
083: public Object doGetContentAt(int col, int row) {
084: Object value = ""; //$NON-NLS-1$
085: if ((row >= 0) && (row < getRowCount())) {
086: if (col == 0) {
087: if (row < getFixedHeaderRowCount()) {
088: value = KEY_HEADER;
089: } else {
090: value = keys.get(row - getFixedHeaderRowCount());
091: }
092: } else if (col == 1) {
093: if (row < getFixedHeaderRowCount()) {
094: value = VALUE_HEADER;
095: } else {
096: value = map.get(keys.get(row
097: - getFixedHeaderRowCount()));
098: }
099: }
100: }
101: return value == null ? "" : value.toString(); //$NON-NLS-1$
102: }
103:
104: public KTableCellEditor doGetCellEditor(int col, int row) {
105: KTableCellEditor editor = null;
106: if (col >= getFixedColumnCount() && row >= getFixedRowCount()) {
107: editor = cellEditor;
108: }
109: return editor;
110: }
111:
112: public void doSetContentAt(int col, int row, Object value) {
113: if (col == 0) {
114: if (value.toString().trim().length() > 0) {
115: if ((row >= getFixedHeaderRowCount())
116: && (row < getRowCount())) {
117: Map oldMap = map;
118: map = new LinkedHashMap();
119: for (int index = 0; index < oldMap.size(); index++) {
120: if (index == row - getFixedHeaderRowCount()) {
121: map.put(value.toString(), oldMap.get(keys
122: .get(index)));
123: } else {
124: map.put(keys.get(index), oldMap.get(keys
125: .get(index)));
126: }
127: }
128: keys.clear();
129: keys.addAll(map.keySet());
130: updateActionSequence();
131: }
132: }
133: } else if (col == 1) {
134: if ((row >= getFixedHeaderRowCount())
135: && (row < getRowCount())) {
136: map.put(keys.get(row - getFixedHeaderRowCount()), value
137: .toString());
138: updateActionSequence();
139: }
140: }
141: }
142:
143: public int doGetRowCount() {
144: return getFixedRowCount() + map.size();
145: }
146:
147: public int getFixedHeaderRowCount() {
148: return 1;
149: }
150:
151: public int doGetColumnCount() {
152: return 2 + getFixedHeaderColumnCount();
153: }
154:
155: public int getFixedHeaderColumnCount() {
156: return 0;
157: }
158:
159: public int getFixedSelectableRowCount() {
160: return 0;
161: }
162:
163: public int getFixedSelectableColumnCount() {
164: return 0;
165: }
166:
167: public boolean isColumnResizable(int col) {
168: return true;
169: }
170:
171: public boolean isRowResizable(int row) {
172: return false;
173: }
174:
175: public int getRowHeightMinimum() {
176: return 22;
177: }
178:
179: public KTableCellRenderer doGetCellRenderer(int col, int row) {
180: if (isFixedCell(col, row))
181: return headerRenderer;
182:
183: return cellRenderer;
184: }
185:
186: public Point doBelongsToCell(int col, int row) {
187: return null;
188: }
189:
190: public int getInitialColumnWidth(int column) {
191: return 100;
192: }
193:
194: public int getInitialRowHeight(int row) {
195: return 22;
196: }
197:
198: void setInput(ActionSequenceInput actionSequenceInput) {
199: this .actionSequenceInput = actionSequenceInput;
200: refresh();
201: }
202:
203: void refresh() {
204: map.clear();
205: keys.clear();
206: if (actionSequenceInput != null) {
207: Object defaultValue = actionSequenceInput.getDefaultValue();
208: if (defaultValue instanceof LinkedHashMap) {
209: map = (LinkedHashMap) defaultValue;
210: keys.addAll(map.keySet());
211: }
212: }
213: }
214:
215: protected void updateActionSequence() {
216: actionSequenceInput.setDefaultValue(map);
217: }
218:
219: public void insertRow(int row) {
220: if (row >= getRowCount()) {
221: throw new IndexOutOfBoundsException();
222: }
223: Map oldMap = map;
224: map = new LinkedHashMap();
225: for (int index = 0; index < oldMap.size(); index++) {
226: if (index == row - getFixedHeaderRowCount()) {
227: String newKey = Messages
228: .getString("InputMapModel.DEFAULT_KEY_NAME"); //$NON-NLS-1$
229: int i = 1;
230: while (keys.contains(newKey)) {
231: newKey = Messages
232: .getString("InputMapModel.DEFAULT_KEY_NAME") + i++; //$NON-NLS-1$
233: }
234: map.put(newKey, Messages
235: .getString("InputMapModel.DEFAULT_VALUE")); //$NON-NLS-1$
236: }
237: Object key = keys.get(index);
238: map.put(key, oldMap.get(key));
239: }
240: keys.clear();
241: keys.addAll(map.keySet());
242: updateActionSequence();
243: }
244:
245: public void addRow() {
246: String newKey = Messages
247: .getString("InputMapModel.DEFAULT_KEY_NAME"); //$NON-NLS-1$
248: int index = 1;
249: while (keys.contains(newKey)) {
250: newKey = Messages
251: .getString("InputMapModel.DEFAULT_KEY_NAME") + index++; //$NON-NLS-1$
252: }
253: map.put(newKey, Messages
254: .getString("InputMapModel.DEFAULT_VALUE")); //$NON-NLS-1$
255: keys.add(newKey);
256: updateActionSequence();
257: }
258:
259: public void deleteRow(int row) {
260: if (row >= getRowCount()) {
261: throw new IndexOutOfBoundsException();
262: }
263: map.remove(keys.get(row - getFixedHeaderRowCount()));
264: keys.remove(row - getFixedHeaderRowCount());
265: updateActionSequence();
266: }
267:
268: public void moveRow(int from, int to) {
269: if (from != to) {
270: if ((from < getFixedHeaderRowCount())
271: || (to < getFixedHeaderRowCount())
272: || (from >= getRowCount()) || (to >= getRowCount())) {
273: throw new IndexOutOfBoundsException();
274: }
275: String key = (String) keys.remove(from
276: - getFixedHeaderRowCount());
277: keys.add(to - getFixedHeaderRowCount(), key);
278: LinkedHashMap newMap = new LinkedHashMap();
279: for (Iterator iter = keys.iterator(); iter.hasNext();) {
280: key = (String) iter.next();
281: newMap.put(key, map.get(key));
282: }
283: map = newMap;
284: }
285: }
286: }
|