001: package org.drools.eclipse.dsl.editor.completion;
002:
003: import java.io.BufferedReader;
004: import java.io.IOException;
005: import java.io.StringReader;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import org.drools.eclipse.DroolsPluginImages;
010: import org.drools.eclipse.dsl.editor.DSLAdapter;
011: import org.drools.eclipse.dsl.editor.DSLRuleEditor;
012: import org.drools.eclipse.editors.AbstractRuleEditor;
013: import org.drools.eclipse.editors.completion.RuleCompletionProcessor;
014: import org.drools.eclipse.editors.completion.RuleCompletionProposal;
015: import org.drools.lang.Location;
016: import org.eclipse.swt.graphics.Image;
017:
018: /**
019: * For handling DSL rules.
020: *
021: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
022: */
023: public class DSLRuleCompletionProcessor extends RuleCompletionProcessor {
024:
025: private static final Image DSL_ICON = DroolsPluginImages
026: .getImage(DroolsPluginImages.DSL_EXPRESSION);
027:
028: public DSLRuleCompletionProcessor(AbstractRuleEditor editor) {
029: super (editor);
030: }
031:
032: protected DSLRuleEditor getDSLRuleEditor() {
033: return (DSLRuleEditor) getEditor();
034: }
035:
036: protected void addRHSCompletionProposals(List list,
037: int documentOffset, String prefix, String backText,
038: String conditions, String consequence) {
039: // super.addRHSCompletionProposals(list, documentOffset, prefix, backText, conditions, consequence);
040: DSLAdapter adapter = getDSLRuleEditor().getDSLAdapter();
041: if (adapter != null) {
042: List dslConsequences = adapter.getDSLTree()
043: .getConsequenceChildrenList(prefix, true);
044: addDSLProposals(list, documentOffset, prefix,
045: dslConsequences);
046: }
047: }
048:
049: protected void addLHSCompletionProposals(List list,
050: int documentOffset, Location location, String prefix,
051: String backText) {
052: // super.addLHSCompletionProposals(list, documentOffset, location, prefix, backText);
053: DSLAdapter adapter = getDSLRuleEditor().getDSLAdapter();
054: if (adapter != null) {
055: String lastobj = this .getLastNonDashLine(backText);
056: String last = this .getLastLine(backText);
057: // we have to check if the last line is when. if it is we set
058: // the last line to zero length string
059: if (last.equals("when")) {
060: last = "";
061: lastobj = "*";
062: }
063: // pass the last string in the backText to getProposals
064: List dslConditions = this .getProposals(adapter, lastobj,
065: last);
066: // if we couldn't find any matches, we add the list from
067: // the DSLAdapter so that there's something
068: if (dslConditions.size() == 0) {
069: dslConditions.addAll(adapter.listConditionItems());
070: }
071: addDSLProposals(list, documentOffset, prefix, dslConditions);
072: }
073: }
074:
075: private void addDSLProposals(final List list, int documentOffset,
076: final String prefix, List dslItems) {
077: Iterator iterator = dslItems.iterator();
078: while (iterator.hasNext()) {
079: String consequence = (String) iterator.next();
080: RuleCompletionProposal p = new RuleCompletionProposal(
081: documentOffset - prefix.length(), prefix.length(),
082: consequence);
083: p.setImage(DSL_ICON);
084: list.add(p);
085: }
086: }
087:
088: /**
089: * because of how the backText works, we need to get the last line, so that
090: * we can pass it to the DSLUtility
091: *
092: * @param backText
093: * @return
094: */
095: public String getLastLine(String backText) {
096: BufferedReader breader = new BufferedReader(new StringReader(
097: backText));
098: String last = "";
099: String line = null;
100: try {
101: while ((line = breader.readLine()) != null) {
102: // only if the line has text do we set last to it
103: if (line.length() > 0) {
104: last = line;
105: }
106: }
107: } catch (IOException e) {
108: // TODO need to log this.
109: // I'm leaving this for mic_hat, so he has something to do
110: }
111: // now that all the conditions for a single object are on the same line
112: // we need to check for the left parenthesis
113: if (last.indexOf("(") > -1) {
114: last = last.substring(last.lastIndexOf("(") + 1);
115: }
116: // if the string has a comma "," we get the substring starting from
117: // the index after the last comma
118: if (last.indexOf(",") > -1) {
119: last = last.substring(last.lastIndexOf(",") + 1);
120: }
121: // if the line ends with right parenthesis, we change it to zero length
122: // string
123: if (last.endsWith(")")) {
124: last = "";
125: }
126: return last;
127: }
128:
129: /**
130: * Returns the last line that doesn't start with a dash
131: *
132: * @param backText
133: * @return
134: */
135: public String getLastNonDashLine(String backText) {
136: BufferedReader breader = new BufferedReader(new StringReader(
137: backText));
138: String last = "";
139: String line = null;
140: try {
141: while ((line = breader.readLine()) != null) {
142: // there may be blank lines, so we trim first
143: line = line.trim();
144: // only if the line has text do we set last to it
145: if (line.length() > 0 && !line.startsWith("-")) {
146: last = line;
147: }
148: }
149: } catch (IOException e) {
150: // TODO need to log this.
151: // I'm leaving this for mic_hat, so he has something to do
152: }
153: if (last.indexOf("(") > -1 && !last.endsWith(")")) {
154: last = last.substring(0, last.indexOf("("));
155: } else if (last.indexOf("(") > -1 && last.endsWith(")")) {
156: last = "";
157: }
158: return last;
159: }
160:
161: /**
162: * The DSLTree is configurable. It can either return just the child of the
163: * last token found, or it can traverse the tree and generate all the
164: * combinations beneath the last matching node. TODO I don't know how to add
165: * configuration to the editor, so it needs to be hooked up to the
166: * configuration for the editor later.
167: *
168: * @param last
169: * @return
170: */
171: protected List getProposals(DSLAdapter adapter, String obj,
172: String last) {
173: if (last.length() == 0) {
174: last = " ";
175: }
176: return adapter.getDSLTree().getChildrenList(obj, last, true);
177: }
178: }
|