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.swt.SWT;
016: import org.eclipse.swt.events.KeyEvent;
017: import org.eclipse.swt.events.MouseEvent;
018: import org.eclipse.swt.graphics.Point;
019: import org.eclipse.swt.widgets.Composite;
020:
021: import de.kupzog.ktable.KTable;
022: import de.kupzog.ktable.KTableCellEditor;
023:
024: public class PentahoKTable extends KTable {
025:
026: public static final boolean isMacOS = System
027: .getProperty("os.name").startsWith("Mac"); //$NON-NLS-1$ //$NON-NLS-2$
028:
029: public PentahoKTable(Composite parent, int style) {
030: // Initialize canvas to draw on.
031: super (parent, style);
032: }
033:
034: public boolean isColumnVisible(int col) {
035: checkWidget();
036: if (m_Model == null)
037: return false;
038: return ((col >= m_LeftColumn && col < m_LeftColumn
039: + m_ColumnsVisible) || col < (getFixedColumnCount()));
040:
041: }
042:
043: public boolean isCellVisible(int col, int row) {
044: return isRowVisible(row) && isColumnVisible(col);
045: }
046:
047: protected void onMouseDown(MouseEvent e) {
048: super .onMouseDown(e);
049: if (isMacOS) {
050: redraw();
051: }
052: }
053:
054: protected void onKeyDown(KeyEvent e) {
055: boolean focusChanged = false;
056: int newFocusRow = m_FocusRow;
057: int newFocusCol = m_FocusCol;
058:
059: if (m_Model == null)
060: return;
061:
062: if ((e.character == ' ') || (e.character == '\r')) {
063: KTableCellEditor editor = m_Model.getCellEditor(m_FocusCol,
064: m_FocusRow);
065: if (editor != null
066: && (editor.getActivationSignals() & KTableCellEditor.KEY_RETURN_AND_SPACE) != 0
067: && editor.isApplicable(
068: KTableCellEditor.KEY_RETURN_AND_SPACE,
069: this , m_FocusCol, m_FocusRow, null,
070: e.character + "", e.stateMask)) { //$NON-NLS-1$
071: openEditorInFocus();
072: return;
073: }
074: }
075:
076: if (e.keyCode == SWT.HOME) {
077: newFocusCol = m_Model.getFixedHeaderColumnCount();
078: if (newFocusRow == -1)
079: newFocusRow = m_Model.getFixedHeaderRowCount();
080: focusChanged = true;
081: } else if (e.keyCode == SWT.END) {
082: newFocusCol = m_Model.getColumnCount() - 1;
083: if (newFocusRow == -1)
084: newFocusRow = m_Model.getFixedHeaderRowCount();
085: focusChanged = true;
086: } else if (e.keyCode == SWT.ARROW_LEFT
087: || (e.keyCode == SWT.TAB && (e.stateMask & SWT.SHIFT) != 0)) {
088: if (!isRowSelectMode()) {
089: if (newFocusCol > m_Model.getFixedHeaderColumnCount()) {
090: Point current = m_Model.belongsToCell(m_FocusCol,
091: m_FocusRow);
092: if (current == null)
093: current = new Point(m_FocusCol, m_FocusRow);
094: Point newPt = m_Model.belongsToCell(current.x - 1,
095: current.y);
096: if (newPt == null)
097: newPt = new Point(current.x - 1, current.y);
098: newFocusCol = newPt.x;
099: newFocusRow = newPt.y;
100: } else if (newFocusCol == m_Model
101: .getFixedHeaderColumnCount()
102: && newFocusRow >= m_Model
103: .getFixedHeaderRowCount()
104: && e.keyCode == SWT.TAB) {
105: // wrap arount when traversing:
106: newFocusCol = m_Model.getColumnCount() - 1;
107: newFocusRow--;
108: }
109: }
110: focusChanged = true;
111: } else if (e.keyCode == SWT.ARROW_RIGHT
112: || (e.keyCode == SWT.TAB && (e.stateMask & SWT.SHIFT) == 0)) {
113: if (!isRowSelectMode()) {
114: if (newFocusCol == -1) {
115: newFocusCol = m_Model.getFixedHeaderColumnCount();
116: newFocusRow = m_Model.getFixedHeaderRowCount();
117: } else if (newFocusCol < m_Model.getColumnCount() - 1) {
118: Point old = new Point(m_FocusCol, m_FocusRow);
119: newFocusCol++;
120: Point next = m_Model.belongsToCell(newFocusCol,
121: newFocusRow);
122: if (next == null)
123: next = new Point(newFocusCol, newFocusRow);
124: while (next.equals(old)
125: && newFocusCol < m_Model.getColumnCount() - 1) {
126: newFocusCol++;
127: next = m_Model.belongsToCell(newFocusCol,
128: newFocusRow);
129: if (next == null)
130: next = new Point(newFocusCol, newFocusRow);
131: }
132: newFocusCol = next.x;
133: } else if (m_FocusCol == m_Model.getColumnCount() - 1
134: && m_FocusRow != m_Model.getRowCount() - 1
135: && e.keyCode == SWT.TAB) {
136: newFocusCol = m_Model.getFixedHeaderColumnCount();
137: newFocusRow++;
138: }
139: }
140: focusChanged = true;
141: } else if (e.keyCode == SWT.ARROW_DOWN) {
142: if (newFocusRow == -1) {
143: newFocusRow = m_Model.getFixedHeaderRowCount();
144: newFocusCol = m_Model.getFixedHeaderColumnCount();
145: } else if (newFocusRow < m_Model.getRowCount() - 1) {
146: Point old = new Point(m_FocusCol, m_FocusRow);
147: newFocusRow++;
148: Point next = m_Model.belongsToCell(newFocusCol,
149: newFocusRow);
150: if (next == null)
151: next = new Point(newFocusCol, newFocusRow);
152: while (next.equals(old)) {
153: newFocusRow++;
154: next = m_Model.belongsToCell(newFocusCol,
155: newFocusRow);
156: if (next == null)
157: next = new Point(newFocusCol, newFocusRow);
158: }
159: }
160: focusChanged = true;
161: } else if (e.keyCode == SWT.ARROW_UP) {
162: if (newFocusRow > m_Model.getFixedHeaderRowCount()) {
163: Point current = m_Model.belongsToCell(m_FocusCol,
164: m_FocusRow);
165: if (current == null)
166: current = new Point(m_FocusCol, m_FocusRow);
167: newFocusCol = current.x;
168: newFocusRow = current.y - 1;
169: }
170: focusChanged = true;
171: } else if (e.keyCode == SWT.PAGE_DOWN) {
172: newFocusRow += m_RowsVisible - 1;
173: if (newFocusRow >= m_Model.getRowCount())
174: newFocusRow = m_Model.getRowCount() - 1;
175: if (newFocusCol == -1)
176: newFocusCol = m_Model.getFixedHeaderColumnCount();
177: focusChanged = true;
178: } else if (e.keyCode == SWT.PAGE_UP) {
179: newFocusRow -= m_RowsVisible - 1;
180: if (newFocusRow < m_Model.getFixedHeaderRowCount())
181: newFocusRow = m_Model.getFixedHeaderRowCount();
182: if (newFocusCol == -1)
183: newFocusCol = m_Model.getFixedHeaderColumnCount();
184: focusChanged = true;
185: } else if (isEditOnKeyEvent()) {
186: // check if a key was pressed that forces editing:
187: if (e.keyCode == SWT.DEL || e.keyCode == SWT.BS) {
188: KTableCellEditor editor = m_Model.getCellEditor(
189: m_FocusCol, m_FocusRow);
190: if (editor != null
191: && (editor.getActivationSignals() & KTableCellEditor.KEY_ANY) != 0
192: && editor.isApplicable(
193: KTableCellEditor.KEY_ANY, this ,
194: m_FocusCol, m_FocusRow, null,
195: e.character + "", e.stateMask)) { //$NON-NLS-1$
196: openEditorInFocus();
197: if (m_CellEditor != null) {
198: m_CellEditor.setContent(""); //$NON-NLS-1$
199: }
200: }
201: return;
202: } else if ((Character.isLetterOrDigit(e.character) || e.keyCode > 32
203: && e.keyCode < 254 && e.keyCode != 127)
204: && e.keyCode != SWT.CTRL
205: && e.keyCode != SWT.ALT
206: && (e.stateMask & SWT.CONTROL) == 0
207: && (e.stateMask & SWT.ALT) == 0) {
208:
209: KTableCellEditor editor = m_Model.getCellEditor(
210: m_FocusCol, m_FocusRow);
211: if (editor != null
212: && (editor.getActivationSignals() & KTableCellEditor.KEY_ANY) != 0
213: && editor.isApplicable(
214: KTableCellEditor.KEY_ANY, this ,
215: m_FocusCol, m_FocusRow, null,
216: e.character + "", e.stateMask)) { //$NON-NLS-1$
217: openEditorInFocus();
218: if (m_CellEditor != null) {
219: m_CellEditor.setContent(e.character + ""); //$NON-NLS-1$
220: if (isMacOS) {
221: e.doit = false;
222: }
223: }
224: return;
225: }
226: }
227: }
228:
229: if (focusChanged) {
230: // make sure it is a valid, visible cell and not overlapped:
231: Point valid = m_Model.belongsToCell(newFocusCol,
232: newFocusRow);
233: if (valid != null) {
234: newFocusCol = valid.x;
235: newFocusRow = valid.y;
236: }
237: focusCell(newFocusCol, newFocusRow, e.stateMask);
238: if (!isCellFullyVisible(m_FocusCol, m_FocusRow))
239: scrollToFocus();
240: }
241: }
242: }
|