use of java.awt.dnd.DragGestureListener in project vcell by virtualcell.
the class EditorPalette method addTemplate.
/**
* @param name
* @param icon
* @param cell
*/
public void addTemplate(final String name, ImageIcon icon, mxCell cell) {
mxRectangle bounds = (mxGeometry) cell.getGeometry().clone();
final mxGraphTransferable t = new mxGraphTransferable(new Object[] { cell }, bounds);
// Scales the image if it's too large for the library
if (icon != null) {
if (icon.getIconWidth() > 32 || icon.getIconHeight() > 32) {
icon = new ImageIcon(icon.getImage().getScaledInstance(32, 32, 0));
}
}
final JLabel entry = new JLabel(icon);
entry.setPreferredSize(new Dimension(50, 50));
entry.setBackground(EditorPalette.this.getBackground().brighter());
entry.setFont(new Font(entry.getFont().getFamily(), 0, 10));
entry.setVerticalTextPosition(JLabel.BOTTOM);
entry.setHorizontalTextPosition(JLabel.CENTER);
entry.setIconTextGap(0);
entry.setToolTipText(name);
entry.setText(name);
entry.addMouseListener(new MouseListener() {
/*
* (non-Javadoc)
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
*/
public void mousePressed(MouseEvent e) {
setSelectionEntry(entry, t);
}
/*
* (non-Javadoc)
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
*/
public void mouseClicked(MouseEvent e) {
}
/*
* (non-Javadoc)
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
*/
public void mouseEntered(MouseEvent e) {
}
/*
* (non-Javadoc)
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
*/
public void mouseExited(MouseEvent e) {
}
/*
* (non-Javadoc)
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
*/
public void mouseReleased(MouseEvent e) {
}
});
// Install the handler for dragging nodes into a graph
DragGestureListener dragGestureListener = new DragGestureListener() {
/**
*/
public void dragGestureRecognized(DragGestureEvent e) {
e.startDrag(null, mxSwingConstants.EMPTY_IMAGE, new Point(), t, null);
}
};
DragSource dragSource = new DragSource();
dragSource.createDefaultDragGestureRecognizer(entry, DnDConstants.ACTION_COPY, dragGestureListener);
add(entry);
}
Aggregations