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 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.GenericCallback;
023: import org.drools.brms.client.common.LoadingPopup;
024: import org.drools.brms.client.packages.PackageBuilderWidget;
025: import org.drools.brms.client.rpc.BuilderResult;
026: import org.drools.brms.client.rpc.RepositoryServiceFactory;
027: import org.drools.brms.client.rpc.RuleAsset;
028:
029: import com.google.gwt.user.client.ui.Button;
030: import com.google.gwt.user.client.ui.ClickListener;
031: import com.google.gwt.user.client.ui.FlexTable;
032: import com.google.gwt.user.client.ui.HTML;
033: import com.google.gwt.user.client.ui.HasHorizontalAlignment;
034: import com.google.gwt.user.client.ui.HasVerticalAlignment;
035: import com.google.gwt.user.client.ui.HorizontalPanel;
036: import com.google.gwt.user.client.ui.Image;
037: import com.google.gwt.user.client.ui.ScrollPanel;
038: import com.google.gwt.user.client.ui.Widget;
039:
040: /**
041: * This widget wraps a rule asset widget, and provides actions to validate and view source.
042: * @author Michael Neale
043: */
044: public class RuleValidatorWrapper extends DirtyableComposite {
045:
046: private RuleAsset asset;
047: private DirtyableFlexTable layout;
048:
049: public boolean isDirty() {
050: return layout.hasDirty();
051: }
052:
053: public RuleValidatorWrapper(Widget editor, RuleAsset asset) {
054: this .asset = asset;
055:
056: layout = new DirtyableFlexTable();
057: layout.setStyleName("asset-editor-Layout");
058: layout.setWidget(0, 0, editor);
059: if (!asset.isreadonly)
060: layout.setWidget(1, 0, validatorActions());
061: layout.getCellFormatter().setAlignment(1, 0,
062: HasHorizontalAlignment.ALIGN_RIGHT,
063: HasVerticalAlignment.ALIGN_MIDDLE);
064:
065: layout.setWidth("100%");
066: layout.setHeight("100%");
067:
068: initWidget(layout);
069: }
070:
071: private Widget validatorActions() {
072: HorizontalPanel horiz = new HorizontalPanel();
073: Button viewSource = new Button("View source");
074: horiz.add(viewSource);
075:
076: Button validate = new Button("Validate");
077: horiz.add(validate);
078:
079: viewSource.addClickListener(new ClickListener() {
080: public void onClick(Widget w) {
081: doViewsource();
082: }
083:
084: });
085:
086: validate.addClickListener(new ClickListener() {
087: public void onClick(Widget w) {
088: doValidate();
089: }
090: });
091:
092: horiz.setStyleName("asset-validator-Buttons");
093: return horiz;
094: }
095:
096: private void doValidate() {
097:
098: LoadingPopup.showMessage("Validating item, please wait...");
099: RepositoryServiceFactory.getService().buildAsset(asset,
100: new GenericCallback() {
101: public void onSuccess(Object data) {
102: BuilderResult[] results = (BuilderResult[]) data;
103: showBuilderErrors(results);
104: }
105: });
106:
107: }
108:
109: private void doViewsource() {
110: LoadingPopup.showMessage("Calculating source...");
111: RepositoryServiceFactory.getService().buildAssetSource(
112: this .asset, new GenericCallback() {
113: public void onSuccess(Object data) {
114: String src = (String) data;
115: showSource(src);
116: }
117: });
118:
119: }
120:
121: private void showSource(String src) {
122: PackageBuilderWidget.showSource(src, this .asset.metaData.name);
123: LoadingPopup.close();
124: }
125:
126: private void showBuilderErrors(BuilderResult[] results) {
127: FormStylePopup pop = new FormStylePopup(
128: "images/package_builder.png", "Validation results");
129: if (results == null || results.length == 0) {
130: pop
131: .addRow(new HTML(
132: "<img src='images/tick_green.gif'/><i>Rule built successfully.</i>"));
133: } else {
134: FlexTable errTable = new FlexTable();
135: errTable.setStyleName("build-Results");
136: for (int i = 0; i < results.length; i++) {
137: int row = i;
138: final BuilderResult res = results[i];
139: errTable.setWidget(row, 0,
140: new Image("images/error.gif"));
141: if (res.assetFormat.equals("package")) {
142: errTable.setText(row, 1,
143: "[package configuration problem] "
144: + res.message);
145: } else {
146: errTable.setText(row, 1, res.message);
147: }
148:
149: }
150: ScrollPanel scroll = new ScrollPanel(errTable);
151: //scroll.setAlwaysShowScrollBars(true);
152: //scroll.setSize("100%","25em");
153: scroll.setWidth("100%");
154: //scroll.setScrollPosition( 100 );
155: //errTable.setWidth( "60%" );
156: pop.addRow(scroll);
157: // pop.setWidth( "70%" );
158: // pop.setHeight( "50%" );
159:
160: }
161: pop.setPopupPosition(100, 100);
162: pop.show();
163: LoadingPopup.close();
164: }
165:
166: }
|