]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/dnd/DnDContext.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / dnd / DnDContext.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  *\r
14  * @author Toni Kalajainen\r
15  */\r
16 package org.simantics.g2d.dnd;\r
17 \r
18 import java.awt.geom.Point2D;\r
19 import java.util.ArrayList;\r
20 import java.util.HashMap;\r
21 import java.util.List;\r
22 import java.util.Map;\r
23 \r
24 import org.simantics.utils.datastructures.context.Context;\r
25 import org.simantics.utils.datastructures.hints.HintContext;\r
26 import org.simantics.utils.datastructures.hints.IHintContext;\r
27 \r
28 /**\r
29  * Contains an ordered set of context items (see {@link #orderedItems}) which\r
30  * works around the fact that {@link Context} base implementation does not\r
31  * preserve any kind of order information for the added drag items, which is\r
32  * often useful for user experience's sake.\r
33  * \r
34  * <b>WARNING</b> DnDContext is not as thread-safe as its base implementation\r
35  * {@link Context}.\r
36  * \r
37  * @author Tuukka Lehtonen\r
38  */\r
39 public class DnDContext extends Context<IDragItem> implements IDnDContext {\r
40 \r
41     IHintContext            hintCtx       = new HintContext();\r
42 \r
43     List<IDragItem>         orderedItems  = new ArrayList<IDragItem>();\r
44 \r
45     Map<IDragItem, Point2D> itemPositions = new HashMap<IDragItem, Point2D>();\r
46 \r
47     public DnDContext() {\r
48         super(IDragItem.class);\r
49     }\r
50 \r
51     @Override\r
52     public IHintContext getHints() {\r
53         return hintCtx;\r
54     }\r
55 \r
56     @Override\r
57     public synchronized void setItemPosition(IDragItem item, Point2D pos) {\r
58         if (item == null)\r
59             throw new NullPointerException("trying to set position " + pos + " for null IDragItem");\r
60         if (pos == null)\r
61             throw new NullPointerException("trying to set null position for dragged item " + item);\r
62         itemPositions.put(item, pos);\r
63     }\r
64 \r
65     @Override\r
66     public synchronized Point2D getItemPosition(IDragItem item) {\r
67         return itemPositions.get(item);\r
68     }\r
69 \r
70     @Override\r
71     public void add(IDragItem item) {\r
72         assertNotDisposed();\r
73         orderedItems.add(item);\r
74         boolean added = false;\r
75         try {\r
76             super.add(item);\r
77             added = true;\r
78         } finally {\r
79             if (!added)\r
80                 orderedItems.remove(item);\r
81         }\r
82     }\r
83 \r
84     @Override\r
85     public boolean remove(IDragItem item) {\r
86         assertNotDisposed();\r
87         boolean existed = super.remove(item);\r
88         if (existed)\r
89             orderedItems.remove(item);\r
90         return existed;\r
91     }\r
92 \r
93     @Override\r
94     public void clear() {\r
95         assertNotDisposed();\r
96         orderedItems.clear();\r
97         super.clear();\r
98     }\r
99 \r
100     @Override\r
101     public IDragItem[] toArray() {\r
102         assertNotDisposed();\r
103         return orderedItems.toArray(new IDragItem[orderedItems.size()]);\r
104     }\r
105 \r
106 }\r