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:
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.events.SelectionEvent;
020: import org.eclipse.swt.events.SelectionListener;
021: import org.eclipse.swt.widgets.Button;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.swt.widgets.Control;
024: import org.pentaho.actionsequence.dom.ActionInputConstant;
025: import org.pentaho.actionsequence.dom.IActionInputValueProvider;
026: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
027:
028: /**
029: * An action definition input editor that is backed by an SWT check box button.
030: *
031: * @author Angelo Rodriguez
032: */
033: public class NewActionInputCheckBox implements IActionSequenceControl,
034: SelectionListener {
035:
036: protected Button button;
037: protected ActionDefinition actionDefinition;
038: protected String inputName;
039: protected Method getValueMethod;
040: protected Method setValueMethod;
041:
042: /**
043: * @param toolkit the toolkit being used to create UI components
044: * @param label the label to be placed next to the check
045: * @param parent the parent control
046: * @param layoutData the check box layout data. Can be null.
047: */
048: public NewActionInputCheckBox(String label, Composite parent,
049: Object layoutData) {
050: button = WidgetFactory.createButton(parent, label, SWT.CHECK);
051:
052: if (layoutData != null) {
053: button.setLayoutData(layoutData);
054: }
055:
056: button.addSelectionListener(this );
057: }
058:
059: /* (non-Javadoc)
060: * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
061: */
062: public void widgetDefaultSelected(SelectionEvent e) {
063: }
064:
065: /* (non-Javadoc)
066: * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
067: */
068: public void widgetSelected(SelectionEvent e) {
069: updateActionSequence();
070: }
071:
072: /**
073: * Sets the target action definition input.
074: * @param actionDefinition the action definition being managed.
075: * @param inputName the name of the input within the action definition
076: */
077: public void setTargetInput(ActionDefinition actionDefinition,
078: String inputName) {
079: this .actionDefinition = actionDefinition;
080: if ((inputName != null) && (inputName.trim().length() > 0)) {
081: String propertyName = getPropertyName(inputName);
082: try {
083: getValueMethod = actionDefinition.getClass().getMethod(
084: "get" + propertyName, new Class[0]); //$NON-NLS-1$
085: } catch (Exception ex) {
086: }
087: try {
088: setValueMethod = actionDefinition
089: .getClass()
090: .getMethod(
091: "set" + propertyName, new Class[] { IActionInputValueProvider.class }); //$NON-NLS-1$
092: } catch (Exception ex) {
093: }
094: } else {
095: getValueMethod = null;
096: setValueMethod = null;
097: }
098: refresh();
099: }
100:
101: /**
102: * @return The action definition whose input is being managed.
103: */
104: public ActionDefinition getActionDefinition() {
105: return actionDefinition;
106: }
107:
108: /**
109: * @return The name of the action definition input being managed.
110: */
111: public String getInputName() {
112: return inputName;
113: }
114:
115: /* (non-Javadoc)
116: * @see org.pentaho.designstudio.controls.IActionSequenceControl#refresh()
117: */
118: public void refresh() {
119: button.removeSelectionListener(this );
120: boolean booleanValue = false;
121:
122: try {
123: if (getValueMethod != null) {
124: booleanValue = ((IActionInputValueProvider) getValueMethod
125: .invoke(actionDefinition, new Object[0]))
126: .getBooleanValue(false);
127: }
128: } catch (IllegalArgumentException e) {
129: // TODO Auto-generated catch block
130: e.printStackTrace();
131: } catch (IllegalAccessException e) {
132: // TODO Auto-generated catch block
133: e.printStackTrace();
134: } catch (InvocationTargetException e) {
135: // TODO Auto-generated catch block
136: e.printStackTrace();
137: }
138: button.setSelection(booleanValue);
139: button.addSelectionListener(this );
140: }
141:
142: /* (non-Javadoc)
143: * @see org.pentaho.designstudio.controls.IActionSequenceControl#getControl()
144: */
145: public Control getControl() {
146: return button;
147: }
148:
149: /* (non-Javadoc)
150: * @see org.pentaho.designstudio.controls.IActionSequenceControl#updateActionSequence()
151: */
152: public void updateActionSequence() {
153: try {
154: setValueMethod.invoke(actionDefinition,
155: new Object[] { new ActionInputConstant(button
156: .getSelection()) });
157: } catch (IllegalArgumentException e) {
158: // TODO Auto-generated catch block
159: e.printStackTrace();
160: } catch (IllegalAccessException e) {
161: // TODO Auto-generated catch block
162: e.printStackTrace();
163: } catch (InvocationTargetException e) {
164: // TODO Auto-generated catch block
165: e.printStackTrace();
166: }
167: }
168:
169: private String getPropertyName(String inputName) {
170: StringBuffer stringBuffer = new StringBuffer(inputName
171: .toLowerCase());
172: stringBuffer.setCharAt(0, Character.toUpperCase(stringBuffer
173: .charAt(0)));
174: for (int index = stringBuffer.toString().indexOf('-'); index != -1; index = stringBuffer
175: .toString().indexOf('-')) {
176: stringBuffer.deleteCharAt(index);
177: if (index < stringBuffer.length()) {
178: stringBuffer.setCharAt(index, Character
179: .toUpperCase(stringBuffer.charAt(index)));
180: }
181: }
182: for (int index = stringBuffer.toString().indexOf('_'); index != -1; index = stringBuffer
183: .toString().indexOf('_')) {
184: stringBuffer.deleteCharAt(index);
185: if (index < stringBuffer.length()) {
186: stringBuffer.setCharAt(index, Character
187: .toUpperCase(stringBuffer.charAt(index)));
188: }
189: }
190: return stringBuffer.toString();
191: }
192: }
|