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 javax.swing.table.DefaultTableModel;
018:
019: import org.dom4j.Element;
020: import org.eclipse.swt.SWT;
021: import org.eclipse.swt.events.ModifyEvent;
022: import org.eclipse.swt.events.ModifyListener;
023: import org.eclipse.swt.events.SelectionEvent;
024: import org.eclipse.swt.events.SelectionListener;
025: import org.eclipse.swt.widgets.Combo;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.Control;
028: import org.pentaho.actionsequence.dom.ActionOutput;
029: import org.pentaho.actionsequence.dom.ActionSequenceDocument;
030: import org.pentaho.actionsequence.dom.ActionSequenceInput;
031: import org.pentaho.actionsequence.dom.IActionInputVariable;
032: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
033:
034: /**
035: * An action definition input editor that is backed by an SWT combo box.
036: *
037: * @author Angelo Rodriguez
038: */
039: public class SecureFilterColumnCombo implements IActionSequenceControl,
040: ModifyListener, SelectionListener {
041: protected Combo combo;
042: protected ActionDefinition actionDefinition;
043: protected String inputName;
044: protected String filterOn;
045: protected int columnType;
046:
047: public static final int DISPLAY_COLUMN_TYPE = 1;
048: public static final int VALUE_COLUMN_TYPE = 2;
049:
050: private static final String VALUE_COLUMN_ATTR_NAME = "value-col-name"; //$NON-NLS-1$
051: private static final String DISPLAY_COLUMN_ATTR_NAME = "display-col-name"; //$NON-NLS-1$
052:
053: /**
054: * @param toolkit the toolkit being used to create UI components
055: * @param parent the parent control
056: * @param layoutData the check box layout data. Can be null.
057: */
058: public SecureFilterColumnCombo(Composite parent, Object layoutData,
059: int columnType) {
060: this .columnType = columnType;
061: combo = WidgetFactory.createCombo(parent, SWT.NONE);
062: if (layoutData != null) {
063: combo.setLayoutData(layoutData);
064: }
065:
066: combo.addModifyListener(this );
067: // combo.addSelectionListener(this);
068: }
069:
070: /**
071: * Sets the target action definition input.
072: * @param actionDefinition the action definition being managed.
073: * @param inputName the name of the input within the action definition
074: * @param types the data types supported by this input
075: */
076: public void setTargetInput(ActionDefinition actionDefinition,
077: String inputName) {
078: this .actionDefinition = actionDefinition;
079: this .inputName = inputName;
080: refreshAvailComboOptions();
081: refresh();
082: }
083:
084: /**
085: * @return The action definition whose input is being managed.
086: */
087: public ActionDefinition getActionDefinition() {
088: return actionDefinition;
089: }
090:
091: public String getInputName() {
092: return inputName;
093: }
094:
095: public String getFilterOn() {
096: return filterOn;
097: }
098:
099: public void setFilterOn(String filterOn) {
100: this .filterOn = filterOn;
101: refreshAvailComboOptions();
102: refresh();
103: }
104:
105: /* (non-Javadoc)
106: * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
107: */
108: public void modifyText(ModifyEvent e) {
109: updateActionSequence();
110: }
111:
112: public void refresh() {
113: combo.removeModifyListener(this );
114: // combo.removeSelectionListener(this);
115: String comboText = ""; //$NON-NLS-1$
116: if ((actionDefinition != null) && (inputName != null)
117: && (inputName.trim().length() > 0)) {
118: Element element = actionDefinition
119: .getComponentDefElement("selections/" + inputName + "/filter"); //$NON-NLS-1$ //$NON-NLS-2$
120: if (element != null) {
121: comboText = element
122: .attributeValue(columnType == VALUE_COLUMN_TYPE ? VALUE_COLUMN_ATTR_NAME
123: : DISPLAY_COLUMN_ATTR_NAME);
124: }
125: }
126: combo.setText(comboText == null ? "" : comboText); //$NON-NLS-1$
127: // combo.addSelectionListener(this);
128: combo.addModifyListener(this );
129: }
130:
131: public void widgetDefaultSelected(SelectionEvent e) {
132: }
133:
134: public void widgetSelected(SelectionEvent e) {
135: updateActionSequence();
136: }
137:
138: public void updateActionSequence() {
139: if ((actionDefinition != null) && (inputName != null)
140: && (inputName.trim().length() > 0)) {
141: actionDefinition
142: .setComponentDefinitionAttribute(
143: "selections/" + inputName + "/filter", columnType == VALUE_COLUMN_TYPE ? VALUE_COLUMN_ATTR_NAME : DISPLAY_COLUMN_ATTR_NAME, combo.getText().trim()); //$NON-NLS-1$ //$NON-NLS-2$
144: }
145: }
146:
147: protected void refreshAvailComboOptions() {
148: combo.removeModifyListener(this );
149: // combo.removeSelectionListener(this);
150: ArrayList columns = new ArrayList();
151: if ((actionDefinition != null) && (filterOn != null)
152: && (filterOn.trim().length() > 0)) {
153: IActionInputVariable[] availInputs = actionDefinition
154: .getAvailInputVariables(new String[] {
155: ActionSequenceDocument.PROPERTY_MAP_LIST_TYPE,
156: ActionSequenceDocument.RESULTSET_TYPE });
157: for (int i = 0; i < availInputs.length; i++) {
158: if (availInputs[i] instanceof ActionSequenceInput) {
159: ActionSequenceInput actionSequenceInput = (ActionSequenceInput) availInputs[i];
160: if (actionSequenceInput.getName().equals(filterOn)) {
161: DefaultTableModel tableModel = (DefaultTableModel) actionSequenceInput
162: .getDefaultValue();
163: if (tableModel != null) {
164: for (int j = 0; j < tableModel
165: .getColumnCount(); j++) {
166: String name = tableModel
167: .getColumnName(j);
168: if ((name != null)
169: && (name.trim().length() > 0)) {
170: columns.add(name.trim());
171: }
172: }
173: }
174: }
175: } else if (availInputs[i] instanceof ActionOutput) {
176: ActionOutput actionOutput = (ActionOutput) availInputs[i];
177: if (actionOutput.getPublicName().equals(filterOn)) {
178: ActionDefinition actionDef = actionOutput
179: .getActionDefinition();
180: if (actionDef.getComponentName().endsWith(
181: "SQLLookupRule")) { //$NON-NLS-1$
182: ActionOutput[] actionOutputs = actionDef
183: .getOutputParams(new String[] {
184: ActionSequenceDocument.STRING_TYPE,
185: ActionSequenceDocument.INTEGER_TYPE });
186: for (int j = 0; j < actionOutputs.length; j++) {
187: columns.add(actionOutputs[j].getName());
188: }
189: }
190: }
191: }
192: }
193: }
194: combo.setItems((String[]) columns.toArray(new String[0]));
195: // combo.addSelectionListener(this);
196: combo.addModifyListener(this );
197: }
198:
199: public Control getControl() {
200: return combo;
201: }
202: }
|