43 lines
1.1 KiB
Java
43 lines
1.1 KiB
Java
package de.ph87.electro.sidebar;
|
|
|
|
import de.ph87.electro.circuit.part.Part;
|
|
import lombok.Getter;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.datatransfer.StringSelection;
|
|
import java.awt.dnd.DnDConstants;
|
|
import java.awt.dnd.DragSource;
|
|
|
|
import static de.ph87.electro.CONFIG.RASTER;
|
|
|
|
@Getter
|
|
public class SidebarPart extends JPanel {
|
|
|
|
private final Part part;
|
|
|
|
public SidebarPart(final Part part) {
|
|
this.part = part;
|
|
setTransferHandler(new TransferHandler("text"));
|
|
setPreferredSize(new Dimension(RASTER, RASTER));
|
|
createDragSource(part);
|
|
}
|
|
|
|
private void createDragSource(final Part part) {
|
|
final DragSource dragSource = new DragSource();
|
|
dragSource.createDefaultDragGestureRecognizer(
|
|
this,
|
|
DnDConstants.ACTION_COPY_OR_MOVE,
|
|
event -> dragSource.startDrag(event, DragSource.DefaultCopyDrop, new StringSelection(part.getClass().getSimpleName()), null)
|
|
);
|
|
}
|
|
|
|
@Override
|
|
public void paint(final Graphics g) {
|
|
part.draw((Graphics2D) g);
|
|
g.setColor(Color.black);
|
|
g.drawRect(0, 0, RASTER - 1, RASTER - 1);
|
|
}
|
|
|
|
}
|