View Javadoc
1   /*
2    * Copyright 2000-2013 Vaadin Ltd.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  
17  package com.vaadin.client.ui.upload;
18  
19  import com.google.gwt.core.client.Scheduler;
20  import com.google.gwt.core.client.Scheduler.RepeatingCommand;
21  import com.google.gwt.dom.client.Style;
22  import com.google.gwt.dom.client.Style.Unit;
23  import com.vaadin.client.ApplicationConnection;
24  import com.vaadin.client.Paintable;
25  import com.vaadin.client.UIDL;
26  import com.vaadin.client.ui.AbstractComponentConnector;
27  import com.vaadin.client.ui.VUpload;
28  import com.vaadin.shared.ui.Connect;
29  import com.vaadin.ui.Upload;
30  
31  @Connect(Upload.class)
32  public class UploadConnector extends AbstractComponentConnector implements
33          Paintable {
34  
35      @Override
36      public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
37          if (!isRealUpdate(uidl)) {
38              return;
39          }
40          if (uidl.hasAttribute("notStarted")) {
41              getWidget().t.schedule(400);
42              return;
43          }
44          if (uidl.hasAttribute("forceSubmit")) {
45              getWidget().submit();
46              return;
47          }
48          getWidget().setImmediate(getState().immediate);
49          getWidget().client = client;
50          getWidget().paintableId = uidl.getId();
51          getWidget().nextUploadId = uidl.getIntAttribute("nextid");
52          final String action = client.translateVaadinUri(uidl
53                  .getStringVariable("action"));
54          getWidget().element.setAction(action);
55          if (uidl.hasAttribute("buttoncaption")) {
56              final String caption = uidl.getStringAttribute("buttoncaption");
57  
58              // make sure button doesn't end with 0 width.
59              getWidget().submitButton.setText(caption);
60              Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
61  
62                  @Override
63                  public boolean execute() {
64                      Style style = getWidget().submitButton.getElement().getStyle();
65                      if (style.getWidth().isEmpty()) {
66                          style.setWidth(1, Unit.PX);
67                      } else {
68                          style.clearWidth();
69                      }
70                      return getWidget().submitButton.getOffsetWidth() < 25;
71                  }
72              }, 100);
73              getWidget().submitButton.setVisible(true);
74          } else {
75              getWidget().submitButton.setVisible(false);
76          }
77          getWidget().fu.setName(getWidget().paintableId + "_file");
78  
79          if (!isEnabled() || isReadOnly()) {
80              getWidget().disableUpload();
81          } else if (!uidl.getBooleanAttribute("state")) {
82              // Enable the button only if an upload is not in progress
83              getWidget().enableUpload();
84              getWidget().ensureTargetFrame();
85          }
86      }
87  
88      @Override
89      public VUpload getWidget() {
90          return (VUpload) super.getWidget();
91      }
92  }