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 org.eclipse.jface.action.Action;
016: import org.eclipse.jface.action.IMenuListener;
017: import org.eclipse.jface.action.IMenuManager;
018: import org.eclipse.jface.action.MenuManager;
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.events.FocusListener;
021: import org.eclipse.swt.events.MouseEvent;
022: import org.eclipse.swt.graphics.Cursor;
023: import org.eclipse.swt.graphics.Image;
024: import org.eclipse.swt.graphics.Point;
025: import org.eclipse.swt.graphics.Rectangle;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.Menu;
028: import org.pentaho.actionsequence.dom.ActionSequenceInput;
029: import org.pentaho.designstudio.messages.Messages;
030:
031: import de.kupzog.ktable.KTableCellEditor;
032: import de.kupzog.ktable.KTableCellSelectionListener;
033: import de.kupzog.ktable.SWTX;
034:
035: public class InputTableViewer extends PentahoKTable implements
036: FocusListener {
037:
038: class AddRowAction extends Action {
039: AddRowAction() {
040: super (Messages.getString("InputTableViewer.ADD_ROW")); //$NON-NLS-1$
041: }
042:
043: public void run() {
044: addRow();
045: setSelection(getModel().getFixedHeaderColumnCount(),
046: getModel().getRowCount() - 1, true);
047: }
048: }
049:
050: class AddColumnAction extends Action {
051: AddColumnAction() {
052: super (Messages.getString("InputTableViewer.ADD_COLUMN")); //$NON-NLS-1$
053: }
054:
055: public void run() {
056: addColumn();
057: }
058: }
059:
060: class InsertRowBeforeAction extends Action {
061: InsertRowBeforeAction() {
062: super (Messages.getString("InputTableViewer.ADD_ROW_BEFORE")); //$NON-NLS-1$
063: }
064:
065: public void run() {
066: Point[] selection = getCellSelection();
067: insertRow(selection[0].y);
068: }
069: }
070:
071: class InsertRowAfterAction extends Action {
072: InsertRowAfterAction() {
073: super (Messages.getString("InputTableViewer.ADD_ROW_AFTER")); //$NON-NLS-1$
074: }
075:
076: public void run() {
077: Point[] selection = getCellSelection();
078: if (selection[0].y == getModel().getRowCount() - 1) {
079: addRow();
080: } else {
081: insertRow(selection[0].y + 1);
082: }
083: }
084: }
085:
086: class InsertColumnBeforeAction extends Action {
087: InsertColumnBeforeAction() {
088: super (Messages
089: .getString("InputTableViewer.ADD_COLUMN_BEFORE")); //$NON-NLS-1$
090: }
091:
092: public void run() {
093: Point[] selection = getCellSelection();
094: insertColumn(selection[0].x);
095: }
096: }
097:
098: class InsertColumnAfterAction extends Action {
099: InsertColumnAfterAction() {
100: super (Messages
101: .getString("InputTableViewer.ADD_COLUMN_AFTER")); //$NON-NLS-1$
102: }
103:
104: public void run() {
105: Point[] selection = getCellSelection();
106: if (selection[0].x == getModel().getColumnCount() - 1) {
107: addColumn();
108: } else {
109: insertColumn(selection[0].x + 1);
110: }
111: }
112: }
113:
114: class DeleteRowAction extends Action {
115: DeleteRowAction() {
116: super (Messages.getString("InputTableViewer.DELETE_ROW")); //$NON-NLS-1$
117: }
118:
119: public void run() {
120: Point[] selection = getCellSelection();
121: deleteRow(selection[0].y);
122: }
123: }
124:
125: class DeleteColumnAction extends Action {
126: DeleteColumnAction() {
127: super (Messages.getString("InputTableViewer.DELETE_COLUMN")); //$NON-NLS-1$
128: }
129:
130: public void run() {
131: Point[] selection = getCellSelection();
132: deleteColumn(selection[0].x);
133: }
134: }
135:
136: static final Point ORIGIN = new Point(0, 0);
137:
138: AddRowAction addRowAction = new AddRowAction();
139: AddColumnAction addColumnAction = new AddColumnAction();
140: InsertRowBeforeAction insertRowBeforeAction = new InsertRowBeforeAction();
141: InsertRowAfterAction insertRowAfterAction = new InsertRowAfterAction();
142: InsertColumnBeforeAction insertColumnBeforeAction = new InsertColumnBeforeAction();
143: InsertColumnAfterAction insertColumnAfterAction = new InsertColumnAfterAction();
144: DeleteRowAction deleteRowAction = new DeleteRowAction();
145: DeleteColumnAction deleteColumnAction = new DeleteColumnAction();
146: MenuManager insertRowSubMenuManager = new MenuManager("Insert Row"); //$NON-NLS-1$
147: MenuManager insertColumnSubMenuManager = new MenuManager(
148: "Insert Column"); //$NON-NLS-1$
149: MenuManager menuManager;
150: Menu menu;
151:
152: public InputTableViewer(Composite composite, int style,
153: boolean saveAsPropertyMapList) {
154: super (composite, SWT.V_SCROLL | SWT.H_SCROLL
155: | SWTX.FILL_WITH_LASTCOL | SWTX.EDIT_ON_KEY);
156: setModel(new InputTableModel(saveAsPropertyMapList));
157: if (SWT.getPlatform().equals("win32")) { //$NON-NLS-1$
158:
159: // Column Resize
160:
161: Image column_resizeCursor = SWTX.loadImageResource(
162: getDisplay(), "/icons/column_resize_win32.gif"); //$NON-NLS-1$
163:
164: // we set the hotspot to the center, so calculate the number of pixels from hotspot to lower border:
165:
166: Rectangle columnresizeBound = column_resizeCursor
167: .getBounds();
168:
169: Point columnresizeSize = new Point(
170: columnresizeBound.width / 2,
171: columnresizeBound.height / 2);
172:
173: setDefaultColumnResizeCursor(new Cursor(getDisplay(),
174: column_resizeCursor.getImageData(),
175: columnresizeSize.x, columnresizeSize.y));
176:
177: } else {
178:
179: // Column Resize
180:
181: Image column_resizeCursor = SWTX.loadImageResource(
182: getDisplay(), "/icons/column_resize.gif"); //$NON-NLS-1$
183: Image column_resizeCursor_mask = SWTX.loadImageResource(
184: getDisplay(), "/icons/column_resize_mask.gif"); //$NON-NLS-1$
185:
186: // we set the hotspot to the center, so calculate the number of pixels from hotspot to lower border:
187:
188: Rectangle columnresizeBound = column_resizeCursor
189: .getBounds();
190:
191: Point columnresizeSize = new Point(
192: columnresizeBound.width / 2,
193: columnresizeBound.height / 2);
194:
195: setDefaultColumnResizeCursor(new Cursor(getDisplay(),
196: column_resizeCursor_mask.getImageData(),
197: column_resizeCursor.getImageData(),
198: columnresizeSize.x, columnresizeSize.y));
199:
200: }
201:
202: insertRowSubMenuManager.add(insertRowBeforeAction);
203: insertRowSubMenuManager.add(insertRowAfterAction);
204:
205: insertColumnSubMenuManager.add(insertColumnBeforeAction);
206: insertColumnSubMenuManager.add(insertColumnAfterAction);
207:
208: menuManager = new MenuManager();
209: menuManager.add(addRowAction);
210: menuManager.add(addColumnAction);
211: menuManager.add(insertRowSubMenuManager);
212: menuManager.add(insertColumnSubMenuManager);
213:
214: menuManager.add(deleteRowAction);
215: menuManager.add(deleteColumnAction);
216:
217: menuManager.addMenuListener(new IMenuListener() {
218: public void menuAboutToShow(IMenuManager manager) {
219: Point[] selections = getCellSelection();
220: if (selections.length == 0) {
221: insertRowAfterAction.setEnabled(false);
222: insertRowBeforeAction.setEnabled(false);
223: insertColumnAfterAction.setEnabled(false);
224: insertColumnBeforeAction.setEnabled(false);
225: deleteRowAction.setEnabled(false);
226: deleteColumnAction.setEnabled(false);
227: } else {
228: insertRowAfterAction.setEnabled(true);
229: insertRowBeforeAction.setEnabled(true);
230: insertColumnBeforeAction.setEnabled(true);
231: insertColumnAfterAction.setEnabled(true);
232: deleteRowAction.setEnabled(true);
233: deleteColumnAction.setEnabled(true);
234: }
235: }
236: });
237: menu = menuManager.createContextMenu(this );
238: setMenu(menu);
239:
240: addCellSelectionListener(new KTableCellSelectionListener() {
241: public void cellSelected(int col, int row, int statemask) {
242: InputTableModel inputTableModel = (InputTableModel) getModel();
243: if (row != (inputTableModel.getRowCount() - 1)) {
244: removeFillerRow();
245: }
246: }
247:
248: public void fixedCellSelected(int col, int row,
249: int statemask) {
250: removeFillerRow();
251: }
252:
253: private void removeFillerRow() {
254: InputTableModel inputTableModel = (InputTableModel) getModel();
255: int lastRow = inputTableModel.getRowCount() - 1;
256: if (lastRow != 0) {
257: boolean hasFillerRow = true;
258: for (int i = inputTableModel
259: .getFixedHeaderColumnCount(); (i < inputTableModel
260: .getColumnCount())
261: && hasFillerRow; i++) {
262: hasFillerRow = (inputTableModel.getContentAt(i,
263: lastRow) == InputTableModel.NEW_ROW_FILLER);
264: }
265: if (hasFillerRow) {
266: inputTableModel.deleteRow(lastRow);
267: redraw();
268: }
269: }
270: }
271: });
272: addFocusListener(this );
273: }
274:
275: protected void onMouseDoubleClick(MouseEvent e) {
276: if (m_Model == null)
277: return;
278: if (e.button == 1) {
279: Point click = new Point(e.x, e.y);
280: Point cell = getCellForCoordinates(e.x, e.y);
281: if (isHeaderCell(cell.x, cell.y)) {
282: KTableCellEditor editor = m_Model.getCellEditor(cell.x,
283: cell.y);
284: if (editor != null
285: && (editor.getActivationSignals() & KTableCellEditor.DOUBLECLICK) != 0
286: && editor.isApplicable(
287: KTableCellEditor.DOUBLECLICK, this ,
288: cell.x, cell.y, click, null,
289: e.stateMask)) {
290: int oldFocusCol = m_FocusCol;
291: int oldFocusRow = m_FocusRow;
292: m_FocusCol = cell.x;
293: m_FocusRow = cell.y;
294: openEditorInFocus();
295: m_FocusCol = oldFocusCol;
296: m_FocusRow = oldFocusRow;
297: }
298: fireFixedCellDoubleClicked(cell.x, cell.y, e.stateMask);
299: } else {
300: KTableCellEditor editor = m_Model.getCellEditor(
301: m_FocusCol, m_FocusRow);
302: Rectangle rect = getCellRect(m_FocusCol, m_FocusRow);
303: if (editor != null
304: && rect.contains(click)
305: && (editor.getActivationSignals() & KTableCellEditor.DOUBLECLICK) != 0
306: && editor.isApplicable(
307: KTableCellEditor.DOUBLECLICK, this ,
308: m_FocusCol, m_FocusRow, click, null,
309: e.stateMask))
310: openEditorInFocus();
311: fireCellDoubleClicked(cell.x, cell.y, e.stateMask);
312: }
313: }
314: }
315:
316: public void setInput(ActionSequenceInput actionSequenceInput) {
317: ((InputTableModel) getModel()).setInput(actionSequenceInput);
318: redraw();
319: scroll(getModel().getFixedHeaderColumnCount(), getModel()
320: .getFixedHeaderRowCount());
321: }
322:
323: public void refresh() {
324: ((InputTableModel) getModel()).refresh();
325: redraw();
326: scroll(getModel().getFixedHeaderColumnCount(), getModel()
327: .getFixedHeaderRowCount());
328: }
329:
330: public void deleteRow(int row) {
331: ((InputTableModel) getModel()).deleteRow(row);
332: redraw();
333: }
334:
335: public void deleteColumn(int column) {
336: ((InputTableModel) getModel()).deleteColumn(column);
337: redraw();
338: }
339:
340: public void addRow() {
341: InputTableModel model = (InputTableModel) getModel();
342: model.addRow();
343: forceFocus();
344: setSelection(model.getFixedColumnCount(),
345: model.getRowCount() - 1, true);
346: redraw();
347: }
348:
349: public void addColumn() {
350: InputTableModel model = (InputTableModel) getModel();
351: model.addColumn();
352: forceFocus();
353: setSelection(model.getColumnCount() - 1, model.getRowCount(),
354: true);
355: redraw();
356: }
357:
358: public void insertRow(int row) {
359: InputTableModel model = (InputTableModel) getModel();
360: model.insertRow(row);
361: forceFocus();
362: setSelection(model.getFixedColumnCount(), row, true);
363: redraw();
364: }
365:
366: public void insertColumn(int column) {
367: InputTableModel model = (InputTableModel) getModel();
368: model.insertColumn(column);
369: forceFocus();
370: setSelection(column, getFixedRowCount(), true);
371: redraw();
372: }
373:
374: public void focusGained(org.eclipse.swt.events.FocusEvent e) {
375: }
376:
377: public void focusLost(org.eclipse.swt.events.FocusEvent e) {
378: InputTableModel model = (InputTableModel) getModel();
379: if (!model.headerEditor.isFocused() && !model.cellEditor.isOpen) {
380: if (model.getRowCount() > model.getFixedRowCount()) {
381: boolean isNewRow = true;
382: int lastRowIndex = model.getRowCount() - 1;
383: for (int i = model.getFixedColumnCount(); i < model
384: .getColumnCount()
385: && isNewRow; i++) {
386: isNewRow = (model.getContentAt(i, lastRowIndex) == InputTableModel.NEW_ROW_FILLER);
387: }
388: if (isNewRow) {
389: model.deleteRow(lastRowIndex);
390: redraw();
391: }
392: }
393: }
394: }
395:
396: public void moveRow(int from, int to) {
397: if (from != to) {
398: InputTableModel model = (InputTableModel) getModel();
399: model.moveRow(from, to);
400: redraw();
401: }
402: }
403:
404: }
|