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