XLine.java :  » XML-UI » xui32 » com » xoetrope » carousel » survey » Java Open Source

Java Open Source » XML UI » xui32 
xui32 » com » xoetrope » carousel » survey » XLine.java
package com.xoetrope.carousel.survey;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JLayeredPane;
import javax.swing.SwingUtilities;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XProjectManager;

/**
 * A view of the line connection two question group views or connecting
 * rule view with target question group view.
 *
 * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
 * the GNU Public License (GPL), please see license.txt for more details. If
 * you make commercial use of this software you must purchase a commercial
 * license from Xoetrope.</p>
 * <p> $Revision: 1.5 $</p>
 */
public class XLine extends JComponent implements MouseListener, MouseMotionListener
{    
  public static Color LINE_COLOR = Color.BLACK;  
  public static Color DRAG_COLOR = Color.RED;
  public static double ARLEN = 25;
  public static double ARANG = 20;
      
  protected int orientation;
  protected XRulesEditorPane editorPane;  
  protected boolean mousePressed;
  protected boolean mouseDragged;
  protected MouseEvent firstMouseEvent;
  protected Polygon shape;
  protected Point dragPoint;
  protected XSource source;
  protected XGroupView groupView;
  protected XGroupView intersectedGroupView;
  protected double arLen;

  protected static double getScale()
  {
    return XRulesEditorPane.SCALE;
  }
  
  public XLine( XSource s, XGroupView gv, XRulesEditorPane ep )
  {
    super();
    source = s;
    groupView = gv;
    intersectedGroupView = null;
    editorPane = ep;
    mousePressed = false;
    mouseDragged = false;
    firstMouseEvent = null;
    dragPoint = null;
    shape = null;
    arLen = ARLEN;
    addMouseListener( this );
    addMouseMotionListener( this );
    editorPane.add( this, JLayeredPane.PALETTE_LAYER );
    setLocation();
  }

  public void setLocation()
  {
    if ( groupView == null ) return;
    
    orientation = 0;
    int x = 10, y = 10, w = 10, h = 10;
        
    int gx1 = ( mouseDragged ? ( int ) dragPoint.getX() : ( int ) groupView.getLocation().getX() );
    int gy1 = ( mouseDragged ? ( int ) dragPoint.getY() : ( int ) groupView.getLocation().getY() );
    int gx2 = gx1 + ( mouseDragged ? 0 : ( int ) groupView.getSize().getWidth() );
    int gy2 = gy1 + ( mouseDragged ? 0 : ( int ) groupView.getSize().getHeight() );        
    
    int gx = ( gx1 + gx2 ) / 2;
    int gy = ( gy1 + gy2 ) / 2;
    
    int gix = 0, giy = 0;
    Point sourcePoint = source.getCenterPoint();
    int rix = sourcePoint.x, riy = sourcePoint.y;
    int dx = rix - gx;
    int dy = riy - gy;
    double xy = ( double ) dx / dy;
    double yx = ( double ) dy / dx;        
    
    if ( rix <= gx ) {
      if ( riy <= gy ) {        
        orientation = 1;        
        giy = ( int ) ( ( gx1 - rix ) * yx + riy );
        gix = ( int ) ( ( gy1 - riy ) * xy + rix );
        if ( giy >= gy1 && giy <= gy2 ) gix = gx1;
        else giy = gy1;        
      } 
      else {        
        orientation = 2;        
        giy = ( int ) ( ( gx1 - rix ) * yx + riy );
        gix = ( int ) ( ( gy2 - riy ) * xy + rix );
        if ( giy >= gy1 && giy <= gy2 ) 
          gix = gx1;
        else 
          giy = gy2;        
      }
    } 
    else {
      if ( riy <= gy ) {
        orientation = 3; 
        giy = ( int ) ( ( gx2 - rix ) * yx + riy );
        gix = ( int ) ( ( gy1 - riy ) * xy + rix );
        if ( giy >= gy1 && giy <= gy2 ) 
          gix = gx2;
        else 
          giy = gy1;                
      } 
      else {
        orientation = 4; 
        giy = ( int ) ( ( gx2 - rix ) * yx + riy );
        gix = ( int ) ( ( gy2 - riy ) * xy + rix );
        if ( giy >= gy1 && giy <= gy2 ) 
          gix = gx2;
        else 
          giy = gy2;                        
      }    
    }
    
    int arLen = ( int ) getArLen();
    w = Math.abs( rix - gix ) + 2 * arLen;
    h = Math.abs( riy - giy ) + 2 * arLen;
    x = Math.min( rix, gix ) - arLen;
    y = Math.min( riy, giy ) - arLen;
        
    setSize( w, h );
    setLocation( x, y );
  }
  
