palettebutton.java

来自「全面实现ilog地功能,没有使用第三方lib.」· Java 代码 · 共 116 行

JAVA
116
字号
/* * This source code is part of TWaver 1.3.1 * * SERVA Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * Copyright 2000-2005 SERVA Software, Inc. All rights reserved. */package demo.editor;

import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Insets;import java.awt.datatransfer.StringSelection;import java.awt.datatransfer.Transferable;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.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.JLabel;import javax.swing.SwingConstants;import javax.swing.border.Border;import twaver.Element; 
public class PaletteButton extends JLabel implements DragGestureListener, DragSourceListener {
    private DragSource dragSource = new DragSource();
    private final static int SIDE = 24;
    private final static Dimension SIZE = new Dimension(SIDE, SIDE);
    private final static Border FOCUS_BORDER = new Border() {
        private Insets insets = new Insets(1, 1, 1, 1);
        public void paintBorder(Component c,
                                Graphics g,
                                int x,
                                int y,
                                int width,
                                int height) {
            g.setColor(Color.lightGray);
            g.draw3DRect(x, y, width - 1, height - 1, true);
        }

        public Insets getBorderInsets(Component c) {
            return insets;
        }

        public boolean isBorderOpaque() {
            return true;
        }
    };
    private final static Border NO_FOCUS_BORDER = null;
    private final Class type;
    private Element element = null;

    public PaletteButton(Class type) {
        this.type = type;
        try {
            element = (Element) type.newInstance();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_MOVE, this);

        this.setVerticalAlignment(SwingConstants.CENTER);
        this.setHorizontalAlignment(SwingConstants.CENTER);
        this.setSize(SIZE);
        this.setMinimumSize(SIZE);
        this.setMaximumSize(SIZE);
        this.setPreferredSize(SIZE);
        this.setIcon(element.getIcon());
        this.setToolTipText(type.getName());
        this.setBorder(NO_FOCUS_BORDER);
        this.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                setBorder(FOCUS_BORDER);
            }

            public void mouseExited(MouseEvent e) {
                setBorder(NO_FOCUS_BORDER);
            }
        });
    }

    public Element getElement() {
        return element;
    }

    public void dragGestureRecognized(DragGestureEvent evt) {
        Transferable t = new StringSelection(type.getName());
        dragSource.startDrag(evt, DragSource.DefaultCopyDrop, t, this);
        this.setBorder(NO_FOCUS_BORDER);
    }

    public void dragEnter(DragSourceDragEvent evt) {
    }

    public void dragOver(DragSourceDragEvent evt) {
    }

    public void dragExit(DragSourceEvent evt) {
    }

    public void dropActionChanged(DragSourceDragEvent evt) {
    }

    public void dragDropEnd(DragSourceDropEvent evt) {
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?