001: package com.bostechcorp.cbesb.console.client;
002:
003: import com.bostechcorp.cbesb.console.client.dialogs.FileUploadDialog;
004: import com.bostechcorp.cbesb.console.client.dialogs.MessageViewDialog;
005: import com.bostechcorp.cbesb.console.i18n.ConsoleMessages;
006: import com.google.gwt.core.client.GWT;
007: import com.google.gwt.user.client.ui.Button;
008: import com.google.gwt.user.client.ui.ClickListener;
009: import com.google.gwt.user.client.ui.Composite;
010: import com.google.gwt.user.client.ui.DialogBox;
011: import com.google.gwt.user.client.ui.FileUpload;
012: import com.google.gwt.user.client.ui.FormHandler;
013: import com.google.gwt.user.client.ui.FormPanel;
014: import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
015: import com.google.gwt.user.client.ui.FormSubmitEvent;
016: import com.google.gwt.user.client.ui.TextBox;
017: import com.google.gwt.user.client.ui.VerticalPanel;
018: import com.google.gwt.user.client.ui.Widget;
019:
020: public class FileUploadPanel extends Composite {
021: public static String ACTION_COMPONENT = "uploadComponent";
022:
023: public static String ACTION_ASSEMBLY = "uploadAssembly";
024:
025: public static String LICENSE_IMPORT = "uploadLicense";
026: final TextBox pathText = new TextBox();
027:
028: final FormPanel form = new FormPanel();
029:
030: Admin admin = null;
031:
032: DialogBox parent = null;
033: FileUpload upload;
034: final VerticalPanel panel = new VerticalPanel();
035:
036: public FileUploadPanel(final Admin admin, String actionType) {
037: this (admin, actionType, null);
038: }
039:
040: public FileUploadPanel(final Admin admin, String actionType,
041: DialogBox parentDialog) {
042:
043: // Create a FormPanel and point it at a service.
044: this .admin = admin;
045: this .parent = parentDialog;
046:
047: if (actionType.equals(ACTION_COMPONENT))
048: form.setAction(GWT.getModuleBaseURL() + "/uploadComponent");
049: else if (actionType.equals(ACTION_ASSEMBLY))
050: form.setAction(GWT.getModuleBaseURL() + "/uploadAssembly");
051: else if (actionType.equals(LICENSE_IMPORT))
052: form.setAction(GWT.getModuleBaseURL() + "/uploadLicense");
053: // Because we're going to add a FileUpload widget, we'll need to set the
054: // form to use the POST method, and multipart MIME encoding.
055: form.setEncoding(FormPanel.ENCODING_MULTIPART);
056: form.setMethod(FormPanel.METHOD_POST);
057:
058: // Create a panel to hold all of the form widgets.
059:
060: form.setWidget(panel);
061: panel.setWidth("300");
062: // panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
063: // Create a FileUpload widget.
064: reloadWidgets();
065:
066: // Add an event handler to the form.
067: form.addFormHandler(new FormHandler() {
068: public void onSubmitComplete(FormSubmitCompleteEvent event) {
069: // When the form submission is successfully completed, this
070: // event is
071: // fired. Assuming the service returned a response of type
072: // text/plain,
073: // we can get the result text here (see the FormPanel
074: // documentation for
075: // further explanation).
076: String result = event.getResults();
077: if (result != null) {
078: /*if(result.startsWith(((ConsoleMessages)GWT.create(ConsoleMessages.class)).Submit()))
079: Window.alert(result);*/
080: if (!result.startsWith(((ConsoleMessages) GWT
081: .create(ConsoleMessages.class)).Error())) {
082: if (form.getAction().equalsIgnoreCase(
083: GWT.getModuleBaseURL()
084: + "/uploadLicense")) {
085:
086: admin.refreshImport();
087: } else {
088: if (!result.trim().equalsIgnoreCase(""))
089: MessageViewDialog.showInfo(result);
090: //Window.alert(result);
091: admin.refresh();
092: }
093: }
094: }
095: if (parent != null) {
096: parent.hide();
097: }
098:
099: panel.clear();
100: reloadWidgets();
101:
102: pathText.setText("");
103:
104: }
105:
106: public void onSubmit(FormSubmitEvent event) {
107: // This event is fired just before the form is submitted. We can
108: // take
109: // this opportunity to perform validation.
110: // if (tb.getText().length() == 0) {
111: // Window.alert("The text box must not be empty");
112: // event.setCancelled(true);
113: // }
114: }
115: });
116: initWidget(form);
117: }
118:
119: private void reloadWidgets() {
120: upload = new FileUpload();
121: upload.setName(((ConsoleMessages) GWT
122: .create(ConsoleMessages.class)).uploadFormElement());
123: panel.add(upload);
124: upload.setWidth("100%");
125: // Add a 'submit' button.
126: panel.add(new Button(((ConsoleMessages) GWT
127: .create(ConsoleMessages.class)).Submit(),
128: new ClickListener() {
129: public void onClick(Widget sender) {
130: // if(pathText.getText().equals("")){
131: // return;
132: // //form.getTitle()
133: // }
134: form.submit();
135: recordFileName();
136: }
137: }));
138: }
139:
140: public void recordFileName() {
141:
142: if (parent != null)
143: ((FileUploadDialog) parent).setFileName(upload
144: .getFilename());
145: }
146:
147: }
|