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