001    /*
002      Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017    */
018    
019    
020    package org.objectweb.jac.ide.diagrams;
021    
022    import CH.ifa.draw.framework.DrawingEditor;
023    import CH.ifa.draw.framework.Figure;
024    import CH.ifa.draw.framework.FigureEnumeration;
025    import CH.ifa.draw.standard.AbstractTool;
026    import java.awt.event.MouseEvent;
027    import java.util.HashSet;
028    
029    /**
030     * This drag tracker handles figures attached to links so that they do
031     * not drift.
032     */
033    public class DragTracker extends AbstractTool {
034    
035       Figure  anchorFigure;
036       int     lastX, lastY;      // previous mouse position
037    
038       public DragTracker(DrawingEditor newDrawingEditor, Figure anchor) {
039          super(newDrawingEditor);
040          anchorFigure = anchor;
041       }
042    
043       public void mouseDown(MouseEvent e, int x, int y) {
044          super.mouseDown(e, x, y);
045          lastX = x;
046          lastY = y;
047    
048          if (e.isShiftDown()) {
049             view().toggleSelection(anchorFigure);
050             anchorFigure = null;
051          } else if (!view().isFigureSelected(anchorFigure)) {
052             view().clearSelection();
053             view().addToSelection(anchorFigure);
054          }
055          view().repairDamage();
056       }
057    
058       public void mouseDrag(MouseEvent e, int x, int y) {
059          super.mouseDrag(e, x, y);
060          
061          if ((Math.abs(x - fAnchorX) > 4) || (Math.abs(y - fAnchorY) > 4)) {
062             org.objectweb.jac.util.Log.trace("tools","MOVED!");
063    
064             // Get the link figures
065             HashSet linkFigures = new HashSet();
066             FigureEnumeration figures = view().selectionElements();
067             while (figures.hasMoreElements()) {
068                Figure figure = figures.nextFigure();
069                if (figure instanceof LinkFigure) {
070                   linkFigures.add(((LinkFigure)figure).getSubstance());
071                }
072             }
073    
074             figures = view().selectionElements();
075             while (figures.hasMoreElements()) {
076                Figure figure = figures.nextFigure();
077                if (! ((figure instanceof AttachedTextFigure) 
078                       && linkFigures.contains(((AttachedTextFigure)figure).getSubstance()))) {
079                   figure.moveBy(x - lastX, y - lastY);
080                }
081             }
082          }
083          lastX = x;
084          lastY = y;
085       }
086    
087       public void activate() {
088       }
089    
090       public void deactivate() {
091       }
092    }