001: package org.drools.eclipse.flow.ruleflow.view.property.variable;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.drools.eclipse.DroolsEclipsePlugin;
020: import org.drools.eclipse.flow.common.datatype.DefaultDataTypeRegistry;
021: import org.drools.eclipse.flow.common.view.datatype.editor.DataTypeEditor;
022: import org.drools.eclipse.flow.common.view.datatype.editor.impl.DataTypeCombo;
023: import org.drools.eclipse.flow.common.view.datatype.editor.impl.DataTypeEditorComposite;
024: import org.drools.eclipse.flow.common.view.datatype.editor.impl.EditorComposite;
025: import org.drools.eclipse.flow.common.view.property.EditBeanDialog;
026: import org.drools.ruleflow.common.datatype.DataType;
027: import org.drools.ruleflow.core.Variable;
028: import org.eclipse.core.runtime.IStatus;
029: import org.eclipse.core.runtime.Status;
030: import org.eclipse.jface.dialogs.ErrorDialog;
031: import org.eclipse.jface.viewers.ISelectionChangedListener;
032: import org.eclipse.jface.viewers.SelectionChangedEvent;
033: import org.eclipse.swt.SWT;
034: import org.eclipse.swt.layout.GridData;
035: import org.eclipse.swt.layout.GridLayout;
036: import org.eclipse.swt.widgets.Composite;
037: import org.eclipse.swt.widgets.Control;
038: import org.eclipse.swt.widgets.Label;
039: import org.eclipse.swt.widgets.Shell;
040: import org.eclipse.swt.widgets.Text;
041:
042: /**
043: * Dialog for editing variables.
044: *
045: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
046: */
047: public class VariableDialog extends EditBeanDialog {
048:
049: private Text nameText;
050: private DataTypeCombo dataTypeCombo;
051: private DataTypeEditorComposite dataTypeEditorComposite;
052: private EditorComposite editorComposite;
053:
054: public VariableDialog(Shell parentShell) {
055: super (parentShell, "Edit Variable");
056: }
057:
058: protected Control createDialogArea(Composite parent) {
059: final Composite composite = (Composite) super
060: .createDialogArea(parent);
061: GridLayout gridLayout = new GridLayout();
062: gridLayout.numColumns = 2;
063: composite.setLayout(gridLayout);
064:
065: Label nameLabel = new Label(composite, SWT.NONE);
066: nameLabel.setText("Name: ");
067: nameText = new Text(composite, SWT.NONE);
068: GridData gridData = new GridData();
069: gridData.grabExcessHorizontalSpace = true;
070: gridData.horizontalAlignment = GridData.FILL;
071: nameText.setLayoutData(gridData);
072: String name = ((Variable) getValue()).getName();
073: nameText.setText(name == null ? "" : name);
074:
075: Label typeLabel = new Label(composite, SWT.NONE);
076: typeLabel.setText("Type: ");
077:
078: dataTypeCombo = new DataTypeCombo(composite, SWT.NONE,
079: DefaultDataTypeRegistry.getInstance());
080: DataType dataType = ((Variable) getValue()).getType();
081: dataTypeCombo.setDataType(dataType);
082: dataTypeCombo
083: .addSelectionChangedListener(new ISelectionChangedListener() {
084: public void selectionChanged(
085: SelectionChangedEvent event) {
086: DataType dataType = dataTypeCombo.getDataType();
087: dataTypeEditorComposite.setDataType(dataType);
088: editorComposite.setDataType(dataType);
089: composite.layout();
090: }
091: });
092:
093: new Label(composite, SWT.NONE);
094:
095: dataTypeEditorComposite = new DataTypeEditorComposite(
096: composite, SWT.NONE, DefaultDataTypeRegistry
097: .getInstance());
098: gridData = new GridData();
099: gridData.grabExcessHorizontalSpace = true;
100: gridData.horizontalAlignment = GridData.FILL;
101: dataTypeEditorComposite.setLayoutData(gridData);
102: dataTypeEditorComposite.setDataType(dataType);
103: dataTypeEditorComposite
104: .addListener(new DataTypeEditor.DataTypeListener() {
105: public void dataTypeChanged(DataType dataType) {
106: editorComposite.setDataType(dataType);
107: composite.layout();
108: }
109: });
110:
111: Label valueLabel = new Label(composite, SWT.NONE);
112: valueLabel.setText("Value: ");
113: gridData = new GridData();
114: gridData.verticalAlignment = SWT.TOP;
115: valueLabel.setLayoutData(gridData);
116: editorComposite = new EditorComposite(composite, SWT.NONE,
117: DefaultDataTypeRegistry.getInstance());
118: gridData = new GridData();
119: gridData.horizontalAlignment = GridData.FILL;
120: gridData.grabExcessHorizontalSpace = true;
121: editorComposite.setLayoutData(gridData);
122: editorComposite.setDataType(dataType);
123: editorComposite.setValue(((Variable) getValue()).getValue());
124:
125: Composite bottom = new Composite(composite, SWT.NONE);
126: gridData = new GridData();
127: gridData.grabExcessVerticalSpace = true;
128: gridData.horizontalSpan = 2;
129: bottom.setLayoutData(gridData);
130:
131: return composite;
132: }
133:
134: protected Object updateValue(Object value) {
135: Variable variable = (Variable) getValue();
136: variable.setName(nameText.getText());
137: try {
138: variable.setType(dataTypeEditorComposite.getDataType());
139: } catch (IllegalArgumentException e) {
140: showError(e.getMessage());
141: throw e;
142: }
143: try {
144: variable.setValue(editorComposite.getValue());
145: } catch (IllegalArgumentException e) {
146: showError(e.getMessage());
147: throw e;
148: }
149: return variable;
150: }
151:
152: private void showError(String error) {
153: ErrorDialog.openError(getShell(), "Error", error, new Status(
154: IStatus.ERROR, DroolsEclipsePlugin.getDefault()
155: .getBundle().getSymbolicName(), IStatus.ERROR,
156: error, null));
157: }
158: }
|