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

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

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
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.Observable;
import java.util.Observer;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JLayeredPane;

/**
 * A view of the line connecting question group view and rule 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 XGroupRuleLine extends JComponent
{
  
  private static final Color LINE_COLOR = Color.BLACK;
  
  protected XGroupView groupView;
  protected XRuleView ruleView;
  protected int orientation;
  protected int expanderNr;
  protected XRulesEditorPane editorPane;
  
  public XGroupRuleLine( XGroupView g, XRuleView r, XRulesEditorPane ep )
  {
    super();
    setSize( 10, 10 ); //???
    groupView = g;
    ruleView = r; 
    setFocusable( false );
    expanderNr = -1;
    editorPane = ep;
    editorPane.add( this, JLayeredPane.PALETTE_LAYER );
    setLocation();
  }
  
  public XGroupView getGroupView()
  {
    return groupView;
  }
  
  public XRuleView getRuleView()
  {
    return ruleView;
  }
  
  public void moveBy( int dx, int dy )
  {
    int x = ( int ) getLocation().getX() + dx;
    int y = ( int ) getLocation().getY() + dy;
    setLocation( x, y );
  }
  
  public void setLocation()
  {
    //int x = 0, y = 0, w = 0, h = 0;    
    int x = 10, y = 10, w = 10, h = 10;
    orientation = 0;
    int newExpanderNr = -1;
    
    int gx1 = ( int ) groupView.getLocation().getX();
    int gy1 = ( int ) groupView.getLocation().getY();
    int gx2 = gx1 + ( int ) groupView.getSize().getWidth();
    int gy2 = gy1 + ( int ) groupView.getSize().getHeight();
    int cgx = ( gx1 + gx2 ) / 2;
    int cgy = ( gy1 + gy2 ) / 2;
    
    int rx1 = ( int ) ruleView.getLocation().getX();
    int ry1 = ( int ) ruleView.getLocation().getY();
    int rx2 = rx1 + ( int ) ruleView.getSize().getWidth();
    int ry2 = ry1 + ( int ) ruleView.getSize().getHeight();
    int crx = ( rx1 + rx2 ) / 2;
    int cry = ( ry1 + ry2 ) / 2;

    if ( ry1 > gy2 || gy1 > ry2 ) {          
      w = Math.abs( crx - cgx );
      h = Math.min( Math.abs( ry1 - gy2) , Math.abs( ry2 - gy1 ) );
      x = Math.min( cgx, crx );
      y = Math.min( gy2, ry2 );
      if ( cgy < cry ) {
        orientation = ( cgx < crx ? 1 : 2 );
        newExpanderNr = 2;
      } else {
        orientation = ( cgx < crx ? 2 : 1 );
        newExpanderNr = 0;
      }
   } 
   else if ( rx1 > gx2 || gx1 > rx2 ) {
      w = Math.min( Math.abs( rx1 - gx2 ), Math.abs( rx2 - gx1 ) );
      h = Math.abs( cgy - cry );
      x = Math.min( gx2, rx2 );
      y = Math.min( cgy, cry );
      if ( cgx < crx ) {
        orientation = ( cgy < cry ? 3 : 4 );
        newExpanderNr = 1;
      } 
      else {
        orientation = ( cgy < cry ? 4 : 3 );
        newExpanderNr = 3;
      }
   }   
    
   setSize( w + 1, h + 1 );
   setLocation( x, y );
   
   if ( newExpanderNr >= 0 && newExpanderNr != expanderNr ) {
     if ( expanderNr >= 0 )
         groupView.removeGroupRuleLine( expanderNr, this );     
     groupView.addGroupRuleLine( newExpanderNr, this );
     expanderNr = newExpanderNr;     
   }
  }
  
  protected void paintLine( Graphics2D g )
  {
    g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
    g.setColor( LINE_COLOR );
    
    int w = getSize().width - 1;
    int h = getSize().height - 1;       
    
    switch ( orientation ) {
      case 1:
        g.drawLine( 0, 0, 0, h / 2 );
        g.drawLine( 0, h / 2, w, h / 2 );
        g.drawLine( w, h / 2, w, h );
        break;
      case 2:
        g.drawLine( 0, h, 0, h / 2 );
        g.drawLine( 0, h / 2, w, h / 2 );
        g.drawLine( w, h / 2, w, 0 );
        break;
      case 3:
        g.drawLine( 0, 0, w / 2, 0 );
        g.drawLine( w / 2, 0, w / 2, h );
        g.drawLine( w / 2, h, w, h );
        break;
      case 4:
        g.drawLine( 0, h, w / 2, h );
        g.drawLine( w / 2, h, w / 2, 0 );
        g.drawLine( w / 2, 0, w, 0 );
        break;
    }
    
  }
  
  public void paint( Graphics gr )
  {        
    Graphics2D g = ( Graphics2D ) gr; 
    paintLine( g );
  }
  
}
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.