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