mirror of
https://github.com/magefree/mage.git
synced 2025-12-24 12:31:59 -08:00
* UI: added drag & drop text to deck editor (like drag & drop deck file);
This commit is contained in:
parent
5eecfb2a86
commit
b6f075c505
4 changed files with 69 additions and 44 deletions
|
|
@ -1,5 +1,7 @@
|
|||
package mage.cards.decks;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.datatransfer.DataFlavor;
|
||||
import java.awt.datatransfer.UnsupportedFlavorException;
|
||||
|
|
@ -14,6 +16,7 @@ import java.util.List;
|
|||
|
||||
public class DnDDeckTargetListener extends DropTargetAdapter {
|
||||
|
||||
private static final transient Logger logger = Logger.getLogger(DnDDeckTargetListener.class);
|
||||
private static final DataFlavor fileFlavor = DataFlavor.javaFileListFlavor;
|
||||
private static final DataFlavor plainTextFlavor = DataFlavor.stringFlavor;
|
||||
|
||||
|
|
@ -35,6 +38,12 @@ public class DnDDeckTargetListener extends DropTargetAdapter {
|
|||
return copyOrMove && flavorSupported;
|
||||
}
|
||||
|
||||
private boolean isAcceptable(DropTargetDropEvent dtde) {
|
||||
boolean copyOrMove = isCopyOrMove(dtde.getDropAction());
|
||||
boolean flavorSupported = dtde.isDataFlavorSupported(plainTextFlavor) || dtde.isDataFlavorSupported(fileFlavor);
|
||||
return copyOrMove && flavorSupported;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dragEnter(DropTargetDragEvent dtde) {
|
||||
if (isAcceptable(dtde)) {
|
||||
|
|
@ -64,9 +73,7 @@ public class DnDDeckTargetListener extends DropTargetAdapter {
|
|||
|
||||
@Override
|
||||
public void drop(DropTargetDropEvent dtde) {
|
||||
if (isCopyOrMove(dtde.getDropAction()) && dtde.isDataFlavorSupported(fileFlavor)) {
|
||||
dtde.acceptDrop(TransferHandler.COPY);
|
||||
} else if (isCopyOrMove(dtde.getDropAction()) && dtde.isDataFlavorSupported(plainTextFlavor)) {
|
||||
if (isAcceptable(dtde)) {
|
||||
dtde.acceptDrop(TransferHandler.COPY);
|
||||
} else {
|
||||
dtde.rejectDrop();
|
||||
|
|
@ -89,7 +96,7 @@ public class DnDDeckTargetListener extends DropTargetAdapter {
|
|||
}
|
||||
}
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
logger.error("Unsupported drag and drop data", e);
|
||||
dtde.dropComplete(false);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,19 @@
|
|||
|
||||
package mage.util;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author LevelX2
|
||||
*/
|
||||
public final class DeckUtil {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(DeckUtil.class);
|
||||
|
||||
public static long fixedHash(String string) {
|
||||
long h = 1125899906842597L; // prime
|
||||
int len = string.length();
|
||||
|
|
@ -16,4 +23,23 @@ public final class DeckUtil {
|
|||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
public static String writeTextToTempFile(String text) {
|
||||
return writeTextToTempFile("cbimportdeck", ".txt", text);
|
||||
}
|
||||
|
||||
public static String writeTextToTempFile(String filePrefix, String fileSuffix, String text) {
|
||||
BufferedWriter bw = null;
|
||||
try {
|
||||
File temp = File.createTempFile(filePrefix, fileSuffix);
|
||||
bw = new BufferedWriter(new FileWriter(temp));
|
||||
bw.write(text);
|
||||
return temp.getPath();
|
||||
} catch (IOException e) {
|
||||
logger.error("Can't write deck file to temp file", e);
|
||||
} finally {
|
||||
StreamUtils.closeQuietly(bw);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue