001: package org.drools.eclipse.dsl.editor;
002:
003: import org.drools.lang.dsl.DSLMappingEntry;
004: import org.drools.lang.dsl.DSLMappingEntry.Section;
005: import org.eclipse.jface.dialogs.TitleAreaDialog;
006: import org.eclipse.swt.SWT;
007: import org.eclipse.swt.layout.GridData;
008: import org.eclipse.swt.layout.GridLayout;
009: import org.eclipse.swt.widgets.Combo;
010: import org.eclipse.swt.widgets.Composite;
011: import org.eclipse.swt.widgets.Control;
012: import org.eclipse.swt.widgets.Label;
013: import org.eclipse.swt.widgets.Shell;
014: import org.eclipse.swt.widgets.Text;
015:
016: /**
017: * This provides an editor for mapping language mappings.
018: * This is preferable to in place editing, as it fits the usage pattern of read lots,
019: * edit little.
020: *
021: * This is a simple popup modal dialog.
022: *
023: * @author Michael Neale
024: */
025: public class MappingEditor extends TitleAreaDialog {
026:
027: private static final int SCOPE_KEYWORD = 0;
028: private static final int SCOPE_WHEN = 1;
029: private static final int SCOPE_THEN = 2;
030: private static final int SCOPE_ALL = 3;
031:
032: private static final String SCOPE_STR_KEYWORD = "keyword";
033: private static final String SCOPE_STR_WHEN = "condition";
034: private static final String SCOPE_STR_THEN = "consequence";
035: private static final String SCOPE_STR_ALL = "*";
036:
037: private Text exprText;
038: private Text mappingText;
039: private Text objText;
040: private Combo scopeCombo;
041: private boolean cancelled;
042:
043: private DSLMappingEntry model;
044:
045: protected MappingEditor(Shell parent) {
046: super (parent);
047: }
048:
049: /**
050: * Pass in a NLMapping item for display/edits.
051: * Changes will be applied to this object only if the user clicks OK.
052: */
053: public void setNLMappingItem(DSLMappingEntry item) {
054: model = item;
055: setSection(model.getSection());
056: exprText.setText(model.getMappingKey() == null ? "" : model
057: .getMappingKey());
058: mappingText.setText(model.getMappingValue() == null ? ""
059: : model.getMappingValue());
060: objText.setText(model.getMetaData().getMetaData() == null ? ""
061: : model.getMetaData().getMetaData());
062: }
063:
064: private void setSection(Section section) {
065: if (section == DSLMappingEntry.CONDITION) {
066: scopeCombo.select(SCOPE_WHEN);
067: } else if (section == DSLMappingEntry.CONSEQUENCE) {
068: scopeCombo.select(SCOPE_THEN);
069: } else if (section == DSLMappingEntry.ANY) {
070: scopeCombo.select(SCOPE_ALL);
071: } else if (section == DSLMappingEntry.KEYWORD) {
072: scopeCombo.select(SCOPE_KEYWORD);
073: } else {
074: throw new IllegalArgumentException("Unknown scope type: "
075: + section);
076: }
077: }
078:
079: private Section getSection(String sectionStr) {
080: DSLMappingEntry.Section section = DSLMappingEntry.ANY;
081: if (SCOPE_STR_KEYWORD.equals(sectionStr)) {
082: section = DSLMappingEntry.KEYWORD;
083: } else if (SCOPE_STR_WHEN.equals(sectionStr)) {
084: section = DSLMappingEntry.CONDITION;
085: } else if (SCOPE_STR_THEN.equals(sectionStr)) {
086: section = DSLMappingEntry.CONSEQUENCE;
087: }
088: return section;
089: }
090:
091: protected void cancelPressed() {
092: this .cancelled = true;
093: super .cancelPressed();
094: }
095:
096: protected void okPressed() {
097: this .cancelled = false;
098: this .model.setMappingKey(this .exprText.getText());
099: this .model.setMappingValue(this .mappingText.getText());
100: this .model.setSection(this
101: .getSection(this .scopeCombo.getText()));
102: this .model
103: .setMetaData(new DSLMappingEntry.DefaultDSLEntryMetaData(
104: this .objText.getText()));
105: super .okPressed();
106: }
107:
108: /** This will tell if the user cancelled the edit */
109: public boolean isCancelled() {
110: return cancelled;
111: }
112:
113: protected Control createDialogArea(Composite parent) {
114:
115: //set the overall layout
116: GridLayout gridLayout = new GridLayout();
117: gridLayout.marginHeight = 10;
118: gridLayout.verticalSpacing = 10;
119: gridLayout.marginWidth = 10;
120: gridLayout.numColumns = 2;
121: parent.setLayout(gridLayout);
122:
123: //setup fields
124: createExpressionField(parent);
125: createMappingField(parent);
126: createObjectField(parent);
127: createScopeField(parent);
128:
129: // create the top level composite wrapper
130: Composite composite = new Composite(parent, SWT.NONE);
131: GridLayout layout = new GridLayout();
132: layout.marginHeight = 10;
133: layout.marginWidth = 10;
134: layout.verticalSpacing = 10;
135: composite.setLayout(layout);
136: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
137: composite.setFont(parent.getFont());
138:
139: return composite;
140: }
141:
142: private void createMappingField(Composite parent) {
143: Label mappingLbl = new Label(parent, SWT.NONE);
144: mappingLbl.setText("Rule mapping:");
145: mappingLbl.setFont(parent.getFont());
146: mappingLbl.setLayoutData(new GridData(
147: GridData.HORIZONTAL_ALIGN_END));
148:
149: mappingText = new Text(parent, SWT.BORDER);
150: GridData data = new GridData();
151: data.widthHint = 450;
152: data.horizontalAlignment = GridData.FILL;
153: data.grabExcessHorizontalSpace = true;
154: mappingText.setLayoutData(data);
155:
156: mappingText
157: .setToolTipText("Enter the rule language mapping that the \nlanguage item will be translated to."
158: + " Use the named variables (holes) \nthat you specify in the language expression above.");
159:
160: }
161:
162: private void createExpressionField(Composite parent) {
163: Label exprLbl = new Label(parent, SWT.NONE);
164: exprLbl.setText("Language expression:");
165: exprLbl.setFont(parent.getFont());
166: exprLbl.setLayoutData(new GridData(
167: GridData.HORIZONTAL_ALIGN_END));
168:
169: exprText = new Text(parent, SWT.BORDER);
170: GridData data = new GridData();
171: data.widthHint = 450;
172: data.horizontalAlignment = GridData.FILL;
173: data.grabExcessHorizontalSpace = true;
174: exprText.setLayoutData(data);
175: exprText
176: .setToolTipText("Enter the language expression that you want to use in a rule.\n"
177: + "Use curly brackets to mark 'holes' where the values will be extracted\n"
178: + "from in the rule source. "
179: + "Such as: Person has a name of {name} \n"
180: + "This will then parse the rule source to extract the data out of \n"
181: + "the place where {name} would appear.");
182: }
183:
184: private void createObjectField(Composite parent) {
185: Label objectLbl = new Label(parent, SWT.NONE);
186: objectLbl.setText("Object:");
187: objectLbl.setFont(parent.getFont());
188: objectLbl.setLayoutData(new GridData(
189: GridData.HORIZONTAL_ALIGN_END));
190:
191: objText = new Text(parent, SWT.BORDER);
192: GridData data = new GridData();
193: data.widthHint = 450;
194: data.horizontalAlignment = GridData.FILL;
195: data.grabExcessHorizontalSpace = true;
196: objText.setLayoutData(data);
197:
198: objText.setToolTipText("Enter the name of the object.");
199:
200: }
201:
202: private void createScopeField(Composite parent) {
203:
204: //type
205: Label scopeLbl = new Label(parent, SWT.NONE);
206: scopeLbl.setText("Scope:");
207: scopeLbl.setFont(parent.getFont());
208: scopeLbl.setLayoutData(new GridData(
209: GridData.HORIZONTAL_ALIGN_END));
210:
211: scopeCombo = new Combo(parent, SWT.READ_ONLY);
212:
213: scopeCombo.add(SCOPE_STR_KEYWORD, SCOPE_KEYWORD);
214: scopeCombo.add(SCOPE_STR_WHEN, SCOPE_WHEN);
215: scopeCombo.add(SCOPE_STR_THEN, SCOPE_THEN);
216: scopeCombo.add(SCOPE_STR_ALL, SCOPE_ALL);
217:
218: scopeCombo.select(SCOPE_ALL); //the default
219:
220: scopeCombo.setLayoutData(new GridData(
221: GridData.HORIZONTAL_ALIGN_BEGINNING));
222: scopeCombo.setFont(parent.getFont());
223: scopeCombo
224: .setToolTipText("This specifies what part of the rule the expression applies. Indicating '*' means global.");
225:
226: }
227:
228: }
|