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.List;
020:
021: import org.drools.eclipse.editors.DRLSourceViewerConfig;
022: import org.drools.eclipse.editors.scanners.DRLPartionScanner;
023: import org.drools.ruleflow.core.Constraint;
024: import org.drools.ruleflow.core.RuleFlowProcess;
025: import org.drools.ruleflow.core.impl.ConstraintImpl;
026: import org.eclipse.jface.dialogs.Dialog;
027: import org.eclipse.jface.resource.JFaceResources;
028: import org.eclipse.jface.text.Document;
029: import org.eclipse.jface.text.IDocument;
030: import org.eclipse.jface.text.IDocumentPartitioner;
031: import org.eclipse.jface.text.contentassist.ContentAssistant;
032: import org.eclipse.jface.text.contentassist.IContentAssistant;
033: import org.eclipse.jface.text.reconciler.IReconciler;
034: import org.eclipse.jface.text.rules.FastPartitioner;
035: import org.eclipse.jface.text.source.ISourceViewer;
036: import org.eclipse.jface.text.source.SourceViewer;
037: import org.eclipse.swt.SWT;
038: import org.eclipse.swt.events.KeyEvent;
039: import org.eclipse.swt.events.KeyListener;
040: import org.eclipse.swt.events.SelectionAdapter;
041: import org.eclipse.swt.events.SelectionEvent;
042: import org.eclipse.swt.events.SelectionListener;
043: import org.eclipse.swt.graphics.Point;
044: import org.eclipse.swt.layout.GridData;
045: import org.eclipse.swt.layout.GridLayout;
046: import org.eclipse.swt.widgets.Button;
047: import org.eclipse.swt.widgets.Composite;
048: import org.eclipse.swt.widgets.Control;
049: import org.eclipse.swt.widgets.Label;
050: import org.eclipse.swt.widgets.Shell;
051: import org.eclipse.swt.widgets.TabFolder;
052: import org.eclipse.swt.widgets.TabItem;
053: import org.eclipse.swt.widgets.Text;
054:
055: /**
056: * Dialog for editing constraints.
057: *
058: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
059: */
060: public class RuleFlowConstraintDialog extends Dialog {
061:
062: private Constraint constraint;
063: private RuleFlowProcess process;
064: private boolean success;
065: private Button alwaysTrue;
066: private Text nameText;
067: private Text priorityText;
068: private TabFolder tabFolder;
069: private SourceViewer constraintViewer;
070: private ConstraintCompletionProcessor completionProcessor;
071:
072: public RuleFlowConstraintDialog(Shell parentShell,
073: RuleFlowProcess process) {
074: super (parentShell);
075: this .process = process;
076: setShellStyle(getShellStyle() | SWT.RESIZE);
077: }
078:
079: protected void configureShell(Shell newShell) {
080: super .configureShell(newShell);
081: newShell.setText("Constraint editor");
082: }
083:
084: protected Point getInitialSize() {
085: return new Point(600, 450);
086: }
087:
088: private Control createTextualEditor(Composite parent) {
089: constraintViewer = new SourceViewer(parent, null, SWT.BORDER);
090: constraintViewer.configure(new DRLSourceViewerConfig(null) {
091: public IReconciler getReconciler(ISourceViewer sourceViewer) {
092: return null;
093: }
094:
095: public IContentAssistant getContentAssistant(
096: ISourceViewer sourceViewer) {
097: ContentAssistant assistant = new ContentAssistant();
098: completionProcessor = new ConstraintCompletionProcessor(
099: process);
100: assistant.setContentAssistProcessor(
101: completionProcessor,
102: IDocument.DEFAULT_CONTENT_TYPE);
103: assistant
104: .setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
105: return assistant;
106: }
107: });
108: IDocument document = new Document();
109: constraintViewer.setDocument(document);
110: IDocumentPartitioner partitioner = new FastPartitioner(
111: new DRLPartionScanner(),
112: DRLPartionScanner.LEGAL_CONTENT_TYPES);
113: partitioner.connect(document);
114: document.setDocumentPartitioner(partitioner);
115: constraintViewer.getControl().addKeyListener(new KeyListener() {
116: public void keyPressed(KeyEvent e) {
117: if (e.character == ' ' && e.stateMask == SWT.CTRL) {
118: constraintViewer
119: .doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
120: }
121: }
122:
123: public void keyReleased(KeyEvent e) {
124: }
125: });
126: return constraintViewer.getControl();
127: }
128:
129: private String getConstraintText() {
130: return constraintViewer.getDocument().get();
131: }
132:
133: private void setConstraintText(String text) {
134: constraintViewer.getDocument().set(text);
135: }
136:
137: public Control createDialogArea(Composite parent) {
138:
139: GridLayout layout = new GridLayout();
140: parent.setLayout(layout);
141: layout.numColumns = 2;
142:
143: Composite top = new Composite(parent, SWT.NONE);
144: GridData gd = new GridData();
145: gd.horizontalSpan = 2;
146: gd.grabExcessHorizontalSpace = true;
147: top.setLayoutData(gd);
148:
149: layout = new GridLayout();
150: layout.numColumns = 3;
151: top.setLayout(layout);
152:
153: Label l1 = new Label(top, SWT.None);
154: l1.setText("Name:");
155: gd = new GridData();
156: l1.setLayoutData(gd);
157: nameText = new Text(top, SWT.BORDER);
158: gd = new GridData();
159: gd.horizontalAlignment = GridData.FILL;
160: gd.widthHint = 200;
161: gd.horizontalSpan = 2;
162: nameText.setLayoutData(gd);
163:
164: Label l2 = new Label(top, SWT.NONE);
165: gd = new GridData();
166: l2.setLayoutData(gd);
167: l2.setText("Priority:");
168: priorityText = new Text(top, SWT.BORDER);
169: gd = new GridData();
170: gd.widthHint = 200;
171: gd.horizontalSpan = 2;
172: priorityText.setLayoutData(gd);
173:
174: alwaysTrue = new Button(top, SWT.CHECK);
175: alwaysTrue.setText("Always true");
176: gd = new GridData();
177: gd.horizontalSpan = 2;
178: alwaysTrue.setLayoutData(gd);
179:
180: Button importButton = new Button(top, SWT.PUSH);
181: importButton.setText("Imports ...");
182: importButton.setFont(JFaceResources.getDialogFont());
183: importButton.addSelectionListener(new SelectionAdapter() {
184: public void widgetSelected(SelectionEvent event) {
185: importButtonPressed();
186: }
187: });
188: gd = new GridData();
189: gd.horizontalAlignment = GridData.END;
190: importButton.setLayoutData(gd);
191:
192: tabFolder = new TabFolder(parent, SWT.NONE);
193: gd = new GridData();
194: gd.horizontalSpan = 3;
195: gd.grabExcessHorizontalSpace = true;
196: gd.grabExcessVerticalSpace = true;
197: gd.verticalAlignment = GridData.FILL;
198: gd.horizontalAlignment = GridData.FILL;
199: tabFolder.setLayoutData(gd);
200: TabItem textEditorTab = new TabItem(tabFolder, SWT.NONE);
201: textEditorTab.setText("Textual Editor");
202:
203: textEditorTab.setControl(createTextualEditor(tabFolder));
204:
205: alwaysTrue.addSelectionListener(new SelectionListener() {
206: public void widgetSelected(SelectionEvent e) {
207: tabFolder.setVisible(!alwaysTrue.getSelection());
208: }
209:
210: public void widgetDefaultSelected(SelectionEvent e) {
211: widgetSelected(e);
212: }
213: });
214:
215: return tabFolder;
216: }
217:
218: private void importButtonPressed() {
219: final Runnable r = new Runnable() {
220: public void run() {
221: RuleFlowImportsDialog dialog = new RuleFlowImportsDialog(
222: getShell(), process);
223: dialog.create();
224: int code = dialog.open();
225: if (code != CANCEL) {
226: List imports = dialog.getImports();
227: process.setImports(imports);
228: completionProcessor.reset();
229: }
230: }
231: };
232: r.run();
233: }
234:
235: protected void okPressed() {
236: int selectionIndex = tabFolder.getSelectionIndex();
237: if (selectionIndex == 1) {
238: updateTranslation();
239: } else {
240: updateConstraint();
241: }
242: super .okPressed();
243: }
244:
245: private void updateTranslation() {
246: // TODO add custom token model checker
247: success = true;
248: constraint = new ConstraintImpl();
249: constraint.setConstraint(null);
250: constraint.setConstraint(getConstraintText());
251: constraint.setName(nameText.getText());
252: try {
253: constraint.setPriority(Integer.parseInt(priorityText
254: .getText()));
255: } catch (NumberFormatException exc) {
256: constraint.setPriority(1);
257: }
258: }
259:
260: public boolean isSuccess() {
261: return success;
262: }
263:
264: public void updateConstraint() {
265: constraint = new ConstraintImpl();
266: if (!alwaysTrue.getSelection()) {
267: constraint.setConstraint(getConstraintText());
268: } else {
269: constraint.setConstraint("eval(true)");
270: }
271: constraint.setName(nameText.getText());
272: try {
273: constraint.setPriority(Integer.parseInt(priorityText
274: .getText()));
275: } catch (NumberFormatException exc) {
276: constraint.setPriority(1);
277: }
278: }
279:
280: public Constraint getConstraint() {
281: return constraint;
282: }
283:
284: public void setConstraint(Constraint constraint) {
285: this .constraint = constraint;
286: if (constraint != null) {
287: if ("eval(true)".equals(constraint.getConstraint())) {
288: alwaysTrue.setSelection(true);
289: } else {
290: setConstraintText(constraint.getConstraint().toString());
291: }
292: tabFolder.setVisible(!alwaysTrue.getSelection());
293: nameText.setText(constraint.getName() == null ? ""
294: : constraint.getName());
295: priorityText.setText(constraint.getPriority() + "");
296: setConstraintText(constraint.getConstraint());
297: } else {
298: priorityText.setText("1");
299: nameText.setText("constraint");
300: }
301: }
302: }
|