From b8c40931ed4e89c8608e0b2d82200bab1c660b70 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Tue, 5 Dec 2023 12:43:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=BB=20=D0=BD=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B4?= =?UTF-8?q?=D0=B0=D1=87=D1=83=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=B2=D0=B8=D0=B3=D0=B0=D1=82=D0=B5=D0=BB=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectStormtrooper/FormPlaneConfig.java | 106 +++++++++++++++++------ 1 file changed, 79 insertions(+), 27 deletions(-) diff --git a/ProjectStormtrooper/FormPlaneConfig.java b/ProjectStormtrooper/FormPlaneConfig.java index d1576bc..208b51a 100644 --- a/ProjectStormtrooper/FormPlaneConfig.java +++ b/ProjectStormtrooper/FormPlaneConfig.java @@ -58,13 +58,49 @@ public class FormPlaneConfig { labelStormtrooper.setTransferHandler(new LabelTransferHandler()); labelStormtrooper.addMouseListener(new LabelMouseAdapter()); - labelEllipseEngines.setTransferHandler(new LabelTransferHandler()); + labelEllipseEngines.setTransferHandler( + new TransferHandler(){ + @Override + public int getSourceActions(JComponent c) { + return TransferHandler.COPY; + } + + @Override + protected Transferable createTransferable(JComponent c) { + return new EngineTransferable(new DrawingEnginesEllipse()); + } + } + ); labelEllipseEngines.addMouseListener(new LabelMouseAdapter()); - labelPyramidEngines.setTransferHandler(new LabelTransferHandler()); + labelPyramidEngines.setTransferHandler( + new TransferHandler(){ + @Override + public int getSourceActions(JComponent c) { + return TransferHandler.COPY; + } + + @Override + protected Transferable createTransferable(JComponent c) { + return new EngineTransferable(new DrawingEnginesPyramid()); + } + } + ); labelPyramidEngines.addMouseListener(new LabelMouseAdapter()); - labelSimpleEngines.setTransferHandler(new LabelTransferHandler()); + labelSimpleEngines.setTransferHandler( + new TransferHandler(){ + @Override + public int getSourceActions(JComponent c) { + return TransferHandler.COPY; + } + + @Override + protected Transferable createTransferable(JComponent c) { + return new EngineTransferable(new DrawingEnginesSimple()); + } + } + ); labelSimpleEngines.addMouseListener(new LabelMouseAdapter()); JPanel[] colorPanels = { @@ -122,12 +158,11 @@ public class FormPlaneConfig { } ); - Canvas canvas = new Canvas(); panelShow.setTransferHandler( new TransferHandler() { @Override public boolean canImport(TransferHandler.TransferSupport support) { - return support.isDataFlavorSupported(DataFlavor.stringFlavor); + return support.isDataFlavorSupported(DataFlavor.stringFlavor) || support.isDataFlavorSupported(EngineTransferable.engineDrawingDataFlavor); } @Override @@ -161,30 +196,19 @@ public class FormPlaneConfig { } else { return false; } - - String dataEngines = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor); - IDrawingEngines iDrawingEngines = null; - int enginesCount = (int) spinnerEnginesCount.getValue(); - switch (dataEngines) { - case "Эллипс" -> { - iDrawingEngines = new DrawingEnginesEllipse(); - } - case "Обычный" -> { - iDrawingEngines = new DrawingEnginesSimple(); - } - case "Пирамид" -> { - iDrawingEngines = new DrawingEnginesPyramid(); - } - } - if (iDrawingEngines != null) { - selectedPlane._drawingEngines = iDrawingEngines; - selectedPlane.SetEnginesCount(enginesCount); - } - panelShow.repaint(); - return true; } catch (UnsupportedFlavorException | IOException e) { - e.printStackTrace(); } + + try { + IDrawingEngines drawingEngines = (IDrawingEngines) support.getTransferable().getTransferData(EngineTransferable.engineDrawingDataFlavor); + int enginesCount = (int) spinnerEnginesCount.getValue(); + selectedPlane._drawingEngines = drawingEngines; + selectedPlane.SetEnginesCount(enginesCount); + } + catch (UnsupportedFlavorException | IOException e) { + } + panelShow.repaint(); + return true; } return false; } @@ -219,6 +243,34 @@ public class FormPlaneConfig { } } + private class EngineTransferable implements Transferable { + private IDrawingEngines drawingEngines; + private static final DataFlavor engineDrawingDataFlavor = new DataFlavor(IDrawingEngines.class, "Engine Drawing"); + + public EngineTransferable(IDrawingEngines drawingEngines) { + this.drawingEngines = drawingEngines; + } + + @Override + public DataFlavor[] getTransferDataFlavors() { + return new DataFlavor[]{engineDrawingDataFlavor}; + } + + @Override + public boolean isDataFlavorSupported(DataFlavor flavor) { + return flavor.equals(engineDrawingDataFlavor); + } + + @Override + public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException { + if (isDataFlavorSupported(flavor)) { + return drawingEngines; + } else { + throw new UnsupportedFlavorException(flavor); + } + } + } + private class ColorTransferable implements Transferable { private Color color; private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");