KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > diagrams > DragTracker


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 */

18
19
20 package org.objectweb.jac.ide.diagrams;
21
22 import CH.ifa.draw.framework.DrawingEditor;
23 import CH.ifa.draw.framework.Figure;
24 import CH.ifa.draw.framework.FigureEnumeration;
25 import CH.ifa.draw.standard.AbstractTool;
26 import java.awt.event.MouseEvent JavaDoc;
27 import java.util.HashSet JavaDoc;
28
29 /**
30  * This drag tracker handles figures attached to links so that they do
31  * not drift.
32  */

33 public class DragTracker extends AbstractTool {
34
35    Figure anchorFigure;
36    int lastX, lastY; // previous mouse position
37

38    public DragTracker(DrawingEditor newDrawingEditor, Figure anchor) {
39       super(newDrawingEditor);
40       anchorFigure = anchor;
41    }
42
43    public void mouseDown(MouseEvent JavaDoc e, int x, int y) {
44       super.mouseDown(e, x, y);
45       lastX = x;
46       lastY = y;
47
48       if (e.isShiftDown()) {
49          view().toggleSelection(anchorFigure);
50          anchorFigure = null;
51       } else if (!view().isFigureSelected(anchorFigure)) {
52          view().clearSelection();
53          view().addToSelection(anchorFigure);
54       }
55       view().repairDamage();
56    }
57
58    public void mouseDrag(MouseEvent JavaDoc e, int x, int y) {
59       super.mouseDrag(e, x, y);
60       
61       if ((Math.abs(x - fAnchorX) > 4) || (Math.abs(y - fAnchorY) > 4)) {
62          org.objectweb.jac.util.Log.trace("tools","MOVED!");
63
64          // Get the link figures
65
HashSet JavaDoc linkFigures = new HashSet JavaDoc();
66          FigureEnumeration figures = view().selectionElements();
67          while (figures.hasMoreElements()) {
68             Figure figure = figures.nextFigure();
69             if (figure instanceof LinkFigure) {
70                linkFigures.add(((LinkFigure)figure).getSubstance());
71             }
72          }
73
74          figures = view().selectionElements();
75          while (figures.hasMoreElements()) {
76             Figure figure = figures.nextFigure();
77             if (! ((figure instanceof AttachedTextFigure)
78                    && linkFigures.contains(((AttachedTextFigure)figure).getSubstance()))) {
79                figure.moveBy(x - lastX, y - lastY);
80             }
81          }
82       }
83       lastX = x;
84       lastY = y;
85    }
86
87    public void activate() {
88    }
89
90    public void deactivate() {
91    }
92 }
93
Popular Tags