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 java.util.List;
020:
021: import org.drools.brms.client.modeldriven.brl.DSLSentence;
022:
023: import com.google.gwt.user.client.ui.Button;
024: import com.google.gwt.user.client.ui.ClickListener;
025: import com.google.gwt.user.client.ui.FocusListener;
026: import com.google.gwt.user.client.ui.HorizontalPanel;
027: import com.google.gwt.user.client.ui.KeyboardListener;
028: import com.google.gwt.user.client.ui.ListBox;
029: import com.google.gwt.user.client.ui.PopupPanel;
030: import com.google.gwt.user.client.ui.TextBox;
031: import com.google.gwt.user.client.ui.VerticalPanel;
032: import com.google.gwt.user.client.ui.Widget;
033:
034: /**
035: * This is a popup list for "content assistance" - although on the web,
036: * its not assistance - its mandatory ;)
037: */
038: public class ChoiceList extends PopupPanel {
039:
040: private ListBox list;
041: private final DSLSentence[] sentences;
042: private HorizontalPanel buttons;
043: private TextBox filter;
044:
045: /**
046: * Pass in a list of suggestions for the popup lists.
047: * Set a click listener to get notified when the OK button is clicked.
048: */
049: public ChoiceList(final DSLSentence[] sen, final DSLRuleEditor self) {
050: super (true);
051:
052: this .sentences = sen;
053: filter = new TextBox();
054: filter.setWidth("100%");
055: final String defaultMessage = "<enter text to filter list>";
056: filter.setText(defaultMessage);
057: filter.addFocusListener(new FocusListener() {
058: public void onFocus(Widget w) {
059: filter.setText("");
060: }
061:
062: public void onLostFocus(Widget w) {
063: filter.setText(defaultMessage);
064: }
065: });
066: filter.addKeyboardListener(new KeyboardListener() {
067:
068: public void onKeyDown(Widget arg0, char arg1, int arg2) {
069:
070: }
071:
072: public void onKeyPress(Widget arg0, char arg1, int arg2) {
073: }
074:
075: public void onKeyUp(Widget arg0, char arg1, int arg2) {
076: if (arg1 == KEY_ENTER) {
077: applyChoice(self);
078: } else {
079: populateList(ListUtil.filter(sentences, filter
080: .getText()));
081: }
082: }
083:
084: });
085: filter.setFocus(true);
086:
087: VerticalPanel panel = new VerticalPanel();
088: panel.add(filter);
089:
090: list = new ListBox();
091: list.setVisibleItemCount(5);
092:
093: populateList(ListUtil.filter(this .sentences, ""));
094:
095: panel.add(list);
096:
097: Button ok = new Button("ok");
098: ok.addClickListener(new ClickListener() {
099: public void onClick(Widget btn) {
100: applyChoice(self);
101: }
102: });
103:
104: Button cancel = new Button("cancel");
105: cancel.addClickListener(new ClickListener() {
106: public void onClick(Widget btn) {
107: hide();
108: }
109: });
110:
111: buttons = new HorizontalPanel();
112:
113: buttons.add(ok);
114: buttons.add(cancel);
115:
116: panel.add(buttons);
117:
118: add(panel);
119: setStyleName("ks-popups-Popup");
120:
121: }
122:
123: private void applyChoice(final DSLRuleEditor self) {
124: self.insertText(getSelectedItem());
125: hide();
126: }
127:
128: private void populateList(List filtered) {
129: list.clear();
130: for (int i = 0; i < filtered.size(); i++) {
131: list.addItem(((DSLSentence) filtered.get(i)).sentence);
132: }
133: }
134:
135: public String getSelectedItem() {
136: return list.getItemText(list.getSelectedIndex());
137: }
138:
139: }
|