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 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. /**
  17. *
  18. */
  19. package com.vaadin.event.dd.acceptcriteria;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import com.vaadin.event.TransferableImpl;
  23. import com.vaadin.event.dd.DragAndDropEvent;
  24. import com.vaadin.server.PaintException;
  25. import com.vaadin.server.PaintTarget;
  26. import com.vaadin.ui.Component;
  27. /**
  28. * Client side criteria that checks if the drag source is one of the given
  29. * components.
  30. *
  31. * @since 6.3
  32. */
  33. @SuppressWarnings("serial")
  34. public class SourceIs extends ClientSideCriterion {
  35. private final Component[] components;
  36. public SourceIs(Component... component) {
  37. components = component;
  38. }
  39. @Override
  40. public void paintContent(PaintTarget target) throws PaintException {
  41. super.paintContent(target);
  42. int paintedComponents = 0;
  43. for (int i = 0; i < components.length; i++) {
  44. Component c = components[i];
  45. if (c.isAttached()) {
  46. target.addAttribute("component" + paintedComponents++, c);
  47. } else {
  48. Logger.getLogger(SourceIs.class.getName()).log(Level.WARNING,
  49. "SourceIs component {0} at index {1} is not attached to the component hierachy and will thus be ignored",
  50. new Object[] { c.getClass().getName(),
  51. Integer.valueOf(i) });
  52. }
  53. }
  54. target.addAttribute("c", paintedComponents);
  55. }
  56. @Override
  57. public boolean accept(DragAndDropEvent dragEvent) {
  58. if (dragEvent.getTransferable() instanceof TransferableImpl) {
  59. Component sourceComponent = dragEvent.getTransferable()
  60. .getSourceComponent();
  61. for (Component c : components) {
  62. if (c == sourceComponent) {
  63. return true;
  64. }
  65. }
  66. }
  67. return false;
  68. }
  69. }