001: package org.drools.brms.client.packages;
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.ErrorPopup;
020: import org.drools.brms.client.common.FormStyleLayout;
021: import org.drools.brms.client.common.FormStylePopup;
022: import org.drools.brms.client.common.GenericCallback;
023: import org.drools.brms.client.common.HTMLFileManagerFields;
024: import org.drools.brms.client.common.LoadingPopup;
025: import org.drools.brms.client.rpc.RepositoryServiceFactory;
026:
027: import com.google.gwt.core.client.GWT;
028: import com.google.gwt.user.client.Command;
029: import com.google.gwt.user.client.Window;
030: import com.google.gwt.user.client.ui.AbsolutePanel;
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.FileUpload;
034: import com.google.gwt.user.client.ui.FormHandler;
035: import com.google.gwt.user.client.ui.FormPanel;
036: import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
037: import com.google.gwt.user.client.ui.FormSubmitEvent;
038: import com.google.gwt.user.client.ui.HTML;
039: import com.google.gwt.user.client.ui.HorizontalPanel;
040: import com.google.gwt.user.client.ui.KeyboardListener;
041: import com.google.gwt.user.client.ui.RadioButton;
042: import com.google.gwt.user.client.ui.TextArea;
043: import com.google.gwt.user.client.ui.TextBox;
044: import com.google.gwt.user.client.ui.Widget;
045:
046: /**
047: * This is the wizard used when creating new packages or importing them.
048: *
049: * @author Michael Neale
050: */
051: public class NewPackageWizard extends FormStylePopup {
052:
053: private TextBox nameBox;
054: private TextArea descBox;
055: private final FormStyleLayout importLayout = new FormStyleLayout();
056: private final FormStyleLayout newPackageLayout = new FormStyleLayout();
057: private Command afterCreatedEvent;
058:
059: public NewPackageWizard(final Command afterCreatedEvent) {
060: super ("images/new_wiz.gif", "Create a new package");
061: nameBox = new TextBox();
062: descBox = new TextArea();
063:
064: this .afterCreatedEvent = afterCreatedEvent;
065:
066: newPackageLayout
067: .addRow(new HTML(
068: "<i><small>Create a new package in the BRMS</small></i>"));
069: importLayout
070: .addRow(new HTML(
071: "<i><small>Importing a package from an existing DRL will create the package in the BRMS if it "
072: + "does not already exist. If it does exist, any new rules found will be merged into the BRMS package.</small></i>"));
073: importLayout
074: .addRow(new HTML(
075: "<i><small>Any new rules created will not have any categories assigned initially, "
076: + "but rules will be stored individually (ie normalised). Functions, queries, imports etc will show up in the package configuration.</small></i>"));
077: importLayout
078: .addRow(new HTML(
079: "<i><small>Any DSLs or models required by the imported package will need to be uploaded seperately.</small></i>"));
080:
081: newPackageLayout.addAttribute("Name:", nameBox);
082: newPackageLayout.addAttribute("Description:", descBox);
083:
084: nameBox
085: .setTitle("The name of the package. Avoid spaces, use underscore instead.");
086:
087: RadioButton newPackage = new RadioButton("action",
088: "Create new package");
089: RadioButton importPackage = new RadioButton("action",
090: "Import from drl file");
091: newPackage.setChecked(true);
092: newPackageLayout.setVisible(true);
093:
094: newPackage.addClickListener(new ClickListener() {
095: public void onClick(Widget w) {
096: newPackageLayout.setVisible(true);
097: importLayout.setVisible(false);
098: }
099: });
100:
101: importLayout.setVisible(false);
102:
103: importPackage.addClickListener(new ClickListener() {
104: public void onClick(Widget arg0) {
105: newPackageLayout.setVisible(false);
106: importLayout.setVisible(true);
107: }
108: });
109: AbsolutePanel ab = new AbsolutePanel();
110: ab.add(newPackage);
111: ab.add(importPackage);
112: addRow(ab);
113:
114: addRow(newPackageLayout);
115: addRow(importLayout);
116:
117: importLayout.addAttribute("DRL file to import:",
118: newImportWidget());
119:
120: Button create = new Button("Create package");
121: create.addClickListener(new ClickListener() {
122: public void onClick(Widget w) {
123: if (validatePackageName(nameBox.getText())) {
124: createPackageAction(nameBox.getText(), descBox
125: .getText(), afterCreatedEvent);
126: hide();
127: } else {
128: nameBox.setText("");
129: Window
130: .alert("Invalid package name, use java-style package name");
131: }
132: }
133:
134: private boolean validatePackageName(String text) {
135: return text.matches("[a-zA-Z\\.]*");
136: }
137: });
138:
139: newPackageLayout.addAttribute("", create);
140:
141: setStyleName("ks-popups-Popup");
142:
143: }
144:
145: private void createPackageAction(final String name,
146: final String descr, final Command refresh) {
147: LoadingPopup.showMessage("Creating package - please wait...");
148: RepositoryServiceFactory.getService().createPackage(name,
149: descr, new GenericCallback() {
150: public void onSuccess(Object data) {
151: LoadingPopup.close();
152: refresh.execute();
153: }
154: });
155: }
156:
157: private Widget newImportWidget() {
158:
159: final FormPanel uploadFormPanel = new FormPanel();
160: uploadFormPanel.setAction(GWT.getModuleBaseURL() + "package");
161: uploadFormPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
162: uploadFormPanel.setMethod(FormPanel.METHOD_POST);
163:
164: HorizontalPanel panel = new HorizontalPanel();
165: uploadFormPanel.setWidget(panel);
166:
167: final FileUpload upload = new FileUpload();
168: upload.setName(HTMLFileManagerFields.CLASSIC_DRL_IMPORT);
169: panel.add(upload);
170:
171: panel.add(new Button("Import", new ClickListener() {
172: public void onClick(Widget sender) {
173: if (Window
174: .confirm("Are you sure you want to import this package? If the package already exists in the BRMS it will be merged.")) {
175: LoadingPopup
176: .showMessage("Importing drl package, please wait, as this could take some time...");
177: uploadFormPanel.submit();
178: }
179: }
180:
181: }));
182:
183: uploadFormPanel.addFormHandler(new FormHandler() {
184: public void onSubmitComplete(FormSubmitCompleteEvent event) {
185: if (event.getResults().indexOf("OK") > -1) {
186: Window.alert("Package was imported successfully. ");
187: afterCreatedEvent.execute();
188: hide();
189: } else {
190: ErrorPopup
191: .showMessage("Unable to import into the package. ["
192: + event.getResults() + "]");
193: }
194: LoadingPopup.close();
195: }
196:
197: public void onSubmit(FormSubmitEvent event) {
198: if (upload.getFilename().length() == 0) {
199: Window
200: .alert("You did not choose a drl file to import !");
201: event.setCancelled(true);
202: } else if (!upload.getFilename().endsWith(".drl")) {
203: Window.alert("You can only import '.drl' files.");
204: event.setCancelled(true);
205: }
206:
207: }
208: });
209:
210: return uploadFormPanel;
211: }
212:
213: }
|