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.Arrays;
017: import java.util.Iterator;
018:
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.events.FocusListener;
021: import org.eclipse.swt.events.MenuEvent;
022: import org.eclipse.swt.events.MenuListener;
023: import org.eclipse.swt.events.MouseEvent;
024: import org.eclipse.swt.events.SelectionEvent;
025: import org.eclipse.swt.events.SelectionListener;
026: import org.eclipse.swt.graphics.Cursor;
027: import org.eclipse.swt.graphics.Image;
028: import org.eclipse.swt.graphics.Point;
029: import org.eclipse.swt.graphics.Rectangle;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.swt.widgets.Control;
032: import org.eclipse.swt.widgets.Menu;
033: import org.eclipse.swt.widgets.MenuItem;
034: import org.pentaho.actionsequence.dom.ActionSequenceInput;
035: import org.pentaho.designstudio.editors.actionsequence.ActionSequenceEditorPlugin;
036: import org.pentaho.designstudio.editors.actionsequence.preferences.InputSourcesEditor;
037: import org.pentaho.designstudio.editors.actionsequence.preferences.PreferenceConstants;
038: import org.pentaho.designstudio.messages.Messages;
039:
040: import de.kupzog.ktable.KTable;
041: import de.kupzog.ktable.KTableCellEditor;
042: import de.kupzog.ktable.SWTX;
043:
044: public class InputSrcViewer extends KTable implements FocusListener {
045:
046: static final Point ORIGIN = new Point(0, 0);
047:
048: public InputSrcViewer(Composite composite, int style) {
049: super (composite, SWT.V_SCROLL | SWT.H_SCROLL
050: | SWTX.FILL_WITH_LASTCOL | SWTX.EDIT_ON_KEY);
051: setModel(new InputSrcModel());
052: if (SWT.getPlatform().equals("win32")) { //$NON-NLS-1$
053:
054: // Column Resize
055:
056: Image column_resizeCursor = SWTX.loadImageResource(
057: getDisplay(), "/icons/column_resize_win32.gif"); //$NON-NLS-1$
058:
059: // we set the hotspot to the center, so calculate the number of pixels from hotspot to lower border:
060:
061: Rectangle columnresizeBound = column_resizeCursor
062: .getBounds();
063:
064: Point columnresizeSize = new Point(
065: columnresizeBound.width / 2,
066: columnresizeBound.height / 2);
067:
068: setDefaultColumnResizeCursor(new Cursor(getDisplay(),
069: column_resizeCursor.getImageData(),
070: columnresizeSize.x, columnresizeSize.y));
071:
072: } else {
073:
074: // Column Resize
075:
076: Image column_resizeCursor = SWTX.loadImageResource(
077: getDisplay(), "/icons/column_resize.gif"); //$NON-NLS-1$
078: Image column_resizeCursor_mask = SWTX.loadImageResource(
079: getDisplay(), "/icons/column_resize_mask.gif"); //$NON-NLS-1$
080:
081: // we set the hotspot to the center, so calculate the number of pixels from hotspot to lower border:
082:
083: Rectangle columnresizeBound = column_resizeCursor
084: .getBounds();
085:
086: Point columnresizeSize = new Point(
087: columnresizeBound.width / 2,
088: columnresizeBound.height / 2);
089:
090: setDefaultColumnResizeCursor(new Cursor(getDisplay(),
091: column_resizeCursor_mask.getImageData(),
092: column_resizeCursor.getImageData(),
093: columnresizeSize.x, columnresizeSize.y));
094:
095: }
096:
097: Menu menu = createPopuMenu(this );
098: setMenu(menu);
099:
100: addFocusListener(this );
101: }
102:
103: protected void onMouseDoubleClick(MouseEvent e) {
104: if (m_Model == null)
105: return;
106: if (e.button == 1) {
107: Point click = new Point(e.x, e.y);
108: Point cell = getCellForCoordinates(e.x, e.y);
109: if (isHeaderCell(cell.x, cell.y)) {
110: KTableCellEditor editor = m_Model.getCellEditor(cell.x,
111: cell.y);
112: if (editor != null
113: && (editor.getActivationSignals() & KTableCellEditor.DOUBLECLICK) != 0
114: && editor.isApplicable(
115: KTableCellEditor.DOUBLECLICK, this ,
116: cell.x, cell.y, click, null,
117: e.stateMask)) {
118: int oldFocusCol = m_FocusCol;
119: int oldFocusRow = m_FocusRow;
120: m_FocusCol = cell.x;
121: m_FocusRow = cell.y;
122: openEditorInFocus();
123: m_FocusCol = oldFocusCol;
124: m_FocusRow = oldFocusRow;
125: }
126: fireFixedCellDoubleClicked(cell.x, cell.y, e.stateMask);
127: } else {
128: KTableCellEditor editor = m_Model.getCellEditor(
129: m_FocusCol, m_FocusRow);
130: Rectangle rect = getCellRect(m_FocusCol, m_FocusRow);
131: if (editor != null
132: && rect.contains(click)
133: && (editor.getActivationSignals() & KTableCellEditor.DOUBLECLICK) != 0
134: && editor.isApplicable(
135: KTableCellEditor.DOUBLECLICK, this ,
136: m_FocusCol, m_FocusRow, click, null,
137: e.stateMask))
138: openEditorInFocus();
139: fireCellDoubleClicked(cell.x, cell.y, e.stateMask);
140: }
141: }
142: }
143:
144: public void setInput(ActionSequenceInput actionSequenceInput) {
145: ((InputSrcModel) getModel()).setInput(actionSequenceInput);
146: redraw();
147: scroll(getModel().getFixedHeaderColumnCount(), getModel()
148: .getFixedHeaderRowCount());
149: }
150:
151: public void refresh() {
152: ((InputSrcModel) getModel()).refresh();
153: redraw();
154: scroll(getModel().getFixedHeaderColumnCount(), getModel()
155: .getFixedHeaderRowCount());
156: }
157:
158: public void deleteRow(int row) {
159: ((InputSrcModel) getModel()).deleteRow(row);
160: redraw();
161: }
162:
163: public void addSource(String source) {
164: InputSrcModel model = (InputSrcModel) getModel();
165: model.addSource(source);
166: forceFocus();
167: setSelection(1, model.getRowCount() - 1, true);
168: redraw();
169: }
170:
171: public void focusGained(org.eclipse.swt.events.FocusEvent e) {
172: }
173:
174: public void focusLost(org.eclipse.swt.events.FocusEvent e) {
175: InputSrcModel model = (InputSrcModel) getModel();
176: if ((!model.nameCellEditor.isFocused())
177: && (model.getContentAt(0, model.getRowCount() - 1) == InputSrcModel.NEW_ROW_FILLER)) {
178: model.deleteRow(model.getRowCount() - 1);
179: redraw();
180: }
181: }
182:
183: public void moveRow(int from, int to) {
184: if (from != to) {
185: InputSrcModel model = (InputSrcModel) getModel();
186: model.moveRow(from, to);
187: redraw();
188: }
189: }
190:
191: private Menu createPopuMenu(Control parent) {
192:
193: final Menu menu = new Menu(parent);
194:
195: MenuItem insertRowMenuItem = new MenuItem(menu, SWT.CASCADE);
196: insertRowMenuItem.setText(Messages
197: .getString("InputSrcViewer.ADD_SOURCE")); //$NON-NLS-1$
198:
199: final Menu insertRowMenu = new Menu(menu);
200: insertRowMenuItem.setMenu(insertRowMenu);
201: insertRowMenu.addMenuListener(new MenuListener() {
202:
203: public void menuHidden(MenuEvent e) {
204: // TODO Auto-generated method stub
205:
206: }
207:
208: public void menuShown(MenuEvent e) {
209: MenuItem[] menuItems = insertRowMenu.getItems();
210: for (int i = 0; i < menuItems.length; i++) {
211: menuItems[i].dispose();
212: }
213:
214: ArrayList sources = new ArrayList();
215: sources
216: .addAll(Arrays
217: .asList(InputSourcesEditor
218: .parsePrefsString(ActionSequenceEditorPlugin
219: .getDefault()
220: .getPluginPreferences()
221: .getString(
222: PreferenceConstants.INPUT_SOURCES))));
223:
224: for (Iterator iter = sources.iterator(); iter.hasNext();) {
225: MenuItem menuItem = new MenuItem(insertRowMenu,
226: SWT.NONE);
227: menuItem.setText(iter.next().toString());
228: menuItem
229: .addSelectionListener(new SelectionListener() {
230: public void widgetDefaultSelected(
231: SelectionEvent e) {
232: }
233:
234: public void widgetSelected(
235: SelectionEvent e) {
236: addSource(((MenuItem) e.getSource())
237: .getText());
238: }
239: });
240: }
241: }
242:
243: });
244:
245: final MenuItem deleteSourceMenuItem = new MenuItem(menu,
246: SWT.NONE);
247: deleteSourceMenuItem.setText(Messages
248: .getString("InputSrcViewer.DELETE_SOURCE")); //$NON-NLS-1$
249: deleteSourceMenuItem
250: .addSelectionListener(new SelectionListener() {
251: public void widgetDefaultSelected(SelectionEvent e) {
252: }
253:
254: public void widgetSelected(SelectionEvent e) {
255: Point[] selection = getCellSelection();
256: deleteRow(selection[0].y);
257: }
258: });
259:
260: menu.addMenuListener(new MenuListener() {
261: public void menuHidden(MenuEvent e) {
262: }
263:
264: public void menuShown(MenuEvent e) {
265: Point[] selections = getCellSelection();
266: if (selections.length == 0) {
267: deleteSourceMenuItem.setEnabled(false);
268: } else {
269: deleteSourceMenuItem.setEnabled(true);
270: }
271: }
272: });
273: return menu;
274: }
275: }
|