add deck import dialog to deck editor (see #1187).
parent
daa9d385c0
commit
8583bdbbb9
Binary file not shown.
After Width: | Height: | Size: 440 B |
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
|
@ -32,6 +32,7 @@ public enum MagicIcon {
|
|||
HAND_ICON("ui/w_hand.png"),
|
||||
HELP("ui/w_help.png"),
|
||||
ILLEGAL("ui/card_illegal.png"),
|
||||
IMPORT("ui/import-28.png"),
|
||||
INSTANTS("ui/w_instants.png"),
|
||||
KEY("ui/w_key16.png"),
|
||||
LANDS("ui/w_lands.png"),
|
||||
|
@ -46,6 +47,7 @@ public enum MagicIcon {
|
|||
OPTIONS("ui/w_book.png"),
|
||||
OPTION_MENU("ui/m-circle-32.png"),
|
||||
OPTION_MENU_TINY("ui/m-circle-18.png"),
|
||||
PASTE("ui/clipboard-paste-16.png"),
|
||||
PLANESWALKERS("ui/w_planeswalkers.png"),
|
||||
PLUS_ICON("ui/w_plus28.png"),
|
||||
POISON("ui/poison-16.png"),
|
||||
|
|
|
@ -0,0 +1,218 @@
|
|||
package magic.ui.dialog;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.datatransfer.Clipboard;
|
||||
import java.awt.datatransfer.DataFlavor;
|
||||
import java.awt.datatransfer.Transferable;
|
||||
import java.awt.datatransfer.UnsupportedFlavorException;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import javax.swing.AbstractAction;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
import magic.data.MagicIcon;
|
||||
import magic.model.MagicDeck;
|
||||
import magic.ui.MagicImages;
|
||||
import magic.ui.ScreenController;
|
||||
import magic.ui.dialog.button.CancelButton;
|
||||
import magic.ui.dialog.button.MagicDialogButton;
|
||||
import magic.ui.dialog.button.SaveButton;
|
||||
import magic.ui.helpers.MouseHelper;
|
||||
import magic.ui.mwidgets.MPlainTextViewer;
|
||||
import magic.utility.DeckParser;
|
||||
import magic.utility.FileIO;
|
||||
import net.miginfocom.swing.MigLayout;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class DeckImportDialog extends MagicDialog {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(DeckImportDialog.class.getName());
|
||||
|
||||
private static String text = "";
|
||||
|
||||
private final MPlainTextViewer textViewer;
|
||||
private final JButton saveButton = new SaveButton("Import");
|
||||
|
||||
private boolean isCancelled = false;
|
||||
private MagicDeck deck = new MagicDeck();
|
||||
|
||||
public DeckImportDialog(String text) {
|
||||
super("Import deck", new Dimension(600, (int)(ScreenController.getFrame().getSize().height * 0.67D)));
|
||||
DeckImportDialog.text = text;
|
||||
textViewer = new MPlainTextViewer();
|
||||
textViewer.setEditable(true);
|
||||
textViewer.setLineWrap(true);
|
||||
textViewer.setText(text);
|
||||
textViewer.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
if (SwingUtilities.isRightMouseButton(e)) {
|
||||
doPasteClipboardContents();
|
||||
}
|
||||
}
|
||||
});
|
||||
setLayout();
|
||||
setListeners();
|
||||
MouseHelper.showDefaultCursor();
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public DeckImportDialog() {
|
||||
this("");
|
||||
}
|
||||
|
||||
private void parseTextForDeck(String text) {
|
||||
deck = DeckParser.parseText(text);
|
||||
}
|
||||
|
||||
private void setListeners() {
|
||||
saveButton.addActionListener(new AbstractAction() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
parseTextForDeck(textViewer.getText());
|
||||
text = textViewer.getText();
|
||||
dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void doPasteClipboardContents() {
|
||||
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
|
||||
Transferable t = c.getContents(this);
|
||||
if (t != null) {
|
||||
try {
|
||||
textViewer.setText((String) t.getTransferData(DataFlavor.stringFlavor));
|
||||
} catch (UnsupportedFlavorException | IOException ex) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private JButton getPasteButton() {
|
||||
final JButton btn = new MagicDialogButton("");
|
||||
btn.setIcon(MagicImages.getIcon(MagicIcon.PASTE));
|
||||
btn.setToolTipText("Paste [Right-click]");
|
||||
btn.addActionListener(new AbstractAction() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
doPasteClipboardContents();
|
||||
}
|
||||
});
|
||||
return btn;
|
||||
}
|
||||
|
||||
private JButton getClearButton() {
|
||||
final JButton btn = new MagicDialogButton("X");
|
||||
btn.setToolTipText("Clear");
|
||||
btn.addActionListener(new AbstractAction() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
textViewer.setText(null);
|
||||
}
|
||||
});
|
||||
return btn;
|
||||
}
|
||||
|
||||
public static final FileFilter DECK_FILEFILTER = new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(final File file) {
|
||||
return file.isDirectory() || file.getName().endsWith("*.dec");
|
||||
}
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Magarena deck";
|
||||
}
|
||||
};
|
||||
|
||||
private File startDirectory;
|
||||
|
||||
private void doLoadDeck() {
|
||||
final JFileChooser fileChooser = new JFileChooser(startDirectory);
|
||||
fileChooser.setDialogTitle("Load deck");
|
||||
fileChooser.setAcceptAllFileFilterUsed(true);
|
||||
final int action = fileChooser.showOpenDialog(this);
|
||||
if (action == JFileChooser.APPROVE_OPTION) {
|
||||
String filename = fileChooser.getSelectedFile().getAbsolutePath();
|
||||
File aFile = new File(filename);
|
||||
startDirectory = aFile.getParentFile();
|
||||
if (aFile.length() > 2048) {
|
||||
textViewer.setText("File too big to be a valid deck file (greater than 2 MB).");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
textViewer.setText(FileIO.toStr(aFile));
|
||||
} catch (IOException ex) {
|
||||
LOGGER.log(Level.WARNING, null, ex);
|
||||
textViewer.setText(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private JButton getLoadButton() {
|
||||
final JButton btn = new MagicDialogButton("+");
|
||||
btn.setToolTipText("Load deck");
|
||||
btn.addActionListener(new AbstractAction() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
doLoadDeck();
|
||||
}
|
||||
});
|
||||
return btn;
|
||||
}
|
||||
|
||||
private JButton getCancelButton() {
|
||||
final JButton btn = new CancelButton();
|
||||
btn.addActionListener(getCancelAction());
|
||||
return btn;
|
||||
}
|
||||
|
||||
private JPanel getButtonPanel() {
|
||||
final JPanel buttonPanel = new JPanel(new MigLayout("insets 0, alignx right, aligny bottom"));
|
||||
buttonPanel.add(getLoadButton(), "w 40!, alignx left");
|
||||
buttonPanel.add(getClearButton(), "w 40!, alignx left");
|
||||
buttonPanel.add(getPasteButton(), "w 40!, alignx left, push x");
|
||||
buttonPanel.add(getCancelButton(), "w 120");
|
||||
buttonPanel.add(saveButton, "w 120");
|
||||
return buttonPanel;
|
||||
}
|
||||
|
||||
private void setLayout() {
|
||||
final JPanel panel = getDialogContentPanel();
|
||||
panel.setLayout(new MigLayout("flowy, gapy 6"));
|
||||
panel.add(textViewer.component(), "w 100%, h 100%");
|
||||
panel.add(getButtonPanel(), "w 100%, h 30!, pushy, aligny bottom");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractAction getCancelAction() {
|
||||
return new AbstractAction() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
isCancelled = true;
|
||||
dispose();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public boolean isCancelled() {
|
||||
return isCancelled;
|
||||
}
|
||||
|
||||
public MagicDeck getDeck() {
|
||||
return deck;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package magic.ui.mwidgets;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.event.MouseListener;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
@ -15,7 +17,7 @@ public class MPlainTextViewer extends MWidget {
|
|||
private final JTextArea textArea = new JTextArea();
|
||||
|
||||
public MPlainTextViewer() {
|
||||
setDefaultProperties();
|
||||
setDefaultProperties();
|
||||
}
|
||||
|
||||
private void setDefaultProperties() {
|
||||
|
@ -38,15 +40,35 @@ public class MPlainTextViewer extends MWidget {
|
|||
SwingUtilities.invokeLater(() -> {
|
||||
scrollPane.setVScrollBarValue(0);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
textArea.setText(text);
|
||||
resetVerticalScrollbar();
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return textArea.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent component() {
|
||||
return scrollPane.component();
|
||||
}
|
||||
|
||||
public void setEditable(boolean b) {
|
||||
textArea.setEditable(b);
|
||||
textArea.setBackground(b ? Color.WHITE : FontsAndBorders.TEXTAREA_TRANSPARENT_COLOR_HACK);
|
||||
textArea.setFocusable(b);
|
||||
}
|
||||
|
||||
public void setLineWrap(boolean b) {
|
||||
textArea.setLineWrap(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMouseListener(MouseListener ml) {
|
||||
textArea.addMouseListener(ml);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package magic.ui.screen.deck.editor;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
@ -7,6 +8,7 @@ import java.nio.file.InvalidPathException;
|
|||
import java.nio.file.Path;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JOptionPane;
|
||||
import magic.data.DeckType;
|
||||
import magic.data.GeneralConfig;
|
||||
|
@ -15,8 +17,11 @@ import magic.data.MagicSetDefinitions;
|
|||
import magic.model.MagicDeck;
|
||||
import magic.translate.MText;
|
||||
import magic.ui.MagicLogs;
|
||||
import magic.ui.MagicSound;
|
||||
import magic.ui.ScreenController;
|
||||
import magic.ui.WikiPage;
|
||||
import magic.ui.dialog.DeckImportDialog;
|
||||
import magic.ui.helpers.ImageHelper;
|
||||
import magic.ui.screen.HeaderFooterScreen;
|
||||
import magic.ui.screen.MScreen;
|
||||
import magic.ui.screen.interfaces.IDeckConsumer;
|
||||
|
@ -57,11 +62,16 @@ public class DeckEditorScreen extends HeaderFooterScreen implements IDeckConsume
|
|||
|
||||
private static final Logger LOGGER = Logger.getLogger(DeckEditorScreen.class.getName());
|
||||
|
||||
private static final ImageIcon IMPORT_ICON =
|
||||
ImageHelper.getRecoloredIcon(MagicIcon.IMPORT, Color.BLACK, Color.WHITE);
|
||||
|
||||
private ContentPanel contentPanel;
|
||||
private final DeckStatusPanel deckStatusPanel = new DeckStatusPanel();
|
||||
private final IDeckEditorClient deckClient;
|
||||
private final DeckEditorController controller = DeckEditorController.instance;
|
||||
|
||||
private String importText = "";
|
||||
|
||||
public DeckEditorScreen(IDeckEditorClient client) {
|
||||
super(MText.get(_S14));
|
||||
this.deckClient = client;
|
||||
|
@ -121,18 +131,40 @@ public class DeckEditorScreen extends HeaderFooterScreen implements IDeckConsume
|
|||
}
|
||||
}
|
||||
|
||||
private void showDeckImportDialog(String text) {
|
||||
DeckImportDialog dialog = new DeckImportDialog(text);
|
||||
if (!dialog.isCancelled()) {
|
||||
MagicDeck deck = dialog.getDeck();
|
||||
if (deck.isNotEmpty()) {
|
||||
setDeck(deck);
|
||||
importText = dialog.getText();
|
||||
} else {
|
||||
MagicSound.BEEP.play();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void showDeckImportDialog() {
|
||||
showDeckImportDialog(importText);
|
||||
}
|
||||
|
||||
private void setFooterButtons() {
|
||||
addToFooter(PlainMenuButton.build(this::showDecksScreen,
|
||||
addFooterGroup(
|
||||
PlainMenuButton.build(this::showDecksScreen,
|
||||
MagicIcon.OPEN, MText.get(_S4), MText.get(_S5)
|
||||
),
|
||||
PlainMenuButton.build(this::saveDeck,
|
||||
MagicIcon.SAVE, MText.get(_S6), MText.get(_S7)
|
||||
),
|
||||
PlainMenuButton.build(this::showSampleHandScreen,
|
||||
MagicIcon.HAND_ICON, MText.get(_S8), MText.get(_S9)
|
||||
),
|
||||
PlainMenuButton.build(this::showDeckImportDialog,
|
||||
IMPORT_ICON, "Import deck", "")
|
||||
);
|
||||
addFooterGroup(
|
||||
PlainMenuButton.build(this::showDeckTiledCardsScreen,
|
||||
MagicIcon.TILED, MText.get(_S11), MText.get(_S12)
|
||||
),
|
||||
PlainMenuButton.build(this::showSampleHandScreen,
|
||||
MagicIcon.HAND_ICON, MText.get(_S8), MText.get(_S9)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -326,20 +358,21 @@ public class DeckEditorScreen extends HeaderFooterScreen implements IDeckConsume
|
|||
@Override
|
||||
public boolean doFileDropAction(File aFile) {
|
||||
if (aFile.length() > 2048) {
|
||||
ScreenController.showWarningMessage("File too big to be a valid deck file (greater than 2 MB).");
|
||||
showDeckImportDialog("File too big to be a valid deck file (greater than 2 MB).");
|
||||
return true;
|
||||
}
|
||||
String text = getDroppedFileContents(aFile);
|
||||
if (text.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
importText = text;
|
||||
MagicDeck deck = DeckParser.parseText(text);
|
||||
if (deck.isNotEmpty()) {
|
||||
setDeck(deck);
|
||||
return true;
|
||||
} else if (!deck.getDescription().isEmpty()) {
|
||||
LOGGER.log(Level.WARNING, deck.getDescription());
|
||||
return false;
|
||||
showDeckImportDialog(text);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue