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.GenericCallback;
020: import org.drools.brms.client.common.ImageButton;
021: import org.drools.brms.client.common.LoadingPopup;
022: import org.drools.brms.client.rpc.RepositoryServiceAsync;
023: import org.drools.brms.client.rpc.RepositoryServiceFactory;
024: import org.drools.brms.client.rpc.TableConfig;
025: import org.drools.brms.client.rpc.TableDataResult;
026: import org.drools.brms.client.rpc.TableDataRow;
027: import org.drools.brms.client.ruleeditor.EditorLauncher;
028: import org.drools.brms.client.table.DataModel;
029: import org.drools.brms.client.table.SortableTable;
030:
031: import com.google.gwt.user.client.Command;
032: import com.google.gwt.user.client.ui.ClickListener;
033: import com.google.gwt.user.client.ui.Composite;
034: import com.google.gwt.user.client.ui.FlexTable;
035: import com.google.gwt.user.client.ui.HasHorizontalAlignment;
036: import com.google.gwt.user.client.ui.HasVerticalAlignment;
037: import com.google.gwt.user.client.ui.HorizontalPanel;
038: import com.google.gwt.user.client.ui.Image;
039: import com.google.gwt.user.client.ui.Label;
040: import com.google.gwt.user.client.ui.Widget;
041: import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
042:
043: /**
044: * This is a viewer for viewing a list of rules for editing/selection.
045: * This uses the sortable table widget, and can be re-used with different data sets.
046: * (ie no need to throw is away).
047: */
048: public class AssetItemListViewer extends Composite {
049:
050: /** The number of rows to "fill out" */
051: private static final int FILLER_ROWS = 25;
052:
053: public static final String RULE_LIST_TABLE_ID = "rulelist";
054: public static final String ARCHIVED_RULE_LIST_TABLE_ID = "archivedrulelist";
055:
056: private FlexTable outer = new FlexTable();
057: private SortableTable table;
058: private TableConfig tableConfig;
059: private EditItemEvent openItemEvent;
060:
061: private Image refreshIcon = new ImageButton("images/refresh.gif");
062: private Command refresh;
063: private static RepositoryServiceAsync service = RepositoryServiceFactory
064: .getService();
065: private Label itemCounter = new Label();
066:
067: public AssetItemListViewer(EditItemEvent event, String tableconfig) {
068:
069: init();
070:
071: loadTableConfig(tableconfig);
072: this .refreshIcon.setVisible(false);
073: this .openItemEvent = event;
074: this .refreshIcon
075: .setTitle("Refresh current list. Will show any changes.");
076: this .refreshIcon.addClickListener(new ClickListener() {
077: public void onClick(Widget w) {
078: LoadingPopup
079: .showMessage("Refreshing list, please wait...");
080: refresh.execute();
081: }
082: });
083: }
084:
085: /**
086: * Optionally set the refresh command to re-populate the list on demand.
087: * @param refreshCom
088: */
089: public void setRefreshCommand(Command refreshCom) {
090: this .refresh = refreshCom;
091: this .refreshIcon.setVisible(true);
092:
093: }
094:
095: private void loadTableConfig(String tableconfig) {
096: service.loadTableConfig(tableconfig, new GenericCallback() {
097: public void onSuccess(Object o) {
098: tableConfig = (TableConfig) o;
099: loadTableData(null);
100: }
101: });
102: }
103:
104: /**
105: * Initialize the widget goodness.
106: */
107: private void init() {
108: FlexCellFormatter formatter = outer.getFlexCellFormatter();
109:
110: //outer.setStyleName( SortableTable.styleList );
111: outer.setWidth("100%");
112:
113: formatter.setAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT,
114: HasVerticalAlignment.ALIGN_MIDDLE);
115:
116: Image openIcon = new ImageButton("images/open_item.gif");
117: openIcon.addClickListener(new ClickListener() {
118: public void onClick(Widget w) {
119: LoadingPopup
120: .showMessage("Loading item, please wait ...");
121: openItemEvent.open(TableDataRow.getId(table
122: .getSelectedKey()));
123:
124: }
125: });
126: openIcon.setTitle("Open item");
127:
128: outer.setWidget(0, 1, openIcon);
129: formatter.setAlignment(0, 1,
130: HasHorizontalAlignment.ALIGN_RIGHT,
131: HasVerticalAlignment.ALIGN_MIDDLE);
132:
133: initWidget(outer);
134: }
135:
136: /**
137: * This will create a table, and load the data, wrapping it in a scrolling area.
138: * If the data is null, it will just fill it in with something empty
139: * so it looks busy.
140: *
141: * This could probably clear the table, but I just throw it away
142: * let the garbage collector do the hard work.
143: */
144: public void loadTableData(TableDataResult data) {
145: FlexCellFormatter formatter = outer.getFlexCellFormatter();
146: outer.setWidget(1, 0, null);
147:
148: //if no data, just fill it out
149: if (data == null || data.data.length == 0) {
150:
151: DataModel nil = new DataModel() {
152:
153: public int getNumberOfRows() {
154: return 0;
155: }
156:
157: public String getRowId(int row) {
158: return "";
159: }
160:
161: public Comparable getValue(int row, int col) {
162: return "";
163: }
164:
165: public Widget getWidget(int row, int col) {
166: return null;
167: }
168:
169: };
170:
171: table = SortableTable.createTableWidget(nil,
172: tableConfig.headers, FILLER_ROWS, true);
173: itemCounter.setVisible(false);
174: } else {
175: final TableDataRow[] rows = data.data;
176: DataModel mdl = new DataModel() {
177:
178: public int getNumberOfRows() {
179: return rows.length;
180: }
181:
182: public String getRowId(int row) {
183: return rows[row].id;
184: }
185:
186: public Comparable getValue(int row, int col) {
187: return rows[row].values[col];
188: }
189:
190: public Widget getWidget(int row, int col) {
191:
192: if (tableConfig.headers[col].equals("*")) {
193: return new Image(
194: "images/"
195: + EditorLauncher
196: .getAssetFormatIcon(rows[row].format));
197: } else {
198: return null;
199: }
200: }
201:
202: };
203:
204: table = SortableTable.createTableWidget(mdl,
205: this .tableConfig.headers, FILLER_ROWS, true);
206:
207: HorizontalPanel panel = new HorizontalPanel();
208: panel.add(refreshIcon);
209: itemCounter.setVisible(true);
210: itemCounter.setText(" " + data.data.length + " items.");
211: panel.add(itemCounter);
212:
213: outer.setWidget(0, 0, panel);
214: }
215:
216: table.setWidth("100%");
217: outer.setWidget(1, 0, table);
218: formatter.setColSpan(1, 0, 2);
219:
220: }
221:
222: public String getSelectedElementUUID() {
223: return TableDataRow.getId(table.getSelectedKey());
224: }
225: }
|