001    /*
002      Copyright (C) 2002 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    package org.objectweb.jac.ide.diagrams;
020    
021    import CH.ifa.draw.contrib.DragNDropTool;
022    import CH.ifa.draw.framework.DrawingEditor;
023    import CH.ifa.draw.framework.DrawingView;
024    import CH.ifa.draw.framework.Figure;
025    import CH.ifa.draw.framework.Handle;
026    import CH.ifa.draw.framework.Tool;
027    import CH.ifa.draw.standard.HandleTracker;
028    import CH.ifa.draw.standard.SelectAreaTracker;
029    import org.objectweb.jac.aspects.gui.DisplayContext;
030    import java.awt.event.MouseEvent;
031    
032    
033    public class SelectionTool extends AbstractTool {
034    
035       protected Tool fChild = null;
036       protected DisplayContext context;
037    
038       public SelectionTool(DrawingEditor newDrawingEditor, DisplayContext context) {
039          super(newDrawingEditor);
040          this.context = context;
041       }
042    
043       /**
044        * Handles mouse down events and starts the corresponding tracker.
045        */
046       public void mouseDown(MouseEvent e, int x, int y) {
047          // on MS-Windows NT: AWT generates additional mouse down events
048          // when the left button is down && right button is clicked.
049          // To avoid dead locks we ignore such events
050          if (fChild != null) {
051             return;
052          }
053    
054          view().freezeView();
055    
056          Handle handle = view().findHandle(e.getX(), e.getY());
057          if (handle != null) {
058             fChild = createHandleTracker(view(), handle);
059          } else {
060             Figure figure = drawing().findFigureInside(e.getX(), e.getY());
061             if (figure instanceof Selectable) {
062                ((Selectable)figure).onSelect(context);
063             }
064             figure = drawing().findFigure(e.getX(), e.getY());
065             if (figure != null) {
066                fChild = createDragTracker(figure);
067             } else {
068                if (!e.isShiftDown()) {
069                   view().clearSelection();
070                }
071                fChild = createAreaTracker();
072             }
073          }
074          fChild.mouseDown(e, x, y);
075          fChild.activate();
076          view().repairDamage();
077       }
078    
079       /**
080        * Handles mouse moves (if the mouse button is up).
081        * Switches the cursors depending on whats under them.
082        */
083       public void mouseMove(MouseEvent evt, int x, int y) {
084          DragNDropTool.setCursor(evt.getX(), evt.getY(), view());
085          ((DiagramView)editor()).setCoord(x,y);
086          view().repairDamage();
087       }
088    
089       /**
090        * Handles mouse drag events. The events are forwarded to the
091        * current tracker.
092        */
093       public void mouseDrag(MouseEvent e, int x, int y) {
094          if (fChild != null) { // JDK1.1 doesn't guarantee mouseDown, mouseDrag, mouseUp
095             fChild.mouseDrag(e, x, y);
096          }
097          view().repairDamage();
098       }
099    
100       /**
101        * Handles mouse up events. The events are forwarded to the
102        * current tracker.
103        */
104       public void mouseUp(MouseEvent e, int x, int y) {
105          view().unfreezeView();
106          if (fChild != null) { // JDK1.1 doesn't guarantee mouseDown, mouseDrag, mouseUp
107             fChild.mouseUp(e, x, y);
108             fChild.deactivate();
109             fChild = null;
110          }
111          view().repairDamage();
112       }
113    
114       /**
115        * Factory method to create a Handle tracker. It is used to track a handle.
116        */
117       protected Tool createHandleTracker(DrawingView view, Handle handle) {
118          return new HandleTracker(editor(), handle);
119       }
120    
121       /**
122        * Factory method to create a Drag tracker. It is used to drag a figure.
123        */
124       protected Tool createDragTracker(Figure f) {
125          return new DragTracker(editor(), f);
126       }
127    
128       /**
129        * Factory method to create an area tracker. It is used to select an
130        * area.
131        */
132       protected Tool createAreaTracker() {
133          return new SelectAreaTracker(editor());
134       }
135    
136    }