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.categorynav.CategoryExplorerWidget;
020: import org.drools.brms.client.categorynav.CategorySelectHandler;
021: import org.drools.brms.client.common.AssetFormats;
022: import org.drools.brms.client.common.FormStylePopup;
023: import org.drools.brms.client.common.GenericCallback;
024: import org.drools.brms.client.common.ImageButton;
025: import org.drools.brms.client.common.LoadingPopup;
026: import org.drools.brms.client.common.RulePackageSelector;
027: import org.drools.brms.client.common.WarningPopup;
028: import org.drools.brms.client.rpc.RepositoryServiceFactory;
029: import org.drools.brms.client.rulelist.EditItemEvent;
030:
031: import com.google.gwt.user.client.ui.Button;
032: import com.google.gwt.user.client.ui.ClickListener;
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.Label;
036: import com.google.gwt.user.client.ui.ListBox;
037: import com.google.gwt.user.client.ui.TextArea;
038: import com.google.gwt.user.client.ui.TextBox;
039: import com.google.gwt.user.client.ui.Widget;
040:
041: /**
042: * This provides a popup for creating a new rule/asset from scratch.
043: * reuses a few other widgets.
044: */
045: public class NewAssetWizard extends FormStylePopup {
046:
047: private TextBox name = new TextBox();
048: private TextArea description = new TextArea();
049: private String initialCategory;
050:
051: private ListBox formatChooser = getFormatChooser();
052:
053: private RulePackageSelector packageSelector = new RulePackageSelector();
054: private EditItemEvent afterCreate;
055: private boolean showCats;
056: private String format;
057:
058: /** This is used when creating a new rule. */
059: public NewAssetWizard(EditItemEvent afterCreate, boolean showCats,
060: String format, String title) {
061: super ("images/new_wiz.gif", title);
062: this .showCats = showCats;
063: this .format = format;
064:
065: this .afterCreate = afterCreate;
066:
067: addAttribute("Name:", name);
068:
069: if (showCats) {
070: addAttribute("Initial category:", getCatChooser());
071: }
072:
073: if (format == null) {
074: addAttribute("Type (format) of rule:", this .formatChooser);
075: }
076:
077: addAttribute("Package:", packageSelector);
078:
079: description.setVisibleLines(4);
080: description.setWidth("100%");
081: addAttribute("Initial description:", description);
082:
083: Button ok = new Button("OK");
084: ok.addClickListener(new ClickListener() {
085: public void onClick(Widget arg0) {
086: ok();
087: }
088:
089: });
090:
091: addAttribute("", ok);
092:
093: setStyleName("ks-popups-Popup");
094: }
095:
096: /**
097: * This will create a new asset wizard with the given preselected package.
098: */
099: public NewAssetWizard(EditItemEvent event, boolean showCategories,
100: String format2, String title,
101: String currentlySelectedPackage) {
102: this (event, showCategories, format2, title);
103: packageSelector.selectPackage(currentlySelectedPackage);
104:
105: }
106:
107: private Widget getCatChooser() {
108: return new CategoryExplorerWidget(new CategorySelectHandler() {
109: public void selected(String selectedPath) {
110: initialCategory = selectedPath;
111: }
112: });
113: }
114:
115: private ListBox getFormatChooser() {
116:
117: ListBox box = new ListBox();
118:
119: box.addItem("Business rule (using guided editor)",
120: AssetFormats.BUSINESS_RULE);
121: box.addItem("DRL rule (technical rule - text editor)",
122: AssetFormats.DRL);
123: box.addItem("Business rule using a DSL (text editor)",
124: AssetFormats.DSL_TEMPLATE_RULE);
125: box.addItem("Decision table (spreadsheet)",
126: AssetFormats.DECISION_SPREADSHEET_XLS);
127:
128: box.setSelectedIndex(0);
129:
130: return box;
131: }
132:
133: /**
134: * When OK is pressed, it will update the repository with the new rule.
135: */
136: void ok() {
137:
138: if (this .showCats && this .initialCategory == null) {
139: WarningPopup.showMessage(
140: "You have to pick an initial category.", this
141: .getAbsoluteLeft(), this .getAbsoluteTop());
142: return;
143: } else if (this .name.getText() == null
144: || "".equals(this .name.getText())) {
145: WarningPopup.showMessage("Rule must have a name", this
146: .getAbsoluteLeft(), this .getAbsoluteTop());
147: return;
148: }
149:
150: GenericCallback cb = new GenericCallback() {
151: public void onSuccess(Object result) {
152: openEditor((String) result);
153: hide();
154: }
155: };
156:
157: LoadingPopup.showMessage("Please wait ...");
158: RepositoryServiceFactory.getService().createNewRule(
159: name.getText(), description.getText(), initialCategory,
160: packageSelector.getSelectedPackage(), getFormat(), cb);
161:
162: }
163:
164: private String getFormat() {
165: if (format != null)
166: return format;
167: return formatChooser.getValue(formatChooser.getSelectedIndex());
168: }
169:
170: /**
171: * After creating the item we open it in the editor.
172: * @param uuid
173: */
174: protected void openEditor(String uuid) {
175: afterCreate.open(uuid);
176: }
177:
178: }
|