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.ActionSequenceOutput;
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.KTableCellEditor;
041: import de.kupzog.ktable.SWTX;
042:
043: public class OutputDestViewer extends PentahoKTable implements
044: FocusListener {
045:
046: static final Point ORIGIN = new Point(0, 0);
047:
048: public OutputDestViewer(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 OutputDestModel());
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(ActionSequenceOutput actionSequenceOutput) {
145: ((OutputDestModel) getModel()).setInput(actionSequenceOutput);
146: redraw();
147: scroll(getModel().getFixedHeaderColumnCount(), getModel()
148: .getFixedHeaderRowCount());
149: }
150:
151: public void refresh() {
152: ((OutputDestModel) getModel()).refresh();
153: redraw();
154: scroll(getModel().getFixedHeaderColumnCount(), getModel()
155: .getFixedHeaderRowCount());
156: }
157:
158: public void deleteRow(int row) {
159: ((OutputDestModel) getModel()).deleteRow(row);
160: redraw();
161: }
162:
163: public void addDestination(String destination) {
164: OutputDestModel model = (OutputDestModel) getModel();
165: model.addDestination(destination);
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: OutputDestModel model = (OutputDestModel) getModel();
176: if ((!model.cellEditor.isFocused())
177: && (model.getContentAt(0, model.getRowCount() - 1) == OutputDestModel.NEW_ROW_FILLER)) {
178: model.deleteRow(model.getRowCount() - 1);
179: redraw();
180: }
181: }
182:
183: private Menu createPopuMenu(Control parent) {
184:
185: final Menu menu = new Menu(parent);
186:
187: MenuItem insertRowMenuItem = new MenuItem(menu, SWT.CASCADE);
188: insertRowMenuItem.setText(Messages
189: .getString("OutputDestViewer.ADD_DESTINATION")); //$NON-NLS-1$
190: final Menu insertRowMenu = new Menu(menu);
191: insertRowMenuItem.setMenu(insertRowMenu);
192: insertRowMenu.addMenuListener(new MenuListener() {
193:
194: public void menuHidden(MenuEvent e) {
195: // TODO Auto-generated method stub
196:
197: }
198:
199: public void menuShown(MenuEvent e) {
200: MenuItem[] menuItems = insertRowMenu.getItems();
201: for (int i = 0; i < menuItems.length; i++) {
202: menuItems[i].dispose();
203: }
204:
205: ArrayList destinations = new ArrayList();
206: destinations
207: .addAll(Arrays
208: .asList(InputSourcesEditor
209: .parsePrefsString(ActionSequenceEditorPlugin
210: .getDefault()
211: .getPluginPreferences()
212: .getString(
213: PreferenceConstants.OUTPUT_DEST))));
214:
215: for (Iterator iter = destinations.iterator(); iter
216: .hasNext();) {
217: MenuItem menuItem = new MenuItem(insertRowMenu,
218: SWT.NONE);
219: menuItem.setText(iter.next().toString());
220: menuItem
221: .addSelectionListener(new SelectionListener() {
222: public void widgetDefaultSelected(
223: SelectionEvent e) {
224: }
225:
226: public void widgetSelected(
227: SelectionEvent e) {
228: addDestination(((MenuItem) e
229: .getSource()).getText());
230: }
231: });
232: }
233: }
234:
235: });
236:
237: final MenuItem deleteDestMenuItem = new MenuItem(menu, SWT.NONE);
238: deleteDestMenuItem.setText(Messages
239: .getString("OutputDestViewer.DELETE_DESTINATION")); //$NON-NLS-1$
240: deleteDestMenuItem
241: .addSelectionListener(new SelectionListener() {
242: public void widgetDefaultSelected(SelectionEvent e) {
243: }
244:
245: public void widgetSelected(SelectionEvent e) {
246: Point[] selection = getCellSelection();
247: deleteRow(selection[0].y);
248: }
249: });
250:
251: menu.addMenuListener(new MenuListener() {
252: public void menuHidden(MenuEvent e) {
253: }
254:
255: public void menuShown(MenuEvent e) {
256: Point[] selections = getCellSelection();
257: if (selections.length == 0) {
258: deleteDestMenuItem.setEnabled(false);
259: } else {
260: deleteDestMenuItem.setEnabled(true);
261: }
262: }
263: });
264: return menu;
265: }
266: }
|