--- /dev/null
+/*******************************************************************************\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.geom.Point2D;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.simantics.utils.datastructures.context.Context;\r
+import org.simantics.utils.datastructures.hints.HintContext;\r
+import org.simantics.utils.datastructures.hints.IHintContext;\r
+\r
+/**\r
+ * Contains an ordered set of context items (see {@link #orderedItems}) which\r
+ * works around the fact that {@link Context} base implementation does not\r
+ * preserve any kind of order information for the added drag items, which is\r
+ * often useful for user experience's sake.\r
+ * \r
+ * <b>WARNING</b> DnDContext is not as thread-safe as its base implementation\r
+ * {@link Context}.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DnDContext extends Context<IDragItem> implements IDnDContext {\r
+\r
+ IHintContext hintCtx = new HintContext();\r
+\r
+ List<IDragItem> orderedItems = new ArrayList<IDragItem>();\r
+\r
+ Map<IDragItem, Point2D> itemPositions = new HashMap<IDragItem, Point2D>();\r
+\r
+ public DnDContext() {\r
+ super(IDragItem.class);\r
+ }\r
+\r
+ @Override\r
+ public IHintContext getHints() {\r
+ return hintCtx;\r
+ }\r
+\r
+ @Override\r
+ public synchronized void setItemPosition(IDragItem item, Point2D pos) {\r
+ if (item == null)\r
+ throw new NullPointerException("trying to set position " + pos + " for null IDragItem");\r
+ if (pos == null)\r
+ throw new NullPointerException("trying to set null position for dragged item " + item);\r
+ itemPositions.put(item, pos);\r
+ }\r
+\r
+ @Override\r
+ public synchronized Point2D getItemPosition(IDragItem item) {\r
+ return itemPositions.get(item);\r
+ }\r
+\r
+ @Override\r
+ public void add(IDragItem item) {\r
+ assertNotDisposed();\r
+ orderedItems.add(item);\r
+ boolean added = false;\r
+ try {\r
+ super.add(item);\r
+ added = true;\r
+ } finally {\r
+ if (!added)\r
+ orderedItems.remove(item);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public boolean remove(IDragItem item) {\r
+ assertNotDisposed();\r
+ boolean existed = super.remove(item);\r
+ if (existed)\r
+ orderedItems.remove(item);\r
+ return existed;\r
+ }\r
+\r
+ @Override\r
+ public void clear() {\r
+ assertNotDisposed();\r
+ orderedItems.clear();\r
+ super.clear();\r
+ }\r
+\r
+ @Override\r
+ public IDragItem[] toArray() {\r
+ assertNotDisposed();\r
+ return orderedItems.toArray(new IDragItem[orderedItems.size()]);\r
+ }\r
+\r
+}\r