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.8KB

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