001: package org.drools.eclipse.flow.ruleflow.view.property.constraint;
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 java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.drools.eclipse.flow.common.view.property.EditBeanDialog;
025: import org.drools.ruleflow.core.Connection;
026: import org.drools.ruleflow.core.Constraint;
027: import org.drools.ruleflow.core.Split;
028: import org.drools.ruleflow.core.RuleFlowProcess;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.events.SelectionAdapter;
031: import org.eclipse.swt.events.SelectionEvent;
032: import org.eclipse.swt.layout.GridData;
033: import org.eclipse.swt.layout.GridLayout;
034: import org.eclipse.swt.widgets.Button;
035: import org.eclipse.swt.widgets.Composite;
036: import org.eclipse.swt.widgets.Control;
037: import org.eclipse.swt.widgets.Display;
038: import org.eclipse.swt.widgets.Label;
039: import org.eclipse.swt.widgets.Shell;
040:
041: /**
042: * Dialog for editing constraints.
043: *
044: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
045: */
046: public class ConstraintListDialog extends EditBeanDialog {
047:
048: private RuleFlowProcess process;
049: private Split split;
050: private Map newMap;
051: private Map labels = new HashMap();
052:
053: protected ConstraintListDialog(Shell parentShell,
054: RuleFlowProcess process, Split split) {
055: super (parentShell, "Edit Constraints");
056: this .process = process;
057: this .split = split;
058: }
059:
060: protected Control createDialogArea(Composite parent) {
061: Composite composite = (Composite) super
062: .createDialogArea(parent);
063: GridLayout gridLayout = new GridLayout();
064: gridLayout.numColumns = 3;
065: composite.setLayout(gridLayout);
066:
067: List outgoingConnections = split.getOutgoingConnections();
068: labels.clear();
069: for (Iterator it = outgoingConnections.iterator(); it.hasNext();) {
070: Connection outgoingConnection = (Connection) it.next();
071: Label label1 = new Label(composite, SWT.NONE);
072: label1.setText("To node "
073: + outgoingConnection.getTo().getName() + ": ");
074:
075: Label label2 = new Label(composite, SWT.NONE);
076: labels.put(outgoingConnection, label2);
077: GridData gridData = new GridData();
078: gridData.grabExcessHorizontalSpace = true;
079: gridData.horizontalAlignment = GridData.FILL;
080: label2.setLayoutData(gridData);
081: Constraint constraint = (Constraint) newMap
082: .get(outgoingConnection);
083: if (constraint != null) {
084: label2.setText(constraint.getName());
085: }
086:
087: Button editButton = new Button(composite, SWT.NONE);
088: editButton.setText("Edit");
089: editButton.addSelectionListener(new EditButtonListener(
090: outgoingConnection));
091: }
092:
093: return composite;
094: }
095:
096: public void setValue(Object value) {
097: super .setValue(value);
098: this .newMap = new HashMap((Map) value);
099: }
100:
101: protected Object updateValue(Object value) {
102: return newMap;
103: }
104:
105: private void editItem(final Connection connection) {
106:
107: final Runnable r = new Runnable() {
108: public void run() {
109: RuleFlowConstraintDialog dialog = new RuleFlowConstraintDialog(
110: getShell(), process);
111: dialog.create();
112: Constraint constraint = (Constraint) newMap
113: .get(connection);
114: dialog.setConstraint(constraint);
115: int code = dialog.open();
116: if (code != CANCEL) {
117: constraint = dialog.getConstraint();
118: newMap.put(connection, constraint);
119: setConnectionText((Label) labels.get(connection),
120: constraint.getName());
121: }
122: }
123:
124: };
125: r.run();
126: }
127:
128: private void setConnectionText(final Label connection,
129: final String name) {
130: Display.getDefault().asyncExec(new Runnable() {
131: public void run() {
132: connection.setText(name);
133: }
134: });
135: }
136:
137: private class EditButtonListener extends SelectionAdapter {
138: private Connection connection;
139:
140: public EditButtonListener(Connection connection) {
141: this .connection = connection;
142: }
143:
144: public void widgetSelected(SelectionEvent e) {
145: editItem(connection);
146: }
147: }
148: }
|