  protected void paintLine( Graphics2D g )
  {
    g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
    if ( mouseDragged && intersectedGroupView == null )
      g.setColor( DRAG_COLOR );
    else
      g.setColor( LINE_COLOR );
    
    int w = ( int ) getSize().getWidth();
    int h = ( int ) getSize().getHeight();

    int marg = ( int ) getArLen();
    w -= 2 * marg;
    h -= 2 * marg;
    
    double rad = Math.toRadians( ARANG );        
    double a = ( w != 0 ? Math.atan( ( double ) h / w ) : Math.PI / 2 );
    
    int x0 = 0;
    int y0 = 0;
    double arLen = getArLen();
    int x1 = ( int ) ( Math.cos( a - rad ) * arLen );   
    int y1 = ( int ) ( Math.sin( a - rad ) * arLen );
    int x2 = ( int ) ( Math.cos( a + rad ) * arLen );
    int y2 = ( int ) ( Math.sin( a + rad ) * arLen );
    int x3 = 0;
    int y3 = 0;
        
    w += marg; h+= marg;

    switch ( orientation ) {
      case 1:
        x0 = marg; y0 = marg;
        x1 = w - x1; y1 = h - y1;
        x2 = w - x2; y2 = h - y2;
        x3 = w; y3 = h;
        break;
      case 2:       
        x0 = marg; y0 = h;
        x1 = w - x1; y1 = y1 + marg;
        x2 = w - x2; y2 = y2 + marg;
        x3 = w; y3 = marg;
        break;
      case 3:
        x0 = w; y0 = marg;
        x1 = x1 + marg; y1 = h - y1;
        x2 = x2 + marg; y2 = h - y2;
        x3 = marg; y3 = h;
        break;
      case 4:        
        x0 = w; y0 = h;
        x1 = x1 + marg; y1 = y1 + marg;
        x2 = x2 + marg; y2 = y2 + marg;
        x3 = marg; y3 = marg;
        break;
    }        
    
    int[] xPoints = { x1, x3, x2 };
    int[] yPoints = { y1, y3, y2 };
    g.drawLine( x0, y0, x3, y3 );
    g.fillPolygon( xPoints, yPoints, 3 );
    
    int dx = ( x1 - x2 );
    int dy = ( y1 - y2 );
    int px1 = x0 - dx;
    int py1 = y0 - dy;
    int px2 = x0 + dx;
    int py2 = y0 + dy;
    int px3 = x3 - dx;
    int py3 = y3 - dy;
    int px4 = x3 + dx;
    int py4 = y3 + dy;
    int[] pxPoints = { px1, px2, px3, px4 };
    int[] pyPoints = { py1, py2, py3, py4 };
    shape = new Polygon( pxPoints, pyPoints, 4 );    
  }

  public void paint( Graphics g )
  {
    Graphics2D g2d = ( Graphics2D ) g;
    paintLine( g2d );
  }
  
  public boolean contains( int x, int y )
  {
    return ( shape != null && shape.contains( new Point( x, y ) ) );
  }

  public XGroupView getGroupView()
  {
    return groupView;
  }
  
  public void setGroupView( XGroupView gv )
  {
    groupView = gv;
  }

  public XSource getSource()
  {
    return source;
  }
  
  public double getArLen()
  {
    return getScale() * arLen;
  }
  
  public void setArLen( double al )
  {
    arLen = al / getScale();
  }
  
  public void mouseClicked(MouseEvent e) {
  }

  public void mousePressed(MouseEvent e)
  {
    editorPane.moveToFront( this );
    source.moveToFront();
    firstMouseEvent = e;
    mousePressed = true;
  }

  public void mouseReleased(MouseEvent e)
  {
    mousePressed = false;
    mouseDragged = false;
    firstMouseEvent = null;  
    if ( intersectedGroupView != null && !intersectedGroupView.equals( groupView ) ) {
      groupView.removeTargetLine( this );
      groupView = intersectedGroupView;
      source.targetChanged( intersectedGroupView );
      groupView.addTargetLine( this );      
    }
    setLocation();
    editorPane.repaint();
  }

  public void mouseEntered( MouseEvent e )  
  {
  }

  public void mouseExited( MouseEvent e ) 
  {
  }

  public void mouseDragged( MouseEvent e)
  {
    if ( !mousePressed ) 
      return;
    
    mouseDragged = true;    
    dragPoint = SwingUtilities.convertPoint( this, e.getPoint(), editorPane );
    intersectedGroupView = editorPane.getIntGroupView( dragPoint );
    setLocation();
  }

  public void mouseMoved( MouseEvent e ) 
  {
  }  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.