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.eclipse.flow.common.view.property.EditBeanDialog;
024: import org.drools.ruleflow.core.MilestoneNode;
025: import org.drools.ruleflow.core.RuleFlowProcess;
026: import org.eclipse.jface.resource.JFaceResources;
027: import org.eclipse.jface.text.Document;
028: import org.eclipse.jface.text.IDocument;
029: import org.eclipse.jface.text.IDocumentPartitioner;
030: import org.eclipse.jface.text.contentassist.ContentAssistant;
031: import org.eclipse.jface.text.contentassist.IContentAssistant;
032: import org.eclipse.jface.text.reconciler.IReconciler;
033: import org.eclipse.jface.text.rules.FastPartitioner;
034: import org.eclipse.jface.text.source.ISourceViewer;
035: import org.eclipse.jface.text.source.SourceViewer;
036: import org.eclipse.swt.SWT;
037: import org.eclipse.swt.events.KeyEvent;
038: import org.eclipse.swt.events.KeyListener;
039: import org.eclipse.swt.events.SelectionAdapter;
040: import org.eclipse.swt.events.SelectionEvent;
041: import org.eclipse.swt.graphics.Point;
042: import org.eclipse.swt.layout.GridData;
043: import org.eclipse.swt.layout.GridLayout;
044: import org.eclipse.swt.widgets.Button;
045: import org.eclipse.swt.widgets.Composite;
046: import org.eclipse.swt.widgets.Control;
047: import org.eclipse.swt.widgets.Shell;
048: import org.eclipse.swt.widgets.TabFolder;
049: import org.eclipse.swt.widgets.TabItem;
050:
051: /**
052: * Dialog for editing constraints.
053: *
054: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
055: */
056: public class MilestoneConstraintDialog extends EditBeanDialog {
057:
058: private RuleFlowProcess process;
059: private TabFolder tabFolder;
060: private SourceViewer constraintViewer;
061: private ConstraintCompletionProcessor completionProcessor;
062:
063: public MilestoneConstraintDialog(Shell parentShell,
064: RuleFlowProcess process, MilestoneNode milestone) {
065: super (parentShell, "Constraint editor");
066: this .process = process;
067: setValue(milestone.getConstraint());
068: }
069:
070: protected Object updateValue(Object value) {
071: if (tabFolder.getSelectionIndex() == 0) {
072: return getConstraintText();
073: }
074: return null;
075: }
076:
077: protected Point getInitialSize() {
078: return new Point(600, 450);
079: }
080:
081: private Control createTextualEditor(Composite parent) {
082: constraintViewer = new SourceViewer(parent, null, SWT.BORDER);
083: constraintViewer.configure(new DRLSourceViewerConfig(null) {
084: public IReconciler getReconciler(ISourceViewer sourceViewer) {
085: return null;
086: }
087:
088: public IContentAssistant getContentAssistant(
089: ISourceViewer sourceViewer) {
090: ContentAssistant assistant = new ContentAssistant();
091: completionProcessor = new ConstraintCompletionProcessor(
092: process);
093: assistant.setContentAssistProcessor(
094: completionProcessor,
095: IDocument.DEFAULT_CONTENT_TYPE);
096: assistant
097: .setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
098: return assistant;
099: }
100: });
101: String value = (String) getValue();
102: if (value == null) {
103: value = "";
104: }
105: IDocument document = new Document(value);
106: constraintViewer.setDocument(document);
107: IDocumentPartitioner partitioner = new FastPartitioner(
108: new DRLPartionScanner(),
109: DRLPartionScanner.LEGAL_CONTENT_TYPES);
110: partitioner.connect(document);
111: document.setDocumentPartitioner(partitioner);
112: constraintViewer.getControl().addKeyListener(new KeyListener() {
113: public void keyPressed(KeyEvent e) {
114: if (e.character == ' ' && e.stateMask == SWT.CTRL) {
115: constraintViewer
116: .doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
117: }
118: }
119:
120: public void keyReleased(KeyEvent e) {
121: }
122: });
123: return constraintViewer.getControl();
124: }
125:
126: private String getConstraintText() {
127: return constraintViewer.getDocument().get();
128: }
129:
130: public Control createDialogArea(Composite parent) {
131: GridLayout layout = new GridLayout();
132: parent.setLayout(layout);
133: layout.numColumns = 2;
134:
135: Composite top = new Composite(parent, SWT.NONE);
136: GridData gd = new GridData();
137: gd.horizontalSpan = 2;
138: gd.grabExcessHorizontalSpace = true;
139: top.setLayoutData(gd);
140:
141: layout = new GridLayout();
142: layout.numColumns = 3;
143: top.setLayout(layout);
144:
145: Button importButton = new Button(top, SWT.PUSH);
146: importButton.setText("Imports ...");
147: importButton.setFont(JFaceResources.getDialogFont());
148: importButton.addSelectionListener(new SelectionAdapter() {
149: public void widgetSelected(SelectionEvent event) {
150: importButtonPressed();
151: }
152: });
153: gd = new GridData();
154: gd.horizontalAlignment = GridData.END;
155: importButton.setLayoutData(gd);
156:
157: tabFolder = new TabFolder(parent, SWT.NONE);
158: gd = new GridData();
159: gd.horizontalSpan = 3;
160: gd.grabExcessHorizontalSpace = true;
161: gd.grabExcessVerticalSpace = true;
162: gd.verticalAlignment = GridData.FILL;
163: gd.horizontalAlignment = GridData.FILL;
164: tabFolder.setLayoutData(gd);
165: TabItem textEditorTab = new TabItem(tabFolder, SWT.NONE);
166: textEditorTab.setText("Textual Editor");
167:
168: textEditorTab.setControl(createTextualEditor(tabFolder));
169: return tabFolder;
170: }
171:
172: private void importButtonPressed() {
173: final Runnable r = new Runnable() {
174: public void run() {
175: RuleFlowImportsDialog dialog = new RuleFlowImportsDialog(
176: getShell(), process);
177: dialog.create();
178: int code = dialog.open();
179: if (code != CANCEL) {
180: List imports = dialog.getImports();
181: process.setImports(imports);
182: completionProcessor.reset();
183: }
184: }
185: };
186: r.run();
187: }
188: }
|