-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-/*\r
- *\r
- * @author Toni Kalajainen\r
- */\r
-package org.simantics.g2d.dnd;\r
-\r
-import java.awt.Component;\r
-import java.awt.Point;\r
-import java.awt.dnd.DropTarget;\r
-import java.awt.dnd.DropTargetDragEvent;\r
-import java.awt.dnd.DropTargetDropEvent;\r
-import java.awt.dnd.DropTargetEvent;\r
-import java.awt.dnd.DropTargetListener;\r
-import java.awt.geom.Point2D;\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.Comparator;\r
-\r
-import org.simantics.g2d.canvas.ICanvasContext;\r
-import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
-\r
-\r
-/**\r
- * This participant handles drop operations of a canvas.\r
- * \r
- * To implement a drop operation add an implementation of {@link IDropTargetParticipant} to the canvas.\r
- * \r
- * Intantiates DragPainter for the duration of drag operations.\r
- * \r
- * DropInteractor is added by chassis.\r
- */\r
-public class DropInteractor extends AbstractCanvasParticipant implements DropTargetListener {\r
-\r
- private final Component component;\r
- @SuppressWarnings("unused")\r
- private DropTarget dropTarget;\r
- private DnDContext dropCtx;\r
- int allowedOps;\r
-\r
- /**\r
- * Create new abstract drop interactor\r
- * @param context\r
- * @param component\r
- * @param ops see DnDConstants\r
- */\r
- public DropInteractor(Component component) {\r
- super();\r
- this.component = component;\r
- }\r
-\r
- @Override\r
- public void addedToContext(ICanvasContext ctx) {\r
- super.addedToContext(ctx);\r
- installDropTarget();\r
- }\r
-\r
- @Override\r
- public void removedFromContext(ICanvasContext ctx) {\r
- super.removedFromContext(ctx);\r
- component.setDropTarget(null);\r
- }\r
-\r
- private int getAllowedOps() {\r
- int result = 0;\r
- for (IDropTargetParticipant p : getDropParticipants())\r
- result |= p.getAllowedOps();\r
- return result;\r
- }\r
-\r
- void installDropTarget() {\r
- allowedOps = getAllowedOps();\r
- dropTarget = new DropTarget(component, allowedOps, this);\r
- }\r
-\r
- protected Collection<IDropTargetParticipant> getDropParticipants() {\r
- ArrayList<IDropTargetParticipant> participants = new ArrayList<IDropTargetParticipant>(getContext().getItemsByClass(IDropTargetParticipant.class));\r
- Collections.sort(participants, new Comparator<IDropTargetParticipant>() {\r
- @Override\r
- public int compare(IDropTargetParticipant o1, IDropTargetParticipant o2) {\r
- return Double.compare(o2.getPriority(), o1.getPriority());\r
- }\r
- });\r
- return participants;\r
- }\r
-\r
- IDropTargetParticipant dropAccepter = null;\r
- private DragPainter currentDragPainter;\r
-\r
- public Collection<DragPainter> getDragPainters() {\r
- return getContext().getItemsByClass(DragPainter.class);\r
- }\r
-\r
- @Override\r
- public void dragEnter(final DropTargetDragEvent dtde) {\r
- dropAccepter = null;\r
- final Collection<IDropTargetParticipant> participants = getDropParticipants();\r
- if (participants.isEmpty()) {\r
- dtde.rejectDrag();\r
- return;\r
- }\r
- dropCtx = new DnDContext();\r
- syncExec(new Runnable() {\r
- @Override\r
- public void run() {\r
- for (IDropTargetParticipant p : participants) {\r
- p.dragEnter(dtde, dropCtx);\r
- if (dropCtx.toArray().length > 0) {\r
- dropAccepter = p;\r
- break;\r
- }\r
-\r
- }\r
- }});\r
-\r
- if (dropCtx.toArray().length==0)\r
- dtde.rejectDrag();\r
- else\r
- dtde.acceptDrag(dtde.getDropAction());\r
-\r
- // if drag is accepted, start drag interactor\r
- Point mouseLocation = dtde.getLocation();\r
- Point2D mouseControlPos = new Point2D.Double(mouseLocation.getX(), mouseLocation.getY());\r
- currentDragPainter = new DragPainter(dropCtx, mouseControlPos);\r
- getContext().add(currentDragPainter);\r
-\r
- setDirty();\r
- }\r
-\r
- @Override\r
- public void dragExit(final DropTargetEvent dte) {\r
- syncExec(new Runnable() {\r
- @Override\r
- public void run() {\r
- if (dropAccepter != null)\r
- dropAccepter.dragExit(dte, dropCtx);\r
-// final Collection<IDropTargetParticipant> participants = getDropParticipants();\r
-// for (IDropTargetParticipant p : participants)\r
-// p.dragExit(dte, dropCtx);\r
-\r
- endDrag();\r
- }\r
- });\r
- }\r
-\r
-\r
-\r
- @Override\r
- public void dragOver(final DropTargetDragEvent dtde) {\r
- final Collection<DragPainter> interactors = getDragPainters();\r
- if (interactors.isEmpty()) {\r
- dtde.rejectDrag();\r
- return;\r
- }\r
-\r
- syncExec(new Runnable() {\r
- @Override\r
- public void run() {\r
- Point mouseLocation = dtde.getLocation();\r
- Point2D mouseControlPos = new Point2D.Double(mouseLocation.getX(), mouseLocation.getY());\r
- for (DragPainter interactor : interactors)\r
- interactor.setMousePos( mouseControlPos );\r
-\r
- if (dropAccepter != null)\r
- dropAccepter.dragOver(dtde, dropCtx);\r
-// final Collection<IDropTargetParticipant> participants = getDropParticipants();\r
-// for (IDropTargetParticipant p : participants)\r
-// p.dragOver(dtde, dropCtx);\r
-\r
- }\r
- });\r
- }\r
-\r
- @Override\r
- public void drop(final DropTargetDropEvent dtde) {\r
-// ITask task = ThreadLogger.getInstance().begin("Drop");\r
- final Collection<DragPainter> interactors = getDragPainters();\r
- if (interactors.isEmpty()) {\r
- dtde.rejectDrop();\r
- return;\r
- }\r
-\r
- // Try to make sure that all DragItems have a position\r
- // before invoking IDropTargetParticipant.drop.\r
- Point mouseLocation = dtde.getLocation();\r
- Point2D mouseControlPos = new Point2D.Double(mouseLocation.getX(), mouseLocation.getY());\r
- for (DragPainter interactor : interactors)\r
- interactor.setMousePos( mouseControlPos, true );\r
-\r
- // Remove drag painter before dropping just to prevent it from\r
- // doing anything with the DNDContext after this point.\r
- if (currentDragPainter != null) {\r
- getContext().remove(currentDragPainter);\r
- currentDragPainter = null;\r
- }\r
-\r
- syncExec(new Runnable() {\r
- @Override\r
- public void run() {\r
-// DragPainter painter = getDragPainter();\r
- if (dropAccepter != null)\r
- dropAccepter.drop(dtde, dropCtx);\r
-// final Collection<IDropTargetParticipant> participants = getDropParticipants();\r
-// for (IDropTargetParticipant p : participants)\r
-// p.drop(dtde, dropCtx);\r
-\r
- endDrag();\r
- }\r
- });\r
- //dtde.acceptDrop(dtde.getDropAction());\r
-// task.finish();\r
- }\r
-\r
- @Override\r
- public void dropActionChanged(DropTargetDragEvent dtde) {\r
- //System.out.println("dropActionChanged: "+dtde.getDropAction());\r
- // remove interactor when action is canceled\r
- }\r
-\r
- public void endDrag()\r
- {\r
- Collection<DragPainter> dragPainters = getContext().getItemsByClass(DragPainter.class);\r
- if (dragPainters.isEmpty()) return;\r
- for (DragPainter dp : dragPainters)\r
- dp.remove();\r
- dropAccepter = null;\r
- setDirty();\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+/*
+ *
+ * @author Toni Kalajainen
+ */
+package org.simantics.g2d.dnd;
+
+import java.awt.Component;
+import java.awt.Point;
+import java.awt.dnd.DropTarget;
+import java.awt.dnd.DropTargetDragEvent;
+import java.awt.dnd.DropTargetDropEvent;
+import java.awt.dnd.DropTargetEvent;
+import java.awt.dnd.DropTargetListener;
+import java.awt.geom.Point2D;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+
+import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
+
+
+/**
+ * This participant handles drop operations of a canvas.
+ *
+ * To implement a drop operation add an implementation of {@link IDropTargetParticipant} to the canvas.
+ *
+ * Intantiates DragPainter for the duration of drag operations.
+ *
+ * DropInteractor is added by chassis.
+ */
+public class DropInteractor extends AbstractCanvasParticipant implements DropTargetListener {
+
+ private final Component component;
+ @SuppressWarnings("unused")
+ private DropTarget dropTarget;
+ private DnDContext dropCtx;
+ int allowedOps;
+
+ /**
+ * Create new abstract drop interactor
+ * @param context
+ * @param component
+ * @param ops see DnDConstants
+ */
+ public DropInteractor(Component component) {
+ super();
+ this.component = component;
+ }
+
+ @Override
+ public void addedToContext(ICanvasContext ctx) {
+ super.addedToContext(ctx);
+ installDropTarget();
+ }
+
+ @Override
+ public void removedFromContext(ICanvasContext ctx) {
+ super.removedFromContext(ctx);
+ component.setDropTarget(null);
+ }
+
+ private int getAllowedOps() {
+ int result = 0;
+ for (IDropTargetParticipant p : getDropParticipants())
+ result |= p.getAllowedOps();
+ return result;
+ }
+
+ void installDropTarget() {
+ allowedOps = getAllowedOps();
+ dropTarget = new DropTarget(component, allowedOps, this);
+ }
+
+ protected Collection<IDropTargetParticipant> getDropParticipants() {
+ ArrayList<IDropTargetParticipant> participants = new ArrayList<IDropTargetParticipant>(getContext().getItemsByClass(IDropTargetParticipant.class));
+ Collections.sort(participants, new Comparator<IDropTargetParticipant>() {
+ @Override
+ public int compare(IDropTargetParticipant o1, IDropTargetParticipant o2) {
+ return Double.compare(o2.getPriority(), o1.getPriority());
+ }
+ });
+ return participants;
+ }
+
+ IDropTargetParticipant dropAccepter = null;
+ private DragPainter currentDragPainter;
+
+ public Collection<DragPainter> getDragPainters() {
+ return getContext().getItemsByClass(DragPainter.class);
+ }
+
+ @Override
+ public void dragEnter(final DropTargetDragEvent dtde) {
+ dropAccepter = null;
+ final Collection<IDropTargetParticipant> participants = getDropParticipants();
+ if (participants.isEmpty()) {
+ dtde.rejectDrag();
+ return;
+ }
+ dropCtx = new DnDContext();
+ syncExec(new Runnable() {
+ @Override
+ public void run() {
+ for (IDropTargetParticipant p : participants) {
+ p.dragEnter(dtde, dropCtx);
+ if (dropCtx.toArray().length > 0) {
+ dropAccepter = p;
+ break;
+ }
+
+ }
+ }});
+
+ if (dropCtx.toArray().length==0)
+ dtde.rejectDrag();
+ else
+ dtde.acceptDrag(dtde.getDropAction());
+
+ // if drag is accepted, start drag interactor
+ Point mouseLocation = dtde.getLocation();
+ Point2D mouseControlPos = new Point2D.Double(mouseLocation.getX(), mouseLocation.getY());
+ currentDragPainter = new DragPainter(dropCtx, mouseControlPos);
+ getContext().add(currentDragPainter);
+
+ setDirty();
+ }
+
+ @Override
+ public void dragExit(final DropTargetEvent dte) {
+ syncExec(new Runnable() {
+ @Override
+ public void run() {
+ if (dropAccepter != null)
+ dropAccepter.dragExit(dte, dropCtx);
+// final Collection<IDropTargetParticipant> participants = getDropParticipants();
+// for (IDropTargetParticipant p : participants)
+// p.dragExit(dte, dropCtx);
+
+ endDrag();
+ }
+ });
+ }
+
+
+
+ @Override
+ public void dragOver(final DropTargetDragEvent dtde) {
+ final Collection<DragPainter> interactors = getDragPainters();
+ if (interactors.isEmpty()) {
+ dtde.rejectDrag();
+ return;
+ }
+
+ syncExec(new Runnable() {
+ @Override
+ public void run() {
+ Point mouseLocation = dtde.getLocation();
+ Point2D mouseControlPos = new Point2D.Double(mouseLocation.getX(), mouseLocation.getY());
+ for (DragPainter interactor : interactors)
+ interactor.setMousePos( mouseControlPos );
+
+ if (dropAccepter != null)
+ dropAccepter.dragOver(dtde, dropCtx);
+// final Collection<IDropTargetParticipant> participants = getDropParticipants();
+// for (IDropTargetParticipant p : participants)
+// p.dragOver(dtde, dropCtx);
+
+ }
+ });
+ }
+
+ @Override
+ public void drop(final DropTargetDropEvent dtde) {
+// ITask task = ThreadLogger.getInstance().begin("Drop");
+ final Collection<DragPainter> interactors = getDragPainters();
+ if (interactors.isEmpty()) {
+ dtde.rejectDrop();
+ return;
+ }
+
+ // Try to make sure that all DragItems have a position
+ // before invoking IDropTargetParticipant.drop.
+ Point mouseLocation = dtde.getLocation();
+ Point2D mouseControlPos = new Point2D.Double(mouseLocation.getX(), mouseLocation.getY());
+ for (DragPainter interactor : interactors)
+ interactor.setMousePos( mouseControlPos, true );
+
+ // Remove drag painter before dropping just to prevent it from
+ // doing anything with the DNDContext after this point.
+ if (currentDragPainter != null) {
+ getContext().remove(currentDragPainter);
+ currentDragPainter = null;
+ }
+
+ syncExec(new Runnable() {
+ @Override
+ public void run() {
+// DragPainter painter = getDragPainter();
+ if (dropAccepter != null)
+ dropAccepter.drop(dtde, dropCtx);
+// final Collection<IDropTargetParticipant> participants = getDropParticipants();
+// for (IDropTargetParticipant p : participants)
+// p.drop(dtde, dropCtx);
+
+ endDrag();
+ }
+ });
+ //dtde.acceptDrop(dtde.getDropAction());
+// task.finish();
+ }
+
+ @Override
+ public void dropActionChanged(DropTargetDragEvent dtde) {
+ //System.out.println("dropActionChanged: "+dtde.getDropAction());
+ // remove interactor when action is canceled
+ }
+
+ public void endDrag()
+ {
+ Collection<DragPainter> dragPainters = getContext().getItemsByClass(DragPainter.class);
+ if (dragPainters.isEmpty()) return;
+ for (DragPainter dp : dragPainters)
+ dp.remove();
+ dropAccepter = null;
+ setDirty();
+ }
+
+}