001: /*
002: * Created on 11-jan-2005
003: *
004: */
005: package org.drools.eclipse.wizard.rule;
006:
007: import java.io.IOException;
008: import java.io.InputStream;
009:
010: import org.drools.eclipse.DroolsEclipsePlugin;
011: import org.eclipse.jface.viewers.IStructuredSelection;
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.layout.GridData;
014: import org.eclipse.swt.layout.GridLayout;
015: import org.eclipse.swt.widgets.Button;
016: import org.eclipse.swt.widgets.Combo;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Label;
019: import org.eclipse.swt.widgets.Text;
020: import org.eclipse.ui.IWorkbench;
021: import org.eclipse.ui.IWorkbenchWindow;
022: import org.eclipse.ui.PartInitException;
023: import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
024: import org.eclipse.ui.ide.IDE;
025:
026: /**
027: * A page to create a new .drl package/package file.
028: * There is only one page for this wizard, its very simple.
029: *
030: * Enhancements may be made to allow configuration of semantic languages, DSLs (locate a DSL) and other
031: * package level options.
032: *
033: * @author Michael Neale
034: * @author <a href="mailto:kris_verlaenen@hotmail.com">kris verlaenen </a>
035: */
036: public class NewRulePackagePage extends WizardNewFileCreationPage {
037:
038: private static final int TYPE_RULE = 1;
039: private static final int TYPE_PACKAGE = 0;
040: private IWorkbench workbench;
041: private Combo ruleFileType;
042: private Button expander;
043: private Button function;
044: private Text packageName;
045:
046: public NewRulePackagePage(IWorkbench workbench,
047: IStructuredSelection selection) {
048: super ("createDRLFilePage", selection);
049: setTitle("New Rules File");
050: setDescription("Create a new rules file (drl)");
051: this .workbench = workbench;
052: }
053:
054: public void createControl(Composite parent) {
055: super .createControl(parent);
056: setPageComplete(true);
057: super
058: .setMessage("Hint: Press CTRL+SPACE when editing rules to get content sensitive assistance/popups.");
059: }
060:
061: protected void createAdvancedControls(Composite parent) {
062: Composite container = new Composite(parent, SWT.NONE);
063: final GridLayout layout = new GridLayout();
064: layout.numColumns = 2;
065: container.setLayout(layout);
066: setControl(container);
067:
068: //setup the controls.
069: createType(container);
070: createDSL(container);
071: createFunctions(container);
072: createPackageName(container);
073:
074: super .createAdvancedControls(parent);
075: }
076:
077: private void createPackageName(Composite container) {
078: //package name
079: Label pack = new Label(container, SWT.NONE);
080: pack.setText("Rule package name:");
081: pack.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
082: pack.setFont(this .getFont());
083: packageName = new Text(container, SWT.BORDER);
084: packageName
085: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
086: packageName.setToolTipText("Rules require a namespace.");
087: packageName.setFont(this .getFont());
088: }
089:
090: private void createFunctions(Composite container) {
091: //function
092: Label func = new Label(container, SWT.NONE);
093: func.setText("Use functions:");
094: func.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
095: func.setFont(this .getFont());
096: function = new Button(container, SWT.CHECK);
097: function.setSelection(false);
098: function.setLayoutData(new GridData(
099: GridData.HORIZONTAL_ALIGN_BEGINNING));
100: function
101: .setToolTipText("Functions are methods you embed in your rule source.");
102: }
103:
104: private void createDSL(Composite container) {
105: //expander
106: Label exp = new Label(container, SWT.NONE);
107: exp.setText("Use a DSL:");
108: exp.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
109: exp.setFont(this .getFont());
110: expander = new Button(container, SWT.CHECK);
111: expander.setSelection(false);
112: expander.setLayoutData(new GridData(
113: GridData.HORIZONTAL_ALIGN_BEGINNING));
114: expander
115: .setToolTipText("Domain Specific Language: allows you to create your own domain specific languages\n for use in rules.");
116: }
117:
118: private void createType(Composite container) {
119: //type
120: Label type = new Label(container, SWT.NONE);
121: type.setText("Type of rule resource:");
122: type.setFont(this .getFont());
123: type.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
124: ruleFileType = new Combo(container, SWT.READ_ONLY);
125: ruleFileType.add("New DRL (rule package)", TYPE_PACKAGE);
126: ruleFileType.add("New Rule (individual rule)", TYPE_RULE);
127: ruleFileType.select(0);
128: ruleFileType.setLayoutData(new GridData(
129: GridData.FILL_HORIZONTAL));
130: ruleFileType.setFont(this .getFont());
131:
132: }
133:
134: public boolean finish() {
135: if (!validate()) {
136: return false;
137: }
138: String fileName = getFileName();
139: String extension = expander.getSelection() ? ".dslr" : ".drl";
140: if (!fileName.endsWith(extension)) {
141: setFileName(fileName + extension);
142: }
143: org.eclipse.core.resources.IFile newFile = createNewFile();
144: if (newFile == null)
145: return false;
146: try {
147: IWorkbenchWindow dwindow = workbench
148: .getActiveWorkbenchWindow();
149: org.eclipse.ui.IWorkbenchPage page = dwindow
150: .getActivePage();
151: if (page != null)
152: IDE.openEditor(page, newFile, true);
153: } catch (PartInitException e) {
154: DroolsEclipsePlugin.log(e);
155: return false;
156: }
157: return true;
158: }
159:
160: private boolean validate() {
161: if (this .packageName.getText() == null
162: || packageName.getText().equals("")) {
163: setErrorMessage("You must provide a rule package name");
164: return false;
165: } else {
166: return true;
167: }
168: }
169:
170: protected InputStream getInitialContents() {
171:
172: try {
173:
174: DRLGenerator gen = new DRLGenerator();
175: if (this .ruleFileType.getSelectionIndex() == TYPE_RULE) {
176: InputStream template = getTemplate("org/drools/eclipse/wizard/rule/new_rule.drl.template");
177: return gen.generateRule(this .packageName.getText(),
178: template);
179: } else {
180: InputStream template = getTemplate("org/drools/eclipse/wizard/rule/new_package.drl.template");
181: return gen.generatePackage(this .packageName.getText(),
182: function.getSelection(), expander
183: .getSelection(), template);
184: }
185: } catch (IOException e) {
186: return null;
187: } catch (NullPointerException e) {
188: return null;
189: }
190: }
191:
192: private InputStream getTemplate(String templatePath)
193: throws IOException {
194: return DroolsEclipsePlugin.getDefault().getBundle()
195: .getResource(templatePath).openStream();
196: }
197:
198: }
|