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.

VUriFragmentUtility.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import com.google.gwt.dom.client.Document;
  6. import com.google.gwt.event.logical.shared.ValueChangeEvent;
  7. import com.google.gwt.event.logical.shared.ValueChangeHandler;
  8. import com.google.gwt.event.shared.HandlerRegistration;
  9. import com.google.gwt.user.client.History;
  10. import com.google.gwt.user.client.ui.Widget;
  11. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  12. import com.vaadin.terminal.gwt.client.BrowserInfo;
  13. import com.vaadin.terminal.gwt.client.Paintable;
  14. import com.vaadin.terminal.gwt.client.UIDL;
  15. /**
  16. * Client side implementation for UriFragmentUtility. Uses GWT's History object
  17. * as an implementation.
  18. *
  19. */
  20. public class VUriFragmentUtility extends Widget implements Paintable,
  21. ValueChangeHandler<String> {
  22. private String fragment;
  23. private ApplicationConnection client;
  24. private String paintableId;
  25. private boolean immediate;
  26. private HandlerRegistration historyValueHandlerRegistration;
  27. public VUriFragmentUtility() {
  28. setElement(Document.get().createDivElement());
  29. if (BrowserInfo.get().isIE6()) {
  30. getElement().getStyle().setProperty("overflow", "hidden");
  31. getElement().getStyle().setProperty("height", "0");
  32. }
  33. }
  34. @Override
  35. protected void onAttach() {
  36. super.onAttach();
  37. historyValueHandlerRegistration = History.addValueChangeHandler(this);
  38. History.fireCurrentHistoryState();
  39. }
  40. @Override
  41. protected void onDetach() {
  42. super.onDetach();
  43. historyValueHandlerRegistration.removeHandler();
  44. }
  45. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  46. if (client.updateComponent(this, uidl, false)) {
  47. return;
  48. }
  49. String uidlFragment = uidl.getStringVariable("fragment");
  50. immediate = uidl.getBooleanAttribute("immediate");
  51. if (this.client == null) {
  52. // initial paint has some special logic
  53. this.client = client;
  54. paintableId = uidl.getId();
  55. if (!fragment.equals(uidlFragment)) {
  56. // initial server side fragment (from link/bookmark/typed) does
  57. // not equal the one on
  58. // server, send initial fragment to server
  59. History.fireCurrentHistoryState();
  60. }
  61. } else {
  62. if (uidlFragment != null && !uidlFragment.equals(fragment)) {
  63. fragment = uidlFragment;
  64. // normal fragment change from server, add new history item
  65. History.newItem(uidlFragment, false);
  66. }
  67. }
  68. }
  69. public void onValueChange(ValueChangeEvent<String> event) {
  70. String historyToken = event.getValue();
  71. fragment = historyToken;
  72. if (client != null) {
  73. client.updateVariable(paintableId, "fragment", fragment, immediate);
  74. }
  75. }
  76. }