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.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.regex.Matcher;
023: import java.util.regex.Pattern;
024:
025: import org.drools.eclipse.editors.DRLSourceViewerConfig;
026: import org.drools.ruleflow.core.RuleFlowProcess;
027: import org.eclipse.jface.dialogs.Dialog;
028: import org.eclipse.jface.text.Document;
029: import org.eclipse.jface.text.IDocument;
030: import org.eclipse.jface.text.contentassist.IContentAssistant;
031: import org.eclipse.jface.text.reconciler.IReconciler;
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.graphics.Point;
036: import org.eclipse.swt.layout.GridData;
037: import org.eclipse.swt.layout.GridLayout;
038: import org.eclipse.swt.widgets.Composite;
039: import org.eclipse.swt.widgets.Control;
040: import org.eclipse.swt.widgets.Shell;
041: import org.eclipse.swt.widgets.TabFolder;
042: import org.eclipse.swt.widgets.TabItem;
043:
044: /**
045: * Dialog for editing imports.
046: *
047: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
048: */
049: public class RuleFlowImportsDialog extends Dialog {
050:
051: private static final Pattern IMPORT_PATTERN = Pattern.compile(
052: "\\n\\s*import\\s+([^\\s;#]+);?", Pattern.DOTALL);
053:
054: private RuleFlowProcess process;
055: private boolean success;
056: private TabFolder tabFolder;
057: private SourceViewer importsViewer;
058: private List imports;
059:
060: public RuleFlowImportsDialog(Shell parentShell,
061: RuleFlowProcess process) {
062: super (parentShell);
063: this .process = process;
064: setShellStyle(getShellStyle() | SWT.RESIZE);
065: }
066:
067: protected void configureShell(Shell newShell) {
068: super .configureShell(newShell);
069: newShell.setText("Imports editor");
070: }
071:
072: protected Point getInitialSize() {
073: return new Point(600, 450);
074: }
075:
076: private Control createTextualEditor(Composite parent) {
077: importsViewer = new SourceViewer(parent, null, SWT.BORDER);
078: importsViewer.configure(new DRLSourceViewerConfig(null) {
079: public IReconciler getReconciler(ISourceViewer sourceViewer) {
080: return null;
081: }
082:
083: public IContentAssistant getContentAssistant(
084: ISourceViewer sourceViewer) {
085: return null;
086: }
087: });
088: IDocument document = new Document(getProcessImports());
089: importsViewer.setDocument(document);
090: return importsViewer.getControl();
091: }
092:
093: private String getProcessImports() {
094: String result = "# define your imports here: e.g. import com.sample.MyClass\n";
095: List imports = process.getImports();
096: if (imports != null) {
097: for (Iterator iterator = imports.iterator(); iterator
098: .hasNext();) {
099: result += "import " + (String) iterator.next() + "\n";
100: }
101: }
102: return result;
103: }
104:
105: public Control createDialogArea(Composite parent) {
106: GridLayout layout = new GridLayout();
107: parent.setLayout(layout);
108: layout.numColumns = 1;
109: tabFolder = new TabFolder(parent, SWT.NONE);
110: GridData gd = new GridData();
111: gd.grabExcessHorizontalSpace = true;
112: gd.grabExcessVerticalSpace = true;
113: gd.verticalAlignment = GridData.FILL;
114: gd.horizontalAlignment = GridData.FILL;
115: tabFolder.setLayoutData(gd);
116: TabItem textEditorTab = new TabItem(tabFolder, SWT.NONE);
117: textEditorTab.setText("Textual Editor");
118: textEditorTab.setControl(createTextualEditor(tabFolder));
119: return tabFolder;
120: }
121:
122: protected void okPressed() {
123: success = true;
124: updateImports();
125: super .okPressed();
126: }
127:
128: public boolean isSuccess() {
129: return success;
130: }
131:
132: public List getImports() {
133: return imports;
134: }
135:
136: private void updateImports() {
137: this .imports = new ArrayList();
138: Matcher matcher = IMPORT_PATTERN.matcher(importsViewer
139: .getDocument().get());
140: while (matcher.find()) {
141: this .imports.add(matcher.group(1));
142: }
143: }
144: }
|