/**
 * Die Klasse stellt ein Label bereit, das der Mausverfolgung unterliegt. Hierdurch kann z.B. eine
 * Drag-Leiste auf Elementen erm&ouml;glicht werden.
 * Das Label hat per default eine H&ouml;he von 25px, ist in der Systemfarbe
 * <code>SystemColor.inactiveCaption</code> eingef&auml;rbt und mit einer
 * <code>EtchedBorder</code> versehen.<br>
 * Die Positionsberechnung erfolgt durch Verrechnung der Mausposition und der
 * Komponentenposition unter der Annahme, dass das Label oben links in der
 * Komponente steht.
 * 
 */

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.SystemColor;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
/*
 * @JB_DragLabel.java     12.02.2010
 * build: 12.02.2010
 * Copyright 2010 javabeginners.de. All rights reserved
 * @author Joerg Czeschla
 * 
 * This file is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This file 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 General Public License for more details.

 * You should have received a copy of the GNU General Public License along with this file;
 * if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */


public class JB_DragLabel extends JLabel implements MouseListener,
		MouseMotionListener {

	private static final long serialVersionUID = 1L;
	
	/**
	 * Hintergrundfarbe
	 */
	private static final Color BG_COLOR = SystemColor.inactiveCaption;
	
	/**
	 * Randtyp
	 */
	private static final Border BORDER = new EtchedBorder();
	
	/**
	 * H&ouml;he
	 */
	private static final int HEIGHT = 25;


	//Die Komponente, die verschoben werden soll.
	private Window comp;

	private Point mousePoint;

	public JB_DragLabel(Window comp) {
		this.comp = comp;
		this.setOpaque(true);
		this.setBackground(BG_COLOR);
		this.setBorder(BORDER);
		this.setPreferredSize(new Dimension(this.getWidth(), HEIGHT));

		this.addMouseListener(this);
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		this.addMouseMotionListener(this);
		mousePoint = new Point(e.getX(), e.getY());
	}

	public void mouseReleased(MouseEvent e) {
		this.removeMouseMotionListener(this);
	}

	public void mouseDragged(MouseEvent e) {

		java.awt.PointerInfo info = java.awt.MouseInfo.getPointerInfo();
		Point p = info.getLocation();
		int x = p.x;
		int y = p.y;
		int xPos = x - mousePoint.x;
		int yPos = y - mousePoint.y;

		comp.setLocation(xPos, yPos);
	}

	public void mouseMoved(MouseEvent e) {
	}
}

