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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
14 * @author Toni Kalajainen
\r
16 package org.simantics.g2d.dnd;
\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
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
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
34 * <b>WARNING</b> DnDContext is not as thread-safe as its base implementation
\r
37 * @author Tuukka Lehtonen
\r
39 public class DnDContext extends Context<IDragItem> implements IDnDContext {
\r
41 IHintContext hintCtx = new HintContext();
\r
43 List<IDragItem> orderedItems = new ArrayList<IDragItem>();
\r
45 Map<IDragItem, Point2D> itemPositions = new HashMap<IDragItem, Point2D>();
\r
47 public DnDContext() {
\r
48 super(IDragItem.class);
\r
52 public IHintContext getHints() {
\r
57 public synchronized void setItemPosition(IDragItem item, Point2D pos) {
\r
59 throw new NullPointerException("trying to set position " + pos + " for null IDragItem");
\r
61 throw new NullPointerException("trying to set null position for dragged item " + item);
\r
62 itemPositions.put(item, pos);
\r
66 public synchronized Point2D getItemPosition(IDragItem item) {
\r
67 return itemPositions.get(item);
\r
71 public void add(IDragItem item) {
\r
72 assertNotDisposed();
\r
73 orderedItems.add(item);
\r
74 boolean added = false;
\r
80 orderedItems.remove(item);
\r
85 public boolean remove(IDragItem item) {
\r
86 assertNotDisposed();
\r
87 boolean existed = super.remove(item);
\r
89 orderedItems.remove(item);
\r
94 public void clear() {
\r
95 assertNotDisposed();
\r
96 orderedItems.clear();
\r
101 public IDragItem[] toArray() {
\r
102 assertNotDisposed();
\r
103 return orderedItems.toArray(new IDragItem[orderedItems.size()]);
\r