001: package org.drools.brms.client.modeldriven.ui;
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.DirtyableFlexTable;
021: import org.drools.brms.client.common.FormStylePopup;
022: import org.drools.brms.client.common.ImageButton;
023: import org.drools.brms.client.common.Lbl;
024: import org.drools.brms.client.common.YesNoDialog;
025: import org.drools.brms.client.modeldriven.HumanReadable;
026: import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
027: import org.drools.brms.client.modeldriven.brl.ActionFieldValue;
028: import org.drools.brms.client.modeldriven.brl.ActionInsertFact;
029: import org.drools.brms.client.modeldriven.brl.ActionInsertLogicalFact;
030:
031: import com.google.gwt.user.client.Command;
032: import com.google.gwt.user.client.ui.ChangeListener;
033: import com.google.gwt.user.client.ui.ClickListener;
034: import com.google.gwt.user.client.ui.Composite;
035: import com.google.gwt.user.client.ui.FlexTable;
036: import com.google.gwt.user.client.ui.HorizontalPanel;
037: import com.google.gwt.user.client.ui.Image;
038: import com.google.gwt.user.client.ui.KeyboardListener;
039: import com.google.gwt.user.client.ui.Label;
040: import com.google.gwt.user.client.ui.ListBox;
041: import com.google.gwt.user.client.ui.TextBox;
042: import com.google.gwt.user.client.ui.Widget;
043:
044: /**
045: * This is used when asserting a new fact into working memory.
046: *
047: * @author Michael Neale
048: *
049: */
050: public class ActionInsertFactWidget extends DirtyableComposite {
051:
052: private final DirtyableFlexTable layout;
053: private final ActionInsertFact model;
054: private final SuggestionCompletionEngine completions;
055: private final String[] fieldCompletions;
056: private final RuleModeller modeller;
057: private final String factType;
058:
059: public ActionInsertFactWidget(RuleModeller mod,
060: ActionInsertFact set, SuggestionCompletionEngine com) {
061: this .model = set;
062: this .completions = com;
063: this .layout = new DirtyableFlexTable();
064: this .modeller = mod;
065: this .factType = set.factType;
066: this .fieldCompletions = this .completions
067: .getFieldCompletions(set.factType);
068:
069: layout.setStyleName("model-builderInner-Background");
070:
071: doLayout();
072:
073: initWidget(this .layout);
074: }
075:
076: private void doLayout() {
077: layout.clear();
078: layout.setWidget(0, 0, getAssertLabel());
079:
080: DirtyableFlexTable inner = new DirtyableFlexTable();
081:
082: for (int i = 0; i < model.fieldValues.length; i++) {
083: ActionFieldValue val = model.fieldValues[i];
084:
085: inner.setWidget(i, 0, fieldSelector(val));
086: inner.setWidget(i, 1, valueEditor(val));
087: final int idx = i;
088: Image remove = new ImageButton(
089: "images/delete_item_small.gif");
090: remove.addClickListener(new ClickListener() {
091: public void onClick(Widget w) {
092: YesNoDialog diag = new YesNoDialog(
093: "Remove this item?", new Command() {
094: public void execute() {
095: model.removeField(idx);
096: modeller.refreshWidget();
097: }
098: });
099: diag.setPopupPosition(w.getAbsoluteLeft(), w
100: .getAbsoluteTop());
101: diag.show();
102: }
103: });
104: inner.setWidget(i, 2, remove);
105:
106: }
107:
108: layout.setWidget(0, 1, inner);
109:
110: }
111:
112: private Widget valueEditor(final ActionFieldValue val) {
113: String enumKey = this .factType + "." + val.field;
114: if (this .completions.dataEnumLists.containsKey(enumKey)) {
115: return ConstraintValueEditor.enumDropDown(val.value,
116: new ConstraintValueEditor.ValueChanged() {
117: public void valueChanged(String newValue) {
118: val.value = newValue;
119: }
120: }, (String[]) this .completions.dataEnumLists
121: .get(enumKey));
122: } else {
123: final TextBox box = new TextBox();
124: box.setText(val.value);
125: box.addChangeListener(new ChangeListener() {
126: public void onChange(Widget w) {
127: val.value = box.getText();
128: modeller.refreshWidget();
129: }
130: });
131:
132: if (val.type
133: .equals(SuggestionCompletionEngine.TYPE_NUMERIC)) {
134: box.addKeyboardListener(ActionSetFieldWidget
135: .getNumericFilter(box));
136: }
137: return box;
138: }
139: }
140:
141: private Widget fieldSelector(final ActionFieldValue val) {
142: return new Label(val.field);
143: }
144:
145: private Widget getAssertLabel() {
146: HorizontalPanel horiz = new HorizontalPanel();
147:
148: Image edit = new ImageButton("images/add_field_to_fact.gif");
149: edit
150: .setTitle("Add another field to this so you can set its value.");
151: edit.addClickListener(new ClickListener() {
152: public void onClick(Widget w) {
153: showAddFieldPopup(w);
154: }
155: });
156:
157: String assertType = "assert";
158: if (this .model instanceof ActionInsertLogicalFact) {
159: assertType = "assertLogical";
160: }
161: horiz.add(new Lbl(HumanReadable
162: .getActionDisplayName(assertType)
163: + " " + this .model.factType, "modeller-action-Label"));
164: horiz.add(edit);
165: return horiz;
166:
167: }
168:
169: protected void showAddFieldPopup(Widget w) {
170: final FormStylePopup popup = new FormStylePopup(
171: "images/newex_wiz.gif", "Add a field");
172: popup.setStyleName("ks-popups-Popup");
173: final ListBox box = new ListBox();
174: box.addItem("...");
175:
176: for (int i = 0; i < fieldCompletions.length; i++) {
177: box.addItem(fieldCompletions[i]);
178: }
179:
180: box.setSelectedIndex(0);
181:
182: popup.addAttribute("Add field", box);
183: box.addChangeListener(new ChangeListener() {
184: public void onChange(Widget w) {
185: String fieldName = box.getItemText(box
186: .getSelectedIndex());
187: String fieldType = completions.getFieldType(
188: model.factType, fieldName);
189: model.addFieldValue(new ActionFieldValue(fieldName, "",
190: fieldType));
191: modeller.refreshWidget();
192: popup.hide();
193: }
194: });
195:
196: popup.setPopupPosition(w.getAbsoluteLeft(), w.getAbsoluteTop());
197: popup.show();
198:
199: }
200:
201: }
|