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