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.MenuEvent;
017: import org.eclipse.swt.events.MenuListener;
018: import org.eclipse.swt.events.SelectionEvent;
019: import org.eclipse.swt.events.SelectionListener;
020: import org.eclipse.swt.graphics.Image;
021: import org.eclipse.swt.graphics.Point;
022: import org.eclipse.swt.graphics.Rectangle;
023: import org.eclipse.swt.widgets.Composite;
024: import org.eclipse.swt.widgets.Menu;
025: import org.eclipse.swt.widgets.MenuItem;
026: import org.eclipse.swt.widgets.Shell;
027: import org.eclipse.swt.widgets.ToolBar;
028: import org.eclipse.swt.widgets.ToolItem;
029: import org.pentaho.designstudio.editors.actionsequence.ActionSequenceEditorPlugin;
030: import org.pentaho.designstudio.messages.Messages;
031:
032: public class InputMapToolbar {
033:
034: static final Image ADD_ICON = ActionSequenceEditorPlugin
035: .getImageDescriptor(
036: Messages
037: .getString("ActionsMasterDetailBlock.ICON_ADD_ACTION")).createImage(); //$NON-NLS-1$
038:
039: static final Image REMOVE_ICON = ActionSequenceEditorPlugin
040: .getImageDescriptor(
041: Messages
042: .getString("ActionsMasterDetailBlock.ICON_REMOVE_ACTION")).createImage(); //$NON-NLS-1$
043:
044: static final Image MOVE_UP_ICON = ActionSequenceEditorPlugin
045: .getImageDescriptor(
046: Messages
047: .getString("ActionsMasterDetailBlock.ICON_MOVE_UP")).createImage(); //$NON-NLS-1$
048:
049: static final Image MOVE_DOWN_ICON = ActionSequenceEditorPlugin
050: .getImageDescriptor(
051: Messages
052: .getString("ActionsMasterDetailBlock.ICON_MOVE_DOWN")).createImage(); //$NON-NLS-1$
053:
054: InputMapViewer inputMapViewer;
055: ToolBar toolBar;;
056:
057: public InputMapToolbar(Composite parent, InputMapViewer viewer) {
058: inputMapViewer = viewer;
059:
060: toolBar = new ToolBar(parent, SWT.FLAT);
061: ToolItem toolItem = new ToolItem(toolBar, SWT.NULL);
062: toolItem.setImage(MOVE_DOWN_ICON);
063: toolItem.setToolTipText(Messages
064: .getString("InputMapCoolbar.MOVE_ROW_UP")); //$NON-NLS-1$
065: toolItem.addSelectionListener(new SelectionListener() {
066: public void widgetDefaultSelected(SelectionEvent e) {
067: }
068:
069: public void widgetSelected(SelectionEvent e) {
070: Point[] selection = inputMapViewer.getCellSelection();
071: if ((selection.length == 1)
072: && (selection[0].y < inputMapViewer.getModel()
073: .getRowCount() - 1)) {
074: inputMapViewer.moveRow(selection[0].y,
075: selection[0].y + 1);
076: inputMapViewer.setSelection(selection[0].x,
077: selection[0].y + 1, true);
078: }
079: }
080: });
081:
082: toolItem = new ToolItem(toolBar, SWT.NULL);
083: toolItem.setImage(MOVE_UP_ICON);
084: toolItem.setToolTipText(Messages
085: .getString("InputMapCoolbar.MOVE_ROW_DOWN")); //$NON-NLS-1$
086: toolItem.addSelectionListener(new SelectionListener() {
087: public void widgetDefaultSelected(SelectionEvent e) {
088: }
089:
090: public void widgetSelected(SelectionEvent e) {
091: Point[] selection = inputMapViewer.getCellSelection();
092: if ((selection.length == 1)
093: && (selection[0].y > inputMapViewer.getModel()
094: .getFixedHeaderRowCount())) {
095: inputMapViewer.moveRow(selection[0].y,
096: selection[0].y - 1);
097: inputMapViewer.setSelection(selection[0].x,
098: selection[0].y - 1, true);
099: }
100: }
101: });
102:
103: new ToolItem(toolBar, SWT.SEPARATOR);
104:
105: toolItem = new ToolItem(toolBar, SWT.DROP_DOWN);
106: toolItem.setImage(ADD_ICON);
107: toolItem.setToolTipText(Messages
108: .getString("InputMapCoolbar.CREATE_ROW")); //$NON-NLS-1$
109: final Menu addMenu = createAddMenu(toolBar.getShell());
110: toolItem.addSelectionListener(new SelectionListener() {
111: public void widgetDefaultSelected(SelectionEvent e) {
112: }
113:
114: public void widgetSelected(SelectionEvent e) {
115: ToolItem item = (ToolItem) e.widget;
116: Rectangle rectangle = item.getBounds();
117: Point pt = item.getParent().toDisplay(
118: new Point(rectangle.x, rectangle.y));
119: addMenu.setLocation(pt.x, pt.y + rectangle.height);
120: addMenu.setVisible(true);
121: }
122:
123: });
124:
125: toolItem = new ToolItem(toolBar, SWT.NULL);
126: toolItem.setImage(REMOVE_ICON);
127: toolItem.setToolTipText(Messages
128: .getString("InputMapCoolbar.DELETE_ROW")); //$NON-NLS-1$
129: toolItem.addSelectionListener(new SelectionListener() {
130: public void widgetDefaultSelected(SelectionEvent e) {
131: }
132:
133: public void widgetSelected(SelectionEvent e) {
134: Point[] selection = inputMapViewer.getCellSelection();
135: inputMapViewer.deleteRow(selection[0].y);
136: }
137:
138: });
139: }
140:
141: private Menu createAddMenu(Shell shell) {
142:
143: final Menu menu = new Menu(shell, SWT.POP_UP);
144:
145: MenuItem menuItem = new MenuItem(menu, SWT.NONE);
146: menuItem.setText(Messages.getString("InputMapCoolbar.ADD_ROW")); //$NON-NLS-1$
147: menuItem.addSelectionListener(new SelectionListener() {
148: public void widgetDefaultSelected(SelectionEvent e) {
149: }
150:
151: public void widgetSelected(SelectionEvent e) {
152: inputMapViewer.addRow();
153: }
154: });
155:
156: final MenuItem insertRowMenuItem = new MenuItem(menu,
157: SWT.CASCADE);
158: insertRowMenuItem.setText(Messages
159: .getString("InputMapCoolbar.INSERT_ROW")); //$NON-NLS-1$
160: Menu insertRowMenu = new Menu(menu);
161: insertRowMenuItem.setMenu(insertRowMenu);
162:
163: menuItem = new MenuItem(insertRowMenu, SWT.NONE);
164: menuItem.setText(Messages
165: .getString("InputMapCoolbar.INSERT_ROW_BEFORE")); //$NON-NLS-1$
166: menuItem.addSelectionListener(new SelectionListener() {
167: public void widgetDefaultSelected(SelectionEvent e) {
168: }
169:
170: public void widgetSelected(SelectionEvent e) {
171: Point[] selection = inputMapViewer.getCellSelection();
172: inputMapViewer.insertRow(selection[0].y);
173: }
174: });
175:
176: menuItem = new MenuItem(insertRowMenu, SWT.NONE);
177: menuItem.setText(Messages
178: .getString("InputMapCoolbar.INSERT_ROW_AFTER")); //$NON-NLS-1$
179: menuItem.addSelectionListener(new SelectionListener() {
180: public void widgetDefaultSelected(SelectionEvent e) {
181: }
182:
183: public void widgetSelected(SelectionEvent e) {
184: Point[] selection = inputMapViewer.getCellSelection();
185: if (selection[0].y == inputMapViewer.getModel()
186: .getRowCount() - 1) {
187: inputMapViewer.addRow();
188: } else {
189: inputMapViewer.insertRow(selection[0].y + 1);
190: }
191: }
192: });
193:
194: menu.addMenuListener(new MenuListener() {
195: public void menuHidden(MenuEvent e) {
196: }
197:
198: public void menuShown(MenuEvent e) {
199: Point[] selections = inputMapViewer.getCellSelection();
200: if (selections.length == 0) {
201: insertRowMenuItem.setEnabled(false);
202: } else {
203: insertRowMenuItem.setEnabled(true);
204: }
205: }
206: });
207: return menu;
208: }
209:
210: public ToolBar getControl() {
211: return toolBar;
212: }
213:
214: }
|