Skip to content

Commit

Permalink
Merge pull request #1657 from pavelbraginskiy/drag-drop
Browse files Browse the repository at this point in the history
Fix cursor when drag-dropping in MML
  • Loading branch information
HammerGS authored Nov 10, 2024
2 parents 04fbb9f + b99df8a commit 3cdebe9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 74 deletions.
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;
}
}
}

0 comments on commit 3cdebe9

Please sign in to comment.