Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cursor when drag-dropping in MML #1657

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions megameklab/src/megameklab/ui/MegaMekLabMainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -47,7 +47,7 @@ protected void finishSetup() {
setJMenuBar(mmlMenuBar);
reloadTabs();
refreshAll();
this.setDropTarget(new MMLFileDropTarget(this));
this.setTransferHandler(new MMLFileDropTransferHandler(this));
}

protected void setSizeAndLocation() {
Expand Down
4 changes: 2 additions & 2 deletions megameklab/src/megameklab/ui/StartupGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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));
}

/**
Expand Down
70 changes: 0 additions & 70 deletions megameklab/src/megameklab/util/MMLFileDropTarget.java

This file was deleted.

62 changes: 62 additions & 0 deletions megameklab/src/megameklab/util/MMLFileDropTransferHandler.java
Original file line number Diff line number Diff line change
@@ -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<File>) 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;
}
}
}