1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
14 * @author Toni Kalajainen
16 package org.simantics.g2d.dnd;
18 import java.awt.geom.Point2D;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
24 import org.simantics.utils.datastructures.context.Context;
25 import org.simantics.utils.datastructures.hints.HintContext;
26 import org.simantics.utils.datastructures.hints.IHintContext;
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.
34 * <b>WARNING</b> DnDContext is not as thread-safe as its base implementation
37 * @author Tuukka Lehtonen
39 public class DnDContext extends Context<IDragItem> implements IDnDContext {
41 IHintContext hintCtx = new HintContext();
43 List<IDragItem> orderedItems = new ArrayList<IDragItem>();
45 Map<IDragItem, Point2D> itemPositions = new HashMap<IDragItem, Point2D>();
48 super(IDragItem.class);
52 public IHintContext getHints() {
57 public synchronized void setItemPosition(IDragItem item, Point2D pos) {
59 throw new NullPointerException("trying to set position " + pos + " for null IDragItem");
61 throw new NullPointerException("trying to set null position for dragged item " + item);
62 itemPositions.put(item, pos);
66 public synchronized Point2D getItemPosition(IDragItem item) {
67 return itemPositions.get(item);
71 public void add(IDragItem item) {
73 orderedItems.add(item);
74 boolean added = false;
80 orderedItems.remove(item);
85 public boolean remove(IDragItem item) {
87 boolean existed = super.remove(item);
89 orderedItems.remove(item);
101 public IDragItem[] toArray() {
103 return orderedItems.toArray(new IDragItem[orderedItems.size()]);