/*
Copyright @ 2004, The Institute for Genomic Research (TIGR).
All rights reserved.
*/
/*
* $RCSfile: MessageDropper.java,v $
* $Revision: 1.0 $
* $Date: 2003/08/22 19:22:54 $
* $Author: jli $
* $State: Exp $
*/
/***************************************************************************
* Author: Jianwei(Jerry) Li
* Name: MessageDropper (Version 1.0)
* Date: Created: 05/04/2004 and modified: 10/07/2004
* Descp: A Java button that distrubutes a message to a component by Drag And Drop
* methods.
***************************************************************************/
package org.tigr.util.swing;
import java.awt.datatransfer.StringSelection;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.Icon;
import javax.swing.JLabel;
public class MessageDropper extends JLabel
implements DragGestureListener, DragSourceListener, MouseListener {
private boolean gActiveBorder;
private BevelBorder gHigh, gFlat;
private DragSource dragSource;
private LineBorder gLine;
private String msg;
/**************************************************************************
* Constructor: without parameter.
**************************************************************************/
public MessageDropper(){
this(new String(""));
}
/***************************************************************************
* Constructor: with label passed.
***************************************************************************/
public MessageDropper(String label) {
this(label, new String(""));
}
/***************************************************************************
* Constructor: with an icon passed.
***************************************************************************/
public MessageDropper(Icon label) {
this(label, new String(""));
}
/***************************************************************************
* Constructor: with a label and icon passed.
***************************************************************************/
public MessageDropper(String label, Icon icon) {
this(label, icon, new String(""));
}
/***************************************************************************
* Constructor: with label and message passed.
***************************************************************************/
public MessageDropper(String label, String message) {
super(label);
msg = message;
initDrag();
}
/***************************************************************************
* Constructor: with an icon passed.
***************************************************************************/
public MessageDropper(Icon label, String message) {
super(label);
msg = message;
initDrag();
}
/***************************************************************************
* Constructor: with an icon and label passed.
***************************************************************************/
public MessageDropper(String label, Icon icon, String message) {
super(label, icon, JLabel.HORIZONTAL);
msg = message;
initDrag();
}
public void dragGestureRecognized(DragGestureEvent e) {
e.startDrag(DragSource.DefaultCopyDrop, new StringSelection(msg), this);
}
public void dragDropEnd(DragSourceDropEvent e) {
this.setEnabled(true);
if(gActiveBorder){
this.setBorder(gFlat);
}
}
public void dragEnter(DragSourceDragEvent e) {}
public void dragExit(DragSourceEvent e) {}
public void dragOver(DragSourceDragEvent e) {}
public void dropActionChanged(DragSourceDragEvent e) {}
public void mouseClicked(MouseEvent me){
this.firePropertyChange(this.getName(), false, true);
this.setEnabled(true);
}
public void mouseEntered(MouseEvent me){
if(gActiveBorder){
this.setBorder(gHigh);
}
}
public void mouseExited(MouseEvent me){
if(gActiveBorder){
this.setBorder(gFlat);
}
}
public void mousePressed(MouseEvent me){
this.setEnabled(false);
}
public void mouseReleased(MouseEvent me){}
public void needBorder(boolean yes){
if(yes){
this.setBorder(gLine);
}
}
public void setBorderActive(boolean active){
gActiveBorder = active;
}
public void setMessage(String message){
msg = message;
}
private void initDrag(){
gHigh = new BevelBorder(0);
gFlat = new BevelBorder(2);
gLine = new LineBorder(Color.gray, 1);
gActiveBorder = true;
dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(this,
DnDConstants.ACTION_COPY_OR_MOVE, this);
this.addMouseListener(this);
}
}