XRolloverComponent.java :  » XML-UI » xui32 » com » xoetrope » svg » Java Open Source

Java Open Source » XML UI » xui32 
xui32 » com » xoetrope » svg » XRolloverComponent.java
/*
 * XRolloverComponent.java
 *
 * Created on 12 February 2007, 11:40
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.xoetrope.svg;

import com.xoetrope.swing.XGraph;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;

/**
 * 
 *
 * <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.2 $</p>
 */
public class XRolloverComponent extends JComponent implements MouseListener
{
  protected int x, y;
  protected String path;
  protected JComponent [] components;
  protected JLabel marker;
  ImageIcon icon;
  
  /** 
   * Creates a new instance of XRolloverComponent 
   * @param x the x-coordinate of this component.
   * @param y the y-coordinate of this component.
   * @param component the <CODE>Component</CODE> that will be displayed when there is a rollover event.
   */
  public XRolloverComponent( JComponent [] components ) 
  {
    this.components = components;
    icon = new ImageIcon( getClass().getResource( "images/marker2.gif" ) );  
    marker = new JLabel( icon );
    marker.addMouseListener( this );
  }

  /**
   * If the mouse is detected entering this component, display the rollover event component.
   * @param e the fired <CODE>MouseEvent</CODE>.
   */
  public void mouseEntered( MouseEvent e ) 
  { 
    try {
      Thread.sleep( 200 );
    } 
    catch (InterruptedException ex){
      ex.printStackTrace();
    }       
    
    int parentWidth = getParent().getWidth();
    int parentHeight = getParent().getHeight();
    double x = 0, y = 0;
    
    for( int i = 0; i < components.length; i++ ){    
      if( i == 0 ){
        if( getX() < ( parentWidth / 2 ) && getY() < ( parentHeight / 2 ) ){  // 1
          x = getX() + 20;
          y = getY();
        }       
        else if( getX() >= ( parentWidth / 2 ) && getY() < ( parentHeight / 2 ) ){ // 2
          x = getX() - ( components[ i ].getWidth() + 10 );
          y = getY();
        }
        else if( getX() < ( parentWidth / 2 ) && getY() >= ( parentHeight / 2 ) ){ // 3
          x = getX() + 20;
          y = getY() - ( components[ i ].getHeight() - 10 );
        }
        else if( getX() >= ( parentWidth / 2 ) && getY() >= ( parentHeight / 2 ) ){  // 4
          x = getX() - ( components[ i ].getWidth() + 10 );
          y = getY() - ( components[ i ].getHeight() - 10 );
        }      
      }
      else{
         if( getX() < ( parentWidth / 2 ) && getY() < ( parentHeight / 2 ) ){  // 1
          x = getX() + components[ i - 1 ].getWidth() + 30;
          y = getY();
        }       
        else if( getX() >= ( parentWidth / 2 ) && getY() < ( parentHeight / 2 ) ){ // 2
          x = getX() - ( components[ i ].getWidth() + 20 + components[ i - 1 ].getWidth() );
          y = getY();
        }
        else if( getX() < ( parentWidth / 2 ) && getY() >= ( parentHeight / 2 ) ){ // 3
          x = getX() + components[ i - 1 ].getWidth() + 30;
          y = getY() - ( components[ i ].getHeight() - 10 );
        }
        else if( getX() >= ( parentWidth / 2 ) && getY() >= ( parentHeight / 2 ) ){  // 4
          x = getX() - ( components[ i ].getWidth() + components[ i - 1 ].getWidth() + 20 );
          y = getY() - ( components[ i ].getHeight() - 10 ); 
        }        
      }
      
      components[ i ].setBounds( (int)x, (int)y, components[ i ].getWidth(), components[ i ].getHeight() );
      getParent().add( components[ i ], 0 );    
      components[i].repaint();
    }
    getParent().repaint(); 
  }

  /**
   * If the mouse is detected exiting this component, hide the rollover event component.
   * @param e the fired <CODE>MouseEvent</CODE>.
   */
  public void mouseExited( MouseEvent e ) 
  {
    try {
      Thread.sleep( 400 );
    } 
    catch (InterruptedException ex){
      ex.printStackTrace();
    }       
    
    for( int i = 0; i < components.length; i++ ){
      getParent().remove( components[ i ] );
    } 
    getParent().repaint(); 
  }    

  /**
   * Sets the size of the component when it is painted.
   */
  public void paintComponent( Graphics g )
  {
    marker.setBounds( 0, 0, 10, 10 );
    add( marker );
  }
  
  public void mouseClicked(MouseEvent e) 
  {
  }

  public void mousePressed(MouseEvent e) 
  {
  }

  public void mouseReleased(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.