* UI: added drag & drop text to deck editor (like drag & drop deck file);

This commit is contained in:
Oleg Agafonov 2019-03-13 04:21:31 +04:00
parent 5eecfb2a86
commit b6f075c505
4 changed files with 69 additions and 44 deletions

View file

@ -24,6 +24,7 @@ import mage.client.util.audio.AudioManager;
import mage.components.CardInfoPane;
import mage.game.GameException;
import mage.remote.Session;
import mage.util.DeckUtil;
import mage.view.CardView;
import mage.view.SimpleCardView;
import org.apache.log4j.Logger;
@ -36,8 +37,8 @@ import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.util.List;
import java.util.*;
import java.util.concurrent.*;
/**
@ -450,11 +451,19 @@ public class DeckEditorPanel extends javax.swing.JPanel {
if (mode == DeckEditorMode.FREE_BUILDING) {
setDropTarget(new DropTarget(this, new DnDDeckTargetListener() {
@Override
protected boolean handleFilesDrop(boolean move, List<File> files) {
loadDeck(files.get(0).getAbsolutePath());
return true;
}
@Override
protected boolean handlePlainTextDrop(boolean move, String text) {
String tmpFile = DeckUtil.writeTextToTempFile(text);
loadDeck(tmpFile);
return true;
}
}));
}
@ -585,9 +594,9 @@ public class DeckEditorPanel extends javax.swing.JPanel {
text = Integer.toString(minute) + ':';
}
if (second < 10) {
text = text + '0' + Integer.toString(second);
text = text + '0' + second;
} else {
text = text + Integer.toString(second);
text = text + second;
}
this.txtTimeRemaining.setText(text);
if (s == 60) {
@ -799,12 +808,10 @@ public class DeckEditorPanel extends javax.swing.JPanel {
}
private boolean loadDeck(String file) {
Deck newDeck = null;
StringBuilder errorMessages = new StringBuilder();
MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR));
try {
newDeck = Deck.load(DeckImporter.importDeckFromFile(file, errorMessages), true, true);
StringBuilder errorMessages = new StringBuilder();
Deck newDeck = Deck.load(DeckImporter.importDeckFromFile(file, errorMessages), true, true);
processAndShowImportErrors(errorMessages);
if (newDeck != null) {
@ -1118,7 +1125,7 @@ class DeckFilter extends FileFilter {
if (i > 0 && i < s.length() - 1) {
ext = s.substring(i + 1).toLowerCase(Locale.ENGLISH);
}
return (ext == null) ? false : ext.equals("dck");
return (ext != null) && ext.equals("dck");
}
@Override
@ -1143,14 +1150,12 @@ class ImportFilter extends FileFilter {
ext = s.substring(i + 1).toLowerCase(Locale.ENGLISH);
}
if (ext != null) {
if (ext.equalsIgnoreCase("dec")
return ext.equalsIgnoreCase("dec")
|| ext.equalsIgnoreCase("mwdeck")
|| ext.equalsIgnoreCase("txt")
|| ext.equalsIgnoreCase("dek")
|| ext.equalsIgnoreCase("cod")
|| ext.equalsIgnoreCase("o8d")) {
return true;
}
|| ext.equalsIgnoreCase("o8d");
}
return false;
}

View file

@ -1,29 +1,27 @@
package mage.client.deckeditor;
import mage.util.StreamUtils;
import mage.util.DeckUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Optional;
import javax.swing.*;
public class DeckImportFromClipboardDialog extends JDialog {
private static final String FORMAT_TEXT =
"// Example:\n" +
"//1 Library of Congress\n" +
"//1 Cryptic Gateway\n" +
"//1 Azami, Lady of Scrolls\n" +
"// NB: This is slow as, and will lock your screen :)\n" +
"\n" +
"// Your current clipboard:\n";
"//1 Library of Congress\n" +
"//1 Cryptic Gateway\n" +
"//1 Azami, Lady of Scrolls\n" +
"// NB: This is slow as, and will lock your screen :)\n" +
"\n" +
"// Your current clipboard:\n";
private JPanel contentPane;
private JButton buttonOK;
@ -58,7 +56,7 @@ public class DeckImportFromClipboardDialog extends JDialog {
private Optional<String> getClipboardStringData() {
try {
return Optional.of((String)Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor));
return Optional.of((String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor));
} catch (HeadlessException | UnsupportedFlavorException | IOException e) {
//e.printStackTrace();
}
@ -66,18 +64,7 @@ public class DeckImportFromClipboardDialog extends JDialog {
}
private void onOK() {
BufferedWriter bw = null;
try {
File temp = File.createTempFile("cbimportdeck", ".txt");
bw = new BufferedWriter(new FileWriter(temp));
bw.write(txtDeckList.getText());
tmpPath = temp.getPath();
} catch (IOException e) {
e.printStackTrace();
} finally {
StreamUtils.closeQuietly(bw);
}
tmpPath = DeckUtil.writeTextToTempFile(txtDeckList.getText());
dispose();
}

View file

@ -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);
}
}

View file

@ -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;
}
}