Search in sources :

Example 1 with DragGestureListener

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);
}
Also used : ImageIcon(javax.swing.ImageIcon) MouseEvent(java.awt.event.MouseEvent) DragGestureEvent(java.awt.dnd.DragGestureEvent) DragGestureListener(java.awt.dnd.DragGestureListener) com.mxgraph.model.mxGeometry(com.mxgraph.model.mxGeometry) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) Point(java.awt.Point) com.mxgraph.util.mxPoint(com.mxgraph.util.mxPoint) DragSource(java.awt.dnd.DragSource) Font(java.awt.Font) com.mxgraph.util.mxRectangle(com.mxgraph.util.mxRectangle) MouseListener(java.awt.event.MouseListener) com.mxgraph.swing.util.mxGraphTransferable(com.mxgraph.swing.util.mxGraphTransferable)

Aggregations

com.mxgraph.model.mxGeometry (com.mxgraph.model.mxGeometry)1 com.mxgraph.swing.util.mxGraphTransferable (com.mxgraph.swing.util.mxGraphTransferable)1 com.mxgraph.util.mxPoint (com.mxgraph.util.mxPoint)1 com.mxgraph.util.mxRectangle (com.mxgraph.util.mxRectangle)1 Dimension (java.awt.Dimension)1 Font (java.awt.Font)1 Point (java.awt.Point)1 DragGestureEvent (java.awt.dnd.DragGestureEvent)1 DragGestureListener (java.awt.dnd.DragGestureListener)1 DragSource (java.awt.dnd.DragSource)1 MouseEvent (java.awt.event.MouseEvent)1 MouseListener (java.awt.event.MouseListener)1 ImageIcon (javax.swing.ImageIcon)1 JLabel (javax.swing.JLabel)1