PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/v3.2/nimbits-tds/src/com/nimbits/client/panels/DiagramUploadPanel.java

http://nimbits-server.googlecode.com/
Java | 187 lines | 123 code | 40 blank | 24 comment | 8 complexity | ad82feb37567beb4661da271a0344c3e MD5 | raw file
  1/*
  2 * Copyright (c) 2010 Tonic Solutions LLC.
  3 *
  4 * http://www.nimbits.com
  5 *
  6 *
  7 * Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  8 *
  9 * http://www.gnu.org/licenses/gpl.html
 10 *
 11 * Unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 12 */
 13
 14package com.nimbits.client.panels;
 15
 16import com.extjs.gxt.ui.client.event.*;
 17import com.extjs.gxt.ui.client.widget.LayoutContainer;
 18import com.extjs.gxt.ui.client.widget.button.Button;
 19import com.extjs.gxt.ui.client.widget.form.FileUploadField;
 20import com.extjs.gxt.ui.client.widget.form.FormPanel;
 21import com.extjs.gxt.ui.client.widget.form.FormPanel.Encoding;
 22import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
 23import com.extjs.gxt.ui.client.widget.form.HiddenField;
 24import com.extjs.gxt.ui.client.widget.form.TextField;
 25import com.google.gwt.core.client.GWT;
 26import com.google.gwt.user.client.Element;
 27import com.google.gwt.user.client.Window;
 28import com.google.gwt.user.client.rpc.AsyncCallback;
 29import com.nimbits.client.exception.NimbitsException;
 30import com.nimbits.client.model.Const;
 31import com.nimbits.client.model.diagram.Diagram;
 32import com.nimbits.client.service.diagram.DiagramService;
 33import com.nimbits.client.service.diagram.DiagramServiceAsync;
 34
 35import java.util.ArrayList;
 36import java.util.List;
 37
 38import static com.nimbits.shared.Utils.isEmptyString;
 39
 40/**
 41 * Created by bsautner
 42 * User: benjamin
 43 * Date: 5/20/11
 44 * Time: 3:28 PM
 45 */
 46public class DiagramUploadPanel extends LayoutContainer {
 47
 48    private final List<DiagramAddedListener> DiagramAddedListeners = new ArrayList<DiagramAddedListener>();
 49    private Diagram diagram;
 50
 51    public DiagramUploadPanel(UploadType uploadType) {
 52        this.uploadType = uploadType;
 53    }
 54
 55    public DiagramUploadPanel(UploadType uploadType, Diagram diagram) {
 56        this.uploadType = uploadType;
 57        this.diagram = diagram;
 58
 59    }
 60
 61    public enum UploadType {
 62        newFile, updatedFile
 63    }
 64
 65    private final UploadType uploadType;
 66
 67
 68    public interface DiagramAddedListener {
 69        void onDiagramAdded() throws NimbitsException;
 70
 71    }
 72
 73    public void addDiagramAddedListeners(final DiagramAddedListener listener) {
 74        DiagramAddedListeners.add(listener);
 75    }
 76
 77    void notifyDiagramAddedListener() throws NimbitsException {
 78        for (DiagramAddedListener DiagramAddedListener : DiagramAddedListeners) {
 79            DiagramAddedListener.onDiagramAdded();
 80        }
 81    }
 82
 83    @Override
 84    protected void onRender(Element parent, int index) {
 85        super.onRender(parent, index);
 86        setStyleAttribute("margin", "10px");
 87
 88        final FormPanel panel = new FormPanel();
 89        panel.addListener(Events.Submit, new Listener<FormEvent>() {
 90
 91
 92            @Override
 93            public void handleEvent(FormEvent formEvent) {
 94                try {
 95                    notifyDiagramAddedListener();
 96                } catch (NimbitsException ignored) {
 97
 98                }
 99            }
100        });
101        panel.setHeaderVisible(false);
102        panel.setFrame(false);
103
104        DiagramServiceAsync diagramService = GWT.create(DiagramService.class);
105        //  diagramService.getBlobStoreUrl("http://" + Window.Location.getHost() +  "/service/diagram", new AsyncCallback<String>() {
106        diagramService.getBlobStoreUrl("/service/diagram", new AsyncCallback<String>() {
107            @Override
108            public void onFailure(Throwable throwable) {
109
110            }
111
112            @Override
113            public void onSuccess(String s) {
114                panel.setAction(s);
115            }
116        });
117
118        panel.setAction("http://" + Window.Location.getHost() + "/service/diagram");
119        panel.setEncoding(Encoding.MULTIPART);
120        panel.setMethod(Method.POST);
121        //  panel.setButtonAlign(HorizontalAlignment.CENTER);
122        panel.setWidth(350);
123
124        final TextField<String> name = new TextField<String>();
125
126        name.setFieldLabel(Const.WORD_NAME);
127        name.setName(Const.PARAM_NAME);
128        panel.add(name);
129
130
131        final FileUploadField file = new FileUploadField();
132        file.setAllowBlank(false);
133        file.setName("myFile");
134        file.setFieldLabel("File");
135        panel.add(file);
136
137
138        final HiddenField<UploadType> uploadTypeHiddenField = new HiddenField<UploadType>();
139        uploadTypeHiddenField.setName(Const.PARAM_UPLOAD_TYPE_HIDDEN_FIELD);
140        uploadTypeHiddenField.setValue(uploadType);
141        panel.add(uploadTypeHiddenField);
142        if (uploadType == UploadType.updatedFile && diagram != null) {
143            final HiddenField<Long> diagramId = new HiddenField<Long>();
144            diagramId.setName(Const.PARAM_DIAGRAM_ID);
145            diagramId.setValue(diagram.getId());
146            panel.add(diagramId);
147            name.setValue(diagram.getName().getValue());
148            name.setReadOnly(true);
149            name.setVisible(false);
150        }
151
152
153        final Button btn = new Button("Reset");
154        btn.addSelectionListener(new SelectionListener<ButtonEvent>() {
155            @Override
156            public void componentSelected(ButtonEvent ce) {
157                panel.reset();
158            }
159        });
160        panel.addButton(btn);
161
162        final Button submitBtn = new Button("Submit");
163        submitBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
164            @Override
165            public void componentSelected(ButtonEvent ce) {
166                if (!panel.isValid()) {
167                    return;
168                }
169                // normally would submit the form but for example no server set up to
170                // handle the post
171                if (!isEmptyString(name.getValue()) && file.getValue().toLowerCase().endsWith(".svg")) {
172                    panel.submit();
173
174                }
175
176
177                //
178                // notifyDiagramAddedListener();
179
180            }
181        });
182        panel.addButton(submitBtn);
183
184        add(panel);
185    }
186
187}