You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JavaScriptAPIExample.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.automatedtests.featurebrowser;
  5. import java.util.Date;
  6. import com.vaadin.terminal.PaintException;
  7. import com.vaadin.terminal.PaintTarget;
  8. import com.vaadin.ui.Alignment;
  9. import com.vaadin.ui.Button;
  10. import com.vaadin.ui.CustomComponent;
  11. import com.vaadin.ui.Label;
  12. import com.vaadin.ui.TextField;
  13. import com.vaadin.ui.VerticalLayout;
  14. import com.vaadin.ui.Button.ClickEvent;
  15. /**
  16. * An example using a RichTextArea to edit a Label in XHTML-mode.
  17. *
  18. */
  19. public class JavaScriptAPIExample extends CustomComponent {
  20. public static final String txt = "<p>For advanced client side programmers Toolkit offers a simple method which can be used to force sync client with server. This may be needed for example if another part of a mashup changes things on server.</p> (more examples will be added here as the APIs are made public)<br/><br/><A href=\"javascript:itmill.forceSync();\">javascript:itmill.forceSync();</A>";
  21. private final VerticalLayout main;
  22. private final Label l;
  23. private final TextField editor = new TextField();
  24. public JavaScriptAPIExample() {
  25. // main layout
  26. main = new VerticalLayout();
  27. main.setMargin(true);
  28. setCompositionRoot(main);
  29. editor.setRows(7);
  30. editor.setColumns(50);
  31. // Add the label
  32. l = new Label(txt);
  33. l.setContentMode(Label.CONTENT_XHTML);
  34. main.addComponent(l);
  35. // Edit button with inline click-listener
  36. Button b = new Button("Edit", new Button.ClickListener() {
  37. public void buttonClick(ClickEvent event) {
  38. // swap Label <-> RichTextArea
  39. if (main.getComponentIterator().next() == l) {
  40. editor.setValue(l.getValue());
  41. main.replaceComponent(l, editor);
  42. event.getButton().setCaption("Save");
  43. } else {
  44. l.setValue(editor.getValue());
  45. main.replaceComponent(editor, l);
  46. event.getButton().setCaption("Edit");
  47. }
  48. }
  49. });
  50. main.addComponent(b);
  51. main.setComponentAlignment(b, Alignment.MIDDLE_RIGHT);
  52. //
  53. Label l = new Label(
  54. "This label will update it's server-side value AFTER it's rendered to the client-side. "
  55. + "The client will be synchronized on reload, when you click a button, "
  56. + "or when itmill.forceSync() is called.") {
  57. @Override
  58. public void paintContent(PaintTarget target) throws PaintException {
  59. super.paintContent(target);
  60. Delay d = new Delay(this);
  61. d.start();
  62. }
  63. };
  64. main.addComponent(l);
  65. }
  66. private class Delay extends Thread {
  67. Label label;
  68. public Delay(Label l) {
  69. label = l;
  70. }
  71. @Override
  72. public void run() {
  73. try {
  74. Thread.sleep(500);
  75. label.setValue(new Date().toString());
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. }
  81. }