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.HTMLFileManagerFields;
022: import org.drools.brms.client.rpc.RuleAsset;
023: import org.drools.brms.client.ruleeditor.RuleViewer;
024:
025: import com.google.gwt.core.client.GWT;
026: import com.google.gwt.user.client.Command;
027: import com.google.gwt.user.client.DeferredCommand;
028: import com.google.gwt.user.client.Window;
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.Composite;
032: import com.google.gwt.user.client.ui.FileUpload;
033: import com.google.gwt.user.client.ui.FormHandler;
034: import com.google.gwt.user.client.ui.FormPanel;
035: import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
036: import com.google.gwt.user.client.ui.FormSubmitEvent;
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.TextBox;
041: import com.google.gwt.user.client.ui.Widget;
042:
043: /**
044: * This wraps a file uploader utility for model packages.
045: * Model packages are jar files.
046: *
047: * @author Michael Neale
048: * @author Fernando Meyer
049: */
050:
051: public abstract class AssetAttachmentFileWidget extends Composite {
052:
053: private FormPanel form;
054: private Button ok;
055: private HorizontalPanel busy;
056: private RuleViewer viewer;
057: private FormStyleLayout layout;
058: private RuleAsset asset;
059:
060: public AssetAttachmentFileWidget(final RuleAsset asset,
061: final RuleViewer viewer) {
062: this .viewer = viewer;
063: this .asset = asset;
064: initWidgets(asset.uuid, asset.metaData.name);
065: initAssetHandlers();
066: }
067:
068: protected void initWidgets(final String uuid, String formName) {
069: form = new FormPanel();
070: form.setAction(GWT.getModuleBaseURL() + "asset");
071: form.setEncoding(FormPanel.ENCODING_MULTIPART);
072: form.setMethod(FormPanel.METHOD_POST);
073:
074: FileUpload up = new FileUpload();
075: up.setName(HTMLFileManagerFields.UPLOAD_FIELD_NAME_ATTACH);
076: HorizontalPanel fields = new HorizontalPanel();
077: fields.add(getHiddenField(
078: HTMLFileManagerFields.FORM_FIELD_UUID, uuid));
079:
080: ok = new Button("Upload");
081:
082: fields.add(up);
083: fields.add(ok);
084:
085: form.add(fields);
086:
087: layout = new FormStyleLayout(getIcon(), formName);
088:
089: if (!this .asset.isreadonly)
090: layout.addAttribute("Upload new version:", form);
091:
092: Button dl = new Button("Download");
093: dl.addClickListener(new ClickListener() {
094: public void onClick(Widget w) {
095: Window.open(GWT.getModuleBaseURL() + "asset?"
096: + HTMLFileManagerFields.FORM_FIELD_UUID + "="
097: + uuid, "downloading...",
098: "resizable=no,scrollbars=yes,status=no");
099: }
100: });
101: layout.addAttribute("Download current version:", dl);
102:
103: busy = new HorizontalPanel();
104: busy.setVisible(false);
105: busy.add(new Label("Uploading file..."));
106: busy.add(new Image("images/spinner.gif"));
107:
108: layout.addRow(busy);
109: ok.addClickListener(new ClickListener() {
110: public void onClick(Widget w) {
111: showUploadingBusy();
112: submitUpload();
113: }
114: });
115:
116: initWidget(layout);
117: layout.setWidth("100%");
118: this .setStyleName(getOverallStyleName());
119: }
120:
121: /**
122: * @return The path to the icon to use.
123: */
124: public abstract String getIcon();
125:
126: /**
127: * return the overall style name to use.
128: */
129: public abstract String getOverallStyleName();
130:
131: void initAssetHandlers() {
132: form.addFormHandler(new FormHandler() {
133:
134: public void onSubmit(FormSubmitEvent ev) {
135: }
136:
137: public void onSubmitComplete(FormSubmitCompleteEvent ev) {
138: if (ev.getResults().indexOf("OK") > -1) {
139: viewer.refreshDataAndView();
140: } else {
141: ErrorPopup
142: .showMessage("Unable to upload the file.");
143: }
144: }
145:
146: });
147: }
148:
149: protected void submitUpload() {
150: DeferredCommand.add(new Command() {
151: public void execute() {
152: form.submit();
153: }
154: });
155: }
156:
157: protected void showUploadingBusy() {
158: this .ok.setVisible(false);
159: this .form.setVisible(false);
160: this .busy.setVisible(true);
161: }
162:
163: private TextBox getHiddenField(String name, String value) {
164: TextBox t = new TextBox();
165: t.setName(name);
166: t.setText(value);
167: t.setVisible(false);
168: return t;
169: }
170:
171: public void addDescription(Widget d) {
172: this.layout.addRow(d);
173:
174: }
175:
176: }
|