Clover coverage report - Maven Clover report
Coverage timestamp: Thu Oct 11 2007 08:41:48 CEST
file stats: LOC: 142   Methods: 10
NCLOC: 102   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MagneticGridMover.java 75% 75.5% 90% 77.3%
coverage coverage
 1    /*
 2    * Copyright (C) 2007 Eric MIGNOT - mignots.eric@free.fr
 3    *
 4    * This library is free software; you can redistribute it and/or
 5    * modify it under the terms of the GNU Lesser General Public
 6    * License as published by the Free Software Foundation; either
 7    * version 2.1 of the License, or (at your option) any later version.
 8    *
 9    * This library 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 GNU
 12    * 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 library; if not, write to the Free Software
 16    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 17    */
 18   
 19    package org.ericmignot.modeler.util.graphic;
 20   
 21    import java.awt.*;
 22   
 23   
 24    public class MagneticGridMover
 25    {
 26    int magneticGridStep;
 27    private Point candidatPointToMove;
 28    private Point initialEvent;
 29   
 30  40 public MagneticGridMover(int magneticGridStep)
 31    {
 32  40 if (magneticGridStep < 1)
 33    {
 34  0 magneticGridStep = 1;
 35    }
 36  40 this.magneticGridStep = magneticGridStep;
 37  40 this.candidatPointToMove = null;
 38  40 this.initialEvent = null;
 39    }
 40   
 41  8 public void setCandidatPointToMove(int x, int y)
 42    {
 43  8 this.candidatPointToMove = new Point(x, y);
 44    }
 45  55 public void setCandidatPointToMove(Point candidatPointToMove)
 46    {
 47  55 this.candidatPointToMove = candidatPointToMove;
 48    }
 49  2 public Point getNearestGridPosition(int x, int y)
 50    {
 51  2 Point toReturn = null;
 52   
 53  2 this.setCandidatPointToMove(x, y);
 54  2 this.setInitialEvent(x, y);
 55  2 toReturn = this.getNewPositionFromMouseDraggedEvent(
 56    x + this.magneticGridStep,
 57    y + this.magneticGridStep);
 58   
 59  2 return toReturn;
 60    }
 61  4 public boolean isPointOnGrid(int x, int y)
 62    {
 63  4 boolean toReturn = false;
 64   
 65  4 if (x % this.magneticGridStep == 0 && y % this.magneticGridStep == 0)
 66    {
 67  3 toReturn = true;
 68    }
 69   
 70  4 return toReturn;
 71    }
 72   
 73   
 74  40 public void setInitialEvent(int x, int y)
 75    {
 76  40 this.initialEvent = new Point(x, y);
 77    }
 78  4 public void setInitialEvent(Point initialEvent)
 79    {
 80  4 this.initialEvent = initialEvent;
 81    }
 82   
 83    /**
 84    * Returns null if the candidat point mustn't be moved.<br>
 85    * mouseEvent is assumed to be modeler based
 86    */
 87  42 public Point getNewPositionFromMouseDraggedEvent(int mouseEventX, int mouseEventY)
 88    {
 89  0 if (this.candidatPointToMove == null || this.initialEvent == null) return null;
 90   
 91  42 Point mouseEvent = new Point(mouseEventX, mouseEventY);
 92  42 Point toReturn = null;
 93  42 int x = this.candidatPointToMove.x;
 94  42 int y = this.candidatPointToMove.y;
 95  42 boolean moved = false;
 96   
 97  42 if (mouseEvent.x - this.initialEvent.x >= this.magneticGridStep)
 98    {
 99  27 x = this.candidatPointToMove.x + mouseEvent.x - initialEvent.x;
 100  27 x = this.magneticGridStep * (int)(x / this.magneticGridStep);
 101  27 moved = true;
 102    }
 103  42 if (this.initialEvent.x - mouseEvent.x >= this.magneticGridStep)
 104    {
 105  0 x = this.candidatPointToMove.x - this.initialEvent.x + mouseEvent.x;
 106  0 x = this.magneticGridStep * (int)(x / this.magneticGridStep);
 107  0 moved = true;
 108    }
 109  42 if (mouseEvent.y - this.initialEvent.y >= this.magneticGridStep)
 110    {
 111  6 y = this.candidatPointToMove.y + mouseEvent.y - initialEvent.y;
 112  6 y = this.magneticGridStep * (int)(y / this.magneticGridStep);
 113  6 moved = true;
 114    }
 115  42 if (this.initialEvent.y - mouseEvent.y >= this.magneticGridStep)
 116    {
 117  0 y = this.candidatPointToMove.y - this.initialEvent.y + mouseEvent.y;
 118  0 y = this.magneticGridStep * (int)(y / this.magneticGridStep);
 119  0 moved = true;
 120    }
 121   
 122  42 if (moved)
 123    {
 124  31 toReturn = new Point(x, y);
 125    }
 126   
 127  42 return toReturn;
 128    }
 129   
 130  0 public void debug()
 131    {
 132  0 System.out.println("MagneticGridMover :");
 133  0 System.out.println("\t grid = " + this.magneticGridStep);
 134  0 System.out.println("\t candidat = " + this.candidatPointToMove);
 135  0 System.out.println("\t initialEvent = " + this.initialEvent);
 136    }
 137   
 138  19 public Point getCandidatPointToMove()
 139    {
 140  19 return candidatPointToMove;
 141    }
 142    }