001: package org.drools.brms.client.ruleeditor;
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 org.drools.brms.client.common.DirtyableComposite;
020: import org.drools.brms.client.common.ImageButton;
021: import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
022: import org.drools.brms.client.modeldriven.brl.DSLSentence;
023: import org.drools.brms.client.packages.SuggestionCompletionCache;
024: import org.drools.brms.client.rpc.RuleAsset;
025: import org.drools.brms.client.rpc.RuleContentText;
026:
027: import com.google.gwt.user.client.ui.ChangeListener;
028: import com.google.gwt.user.client.ui.ClickListener;
029: import com.google.gwt.user.client.ui.FlexTable;
030: import com.google.gwt.user.client.ui.Image;
031: import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
032: import com.google.gwt.user.client.ui.TextArea;
033: import com.google.gwt.user.client.ui.VerticalPanel;
034: import com.google.gwt.user.client.ui.Widget;
035:
036: /**
037: * This is a textual rule editor, which provides DSL content assistance. This is
038: * similar (but simpler) to the IDE based one.
039: *
040: * @author michael neale
041: */
042:
043: public class DSLRuleEditor extends DirtyableComposite {
044:
045: private TextArea text;
046: final private RuleContentText data;
047: private DSLSentence[] conditions;
048: private DSLSentence[] actions;
049:
050: public DSLRuleEditor(RuleAsset asset) {
051:
052: RuleContentText cont = (RuleContentText) asset.content;
053:
054: this .data = cont;
055: text = new TextArea();
056: text.setWidth("100%");
057: text.setHeight("100%");
058: text.setVisibleLines(10);
059: text.setText(data.content);
060: text
061: .setTitle("Hint: press control+space for popup assistance, or use one of the icons to the right.");
062:
063: SuggestionCompletionEngine eng = SuggestionCompletionCache
064: .getInstance().getEngineFromCache(
065: asset.metaData.packageName);
066: this .actions = eng.actionDSLSentences;
067: this .conditions = eng.conditionDSLSentences;
068:
069: text.setStyleName("dsl-text-Editor");
070:
071: FlexTable layout = new FlexTable();
072: layout.setWidget(0, 0, text);
073:
074: text.addChangeListener(new ChangeListener() {
075: public void onChange(Widget w) {
076: data.content = text.getText();
077: makeDirty();
078: }
079: });
080:
081: text.addKeyboardListener(new KeyboardListenerAdapter() {
082: public void onKeyDown(Widget arg0, char arg1, int arg2) {
083: if (arg1 == ' ' && arg2 == MODIFIER_CTRL) {
084: showInTextOptions();
085: }
086:
087: if (arg1 == KEY_TAB) {
088: insertText("\t");
089: text.setCursorPos(text.getCursorPos() + 1);
090: text.cancelKey();
091: }
092: }
093: });
094:
095: VerticalPanel vert = new VerticalPanel();
096:
097: Image lhsOptions = new ImageButton("images/new_dsl_pattern.gif");
098: final String msg = "Add a new condition";
099: lhsOptions.setTitle(msg);
100: lhsOptions.addClickListener(new ClickListener() {
101: public void onClick(Widget w) {
102: showSuggestions(conditions);
103: }
104: });
105:
106: Image rhsOptions = new ImageButton("images/new_dsl_action.gif");
107: final String msg2 = "Add an action";
108: rhsOptions.setTitle(msg2);
109: rhsOptions.addClickListener(new ClickListener() {
110: public void onClick(Widget w) {
111: showSuggestions(actions);
112: }
113: });
114:
115: vert.add(lhsOptions);
116: vert.add(rhsOptions);
117:
118: layout.setWidget(0, 1, vert);
119:
120: layout.getCellFormatter().setWidth(0, 0, "95%");
121: layout.getCellFormatter().setWidth(0, 1, "5%");
122:
123: layout.setWidth("100%");
124: layout.setHeight("100%");
125:
126: initWidget(layout);
127: }
128:
129: protected void showInTextOptions() {
130: String prev = text.getText().substring(0,
131: this .text.getCursorPos());
132: if (prev.indexOf("then") > -1) {
133: showSuggestions(this .actions);
134: } else {
135: showSuggestions(this .conditions);
136: }
137: }
138:
139: private void showSuggestions(DSLSentence[] items) {
140: ChoiceList choice = new ChoiceList(items, this );
141: choice.setPopupPosition(text.getAbsoluteLeft() + 20, text
142: .getAbsoluteTop() + 20);
143: choice.show();
144: }
145:
146: void insertText(String ins) {
147: int i = text.getCursorPos();
148: String left = text.getText().substring(0, i);
149: String right = text.getText().substring(i,
150: text.getText().length());
151: text.setText(left + ins + right);
152: this.data.content = text.getText();
153: }
154:
155: }
|