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.controls;
014:
015: import java.util.ArrayList;
016: import java.util.Arrays;
017:
018: import org.eclipse.jface.viewers.IStructuredSelection;
019: import org.eclipse.jface.viewers.StructuredSelection;
020: import org.eclipse.swt.SWT;
021: import org.eclipse.swt.events.SelectionEvent;
022: import org.eclipse.swt.events.SelectionListener;
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.Control;
028: import org.eclipse.swt.widgets.Menu;
029: import org.eclipse.swt.widgets.MenuItem;
030: import org.eclipse.swt.widgets.ToolBar;
031: import org.eclipse.swt.widgets.ToolItem;
032: import org.pentaho.actionsequence.dom.AbstractIOElement;
033: import org.pentaho.actionsequence.dom.ActionInput;
034: import org.pentaho.actionsequence.dom.ActionOutput;
035: import org.pentaho.actionsequence.dom.ActionSequenceInput;
036: import org.pentaho.actionsequence.dom.IActionInputVariable;
037: import org.pentaho.designstudio.editors.actionsequence.ActionSequenceEditorPlugin;
038: import org.pentaho.designstudio.editors.actionsequence.pages.actions.IActionIOFilter;
039: import org.pentaho.designstudio.messages.Messages;
040:
041: public class ActionInputsListToolbar {
042:
043: ToolBar toolBar;
044: protected ActionInputsList actionInputsList;
045: ToolItem addToolItem;
046: ToolItem deleteToolItem;
047: Menu addInputMenu;
048:
049: static final Image MOVE_UP_ICON = ActionSequenceEditorPlugin
050: .getImageDescriptor(
051: Messages
052: .getString("ActionsMasterDetailBlock.ICON_MOVE_UP")).createImage(); //$NON-NLS-1$
053: static final Image MOVE_DOWN_ICON = ActionSequenceEditorPlugin
054: .getImageDescriptor(
055: Messages
056: .getString("ActionsMasterDetailBlock.ICON_MOVE_DOWN")).createImage(); //$NON-NLS-1$
057: static final Image ADD_ACTION_ICON = ActionSequenceEditorPlugin
058: .getImageDescriptor(
059: Messages
060: .getString("ActionsMasterDetailBlock.ICON_ADD_ACTION")).createImage(); //$NON-NLS-1$
061: static final Image REMOVE_ACTION_ICON = ActionSequenceEditorPlugin
062: .getImageDescriptor(
063: Messages
064: .getString("ActionsMasterDetailBlock.ICON_REMOVE_ACTION")).createImage(); //$NON-NLS-1$
065:
066: class AddInputMenuItem {
067: AbstractIOElement actionSequenceIO;
068: MenuItem menuItem;
069:
070: AddInputMenuItem(Menu parent, AbstractIOElement actSeqIO) {
071: menuItem = new MenuItem(parent, SWT.NONE);
072: actionSequenceIO = actSeqIO;
073: if (actionSequenceIO instanceof ActionOutput) {
074: menuItem.setText(((ActionOutput) actionSequenceIO)
075: .getPublicName());
076: } else {
077: menuItem.setText(actionSequenceIO.getName());
078: }
079: menuItem.addSelectionListener(new SelectionListener() {
080: public void widgetDefaultSelected(SelectionEvent e) {
081: }
082:
083: public void widgetSelected(SelectionEvent e) {
084: actionInputsList.addInput(actionSequenceIO);
085: }
086: });
087: }
088: }
089:
090: public ActionInputsListToolbar(Composite parent,
091: ActionInputsList inputsList) {
092: actionInputsList = inputsList;
093:
094: toolBar = new ToolBar(parent, SWT.FLAT);
095:
096: ToolItem toolItem = new ToolItem(toolBar, SWT.NULL);
097: toolItem.setImage(MOVE_DOWN_ICON);
098: toolItem
099: .setToolTipText(Messages
100: .getString("ActionsMasterDetailBlock.MOVE_ACTION_DOWN")); //$NON-NLS-1$
101: toolItem.addSelectionListener(new SelectionListener() {
102: public void widgetDefaultSelected(SelectionEvent e) {
103: }
104:
105: public void widgetSelected(SelectionEvent e) {
106: moveSelectedItemDown();
107: }
108: });
109:
110: toolItem = new ToolItem(toolBar, SWT.NULL);
111: toolItem.setImage(MOVE_UP_ICON);
112: toolItem.setToolTipText(Messages
113: .getString("ActionsMasterDetailBlock.MOVE_ACTION_UP")); //$NON-NLS-1$
114: toolItem.addSelectionListener(new SelectionListener() {
115: public void widgetDefaultSelected(SelectionEvent e) {
116: }
117:
118: public void widgetSelected(SelectionEvent e) {
119: moveSelectedItemUp();
120: }
121: });
122:
123: addToolItem = new ToolItem(toolBar, SWT.SEPARATOR);
124:
125: addToolItem = new ToolItem(toolBar, SWT.DROP_DOWN);
126: addToolItem.setImage(ADD_ACTION_ICON);
127: addToolItem.setToolTipText(Messages
128: .getString("ActionsMasterDetailBlock.ADD_NEW_INPUT")); //$NON-NLS-1$
129:
130: addToolItem.addSelectionListener(new SelectionListener() {
131: public void widgetDefaultSelected(SelectionEvent e) {
132: }
133:
134: public void widgetSelected(SelectionEvent e) {
135: ToolItem item = (ToolItem) e.widget;
136: Rectangle rectangle = item.getBounds();
137: Point pt = item.getParent().toDisplay(
138: new Point(rectangle.x, rectangle.y));
139: addInputMenu.setLocation(pt.x, pt.y + rectangle.height);
140: addInputMenu.setVisible(true);
141: }
142:
143: });
144:
145: deleteToolItem = new ToolItem(toolBar, SWT.NULL);
146: deleteToolItem.setImage(REMOVE_ACTION_ICON);
147: deleteToolItem
148: .setToolTipText(Messages
149: .getString("ActionsMasterDetailBlock.REMOVE_SELECTED_INPUT")); //$NON-NLS-1$
150: deleteToolItem.addSelectionListener(new SelectionListener() {
151: public void widgetDefaultSelected(SelectionEvent e) {
152: }
153:
154: public void widgetSelected(SelectionEvent e) {
155: actionInputsList.removeSelectedIOElement();
156: }
157: });
158:
159: }
160:
161: public void refresh() {
162: if (addInputMenu != null) {
163: addInputMenu.dispose();
164: }
165:
166: addInputMenu = new Menu(actionInputsList.getList().getShell(),
167: SWT.POP_UP);
168: IActionInputVariable[] availInputs = actionInputsList.actionDefinition
169: .getAvailInputVariables();
170: ArrayList availInputNames = new ArrayList();
171: for (int i = 0; i < availInputs.length; i++) {
172: IActionIOFilter actionIOFilter = actionInputsList
173: .getFilter();
174: if ((actionIOFilter == null)
175: || actionIOFilter
176: .accept((AbstractIOElement) availInputs[i])) {
177: if (availInputs[i] instanceof ActionOutput) {
178: ActionOutput actionOutput = (ActionOutput) availInputs[i];
179: if (!availInputNames.contains(actionOutput
180: .getPublicName())) {
181: new AddInputMenuItem(addInputMenu, actionOutput);
182: availInputNames.add(actionOutput
183: .getPublicName());
184: }
185: } else if (availInputs[i] instanceof ActionSequenceInput) {
186: ActionSequenceInput actionSequenceInput = (ActionSequenceInput) availInputs[i];
187: if (!availInputNames.contains(actionSequenceInput
188: .getName())) {
189: new AddInputMenuItem(addInputMenu,
190: actionSequenceInput);
191: availInputNames.add(actionSequenceInput
192: .getName());
193: }
194: }
195: }
196: }
197: }
198:
199: protected void moveSelectedItemUp() {
200: IStructuredSelection structuredSelection = (IStructuredSelection) actionInputsList
201: .getSelection();
202: if (structuredSelection.size() == 1) {
203: ActionInput selectedActionInput = (ActionInput) structuredSelection
204: .getFirstElement();
205: int currentListIndex = 0;
206: for (int i = 0; i < actionInputsList.getList()
207: .getItemCount(); i++) {
208: if (actionInputsList.getElementAt(i).equals(
209: selectedActionInput)) {
210: currentListIndex = i;
211: break;
212: }
213: }
214: if (currentListIndex != 0) {
215: ActionInput prevInput = (ActionInput) actionInputsList
216: .getElementAt(currentListIndex - 1);
217: ArrayList allInputs = new ArrayList(Arrays
218: .asList(actionInputsList.actionDefinition
219: .getAllInputParams()));
220: int siblingIndex = allInputs.indexOf(prevInput);
221: actionInputsList.actionDefinition.setInputParamIndex(
222: selectedActionInput, siblingIndex);
223: actionInputsList.refresh();
224: structuredSelection = new StructuredSelection(
225: selectedActionInput);
226: }
227: }
228: }
229:
230: protected void moveSelectedItemDown() {
231: IStructuredSelection structuredSelection = (IStructuredSelection) actionInputsList
232: .getSelection();
233: if (structuredSelection.size() == 1) {
234: ActionInput selectedActionInput = (ActionInput) structuredSelection
235: .getFirstElement();
236: int currentListIndex = actionInputsList.getList()
237: .getItemCount() - 1;
238: for (int i = 0; i < actionInputsList.getList()
239: .getItemCount(); i++) {
240: if (actionInputsList.getElementAt(i).equals(
241: selectedActionInput)) {
242: currentListIndex = i;
243: break;
244: }
245: }
246: if (currentListIndex != actionInputsList.getList()
247: .getItemCount() - 1) {
248: ActionInput nextInput = (ActionInput) actionInputsList
249: .getElementAt(currentListIndex + 1);
250: ArrayList allInputs = new ArrayList(Arrays
251: .asList(actionInputsList.actionDefinition
252: .getAllInputParams()));
253: int siblingIndex = allInputs.indexOf(nextInput);
254: actionInputsList.actionDefinition.setInputParamIndex(
255: selectedActionInput, siblingIndex);
256: actionInputsList.refresh();
257: structuredSelection = new StructuredSelection(
258: selectedActionInput);
259: }
260: }
261: }
262:
263: public Control getControl() {
264: return toolBar;
265: }
266: }
|