001: /*
002: * XRolloverComponent.java
003: *
004: * Created on 12 February 2007, 11:40
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package com.xoetrope.svg;
011:
012: import com.xoetrope.swing.XGraph;
013: import java.awt.BorderLayout;
014: import java.awt.Color;
015: import java.awt.Component;
016: import java.awt.Graphics;
017: import java.awt.Image;
018: import java.awt.event.MouseEvent;
019: import java.awt.event.MouseListener;
020: import javax.swing.Icon;
021: import javax.swing.ImageIcon;
022: import javax.swing.JComponent;
023: import javax.swing.JLabel;
024:
025: /**
026: *
027: *
028: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
029: * the GNU Public License (GPL), please see license.txt for more details. If
030: * you make commercial use of this software you must purchase a commercial
031: * license from Xoetrope.</p>
032: * <p> $Revision: 1.2 $</p>
033: */
034: public class XRolloverComponent extends JComponent implements
035: MouseListener {
036: protected int x, y;
037: protected String path;
038: protected JComponent[] components;
039: protected JLabel marker;
040: ImageIcon icon;
041:
042: /**
043: * Creates a new instance of XRolloverComponent
044: * @param x the x-coordinate of this component.
045: * @param y the y-coordinate of this component.
046: * @param component the <CODE>Component</CODE> that will be displayed when there is a rollover event.
047: */
048: public XRolloverComponent(JComponent[] components) {
049: this .components = components;
050: icon = new ImageIcon(getClass().getResource(
051: "images/marker2.gif"));
052: marker = new JLabel(icon);
053: marker.addMouseListener(this );
054: }
055:
056: /**
057: * If the mouse is detected entering this component, display the rollover event component.
058: * @param e the fired <CODE>MouseEvent</CODE>.
059: */
060: public void mouseEntered(MouseEvent e) {
061: try {
062: Thread.sleep(200);
063: } catch (InterruptedException ex) {
064: ex.printStackTrace();
065: }
066:
067: int parentWidth = getParent().getWidth();
068: int parentHeight = getParent().getHeight();
069: double x = 0, y = 0;
070:
071: for (int i = 0; i < components.length; i++) {
072: if (i == 0) {
073: if (getX() < (parentWidth / 2)
074: && getY() < (parentHeight / 2)) { // 1
075: x = getX() + 20;
076: y = getY();
077: } else if (getX() >= (parentWidth / 2)
078: && getY() < (parentHeight / 2)) { // 2
079: x = getX() - (components[i].getWidth() + 10);
080: y = getY();
081: } else if (getX() < (parentWidth / 2)
082: && getY() >= (parentHeight / 2)) { // 3
083: x = getX() + 20;
084: y = getY() - (components[i].getHeight() - 10);
085: } else if (getX() >= (parentWidth / 2)
086: && getY() >= (parentHeight / 2)) { // 4
087: x = getX() - (components[i].getWidth() + 10);
088: y = getY() - (components[i].getHeight() - 10);
089: }
090: } else {
091: if (getX() < (parentWidth / 2)
092: && getY() < (parentHeight / 2)) { // 1
093: x = getX() + components[i - 1].getWidth() + 30;
094: y = getY();
095: } else if (getX() >= (parentWidth / 2)
096: && getY() < (parentHeight / 2)) { // 2
097: x = getX()
098: - (components[i].getWidth() + 20 + components[i - 1]
099: .getWidth());
100: y = getY();
101: } else if (getX() < (parentWidth / 2)
102: && getY() >= (parentHeight / 2)) { // 3
103: x = getX() + components[i - 1].getWidth() + 30;
104: y = getY() - (components[i].getHeight() - 10);
105: } else if (getX() >= (parentWidth / 2)
106: && getY() >= (parentHeight / 2)) { // 4
107: x = getX()
108: - (components[i].getWidth()
109: + components[i - 1].getWidth() + 20);
110: y = getY() - (components[i].getHeight() - 10);
111: }
112: }
113:
114: components[i].setBounds((int) x, (int) y, components[i]
115: .getWidth(), components[i].getHeight());
116: getParent().add(components[i], 0);
117: components[i].repaint();
118: }
119: getParent().repaint();
120: }
121:
122: /**
123: * If the mouse is detected exiting this component, hide the rollover event component.
124: * @param e the fired <CODE>MouseEvent</CODE>.
125: */
126: public void mouseExited(MouseEvent e) {
127: try {
128: Thread.sleep(400);
129: } catch (InterruptedException ex) {
130: ex.printStackTrace();
131: }
132:
133: for (int i = 0; i < components.length; i++) {
134: getParent().remove(components[i]);
135: }
136: getParent().repaint();
137: }
138:
139: /**
140: * Sets the size of the component when it is painted.
141: */
142: public void paintComponent(Graphics g) {
143: marker.setBounds(0, 0, 10, 10);
144: add(marker);
145: }
146:
147: public void mouseClicked(MouseEvent e) {
148: }
149:
150: public void mousePressed(MouseEvent e) {
151: }
152:
153: public void mouseReleased(MouseEvent e) {
154: }
155: }
|