]> source.dussan.org Git - vaadin-framework.git/commitdiff
small changes
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Fri, 8 Jun 2007 13:02:44 +0000 (13:02 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Fri, 8 Jun 2007 13:02:44 +0000 (13:02 +0000)
svn changeset:1656/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/Client.java
src/com/itmill/toolkit/terminal/gwt/client/ui/TkSelect.java

index 4be56306d3c94fbf86ffd8d05602bf178c0bccc1..3eccc4932c8827033e037625e8c3ddae83221fd7 100755 (executable)
@@ -103,49 +103,44 @@ public class Client implements EntryPoint {
        private void handleReceivedJSONMessage(Response response) {
                Date start = new Date();
                
-               try{
-                       console.log(response.getText().substring(3) + "}");
-                       JSONValue json = JSONParser
-                       .parse(response.getText().substring(3) + "}");
-                       // Process changes
-                       JSONArray changes = (JSONArray) ((JSONObject) json).get("changes");
-                       for (int i = 0; i < changes.size(); i++) {
+               console.log(response.getText().substring(3) + "}");
+               JSONValue json = JSONParser
+               .parse(response.getText().substring(3) + "}");
+               // Process changes
+               JSONArray changes = (JSONArray) ((JSONObject) json).get("changes");
+               for (int i = 0; i < changes.size(); i++) {
+                       try {
+                               UIDL change = new UIDL((JSONArray) changes.get(i));
+                               console.log("Received the following change: ");
                                try {
-                                       UIDL change = new UIDL((JSONArray) changes.get(i));
-                                       console.log("Received the following change: ");
-                                       try {
-                                               console.dirUIDL(change);
-                                       } catch (Exception e) {
-                                               // TODO: dir doesn't work in any browser although it should work (works in hosted mode)
-                                               // it partially did at some part but now broken.
-                                       }
-                                       UIDL uidl = change.getChildUIDL(0);
-                                       Paintable paintable = getPaintable(uidl.getId());
-                                       if (paintable != null)
-                                               paintable.updateFromUIDL(uidl, this);
-                                       else {
-                                               if (!uidl.getTag().equals("window"))
-                                                       throw new IllegalStateException("Received update for "
-                                                                       + uidl.getTag()
-                                                                       + ", but there is no such paintable ("
-                                                                       + uidl.getId() + ") registered yet.");
-                                               Widget window = createWidgetFromUIDL(uidl);
-                                               // We should also handle other windows 
-                                               RootPanel.get("itmtk-ajax-window").add(window);
-                                       }
-       
-                               } catch (Throwable e) {
-                                       e.printStackTrace();
+                                       console.dirUIDL(change);
+                               } catch (Exception e) {
+                                       console.log(e.getMessage());
+                                       // TODO: dir doesn't work in any browser although it should work (works in hosted mode)
+                                       // it partially did at some part but now broken.
                                }
-       
+                               UIDL uidl = change.getChildUIDL(0);
+                               Paintable paintable = getPaintable(uidl.getId());
+                               if (paintable != null)
+                                       paintable.updateFromUIDL(uidl, this);
+                               else {
+                                       if (!uidl.getTag().equals("window"))
+                                               throw new IllegalStateException("Received update for "
+                                                               + uidl.getTag()
+                                                               + ", but there is no such paintable ("
+                                                               + uidl.getId() + ") registered yet.");
+                                       Widget window = createWidgetFromUIDL(uidl);
+                                       // We should also handle other windows 
+                                       RootPanel.get("itmtk-ajax-window").add(window);
+                               }
+
+                       } catch (Throwable e) {
+                               e.printStackTrace();
                        }
-                       long prosessingTime = (new Date().getTime()) - start.getTime();
-                       console.log(" Processing time was " + String.valueOf(prosessingTime));
-               } catch (Exception e) {
-                       // TODO: remove this try catch when uidl JSON stabilizes
-                       console.log("Error parsing JSON:");
-                       console.log(response.getText().substring(3) + "}");
+
                }
+               long prosessingTime = (new Date().getTime()) - start.getTime();
+               console.log(" Processing time was " + String.valueOf(prosessingTime));
 
        }
 
@@ -216,6 +211,16 @@ public class Client implements EntryPoint {
                addVariableToQueue(paintableId, variableName, newValue ? "true"
                                : "false", immediate);
        }
+       public void updateVariable(String paintableId, String variableName, Object[] values, boolean immediate) {
+               StringBuffer buf = new StringBuffer();
+               for (int i = 0; i < values.length; i++) {
+                       if(i > 0)
+                               buf.append(",");
+                       buf.append(escapeString(values[i].toString()));
+               }
+               addVariableToQueue(paintableId, variableName, buf.toString(), immediate);
+       }
+
 
        public WidgetFactory getWidgetFactory() {
                return widgetFactory;
@@ -225,4 +230,5 @@ public class Client implements EntryPoint {
                this.widgetFactory = widgetFactory;
        }
 
+
 }
index 91c8fdc222b1d2022416590b187c9e3408f597fd..95b9d7b03db35f052c4cd8f1fb71a8dcf6169113 100644 (file)
@@ -1,38 +1,69 @@
 package com.itmill.toolkit.terminal.gwt.client.ui;
 
 import java.util.Iterator;
+import java.util.Vector;
 
+import com.google.gwt.user.client.ui.ChangeListener;
 import com.google.gwt.user.client.ui.Composite;
 import com.google.gwt.user.client.ui.Label;
 import com.google.gwt.user.client.ui.ListBox;
-import com.google.gwt.user.client.ui.TreeItem;
 import com.google.gwt.user.client.ui.VerticalPanel;
-
+import com.google.gwt.user.client.ui.Widget;
 import com.itmill.toolkit.terminal.gwt.client.Client;
 import com.itmill.toolkit.terminal.gwt.client.Paintable;
 import com.itmill.toolkit.terminal.gwt.client.UIDL;
 
-public class TkSelect extends Composite implements Paintable {
+public class TkSelect extends Composite implements Paintable, ChangeListener {
        
        Label caption = new Label();
        ListBox select = new ListBox();
+       private Client client;
+       private String id;
+       private boolean immediate;
+       
        
        public TkSelect() {
                VerticalPanel panel = new VerticalPanel();
                panel.add(caption);
                panel.add(select);
+               select.addChangeListener(this);
                initWidget(panel);
        }
 
        public void updateFromUIDL(UIDL uidl, Client client) {
+               this.client = client;
+               this.id = uidl.getStringAttribute("id");
+               this.immediate = uidl.getBooleanAttribute("immediate");
                
                if (uidl.hasAttribute("caption")) caption.setText(uidl.getStringAttribute("caption")); 
+               
+               if(uidl.hasAttribute("selectmode"))
+                       select.setMultipleSelect(true);
+               else
+                       select.setMultipleSelect(false);
 
                UIDL options = uidl.getChildUIDL(0);
                
+               select.clear();
                for (Iterator i = options.getChildIterator(); i.hasNext();) {
                        UIDL optionUidl = (UIDL)i.next();
                        select.addItem(optionUidl.getStringAttribute("caption"), optionUidl.getStringAttribute("key"));
+                       if(optionUidl.hasAttribute("selected"))
+                               select.setItemSelected(select.getItemCount()-1, true);
+               }
+       }
+
+       public void onChange(Widget sender) {
+               if(select.isMultipleSelect()) {
+                       Vector selectedItemKeys = new Vector();
+                       for(int i = 0; i < select.getItemCount();i++) {
+                               if(select.isItemSelected(i))
+                                       selectedItemKeys.add(select.getValue(i));
+                       }
+                       Object[] values = selectedItemKeys.toArray();
+                       client.updateVariable(id, "selected", values, immediate);
+               } else {
+                       client.updateVariable(id, "selected", new String[] { "" + select.getValue(select.getSelectedIndex())}, immediate);
                }
        }
 }