001: package org.drools.brms.client.rulelist;
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.AutoCompleteTextBoxAsync;
020: import org.drools.brms.client.common.CompletionItemsAsync;
021: import org.drools.brms.client.common.CompletionItemsAsyncReturn;
022: import org.drools.brms.client.common.FormStyleLayout;
023: import org.drools.brms.client.common.GenericCallback;
024: import org.drools.brms.client.common.LoadingPopup;
025: import org.drools.brms.client.rpc.RepositoryServiceFactory;
026: import org.drools.brms.client.rpc.TableDataResult;
027: import org.drools.brms.client.rpc.TableDataRow;
028:
029: import com.google.gwt.user.client.ui.Button;
030: import com.google.gwt.user.client.ui.CheckBox;
031: import com.google.gwt.user.client.ui.ClickListener;
032: import com.google.gwt.user.client.ui.Composite;
033: import com.google.gwt.user.client.ui.FlexTable;
034: import com.google.gwt.user.client.ui.HTML;
035: import com.google.gwt.user.client.ui.HorizontalPanel;
036: import com.google.gwt.user.client.ui.KeyboardListener;
037: import com.google.gwt.user.client.ui.Label;
038: import com.google.gwt.user.client.ui.TextBox;
039: import com.google.gwt.user.client.ui.Widget;
040:
041: /**
042: * This is for quickly finding an asset by name. Partial completion is allowed.
043: * This also uses some auto completion magic.
044: * @author Michael Neale
045: */
046: public class QuickFindWidget extends Composite {
047:
048: private final FormStyleLayout layout;
049: private final FlexTable listPanel;
050: private final TextBox searchBox;
051: private CheckBox archiveBox;
052: private EditItemEvent editEvent;
053: private String[] shortListItems;
054:
055: public QuickFindWidget(EditItemEvent editEvent) {
056: layout = new FormStyleLayout("images/system_search.png", "");
057:
058: searchBox = new AutoCompleteTextBoxAsync(
059: new CompletionItemsAsync() {
060:
061: public void getCompletionItems(String match,
062: CompletionItemsAsyncReturn asyncReturn) {
063: loadShortList(match, asyncReturn);
064:
065: }
066:
067: });
068: searchBox.setStyleName("gwt-TextBox");
069:
070: this .editEvent = editEvent;
071: HorizontalPanel srch = new HorizontalPanel();
072: Button go = new Button("Go");
073: go.addClickListener(new ClickListener() {
074: public void onClick(Widget w) {
075: updateList();
076: }
077: });
078:
079: srch.add(searchBox);
080: srch.add(go);
081:
082: archiveBox = new CheckBox("Include archived items in list");
083: archiveBox.setStyleName("small-Text");
084: archiveBox.setChecked(false);
085:
086: layout.addAttribute("Find items with a name matching:", srch);
087: layout.addRow(archiveBox);
088: layout.addRow(new HTML("<hr/>"));
089:
090: listPanel = new FlexTable();
091: listPanel
092: .setWidget(
093: 0,
094: 0,
095: new HTML(
096: "<img src='images/information.gif'/> Enter the name or part of a name. Alternatively, use the categories to browse."));
097: layout.addRow(listPanel);
098:
099: listPanel.setStyleName("editable-Surface");
100:
101: searchBox.addKeyboardListener(getKeyboardListener());
102:
103: layout.setStyleName("quick-find");
104:
105: initWidget(layout);
106: }
107:
108: /**
109: * This will load a list of items as they are typing.
110: */
111: protected String[] loadShortList(String match,
112: final CompletionItemsAsyncReturn returnItems) {
113: RepositoryServiceFactory.getService().quickFindAsset(match, 5,
114: archiveBox.isChecked(), new GenericCallback() {
115:
116: public void onSuccess(Object data) {
117: TableDataResult result = (TableDataResult) data;
118: String[] items = new String[result.data.length];
119: for (int i = 0; i < result.data.length; i++) {
120: if (!result.data[i].id.equals("MORE")) {
121: items[i] = result.data[i].values[0];
122: }
123: }
124: returnItems.itemReturn(items);
125: }
126:
127: });
128: return shortListItems;
129: }
130:
131: private KeyboardListener getKeyboardListener() {
132: return new KeyboardListener() {
133: public void onKeyDown(Widget arg0, char arg1, int arg2) {
134: }
135:
136: public void onKeyPress(Widget arg0, char arg1, int arg2) {
137: }
138:
139: public void onKeyUp(Widget arg0, char arg1, int arg2) {
140: if (arg1 == KEY_ENTER) {
141:
142: updateList();
143: }
144: }
145:
146: };
147: }
148:
149: protected void updateList() {
150:
151: LoadingPopup.showMessage("Searching...");
152: RepositoryServiceFactory.getService().quickFindAsset(
153: searchBox.getText(), 15, archiveBox.isChecked(),
154: new GenericCallback() {
155: public void onSuccess(Object data) {
156: TableDataResult result = (TableDataResult) data;
157: populateList(result);
158:
159: }
160: });
161:
162: }
163:
164: protected void populateList(TableDataResult result) {
165:
166: FlexTable data = new FlexTable();
167:
168: //if its only one, just open it...
169: if (result.data.length == 1) {
170: editEvent.open(result.data[0].id);
171: }
172:
173: for (int i = 0; i < result.data.length; i++) {
174:
175: final TableDataRow row = result.data[i];
176: if (row.id.equals("MORE")) {
177: data
178: .setWidget(
179: i,
180: 0,
181: new HTML(
182: "<i>There are more items... try narrowing the search terms..</i>"));
183: data.getFlexCellFormatter().setColSpan(i, 0, 3);
184: } else {
185: data.setWidget(i, 0, new Label(row.values[0]));
186: data.setWidget(i, 1, new Label(row.values[1]));
187: Button open = new Button("Open");
188: open.addClickListener(new ClickListener() {
189: public void onClick(Widget w) {
190: editEvent.open(row.id);
191: }
192: });
193:
194: data.setWidget(i, 2, open);
195: }
196:
197: }
198:
199: data.setWidth("100%");
200: listPanel.setWidget(0, 0, data);
201:
202: LoadingPopup.close();
203:
204: }
205:
206: }
|