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

View file

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

View file

@ -1,5 +1,7 @@
package mage.cards.decks; package mage.cards.decks;
import org.apache.log4j.Logger;
import javax.swing.*; import javax.swing.*;
import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.datatransfer.UnsupportedFlavorException;
@ -14,6 +16,7 @@ import java.util.List;
public class DnDDeckTargetListener extends DropTargetAdapter { 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 fileFlavor = DataFlavor.javaFileListFlavor;
private static final DataFlavor plainTextFlavor = DataFlavor.stringFlavor; private static final DataFlavor plainTextFlavor = DataFlavor.stringFlavor;
@ -35,6 +38,12 @@ public class DnDDeckTargetListener extends DropTargetAdapter {
return copyOrMove && flavorSupported; 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 @Override
public void dragEnter(DropTargetDragEvent dtde) { public void dragEnter(DropTargetDragEvent dtde) {
if (isAcceptable(dtde)) { if (isAcceptable(dtde)) {
@ -64,9 +73,7 @@ public class DnDDeckTargetListener extends DropTargetAdapter {
@Override @Override
public void drop(DropTargetDropEvent dtde) { public void drop(DropTargetDropEvent dtde) {
if (isCopyOrMove(dtde.getDropAction()) && dtde.isDataFlavorSupported(fileFlavor)) { if (isAcceptable(dtde)) {
dtde.acceptDrop(TransferHandler.COPY);
} else if (isCopyOrMove(dtde.getDropAction()) && dtde.isDataFlavorSupported(plainTextFlavor)) {
dtde.acceptDrop(TransferHandler.COPY); dtde.acceptDrop(TransferHandler.COPY);
} else { } else {
dtde.rejectDrop(); dtde.rejectDrop();
@ -89,7 +96,7 @@ public class DnDDeckTargetListener extends DropTargetAdapter {
} }
} }
} catch (UnsupportedFlavorException | IOException e) { } catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace(); logger.error("Unsupported drag and drop data", e);
dtde.dropComplete(false); dtde.dropComplete(false);
} }
} }

View file

@ -1,12 +1,19 @@
package mage.util; 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 * @author LevelX2
*/ */
public final class DeckUtil { public final class DeckUtil {
private static final Logger logger = Logger.getLogger(DeckUtil.class);
public static long fixedHash(String string) { public static long fixedHash(String string) {
long h = 1125899906842597L; // prime long h = 1125899906842597L; // prime
int len = string.length(); int len = string.length();
@ -16,4 +23,23 @@ public final class DeckUtil {
} }
return h; 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;
}
} }