Package simtools.diagram

Source Code of simtools.diagram.ElementSelection

/* ==============================================
* Simtools : The tools library used in JSynoptic
* ==============================================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 1999-2003, by :
*     Corporate:
*         Astrium SAS
*         EADS CRC
*     Individual:
*         Claude Cazenave
*     Nicolas Brodu
*
*
* $Id: ElementSelection.java,v 1.1 2009/01/08 16:06:45 ogor Exp $
*
* Changes
* -------
* 25-Sep-2003 : Initial public release (NB);
*
*/
package simtools.diagram;

import java.awt.*;

import java.awt.geom.Rectangle2D;

import javax.swing.undo.UndoableEdit;

import simtools.diagram.undo.TranslateEdit;

/**
* This class holds an element selection and its first and future locations
* @see DiagramComponent
*
* @author Claude Cazenave
*
* @version 1.0 1999
*/
public class ElementSelection {
  protected Element element;
 
  /** The element position before translation operation */
  protected Point initPoint;
 
  /** The element position after translation operation */
  protected Point currentPoint;
 
  /** The element position without taking in account the grid steps */
  protected Point referencePoint;

  /**
   * Constructs a new selected shape
   * @param s the shape
   * @param p its current position
   */
  public ElementSelection(Element s, Point initPoint){
      this.element=s;
      this.initPoint = new Point(initPoint);
      this.currentPoint  =  new Point(initPoint);
      this.referencePoint  =  new Point(initPoint)
  }
 
  public Shape getShape(){
      return element;
  }

  public void translateShape(int dx, int dy){
      element.translate(dx, dy);
      currentPoint.x += dx;
      currentPoint.y += dy;
  }
  public void translateReferencePoint(int dx, int dy){
      referencePoint.x += dx;
      referencePoint.y += dy;
  }
   
    /**
     * @return the translation edit
     */
  public UndoableEdit translateShapeEnd(){
      UndoableEdit res = null;
      res =  new TranslateEdit(currentPoint.x - initPoint.x, currentPoint.y - initPoint.y, element);
     
      initPoint = new Point(currentPoint)
      referencePoint = new Point(initPoint);
      return res;
  }


    /**
     * Draw shape bounds when the shape is selected
     * @param g2
     */
    public void drawBounds(Graphics2D g2) {
        Rectangle2D b=element.getBounds2D();

        int ox,oy,x,xb,y,w,h;
        ox=(int)b.getX();
        oy=(int)b.getY();
        w=(int)b.getWidth();
        h=(int)b.getHeight();

        //      draw a rectangle one pixel inside
        //      because fillRect goes to w-1, h-1 according to doc => it matchs at resolution 100%
        //      and also because g2.draw(b) does apply a Stroke, and then some garbage remains when
        //      the selected shape size change
        //      Note : at resolution 500%, the squares on the side are less than 1px outside the inner rectangle
        //      => funny artefact, but not at all annoying.
        g2.drawRect(ox,oy,w-1,h-1);
        g2.fillRect(ox,oy,5,5);
        x=xb=ox+w-5;
        g2.fillRect(x,oy,5,5);
        y=oy+h-5;
        g2.fillRect(x,y,5,5);
        g2.fillRect(ox,y,5,5);
        x=ox+w/2-2;
        g2.fillRect(x,oy,5,5);
        g2.fillRect(x,y,5,5);
        y=oy+h/2-2;
        g2.fillRect(ox,y,5,5);
        g2.fillRect(xb,y,5,5);
    }
}

TOP

Related Classes of simtools.diagram.ElementSelection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.