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.

VActiveLink.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.vaadin.demo.sampler.gwt.client.ui;
  2. import com.google.gwt.user.client.DOM;
  3. import com.google.gwt.user.client.Event;
  4. import com.google.gwt.user.client.ui.MouseListener;
  5. import com.google.gwt.user.client.ui.Widget;
  6. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  7. import com.vaadin.terminal.gwt.client.UIDL;
  8. import com.vaadin.terminal.gwt.client.ui.VLink;
  9. public class VActiveLink extends VLink {
  10. String id;
  11. ApplicationConnection client;
  12. boolean listening = false;
  13. public VActiveLink() {
  14. addMouseListener(new MouseListener() {
  15. public void onMouseDown(Widget sender, int x, int y) {
  16. }
  17. public void onMouseEnter(Widget sender) {
  18. }
  19. public void onMouseLeave(Widget sender) {
  20. }
  21. public void onMouseMove(Widget sender, int x, int y) {
  22. }
  23. public void onMouseUp(Widget sender, int x, int y) {
  24. Event e = DOM.eventGetCurrentEvent();
  25. if (e.getButton() == Event.BUTTON_MIDDLE) {
  26. sendVariables();
  27. }
  28. }
  29. });
  30. }
  31. /**
  32. * Sends variables, returns true if default handler should be called (i.e if
  33. * server is listening and the link was claimed to be opened by the client)
  34. *
  35. * @return
  36. */
  37. private boolean sendVariables() {
  38. Event e = DOM.eventGetCurrentEvent();
  39. boolean opened = (e.getCtrlKey() || e.getAltKey() || e.getShiftKey()
  40. || e.getMetaKey() || e.getButton() == Event.BUTTON_MIDDLE);
  41. // Works as VLink if no-one is listening
  42. if (listening) {
  43. if (opened) {
  44. // VLink will open, notify server
  45. client.updateVariable(id, "opened", true, false);
  46. } else {
  47. e.preventDefault();
  48. }
  49. client.updateVariable(id, "activated", true, true);
  50. }
  51. return !listening || opened;
  52. }
  53. @Override
  54. public void onClick(Widget sender) {
  55. if (sendVariables()) {
  56. // run default if not listening, or we claimed link was opened
  57. super.onClick(sender);
  58. }
  59. }
  60. @Override
  61. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  62. // Ensure correct implementation,
  63. // but don't let container manage caption etc.
  64. if (client.updateComponent(this, uidl, false)) {
  65. return;
  66. }
  67. // Save details
  68. this.client = client;
  69. id = uidl.getId();
  70. listening = uidl.hasVariable("activated");
  71. super.updateFromUIDL(uidl, client);
  72. }
  73. }