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.

SourceIs.java 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. /**
  5. *
  6. */
  7. package com.vaadin.event.dd.acceptcriteria;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import com.vaadin.event.TransferableImpl;
  11. import com.vaadin.event.dd.DragAndDropEvent;
  12. import com.vaadin.terminal.PaintException;
  13. import com.vaadin.terminal.PaintTarget;
  14. import com.vaadin.ui.Component;
  15. /**
  16. * Client side criteria that checks if the drag source is one of the given
  17. * components.
  18. *
  19. * @since 6.3
  20. */
  21. @SuppressWarnings("serial")
  22. public class SourceIs extends ClientSideCriterion {
  23. private Component[] components;
  24. public SourceIs(Component... component) {
  25. components = component;
  26. }
  27. @Override
  28. public void paintContent(PaintTarget target) throws PaintException {
  29. super.paintContent(target);
  30. int paintedComponents = 0;
  31. for (int i = 0; i < components.length; i++) {
  32. Component c = components[i];
  33. if (c.getApplication() != null) {
  34. target.addAttribute("component" + paintedComponents++, c);
  35. } else {
  36. Logger.getLogger(SourceIs.class.getName())
  37. .log(Level.WARNING,
  38. "SourceIs component {0} at index {1} is not attached to the component hierachy and will thus be ignored",
  39. new Object[] { c.getClass().getName(),
  40. Integer.valueOf(i) });
  41. }
  42. }
  43. target.addAttribute("c", paintedComponents);
  44. }
  45. public boolean accept(DragAndDropEvent dragEvent) {
  46. if (dragEvent.getTransferable() instanceof TransferableImpl) {
  47. Component sourceComponent = ((TransferableImpl) dragEvent
  48. .getTransferable()).getSourceComponent();
  49. for (Component c : components) {
  50. if (c == sourceComponent) {
  51. return true;
  52. }
  53. }
  54. }
  55. return false;
  56. }
  57. }