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:
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.dnd.DND;
019: import org.eclipse.swt.dnd.DropTarget;
020: import org.eclipse.swt.dnd.DropTargetEvent;
021: import org.eclipse.swt.dnd.DropTargetListener;
022: import org.eclipse.swt.dnd.Transfer;
023: import org.eclipse.swt.events.ModifyEvent;
024: import org.eclipse.swt.events.ModifyListener;
025: import org.eclipse.swt.events.SelectionEvent;
026: import org.eclipse.swt.events.SelectionListener;
027: import org.eclipse.swt.widgets.Combo;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.swt.widgets.Control;
030: import org.pentaho.actionsequence.dom.AbstractIOElement;
031: import org.pentaho.actionsequence.dom.ActionLoop;
032: import org.pentaho.actionsequence.dom.ActionOutput;
033: import org.pentaho.actionsequence.dom.ActionSequenceDocument;
034: import org.pentaho.actionsequence.dom.ActionSequenceInput;
035: import org.pentaho.actionsequence.dom.IActionInputVariable;
036:
037: public class ActionLoopCombo implements IActionSequenceControl,
038: ModifyListener, SelectionListener {
039: Combo combo;
040: ActionLoop actionLoop;
041: DropTarget dropTarget;
042:
043: public ActionLoopCombo(Composite parent, Object layoutData) {
044: combo = WidgetFactory.createCombo(parent, SWT.NONE);
045: if (layoutData != null) {
046: combo.setLayoutData(layoutData);
047: }
048:
049: dropTarget = new DropTarget(combo, DND.DROP_MOVE
050: | DND.DROP_COPY | DND.DROP_DEFAULT);
051: dropTarget
052: .setTransfer(new Transfer[] { ActionSequenceParamTransfer
053: .getInstance() });
054:
055: dropTarget.addDropListener(new DropTargetListener() {
056:
057: public void dragEnter(DropTargetEvent event) {
058: if (event.detail == DND.DROP_DEFAULT) {
059: if ((event.operations & DND.DROP_COPY) != 0) {
060: event.detail = DND.DROP_COPY;
061: } else {
062: event.detail = DND.DROP_NONE;
063: }
064: }
065: }
066:
067: public void dragOver(DropTargetEvent event) {
068: }
069:
070: public void dragOperationChanged(DropTargetEvent event) {
071: }
072:
073: public void dragLeave(DropTargetEvent event) {
074: }
075:
076: public void dropAccept(DropTargetEvent event) {
077: }
078:
079: public void drop(DropTargetEvent event) {
080: if (ActionSequenceParamTransfer.getInstance()
081: .isSupportedType(event.currentDataType)) {
082: AbstractIOElement param = (AbstractIOElement) event.data;
083: if ((param instanceof ActionSequenceInput)
084: || (param instanceof ActionOutput)) {
085: performDrop((AbstractIOElement) param);
086: }
087: }
088: }
089: });
090: combo.addModifyListener(this );
091: combo.addSelectionListener(this );
092: }
093:
094: public void setTargetLoop(ActionLoop actionLoop) {
095: this .actionLoop = actionLoop;
096: refresh();
097: }
098:
099: public void modifyText(ModifyEvent e) {
100: updateActionSequence();
101: }
102:
103: public void refresh() {
104: combo.removeModifyListener(this );
105: combo.removeSelectionListener(this );
106: updateAvailLoopVars();
107: combo.setText(ActionUtil.parameterToDisplayText(actionLoop
108: .getLoopOn()));
109: combo.addSelectionListener(this );
110: combo.addModifyListener(this );
111: }
112:
113: public void widgetDefaultSelected(SelectionEvent e) {
114: }
115:
116: public void widgetSelected(SelectionEvent e) {
117: updateActionSequence();
118: }
119:
120: public void updateActionSequence() {
121: String loopOn = ActionUtil.parameterFromDisplayText(combo
122: .getText().trim());
123: actionLoop.setLoopOn(loopOn);
124: }
125:
126: protected void performDrop(AbstractIOElement io) {
127: String droppedType = io.getType();
128: String droppedName = (io instanceof ActionOutput) ? ((ActionOutput) io)
129: .getPublicName()
130: : io.getName();
131: if (ActionSequenceDocument.PROPERTY_MAP_LIST_TYPE
132: .equals(droppedType)
133: || ActionSequenceDocument.STRING_LIST_TYPE
134: .equals(droppedType)
135: || ActionSequenceDocument.RESULTSET_TYPE
136: .equals(droppedType)) {
137:
138: if ((io instanceof ActionSequenceInput)
139: && (actionLoop.getDocument().getInput(io.getName()) != null)) {
140: combo.setText(ActionUtil
141: .parameterToDisplayText(droppedName));
142: } else if (io instanceof ActionOutput) {
143: ActionOutput droppedOutput = (ActionOutput) io;
144: IActionInputVariable[] availInputs = actionLoop
145: .getAvailInputVariables();
146: for (int i = 0; i < availInputs.length; i++) {
147: if (availInputs[i] instanceof ActionOutput) {
148: ActionOutput availInput = (ActionOutput) availInputs[i];
149: if (availInput.getName().equals(
150: droppedOutput.getName())
151: && availInput.getPublicName().equals(
152: droppedOutput.getPublicName())
153: && availInput.getType().equals(
154: droppedOutput.getType())) {
155: combo
156: .setText(ActionUtil
157: .parameterToDisplayText(droppedName));
158: break;
159: }
160: }
161: }
162: }
163: }
164: }
165:
166: protected void updateAvailLoopVars() {
167: combo.setItems(new String[0]);
168: if (actionLoop != null) {
169: ArrayList items = new ArrayList();
170: items.add(""); //$NON-NLS-1$
171: IActionInputVariable[] availInputs = actionLoop
172: .getAvailInputVariables();
173: for (int i = 0; i < availInputs.length; i++) {
174: items.add(ActionUtil
175: .parameterToDisplayText(availInputs[i]
176: .getVariableName()));
177: }
178: combo.setItems((String[]) items.toArray(new String[0]));
179: }
180: }
181:
182: public Control getControl() {
183: return combo;
184: }
185: }
|