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.SWTX;
033:
034: public class InputMapViewer extends PentahoKTable implements
035: FocusListener {
036:
037: class AddRowAction extends Action {
038: AddRowAction() {
039: super (Messages.getString("InputMapViewer.ADD_ROW")); //$NON-NLS-1$
040: }
041:
042: public void run() {
043: addRow();
044: setSelection(getModel().getFixedHeaderColumnCount(),
045: getModel().getRowCount() - 1, true);
046: }
047: }
048:
049: class InsertRowBeforeAction extends Action {
050: InsertRowBeforeAction() {
051: super (Messages.getString("InputMapViewer.ADD_ROW_BEFORE")); //$NON-NLS-1$
052: }
053:
054: public void run() {
055: Point[] selection = getCellSelection();
056: insertRow(selection[0].y);
057: }
058: }
059:
060: class InsertRowAfterAction extends Action {
061: InsertRowAfterAction() {
062: super (Messages.getString("InputMapViewer.ADD_ROW_AFTER")); //$NON-NLS-1$
063: }
064:
065: public void run() {
066: Point[] selection = getCellSelection();
067: if (selection[0].y == getModel().getRowCount() - 1) {
068: addRow();
069: } else {
070: insertRow(selection[0].y + 1);
071: }
072: }
073: }
074:
075: class DeleteRowAction extends Action {
076: DeleteRowAction() {
077: super (Messages.getString("InputMapViewer.DELETE_ROW")); //$NON-NLS-1$
078: }
079:
080: public void run() {
081: Point[] selection = getCellSelection();
082: deleteRow(selection[0].y);
083: }
084: }
085:
086: static final Point ORIGIN = new Point(0, 0);
087:
088: AddRowAction addRowAction = new AddRowAction();
089: DeleteRowAction deleteRowAction = new DeleteRowAction();
090: InsertRowBeforeAction insertRowBeforeAction = new InsertRowBeforeAction();
091: InsertRowAfterAction insertRowAfterAction = new InsertRowAfterAction();
092: MenuManager insertRowSubMenuManager = new MenuManager("Insert Row"); //$NON-NLS-1$
093: MenuManager menuManager;
094: Menu menu;
095:
096: public InputMapViewer(Composite composite, int style) {
097: super (composite, SWT.V_SCROLL | SWT.H_SCROLL
098: | SWTX.FILL_WITH_LASTCOL | SWTX.EDIT_ON_KEY);
099: setModel(new InputMapModel());
100: if (SWT.getPlatform().equals("win32")) { //$NON-NLS-1$
101:
102: // Column Resize
103:
104: Image column_resizeCursor = SWTX.loadImageResource(
105: getDisplay(), "/icons/column_resize_win32.gif"); //$NON-NLS-1$
106:
107: // we set the hotspot to the center, so calculate the number of pixels from hotspot to lower border:
108:
109: Rectangle columnresizeBound = column_resizeCursor
110: .getBounds();
111:
112: Point columnresizeSize = new Point(
113: columnresizeBound.width / 2,
114: columnresizeBound.height / 2);
115:
116: setDefaultColumnResizeCursor(new Cursor(getDisplay(),
117: column_resizeCursor.getImageData(),
118: columnresizeSize.x, columnresizeSize.y));
119:
120: } else {
121:
122: // Column Resize
123:
124: Image column_resizeCursor = SWTX.loadImageResource(
125: getDisplay(), "/icons/column_resize.gif"); //$NON-NLS-1$
126: Image column_resizeCursor_mask = SWTX.loadImageResource(
127: getDisplay(), "/icons/column_resize_mask.gif"); //$NON-NLS-1$
128:
129: // we set the hotspot to the center, so calculate the number of pixels from hotspot to lower border:
130:
131: Rectangle columnresizeBound = column_resizeCursor
132: .getBounds();
133:
134: Point columnresizeSize = new Point(
135: columnresizeBound.width / 2,
136: columnresizeBound.height / 2);
137:
138: setDefaultColumnResizeCursor(new Cursor(getDisplay(),
139: column_resizeCursor_mask.getImageData(),
140: column_resizeCursor.getImageData(),
141: columnresizeSize.x, columnresizeSize.y));
142:
143: }
144:
145: insertRowSubMenuManager.add(insertRowBeforeAction);
146: insertRowSubMenuManager.add(insertRowAfterAction);
147:
148: menuManager = new MenuManager();
149: menuManager.add(addRowAction);
150: menuManager.add(insertRowSubMenuManager);
151: menuManager.add(deleteRowAction);
152:
153: menuManager.addMenuListener(new IMenuListener() {
154: public void menuAboutToShow(IMenuManager manager) {
155: Point[] selections = getCellSelection();
156: deleteRowAction.setEnabled(selections.length > 0);
157: }
158: });
159: menu = menuManager.createContextMenu(this );
160: setMenu(menu);
161:
162: addFocusListener(this );
163: }
164:
165: protected void onMouseDoubleClick(MouseEvent e) {
166: if (m_Model == null)
167: return;
168: if (e.button == 1) {
169: Point click = new Point(e.x, e.y);
170: Point cell = getCellForCoordinates(e.x, e.y);
171: if (isHeaderCell(cell.x, cell.y)) {
172: KTableCellEditor editor = m_Model.getCellEditor(cell.x,
173: cell.y);
174: if (editor != null
175: && (editor.getActivationSignals() & KTableCellEditor.DOUBLECLICK) != 0
176: && editor.isApplicable(
177: KTableCellEditor.DOUBLECLICK, this ,
178: cell.x, cell.y, click, null,
179: e.stateMask)) {
180: int oldFocusCol = m_FocusCol;
181: int oldFocusRow = m_FocusRow;
182: m_FocusCol = cell.x;
183: m_FocusRow = cell.y;
184: openEditorInFocus();
185: m_FocusCol = oldFocusCol;
186: m_FocusRow = oldFocusRow;
187: }
188: fireFixedCellDoubleClicked(cell.x, cell.y, e.stateMask);
189: } else {
190: KTableCellEditor editor = m_Model.getCellEditor(
191: m_FocusCol, m_FocusRow);
192: Rectangle rect = getCellRect(m_FocusCol, m_FocusRow);
193: if (editor != null
194: && rect.contains(click)
195: && (editor.getActivationSignals() & KTableCellEditor.DOUBLECLICK) != 0
196: && editor.isApplicable(
197: KTableCellEditor.DOUBLECLICK, this ,
198: m_FocusCol, m_FocusRow, click, null,
199: e.stateMask))
200: openEditorInFocus();
201: fireCellDoubleClicked(cell.x, cell.y, e.stateMask);
202: }
203: }
204: }
205:
206: public void setInput(ActionSequenceInput actionSequenceInput) {
207: ((InputMapModel) getModel()).setInput(actionSequenceInput);
208: redraw();
209: scroll(getModel().getFixedHeaderColumnCount(), getModel()
210: .getFixedHeaderRowCount());
211: }
212:
213: public void refresh() {
214: ((InputMapModel) getModel()).refresh();
215: redraw();
216: scroll(getModel().getFixedHeaderColumnCount(), getModel()
217: .getFixedHeaderRowCount());
218: }
219:
220: public void deleteRow(int row) {
221: ((InputMapModel) getModel()).deleteRow(row);
222: redraw();
223: }
224:
225: public void addRow() {
226: InputMapModel model = (InputMapModel) getModel();
227: model.addRow();
228: forceFocus();
229: setSelection(model.getFixedColumnCount(),
230: model.getRowCount() - 1, true);
231: redraw();
232: }
233:
234: public void insertRow(int row) {
235: InputMapModel model = (InputMapModel) getModel();
236: model.insertRow(row);
237: forceFocus();
238: setSelection(model.getFixedColumnCount(), row, true);
239: redraw();
240: }
241:
242: public void focusGained(org.eclipse.swt.events.FocusEvent e) {
243: }
244:
245: public void focusLost(org.eclipse.swt.events.FocusEvent e) {
246: InputMapModel model = (InputMapModel) getModel();
247: if ((!model.cellEditor.isFocused())
248: && (model.getContentAt(0, model.getRowCount() - 1) == InputMapModel.NEW_ROW_FILLER)) {
249: model.deleteRow(model.getRowCount() - 1);
250: redraw();
251: }
252: }
253:
254: public void moveRow(int from, int to) {
255: if (from != to) {
256: InputMapModel model = (InputMapModel) getModel();
257: model.moveRow(from, to);
258: redraw();
259: }
260: }
261:
262: }
|