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: /*
014: * Copyright (C) 2004 by Friederich Kupzog Elektronik & Software
015: * All rights reserved. This program and the accompanying materials
016: * are made available under the terms of the Eclipse Public License v1.0
017: * which accompanies this distribution, and is available at
018: * http://www.eclipse.org/legal/epl-v10.html
019: *
020: Author: Friederich Kupzog
021: fkmk@kupzog.de
022: www.kupzog.de/fkmk
023: */
024: package org.pentaho.designstudio.widgets.flowtable;
025:
026: import java.util.Arrays;
027:
028: import javax.swing.table.DefaultTableModel;
029:
030: import org.eclipse.swt.graphics.Point;
031: import org.pentaho.actionsequence.dom.ActionControlStatement;
032: import org.pentaho.actionsequence.dom.ActionIfStatement;
033: import org.pentaho.actionsequence.dom.ActionLoop;
034: import org.pentaho.actionsequence.dom.ActionOutput;
035: import org.pentaho.actionsequence.dom.ActionSequenceDocument;
036: import org.pentaho.actionsequence.dom.ActionSequenceInput;
037: import org.pentaho.actionsequence.dom.ActionSequenceOutput;
038: import org.pentaho.actionsequence.dom.ActionSequenceResource;
039: import org.pentaho.actionsequence.dom.IActionSequenceElement;
040: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
041: import org.pentaho.designstudio.messages.Messages;
042:
043: import de.kupzog.ktable.KTableCellEditor;
044: import de.kupzog.ktable.KTableCellRenderer;
045: import de.kupzog.ktable.KTableModel;
046:
047: public class ActionsFlowModel implements KTableModel {
048:
049: static final int DEFAULT_COLUMN_SPAN = 5;
050:
051: static final int DEFAULT_COLUMN_WIDTH = 30;
052:
053: static final String ACTION_SEQUENCE_INPUTS_HEADER = new String(
054: Messages
055: .getString("ActionsFlowModel.ACTION_SEQUENCE_INPUTS_TITLE")); //$NON-NLS-1$
056:
057: static final String ACTION_SEQUENCE_RESOURCES_HEADER = new String(
058: Messages
059: .getString("ActionsFlowModel.ACTION_SEQUENCE_RESOURCES_TITLE")); //$NON-NLS-1$
060:
061: static final String ACTION_SEQUENCE_OUTPUTS_HEADER = new String(
062: Messages
063: .getString("ActionsFlowModel.ACTION_SEQUENCE_OUTPUTS_TITLE")); //$NON-NLS-1$
064:
065: static final String ACTION_CONNECTOR = new String(""); //$NON-NLS-1$
066:
067: static final String LOOPBACK_CONNECTOR = new String(""); //$NON-NLS-1$
068:
069: static final String LOOP_END_PLACEHOLDER = new String(Messages
070: .getString("ActionsFlowModel.END_LOOP_TITLE")); //$NON-NLS-1$
071:
072: static final String IF_END_PLACEHOLDER = new String(Messages
073: .getString("ActionsFlowModel.END_IF_TITLE")); //$NON-NLS-1$
074:
075: DefaultTableModel tableModel = new DefaultTableModel();
076:
077: static ActionsFlowCellRenderer renderer = new ActionsFlowCellRenderer();
078:
079: ActionSequenceDocument actionSequence;
080:
081: public ActionsFlowModel() {
082: setActionSequence(null);
083: }
084:
085: private void addActionDefinitionToModel(
086: ActionDefinition actionDefinition, int column) {
087: ActionControlStatement parent = actionDefinition.getParent();
088: int connectorColumn = column;
089: if ((parent != null)
090: && parent.getChildren()[0].equals(actionDefinition)) {
091: connectorColumn--;
092: }
093: addRow(ACTION_CONNECTOR, connectorColumn);
094: addRow(actionDefinition, column);
095:
096: ActionOutput[] actionOutputs = actionDefinition
097: .getAllOutputParams();
098: for (int i = 0; i < actionOutputs.length; i++) {
099: addRow(actionOutputs[i], column);
100: }
101: }
102:
103: private void addControlStatementToModel(
104: ActionControlStatement actionControlStatement, int column) {
105: ActionControlStatement parent = actionControlStatement
106: .getParent();
107: int connectorColumn = column;
108: if ((parent != null)
109: && parent.getChildren()[0]
110: .equals(actionControlStatement)) {
111: connectorColumn--;
112: }
113:
114: addRow(ACTION_CONNECTOR, connectorColumn);
115: addRow(actionControlStatement, column);
116:
117: IActionSequenceElement[] children = actionControlStatement
118: .getChildren();
119: for (int i = 0; i < children.length; i++) {
120: if (children[i] instanceof ActionDefinition) {
121: addActionDefinitionToModel(
122: (ActionDefinition) children[i], column + 1);
123: } else {
124: addControlStatementToModel(
125: (ActionControlStatement) children[i],
126: column + 1);
127: }
128: }
129:
130: addRow(ACTION_CONNECTOR, children.length > 0 ? column + 1
131: : column);
132:
133: if (actionControlStatement instanceof ActionIfStatement) {
134: addRow(IF_END_PLACEHOLDER, column);
135: } else {
136: addRow(LOOP_END_PLACEHOLDER, column);
137: }
138: }
139:
140: private void addRow(Object object, int startingCol) {
141: while (tableModel.getColumnCount() < startingCol
142: + DEFAULT_COLUMN_SPAN + 1) {
143: tableModel.addColumn(Integer.toString(startingCol));
144: }
145:
146: Object[] newRow = new Object[tableModel.getColumnCount()];
147: if ((object == ACTION_CONNECTOR)
148: && (startingCol == 1)
149: && ((tableModel.getValueAt(
150: tableModel.getRowCount() - 1, startingCol) instanceof ActionIfStatement) || (tableModel
151: .getValueAt(tableModel.getRowCount() - 1,
152: startingCol) instanceof ActionLoop))) {
153: newRow[0] = LOOPBACK_CONNECTOR;
154: }
155:
156: for (int i = 0; i < startingCol - 1; i = i + 2) {
157: newRow[i] = LOOPBACK_CONNECTOR;
158: }
159:
160: if ((object instanceof ActionIfStatement)
161: || (object instanceof ActionLoop)
162: || (object == IF_END_PLACEHOLDER)
163: || (object == LOOP_END_PLACEHOLDER)) {
164: newRow[startingCol - 1] = LOOPBACK_CONNECTOR;
165: }
166:
167: Arrays.fill(newRow, startingCol, startingCol
168: + DEFAULT_COLUMN_SPAN, object);
169: tableModel.addRow(newRow);
170: }
171:
172: public void setActionSequence(ActionSequenceDocument actionSequence) {
173: this .actionSequence = actionSequence;
174: renderer.setActionSequenceDocument(actionSequence);
175: tableModel = new DefaultTableModel(0, 1);
176: if (actionSequence != null) {
177:
178: addRow(ACTION_SEQUENCE_INPUTS_HEADER, 1);
179: ActionSequenceInput[] actionSequenceInputs = actionSequence
180: .getInputs();
181: for (int i = 0; i < actionSequenceInputs.length; i++) {
182: addRow(actionSequenceInputs[i], 1);
183: }
184:
185: addRow(ACTION_CONNECTOR, 1);
186:
187: addRow(ACTION_SEQUENCE_RESOURCES_HEADER, 1);
188: ActionSequenceResource[] actionSequenceResources = actionSequence
189: .getResources();
190: for (int i = 0; i < actionSequenceResources.length; i++) {
191: addRow(actionSequenceResources[i], 1);
192: }
193:
194: IActionSequenceElement[] executables = actionSequence
195: .getExecutableChildren();
196: for (int i = 0; i < executables.length; i++) {
197: if (executables[i] instanceof ActionDefinition) {
198: addActionDefinitionToModel(
199: (ActionDefinition) executables[i], 1);
200: } else {
201: addControlStatementToModel(
202: (ActionControlStatement) executables[i], 1);
203: }
204: }
205:
206: addRow(ACTION_CONNECTOR, 1);
207:
208: addRow(ACTION_SEQUENCE_OUTPUTS_HEADER, 1);
209: ActionSequenceOutput[] actionSequenceOutputs = actionSequence
210: .getOutputs();
211: for (int i = 0; i < actionSequenceOutputs.length; i++) {
212: addRow(actionSequenceOutputs[i], 1);
213: }
214: } else {
215: addRow(ACTION_SEQUENCE_INPUTS_HEADER, 1);
216: addRow(ACTION_CONNECTOR, 1);
217: addRow(ACTION_SEQUENCE_RESOURCES_HEADER, 1);
218: addRow(ACTION_CONNECTOR, 1);
219: addRow(ACTION_SEQUENCE_OUTPUTS_HEADER, 1);
220: }
221: }
222:
223: /*
224: * overridden from superclass
225: */
226: public Object getContentAt(int col, int row) {
227: return tableModel.getValueAt(row, col);
228: }
229:
230: /*
231: * overridden from superclass
232: */
233: public KTableCellEditor getCellEditor(int col, int row) {
234: return null;
235: }
236:
237: /*
238: * overridden from superclass
239: */
240: public void setContentAt(int col, int row, Object value) {
241: }
242:
243: /*
244: * overridden from superclass
245: */
246: public int getRowCount() {
247: return tableModel.getRowCount();
248: }
249:
250: /*
251: * overridden from superclass
252: */
253: public int getFixedHeaderRowCount() {
254: return 0;
255: }
256:
257: /*
258: * overridden from superclass
259: */
260: public int getColumnCount() {
261: return tableModel.getColumnCount();
262: }
263:
264: /*
265: * overridden from superclass
266: */
267: public int getFixedHeaderColumnCount() {
268: return 0;
269: }
270:
271: /*
272: * overridden from superclass
273: */
274: public int getColumnWidth(int col) {
275: return DEFAULT_COLUMN_WIDTH;
276: }
277:
278: /*
279: * overridden from superclass
280: */
281: public boolean isColumnResizable(int col) {
282: return (false);
283: }
284:
285: /*
286: * overridden from superclass
287: */
288: public void setColumnWidth(int col, int value) {
289: }
290:
291: /*
292: * overridden from superclass
293: */
294: public int getRowHeight(int row) {
295: //System.out.println( "ROW: " + row + " " + renderer.getRowHeight( content[row].imageHeight, content[row].pos ) );
296: // if ( (row >= nodeList.size()) || (row < 0) ) return( 0 );
297: // FlowContent ac = (FlowContent)nodeList.get( row );
298: // Image image = ac.getImage();
299: // return( renderer.getRowHeight( image != null ? image.getBounds().height : 0, ac) );
300: return 25;
301: }
302:
303: /*
304: * overridden from superclass
305: */
306: public boolean isRowResizable(int row) {
307: return false;
308: }
309:
310: /*
311: * overridden from superclass
312: */
313: public int getRowHeightMinimum() {
314: return 20;
315: }
316:
317: /*
318: * overridden from superclass
319: */
320: public void setRowHeight(int row, int value) {
321: }
322:
323: /*
324: * overridden from superclass
325: */
326: public KTableCellRenderer getCellRenderer(int col, int row) {
327: return renderer;
328: }
329:
330: /* (non-Javadoc)
331: * @see de.kupzog.ktable.KTableModel#belongsToCell(int, int)
332: */
333: public Point belongsToCell(int col, int row) {
334: Point point = null;
335: if ((row < tableModel.getRowCount())
336: && (col < tableModel.getColumnCount())) {
337: while ((col > 0)
338: && (tableModel.getValueAt(row, col) != null)
339: && (tableModel.getValueAt(row, col) == tableModel
340: .getValueAt(row, col - 1))) {
341: col--;
342: }
343: point = new Point(col, row);
344: }
345: return point;
346: }
347:
348: /* (non-Javadoc)
349: * @see de.kupzog.ktable.KTableModel#getTooltipAt(int, int)
350: */
351: public String getTooltipAt(int col, int row) {
352: // no tooltip
353: return null;
354: }
355:
356: /* (non-Javadoc)
357: * @see de.kupzog.ktable.KTableModel#getFixedSelectableRowCount()
358: */
359: public int getFixedSelectableRowCount() {
360: // all fixed rows are non-selectable.
361: return 0;
362: }
363:
364: /* (non-Javadoc)
365: * @see de.kupzog.ktable.KTableModel#getFixedSelectableColumnCount()
366: */
367: public int getFixedSelectableColumnCount() {
368: // all fixed columns are non-selctable.
369: return 0;
370: }
371:
372: public void refresh() {
373: setActionSequence(actionSequence);
374: }
375: }
|