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.DirtyableHorizontalPane;
021: import org.drools.brms.client.common.FormStyleLayout;
022: import org.drools.brms.client.common.YesNoDialog;
023: import org.drools.brms.client.modeldriven.brl.RuleAttribute;
024: import org.drools.brms.client.modeldriven.brl.RuleModel;
025:
026: import com.google.gwt.user.client.Command;
027: import com.google.gwt.user.client.ui.ChangeListener;
028: import com.google.gwt.user.client.ui.CheckBox;
029: import com.google.gwt.user.client.ui.ClickListener;
030: import com.google.gwt.user.client.ui.Image;
031: import com.google.gwt.user.client.ui.KeyboardListener;
032: import com.google.gwt.user.client.ui.ListBox;
033: import com.google.gwt.user.client.ui.TextBox;
034: import com.google.gwt.user.client.ui.Widget;
035:
036: /**
037: * Displays a list of rule options (attributes).
038: *
039: * @author Michael Neale
040: */
041: public class RuleAttributeWidget extends DirtyableComposite {
042:
043: private FormStyleLayout layout;
044: private RuleModel model;
045: private RuleModeller parent;
046:
047: public RuleAttributeWidget(RuleModeller parent, RuleModel model) {
048: this .parent = parent;
049: this .model = model;
050: layout = new FormStyleLayout();
051: RuleAttribute[] attrs = model.attributes;
052: for (int i = 0; i < attrs.length; i++) {
053: RuleAttribute at = attrs[i];
054: layout.addAttribute(at.attributeName,
055: getEditorWidget(at, i));
056: }
057:
058: initWidget(layout);
059: }
060:
061: /**
062: * Return a listbox of choices for rule attributes.
063: * @return
064: */
065: public static ListBox getAttributeList() {
066: ListBox list = new ListBox();
067: list.addItem("Choose...");
068:
069: list.addItem("salience");
070: list.addItem("enabled");
071: list.addItem("date-effective");
072: list.addItem("date-expires");
073: list.addItem("no-loop");
074: list.addItem("agenda-group");
075: list.addItem("activation-group");
076: list.addItem("duration");
077: list.addItem("auto-focus");
078: list.addItem("lock-on-active");
079: list.addItem("ruleflow-group");
080: list.addItem("dialect");
081:
082: return list;
083: }
084:
085: private Widget getEditorWidget(final RuleAttribute at, final int idx) {
086: if (at.attributeName.equals("no-loop")) {
087: return getRemoveIcon(idx);
088: }
089:
090: Widget editor = null;
091:
092: if (at.attributeName.equals("enabled")
093: || at.attributeName.equals("auto-focus")
094: || at.attributeName.equals("lock-on-active")) {
095: editor = checkBoxEditor(at);
096: } else {
097: editor = textBoxEditor(at);
098: }
099:
100: DirtyableHorizontalPane horiz = new DirtyableHorizontalPane();
101: horiz.add(editor);
102: horiz.add(getRemoveIcon(idx));
103:
104: return horiz;
105:
106: }
107:
108: private Widget checkBoxEditor(final RuleAttribute at) {
109: final CheckBox box = new CheckBox();
110: if (at.value == null) {
111: box.setChecked(true);
112: at.value = "true";
113: } else {
114: box.setChecked((at.value.equals("true") ? true : false));
115: }
116:
117: box.addClickListener(new ClickListener() {
118: public void onClick(Widget w) {
119: at.value = (box.isChecked()) ? "true" : "false";
120: }
121: });
122: return box;
123: }
124:
125: private TextBox textBoxEditor(final RuleAttribute at) {
126: final TextBox box = new TextBox();
127: box.setVisibleLength((at.value.length() < 3) ? 3 : at.value
128: .length());
129: box.setText(at.value);
130: box.addChangeListener(new ChangeListener() {
131: public void onChange(Widget w) {
132: at.value = box.getText();
133: makeDirty();
134: }
135: });
136:
137: if (at.attributeName.equals("date-effective")
138: || at.attributeName.equals("date-expires")) {
139: if (at.value == null || "".equals(at.value))
140: box.setText("dd-MMM-yyyy");
141:
142: box.setVisibleLength(10);
143: }
144:
145: box.addKeyboardListener(new KeyboardListener() {
146:
147: public void onKeyDown(Widget arg0, char arg1, int arg2) {
148:
149: }
150:
151: public void onKeyPress(Widget arg0, char arg1, int arg2) {
152:
153: }
154:
155: public void onKeyUp(Widget arg0, char arg1, int arg2) {
156: box.setVisibleLength(box.getText().length());
157: }
158:
159: });
160: return box;
161: }
162:
163: private Image getRemoveIcon(final int idx) {
164: Image remove = new Image("images/delete_item_small.gif");
165: remove.addClickListener(new ClickListener() {
166: public void onClick(Widget w) {
167: YesNoDialog diag = new YesNoDialog(
168: "Remove this rule option?", new Command() {
169: public void execute() {
170: model.removeAttribute(idx);
171: parent.refreshWidget();
172: }
173: });
174: diag.setPopupPosition(w.getAbsoluteLeft(), w
175: .getAbsoluteTop());
176: diag.show();
177: }
178: });
179: return remove;
180: }
181:
182: public boolean isDirty() {
183: return layout.isDirty();
184: }
185:
186: }
|