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.lang.reflect.InvocationTargetException;
016: import java.lang.reflect.Method;
017: import java.util.ArrayList;
018: import java.util.Arrays;
019:
020: import org.eclipse.jface.window.Window;
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.events.FocusEvent;
023: import org.eclipse.swt.events.FocusListener;
024: import org.eclipse.swt.events.KeyEvent;
025: import org.eclipse.swt.events.KeyListener;
026: import org.eclipse.swt.events.ModifyEvent;
027: import org.eclipse.swt.events.ModifyListener;
028: import org.eclipse.swt.events.SelectionEvent;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Control;
031: import org.eclipse.swt.widgets.Text;
032: import org.pentaho.actionsequence.dom.ActionOutput;
033: import org.pentaho.actionsequence.dom.ActionSequenceOutput;
034: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
035: import org.pentaho.designstudio.editors.actionsequence.pages.parameters.BrokenParamReferencesDialog;
036: import org.pentaho.designstudio.editors.actionsequence.pages.parameters.UpdateParamReferencesDialog;
037:
038: public class ActionOutputText implements IActionSequenceControl,
039: ModifyListener {
040: protected Text outputText;
041: protected ActionDefinition actionDefinition;
042: String originalOutputName;
043: ArrayList parameterReferences = new ArrayList();
044: Method getOutputMethod;
045: Method setOutputMethod;
046:
047: public ActionOutputText(Composite parent, Object layoutData) {
048: outputText = WidgetFactory.createText(parent, "", SWT.BORDER); //$NON-NLS-1$
049: if (layoutData != null) {
050: outputText.setLayoutData(layoutData);
051: }
052: outputText.addModifyListener(this );
053: outputText.addKeyListener(new KeyListener() {
054: public void keyPressed(KeyEvent e) {
055: e.doit = !Character.isWhitespace(e.character)
056: && (e.character != '-');
057: }
058:
059: public void keyReleased(KeyEvent e) {
060: }
061: });
062: outputText.addFocusListener(new FocusListener() {
063: public void focusGained(FocusEvent e) {
064: originalOutputName = outputText.getText().trim();
065: parameterReferences.clear();
066: if (originalOutputName.length() > 0) {
067: try {
068: ActionOutput actionOutput = (ActionOutput) getOutputMethod
069: .invoke(actionDefinition, null);
070: if (actionOutput != null) {
071: parameterReferences.addAll(Arrays
072: .asList(actionDefinition
073: .getDocument()
074: .getReferencesTo(
075: actionOutput)));
076: ActionSequenceOutput actionSequenceOutput = actionOutput
077: .getDocument().getOutput(
078: actionOutput
079: .getPublicName());
080: if (actionSequenceOutput != null) {
081: parameterReferences
082: .add(actionSequenceOutput);
083: }
084: }
085: } catch (IllegalArgumentException e1) {
086: // TODO Auto-generated catch block
087: e1.printStackTrace();
088: } catch (IllegalAccessException e1) {
089: // TODO Auto-generated catch block
090: e1.printStackTrace();
091: } catch (InvocationTargetException e1) {
092: // TODO Auto-generated catch block
093: e1.printStackTrace();
094: }
095: }
096: }
097:
098: public void focusLost(FocusEvent e) {
099: if (originalOutputName.length() > 0) {
100: String currentParamName = outputText.getText()
101: .trim();
102: if (currentParamName.length() == 0) {
103: if (parameterReferences.size() > 0) {
104: BrokenParamReferencesDialog brokenRefsDialog = new BrokenParamReferencesDialog(
105: outputText.getShell(),
106: parameterReferences);
107: if (brokenRefsDialog.open() == Window.CANCEL) {
108: outputText.setText(originalOutputName);
109: }
110: }
111: } else if (!currentParamName
112: .equals(originalOutputName)
113: && (parameterReferences.size() > 0)) {
114: try {
115: UpdateParamReferencesDialog renameOutputDialog = new UpdateParamReferencesDialog(
116: outputText.getShell(),
117: (ActionOutput) getOutputMethod
118: .invoke(actionDefinition,
119: null),
120: parameterReferences);
121: if (renameOutputDialog.open() == Window.CANCEL) {
122: outputText.setText(originalOutputName);
123: }
124: } catch (IllegalArgumentException e1) {
125: // TODO Auto-generated catch block
126: e1.printStackTrace();
127: } catch (IllegalAccessException e1) {
128: // TODO Auto-generated catch block
129: e1.printStackTrace();
130: } catch (InvocationTargetException e1) {
131: // TODO Auto-generated catch block
132: e1.printStackTrace();
133: }
134: }
135: }
136: }
137: });
138: }
139:
140: public void setTargetOutput(ActionDefinition actionDefinition,
141: String outputName) {
142: this .actionDefinition = actionDefinition;
143: if ((outputName != null) && (outputName.trim().length() > 0)) {
144: String propertyName = getPropertyName(outputName);
145: try {
146: getOutputMethod = actionDefinition.getClass()
147: .getMethod("get" + propertyName, new Class[0]); //$NON-NLS-1$ //$NON-NLS-2$
148: } catch (Exception ex) {
149: }
150: try {
151: setOutputMethod = actionDefinition
152: .getClass()
153: .getMethod(
154: "set" + propertyName, new Class[] { String.class }); //$NON-NLS-1$ //$NON-NLS-2$
155: } catch (Exception ex) {
156: }
157: } else {
158: getOutputMethod = null;
159: setOutputMethod = null;
160: }
161: refresh();
162: }
163:
164: public ActionDefinition getActionDefinition() {
165: return actionDefinition;
166: }
167:
168: public void modifyText(ModifyEvent e) {
169: updateActionSequence();
170: }
171:
172: public void refresh() {
173: outputText.removeModifyListener(this );
174: ActionOutput actionOutput = null;
175:
176: try {
177: if (getOutputMethod != null) {
178: actionOutput = (ActionOutput) getOutputMethod.invoke(
179: actionDefinition, new Object[0]);
180: }
181: } catch (IllegalArgumentException e) {
182: // TODO Auto-generated catch block
183: e.printStackTrace();
184: } catch (IllegalAccessException e) {
185: // TODO Auto-generated catch block
186: e.printStackTrace();
187: } catch (InvocationTargetException e) {
188: // TODO Auto-generated catch block
189: e.printStackTrace();
190: }
191:
192: outputText.setText(actionOutput != null ? actionOutput
193: .getPublicName() : ""); //$NON-NLS-1$
194: outputText.addModifyListener(this );
195: }
196:
197: public Control getControl() {
198: return outputText;
199: }
200:
201: public void widgetSelected(SelectionEvent e) {
202: updateActionSequence();
203: }
204:
205: public void updateActionSequence() {
206: String mapping = outputText.getText().trim();
207: try {
208: if (mapping.length() == 0) {
209: setOutputMethod.invoke(actionDefinition,
210: new Object[] { null });
211: } else {
212: setOutputMethod.invoke(actionDefinition,
213: new Object[] { mapping });
214: }
215: } catch (IllegalArgumentException e) {
216: // TODO Auto-generated catch block
217: e.printStackTrace();
218: } catch (IllegalAccessException e) {
219: // TODO Auto-generated catch block
220: e.printStackTrace();
221: } catch (InvocationTargetException e) {
222: // TODO Auto-generated catch block
223: e.printStackTrace();
224: }
225: }
226:
227: private String getPropertyName(String inputName) {
228: StringBuffer stringBuffer = new StringBuffer(inputName
229: .toLowerCase());
230: stringBuffer.setCharAt(0, Character.toUpperCase(stringBuffer
231: .charAt(0)));
232: for (int index = stringBuffer.toString().indexOf('-'); index != -1; index = stringBuffer
233: .toString().indexOf('-')) {
234: stringBuffer.deleteCharAt(index);
235: if (index < stringBuffer.length()) {
236: stringBuffer.setCharAt(index, Character
237: .toUpperCase(stringBuffer.charAt(index)));
238: }
239: }
240: for (int index = stringBuffer.toString().indexOf('_'); index != -1; index = stringBuffer
241: .toString().indexOf('_')) {
242: stringBuffer.deleteCharAt(index);
243: if (index < stringBuffer.length()) {
244: stringBuffer.setCharAt(index, Character
245: .toUpperCase(stringBuffer.charAt(index)));
246: }
247: }
248: return stringBuffer.toString();
249: }
250: }
|