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.eclipse.jface.action.Action;
020: import org.eclipse.jface.action.IMenuListener;
021: import org.eclipse.jface.action.IMenuManager;
022: import org.eclipse.jface.action.MenuManager;
023: import org.eclipse.jface.viewers.IStructuredContentProvider;
024: import org.eclipse.jface.viewers.IStructuredSelection;
025: import org.eclipse.jface.viewers.LabelProvider;
026: import org.eclipse.jface.viewers.ListViewer;
027: import org.eclipse.jface.viewers.Viewer;
028: import org.eclipse.jface.viewers.ViewerDropAdapter;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.dnd.DND;
031: import org.eclipse.swt.dnd.Transfer;
032: import org.eclipse.swt.dnd.TransferData;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.swt.widgets.Menu;
035: import org.pentaho.actionsequence.dom.AbstractIOElement;
036: import org.pentaho.actionsequence.dom.ActionInput;
037: import org.pentaho.actionsequence.dom.ActionOutput;
038: import org.pentaho.actionsequence.dom.ActionSequenceInput;
039: import org.pentaho.actionsequence.dom.IActionInputVariable;
040: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
041: import org.pentaho.designstudio.editors.actionsequence.pages.actions.IActionIOFilter;
042: import org.pentaho.designstudio.messages.Messages;
043:
044: /**
045: * Table viewer used for viewing and modifying the inputs and outputs of an action
046: * definition element.
047: *
048: * @author Angelo Rodriguez
049: */
050: public class ActionInputsList extends ListViewer {
051:
052: class InputListDropAdapter extends ViewerDropAdapter {
053:
054: InputListDropAdapter() {
055: super (ActionInputsList.this );
056: setFeedbackEnabled(true);
057: }
058:
059: public boolean performDrop(Object data) {
060: ActionInput input = null;
061: if (data instanceof IActionInputVariable) {
062: if ((filter == null)
063: || filter.accept((AbstractIOElement) data)) {
064: input = addInput((AbstractIOElement) data);
065: }
066: }
067: return input != null;
068: }
069:
070: /*
071: * (non-Javadoc)
072: *
073: * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object,
074: * int, org.eclipse.swt.dnd.TransferData)
075: */
076: public boolean validateDrop(Object target, int op,
077: TransferData type) {
078: return canDrop(target, op, type);
079: }
080: }
081:
082: public class ContentProvider implements IStructuredContentProvider {
083:
084: public Object[] getElements(Object ioElement) {
085: ActionInput[] inputs = actionDefinition.getAllInputParams();
086: ArrayList inputsList = new ArrayList();
087: for (int i = 0; i < inputs.length; i++) {
088: if ((filter == null) || filter.accept(inputs[i])) {
089: inputsList.add(inputs[i]);
090: }
091: }
092: return inputsList.toArray();
093: }
094:
095: public void dispose() {
096: }
097:
098: public void inputChanged(Viewer viewer, Object oldInput,
099: Object newInput) {
100: actionDefinition = (ActionDefinition) newInput;
101: }
102: }
103:
104: class AddInputAction extends Action {
105: AbstractIOElement actionSequenceIO;
106:
107: AddInputAction(ActionOutput actionOutput) {
108: super (actionOutput.getPublicName());
109: actionSequenceIO = actionOutput;
110: }
111:
112: AddInputAction(ActionSequenceInput actionSequenceInput) {
113: super (actionSequenceInput.getName());
114: actionSequenceIO = actionSequenceInput;
115: }
116:
117: public void run() {
118: addInput(actionSequenceIO);
119: }
120: }
121:
122: protected class DeleteInputAction extends Action {
123: public DeleteInputAction() {
124: super ("Delete"); //$NON-NLS-1$
125: }
126:
127: public void run() {
128: removeSelectedIOElement();
129: }
130: }
131:
132: ActionDefinition actionDefinition;
133:
134: protected DeleteInputAction deleteIOAction = new DeleteInputAction();
135:
136: IActionIOFilter filter;
137:
138: private InputListDropAdapter dropAdapter;
139:
140: private ContentProvider contentProvider;
141:
142: MenuManager addInputSubMenuManager;
143:
144: /**
145: * Creates an viewer
146: * @param parent the parent of this viewer.
147: * @param toolkit the form toolkit.
148: */
149: public ActionInputsList(Composite parent) {
150: super (parent, SWT.V_SCROLL | SWT.BORDER);
151:
152: contentProvider = new ContentProvider();
153: setContentProvider(contentProvider);
154: setLabelProvider(new LabelProvider() {
155: public String getText(Object element) {
156: String text = ""; //$NON-NLS-1$
157: if (element instanceof AbstractIOElement) {
158: AbstractIOElement ioElement = (AbstractIOElement) element;
159: text = ioElement.getName();
160: }
161: return text;
162: }
163: });
164: createPopupMenu();
165: int ops = DND.DROP_MOVE | DND.DROP_COPY;
166: Transfer[] transfers = new Transfer[] { ActionSequenceParamTransfer
167: .getInstance() };
168: dropAdapter = new InputListDropAdapter();
169: addDropSupport(ops, transfers, dropAdapter);
170:
171: }
172:
173: /**
174: * Creates the popup menu used by this viewer.
175: */
176: protected void createPopupMenu() {
177: MenuManager popupMenu = new MenuManager();
178:
179: addInputSubMenuManager = new MenuManager(
180: Messages
181: .getString("ActionsMasterDetailBlock.UI_INPUTS_MENU_ADD")); //$NON-NLS-1$
182: popupMenu.add(addInputSubMenuManager);
183:
184: popupMenu.add(deleteIOAction);
185: popupMenu.addMenuListener(new IMenuListener() {
186: public void menuAboutToShow(IMenuManager manager) {
187: updatePopupMenuState(manager);
188: }
189: });
190:
191: org.eclipse.swt.widgets.List list = getList();
192: Menu menu = popupMenu.createContextMenu(list);
193: list.setMenu(menu);
194: }
195:
196: /**
197: * Removes the selected inputs/ouputs from the action definition.
198: */
199: protected void removeSelectedIOElement() {
200: IStructuredSelection selection = (IStructuredSelection) getSelection();
201: remove(selection.toArray());
202: List selections = selection.toList();
203: for (Iterator iter = selections.iterator(); iter.hasNext();) {
204: AbstractIOElement selectedElement = (AbstractIOElement) iter
205: .next();
206: actionDefinition
207: .removeInputParam(selectedElement.getName());
208:
209: }
210: refresh();
211: }
212:
213: protected void updatePopupMenuState(IMenuManager menuMgr) {
214: IStructuredSelection selection = (IStructuredSelection) getSelection();
215: deleteIOAction.setEnabled(selection.size() > 0);
216: }
217:
218: /* (non-Javadoc)
219: * @see org.eclipse.jface.viewers.Viewer#inputChanged(java.lang.Object, java.lang.Object)
220: */
221: protected void inputChanged(Object input, Object oldInput) {
222: actionDefinition = (ActionDefinition) input;
223: refreshPopupMenu();
224: super .inputChanged(input, oldInput);
225: }
226:
227: public void setFilter(IActionIOFilter filter) {
228: this .filter = filter;
229: refresh();
230: }
231:
232: public IActionIOFilter getFilter() {
233: return filter;
234: }
235:
236: protected boolean canDrop(Object target, int op, TransferData type) {
237: return true;
238: }
239:
240: protected ActionInput addInput(AbstractIOElement actionSequenceIO) {
241: ActionInput actionInput = null;
242: if ((actionSequenceIO instanceof ActionSequenceInput)
243: && (actionDefinition.getDocument().getInput(
244: actionSequenceIO.getName()) != null)) {
245: actionInput = actionDefinition.addInputParam(
246: ((ActionSequenceInput) actionSequenceIO).getName(),
247: actionSequenceIO.getType());
248: refresh();
249: } else if (actionSequenceIO instanceof ActionOutput) {
250: ActionOutput droppedOutput = (ActionOutput) actionSequenceIO;
251: IActionInputVariable[] availInputs = actionDefinition
252: .getAvailInputVariables();
253: for (int i = 0; i < availInputs.length; i++) {
254: if (availInputs[i] instanceof ActionOutput) {
255: ActionOutput availInput = (ActionOutput) availInputs[i];
256: if (availInput.getName().equals(
257: droppedOutput.getName())
258: && availInput.getPublicName().equals(
259: droppedOutput.getPublicName())
260: && availInput.getType().equals(
261: droppedOutput.getType())) {
262: actionInput = actionDefinition.addInputParam(
263: droppedOutput.getPublicName(),
264: droppedOutput.getType());
265: refresh();
266: break;
267: }
268: }
269: }
270: }
271: return actionInput;
272: }
273:
274: private void refreshPopupMenu() {
275: addInputSubMenuManager.removeAll();
276:
277: IActionInputVariable[] availInputs = actionDefinition
278: .getAvailInputVariables();
279: ArrayList availInputNames = new ArrayList();
280: for (int i = 0; i < availInputs.length; i++) {
281: IActionIOFilter actionIOFilter = getFilter();
282: if ((actionIOFilter == null)
283: || actionIOFilter
284: .accept((AbstractIOElement) availInputs[i])) {
285: if (availInputs[i] instanceof ActionOutput) {
286: ActionOutput actionOutput = (ActionOutput) availInputs[i];
287: if (!availInputNames.contains(actionOutput
288: .getPublicName())) {
289: addInputSubMenuManager.add(new AddInputAction(
290: actionOutput));
291: availInputNames.add(actionOutput
292: .getPublicName());
293: }
294: } else if (availInputs[i] instanceof ActionSequenceInput) {
295: ActionSequenceInput actionSequenceInput = (ActionSequenceInput) availInputs[i];
296: if (!availInputNames.contains(actionSequenceInput
297: .getName())) {
298: addInputSubMenuManager.add(new AddInputAction(
299: actionSequenceInput));
300: availInputNames.add(actionSequenceInput
301: .getName());
302: }
303: }
304: }
305: }
306: }
307: }
|