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));
}
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;
this.widgetFactory = widgetFactory;
}
+
}
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);
}
}
}