diff --git a/megameklab/src/megameklab/ui/MegaMekLabMainUI.java b/megameklab/src/megameklab/ui/MegaMekLabMainUI.java index 1b1e61877..8bee05b13 100644 --- a/megameklab/src/megameklab/ui/MegaMekLabMainUI.java +++ b/megameklab/src/megameklab/ui/MegaMekLabMainUI.java @@ -24,7 +24,7 @@ import megameklab.ui.util.ExitOnWindowClosingListener; import megameklab.ui.util.RefreshListener; import megameklab.util.CConfig; -import megameklab.util.MMLFileDropTarget; +import megameklab.util.MMLFileDropTransferHandler; import javax.swing.*; import java.awt.*; @@ -47,7 +47,7 @@ protected void finishSetup() { setJMenuBar(mmlMenuBar); reloadTabs(); refreshAll(); - this.setDropTarget(new MMLFileDropTarget(this)); + this.setTransferHandler(new MMLFileDropTransferHandler(this)); } protected void setSizeAndLocation() { diff --git a/megameklab/src/megameklab/ui/StartupGUI.java b/megameklab/src/megameklab/ui/StartupGUI.java index d1b80ed78..2b14cc6da 100644 --- a/megameklab/src/megameklab/ui/StartupGUI.java +++ b/megameklab/src/megameklab/ui/StartupGUI.java @@ -29,7 +29,7 @@ import megameklab.ui.dialog.UiLoader; import megameklab.ui.util.ExitOnWindowClosingListener; import megameklab.util.CConfig; -import megameklab.util.MMLFileDropTarget; +import megameklab.util.MMLFileDropTransferHandler; import megameklab.util.UnitUtil; import javax.swing.*; @@ -237,7 +237,7 @@ private void initComponents() { frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); - this.setDropTarget(new MMLFileDropTarget(this)); + this.setTransferHandler(new MMLFileDropTransferHandler(this)); } /** diff --git a/megameklab/src/megameklab/util/MMLFileDropTarget.java b/megameklab/src/megameklab/util/MMLFileDropTarget.java deleted file mode 100644 index 4ca87d121..000000000 --- a/megameklab/src/megameklab/util/MMLFileDropTarget.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2024 - The MegaMek Team. All Rights Reserved. - * - * This file is part of MegaMekLab. - * - * MegaMek is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * MegaMek is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with MegaMek. If not, see . - */ - -package megameklab.util; - -import megamek.logging.MMLogger; -import megameklab.ui.MenuBarOwner; - -import java.awt.datatransfer.DataFlavor; -import java.awt.dnd.DnDConstants; -import java.awt.dnd.DropTarget; -import java.awt.dnd.DropTargetDropEvent; -import java.io.File; -import java.util.List; - -public class MMLFileDropTarget extends DropTarget { - private static final MMLogger logger = MMLogger.create(MMLFileDropTarget.class); - - private final MenuBarOwner owner; - - public MMLFileDropTarget(MenuBarOwner owner) { - this.owner = owner; - } - - @Override - public synchronized void drop(DropTargetDropEvent event) { - try { - event.acceptDrop(DnDConstants.ACTION_COPY); - var files = (List) event.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); - if (files.size() != 1) { - event.dropComplete(false); - return; - } - - var file = files.get(0); - var name = file.getName(); - - if (name.toLowerCase().endsWith(".mtf") || name.toLowerCase().endsWith(".blk")) { - owner.getMMLMenuBar().loadFile(file); - event.dropComplete(true); - } else if (name.toLowerCase().endsWith(".mul")) { - UnitPrintManager.printMUL(owner.getFrame(), CConfig.getBooleanParam(CConfig.MISC_MUL_OPEN_BEHAVIOUR), file); - event.dropComplete(true); - } else { - event.dropComplete(false); - } - - - } catch (Exception e) { - event.dropComplete(false); - logger.warn(e); - } - } -} diff --git a/megameklab/src/megameklab/util/MMLFileDropTransferHandler.java b/megameklab/src/megameklab/util/MMLFileDropTransferHandler.java new file mode 100644 index 000000000..854ca45e1 --- /dev/null +++ b/megameklab/src/megameklab/util/MMLFileDropTransferHandler.java @@ -0,0 +1,62 @@ +package megameklab.util; + +import megamek.logging.MMLogger; +import megameklab.ui.MenuBarOwner; + +import javax.swing.*; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.Transferable; +import java.io.File; +import java.util.List; + +public class MMLFileDropTransferHandler extends TransferHandler { + private static final MMLogger logger = MMLogger.create(MMLFileDropTransferHandler.class); + private final MenuBarOwner owner; + + public MMLFileDropTransferHandler(MenuBarOwner owner) { + this.owner = owner; + } + + @Override + public boolean canImport(TransferSupport support) { + if (!support.isDrop()) { + return false; + } + return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor); + } + + + @Override + public boolean importData(TransferSupport support) { + if (!canImport(support)) { + return false; + } + + try { + var files = (List) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); + if (files.size() != 1) { + logger.error("Cannot open multiple files at a time!", "Import error"); + return false; + } + + var file = files.get(0); + var name = file.getName(); + + if (name.toLowerCase().endsWith(".mtf") || name.toLowerCase().endsWith(".blk")) { + owner.getMMLMenuBar().loadFile(file); + return true; + } else if (name.toLowerCase().endsWith(".mul")) { + UnitPrintManager.printMUL(owner.getFrame(), CConfig.getBooleanParam(CConfig.MISC_MUL_OPEN_BEHAVIOUR), file); + return true; + } else { + logger.error("Can only open files of type .mtf, .blk, and .mul", "Import error"); + return false; + } + + + } catch (Exception e) { + logger.error(e); + return false; + } + } +}