From e471fc9888c52cd4cdd60614fba8493d5e4e7e90 Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Fri, 22 Mar 2019 08:04:28 +0400 Subject: [PATCH] UI: improved import from clipboard dialog and fixed compatibility with NetBeans; --- .../client/deckeditor/DeckEditorPanel.java | 56 +++-- .../deckeditor/DeckImportClipboardDialog.form | 127 +++++++++++ .../deckeditor/DeckImportClipboardDialog.java | 201 ++++++++++++++++++ .../DeckImportFromClipboardDialog.form | 79 ------- .../DeckImportFromClipboardDialog.java | 173 --------------- 5 files changed, 353 insertions(+), 283 deletions(-) create mode 100644 Mage.Client/src/main/java/mage/client/deckeditor/DeckImportClipboardDialog.form create mode 100644 Mage.Client/src/main/java/mage/client/deckeditor/DeckImportClipboardDialog.java delete mode 100644 Mage.Client/src/main/java/mage/client/deckeditor/DeckImportFromClipboardDialog.form delete mode 100644 Mage.Client/src/main/java/mage/client/deckeditor/DeckImportFromClipboardDialog.java diff --git a/Mage.Client/src/main/java/mage/client/deckeditor/DeckEditorPanel.java b/Mage.Client/src/main/java/mage/client/deckeditor/DeckEditorPanel.java index d828a7d855..118ebee986 100644 --- a/Mage.Client/src/main/java/mage/client/deckeditor/DeckEditorPanel.java +++ b/Mage.Client/src/main/java/mage/client/deckeditor/DeckEditorPanel.java @@ -30,7 +30,9 @@ import javax.swing.*; import javax.swing.filechooser.FileFilter; import java.awt.*; import java.awt.dnd.DropTarget; -import java.awt.event.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.HierarchyEvent; import java.io.File; import java.io.IOException; import java.util.List; @@ -680,45 +682,37 @@ public class DeckEditorPanel extends javax.swing.JPanel { } private void importFromClipboard(ActionEvent evt) { - final DeckImportFromClipboardDialog dialog = new DeckImportFromClipboardDialog(); - dialog.pack(); - dialog.setVisible(true); + final DeckImportClipboardDialog dialog = new DeckImportClipboardDialog(); + dialog.showDialog(); - dialog.addWindowListener(new WindowAdapter() { - @Override - public void windowClosed(WindowEvent e) { - loadDeck(dialog.getTmpPath()); - } - }); + if (!dialog.getTmpPath().isEmpty()) { + loadDeck(dialog.getTmpPath()); + } } private void importFromClipboardWithAppend(ActionEvent evt) { - final DeckImportFromClipboardDialog dialog = new DeckImportFromClipboardDialog(); - dialog.pack(); - dialog.setVisible(true); + final DeckImportClipboardDialog dialog = new DeckImportClipboardDialog(); + dialog.showDialog(); - dialog.addWindowListener(new WindowAdapter() { - @Override - public void windowClosed(WindowEvent e) { - Deck deckToAppend = null; - StringBuilder errorMessages = new StringBuilder(); + if (!dialog.getTmpPath().isEmpty()) { + Deck deckToAppend = null; + StringBuilder errorMessages = new StringBuilder(); - MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR)); - try { - deckToAppend = Deck.load(DeckImporter.importDeckFromFile(dialog.getTmpPath(), errorMessages), true, true); - processAndShowImportErrors(errorMessages); + MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR)); + try { + deckToAppend = Deck.load(DeckImporter.importDeckFromFile(dialog.getTmpPath(), errorMessages), true, true); + processAndShowImportErrors(errorMessages); - if (deckToAppend != null) { - deck = Deck.append(deckToAppend, deck); - refreshDeck(); - } - } catch (GameException e1) { - JOptionPane.showMessageDialog(MageFrame.getDesktop(), e1.getMessage(), "Error loading deck", JOptionPane.ERROR_MESSAGE); - } finally { - MageFrame.getDesktop().setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + if (deckToAppend != null) { + deck = Deck.append(deckToAppend, deck); + refreshDeck(); } + } catch (GameException e1) { + JOptionPane.showMessageDialog(MageFrame.getDesktop(), e1.getMessage(), "Error loading deck", JOptionPane.ERROR_MESSAGE); + } finally { + MageFrame.getDesktop().setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } - }); + } } private void exportChoose(java.awt.event.ActionEvent evt) { diff --git a/Mage.Client/src/main/java/mage/client/deckeditor/DeckImportClipboardDialog.form b/Mage.Client/src/main/java/mage/client/deckeditor/DeckImportClipboardDialog.form new file mode 100644 index 0000000000..8493fd1400 --- /dev/null +++ b/Mage.Client/src/main/java/mage/client/deckeditor/DeckImportClipboardDialog.form @@ -0,0 +1,127 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/Mage.Client/src/main/java/mage/client/deckeditor/DeckImportClipboardDialog.java b/Mage.Client/src/main/java/mage/client/deckeditor/DeckImportClipboardDialog.java new file mode 100644 index 0000000000..438c8954df --- /dev/null +++ b/Mage.Client/src/main/java/mage/client/deckeditor/DeckImportClipboardDialog.java @@ -0,0 +1,201 @@ +package mage.client.deckeditor; + +import mage.client.MageFrame; +import mage.client.dialog.MageDialog; +import mage.util.DeckUtil; + +import javax.swing.*; +import java.awt.*; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.event.KeyEvent; +import java.io.IOException; +import java.util.Optional; + +/** + * @author JayDi85 + */ +public class DeckImportClipboardDialog extends MageDialog { + + 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" + + "\n"; + + private String tmpPath; + + public DeckImportClipboardDialog() { + initComponents(); + } + + public void showDialog() { + this.tmpPath = ""; + onRefreshClipboard(); + + this.setModal(true); + this.setResizable(true); + getRootPane().setDefaultButton(buttonOK); + + /* + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + onCancel(); + } + }); + */ + + this.makeWindowCentered(); + + // windows settings + if (this.isModal()) { + MageFrame.getDesktop().add(this, JLayeredPane.MODAL_LAYER); + } else { + MageFrame.getDesktop().add(this, JLayeredPane.PALETTE_LAYER); + } + + // Close on "ESC" + registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + + this.setVisible(true); + } + + private Optional getClipboardStringData() { + try { + return Optional.of((String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor)); + } catch (HeadlessException | UnsupportedFlavorException | IOException e) { + //e.printStackTrace(); + } + return Optional.empty(); + } + + private void onOK() { + tmpPath = DeckUtil.writeTextToTempFile(editData.getText()); + this.removeDialog(); + } + + private void onCancel() { + this.removeDialog(); + } + + private void onRefreshClipboard() { + editData.setText(FORMAT_TEXT + getClipboardStringData().orElse("")); + editData.setCaretPosition(FORMAT_TEXT.length()); + } + + public String getTmpPath() { + return tmpPath; + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + panelData = new javax.swing.JScrollPane(); + editData = new javax.swing.JEditorPane(); + labelData = new javax.swing.JLabel(); + panelCommands = new javax.swing.JPanel(); + buttonOK = new javax.swing.JButton(); + buttonCancel = new javax.swing.JButton(); + + setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); + setTitle("Import from clipboard"); + setMinimumSize(new java.awt.Dimension(400, 400)); + + panelData.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); + panelData.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + panelData.setViewportView(editData); + + labelData.setLabelFor(editData); + labelData.setText("Paste deck in text format to import:"); + + buttonOK.setText("Import"); + buttonOK.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonOKActionPerformed(evt); + } + }); + + buttonCancel.setText("Close"); + buttonCancel.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonCancelActionPerformed(evt); + } + }); + + javax.swing.GroupLayout panelCommandsLayout = new javax.swing.GroupLayout(panelCommands); + panelCommands.setLayout(panelCommandsLayout); + panelCommandsLayout.setHorizontalGroup( + panelCommandsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelCommandsLayout.createSequentialGroup() + .addGap(0, 146, Short.MAX_VALUE) + .addComponent(buttonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(buttonCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) + ); + panelCommandsLayout.setVerticalGroup( + panelCommandsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelCommandsLayout.createSequentialGroup() + .addGap(0, 0, Short.MAX_VALUE) + .addGroup(panelCommandsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(buttonCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(buttonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))) + ); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(panelCommands, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(panelData, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) + .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() + .addComponent(labelData) + .addGap(0, 0, Short.MAX_VALUE))) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap() + .addComponent(labelData) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(panelData, javax.swing.GroupLayout.DEFAULT_SIZE, 448, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(panelCommands, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap()) + ); + + pack(); + }// //GEN-END:initComponents + + private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed + onCancel(); + }//GEN-LAST:event_buttonCancelActionPerformed + + private void buttonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOKActionPerformed + onOK(); + }//GEN-LAST:event_buttonOKActionPerformed + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton buttonCancel; + private javax.swing.JButton buttonOK; + private javax.swing.JEditorPane editData; + private javax.swing.JLabel labelData; + private javax.swing.JPanel panelCommands; + private javax.swing.JScrollPane panelData; + // End of variables declaration//GEN-END:variables +} diff --git a/Mage.Client/src/main/java/mage/client/deckeditor/DeckImportFromClipboardDialog.form b/Mage.Client/src/main/java/mage/client/deckeditor/DeckImportFromClipboardDialog.form deleted file mode 100644 index a1bceb019a..0000000000 --- a/Mage.Client/src/main/java/mage/client/deckeditor/DeckImportFromClipboardDialog.form +++ /dev/null @@ -1,79 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/Mage.Client/src/main/java/mage/client/deckeditor/DeckImportFromClipboardDialog.java b/Mage.Client/src/main/java/mage/client/deckeditor/DeckImportFromClipboardDialog.java deleted file mode 100644 index 8ea2e40567..0000000000 --- a/Mage.Client/src/main/java/mage/client/deckeditor/DeckImportFromClipboardDialog.java +++ /dev/null @@ -1,173 +0,0 @@ -package mage.client.deckeditor; - -import mage.util.DeckUtil; - -import javax.swing.*; -import java.awt.*; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.UnsupportedFlavorException; -import java.awt.event.KeyEvent; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.io.IOException; -import java.util.Optional; - -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"; - - private JPanel contentPane; - private JButton buttonOK; - private JButton buttonCancel; - private JEditorPane txtDeckList; - - private String tmpPath; - - public DeckImportFromClipboardDialog() { - initComponents(); - - onRefreshClipboard(); - - setContentPane(contentPane); - setModal(true); - getRootPane().setDefaultButton(buttonOK); - - buttonOK.addActionListener(e -> onOK()); - buttonCancel.addActionListener(e -> onCancel()); - - setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); - addWindowListener(new WindowAdapter() { - @Override - public void windowClosing(WindowEvent e) { - onCancel(); - } - }); - - // Close on "ESC" - contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); - } - - private Optional getClipboardStringData() { - try { - return Optional.of((String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor)); - } catch (HeadlessException | UnsupportedFlavorException | IOException e) { - //e.printStackTrace(); - } - return Optional.empty(); - } - - private void onOK() { - tmpPath = DeckUtil.writeTextToTempFile(txtDeckList.getText()); - dispose(); - } - - private void onCancel() { - dispose(); - } - - private void onRefreshClipboard() { - txtDeckList.setText(FORMAT_TEXT + getClipboardStringData().orElse("")); - } - - public String getTmpPath() { - return tmpPath; - } - - private void initComponents() { - contentPane = new JPanel(); - JPanel panel1 = new JPanel(); - JPanel panel2 = new JPanel(); - buttonOK = new JButton(); - buttonCancel = new JButton(); - JPanel panel3 = new JPanel(); - txtDeckList = new JEditorPane(); - - { - contentPane.setMinimumSize(new Dimension(540, 450)); - - contentPane.setBorder(new javax.swing.border.CompoundBorder( - new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0), - "Import from Clipboard", javax.swing.border.TitledBorder.CENTER, - javax.swing.border.TitledBorder.TOP, new java.awt.Font("Dialog", java.awt.Font.PLAIN, 12), - java.awt.Color.BLACK), contentPane.getBorder())); - - contentPane.addPropertyChangeListener(e -> { - if ("border".equals(e.getPropertyName())) { - throw new RuntimeException(); - } - }); - - contentPane.addPropertyChangeListener(e -> { - if ("border".equals(e.getPropertyName())) { - throw new RuntimeException(); - } - }); - - contentPane.setLayout(new GridBagLayout()); - ((GridBagLayout) contentPane.getLayout()).columnWidths = new int[]{0, 0}; - ((GridBagLayout) contentPane.getLayout()).rowHeights = new int[]{0, 0, 0}; - ((GridBagLayout) contentPane.getLayout()).columnWeights = new double[]{0.01, 1.0E-4}; - ((GridBagLayout) contentPane.getLayout()).rowWeights = new double[]{0.01, 0.0, 1.0E-4}; - - { - panel1.setLayout(new GridBagLayout()); - ((GridBagLayout) panel1.getLayout()).columnWidths = new int[]{0, 0, 0}; - ((GridBagLayout) panel1.getLayout()).rowHeights = new int[]{0, 0}; - ((GridBagLayout) panel1.getLayout()).columnWeights = new double[]{0.0, 0.01, 1.0E-4}; - ((GridBagLayout) panel1.getLayout()).rowWeights = new double[]{0.01, 1.0E-4}; - - { - panel2.setLayout(new GridBagLayout()); - ((GridBagLayout) panel2.getLayout()).columnWidths = new int[]{0, 4, 0, 0}; - ((GridBagLayout) panel2.getLayout()).rowHeights = new int[]{0, 0}; - ((GridBagLayout) panel2.getLayout()).columnWeights = new double[]{0.01, 0.0, 0.01, 1.0E-4}; - ((GridBagLayout) panel2.getLayout()).rowWeights = new double[]{0.0, 1.0E-4}; - - //---- buttonOK ---- - buttonOK.setText("Import"); - panel2.add(buttonOK, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, - GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, - new Insets(0, 0, 0, 0), 0, 0)); - - //---- buttonCancel ---- - buttonCancel.setText("Cancel"); - panel2.add(buttonCancel, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, - GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, - new Insets(0, 0, 0, 0), 0, 0)); - } - panel1.add(panel2, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, - GridBagConstraints.CENTER, GridBagConstraints.BOTH, - new Insets(0, 0, 0, 0), 0, 0)); - } - contentPane.add(panel1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, - GridBagConstraints.CENTER, GridBagConstraints.BOTH, - new Insets(0, 0, 0, 0), 0, 0)); - - { - panel3.setLayout(new GridBagLayout()); - ((GridBagLayout) panel3.getLayout()).columnWidths = new int[]{0, 0}; - ((GridBagLayout) panel3.getLayout()).rowHeights = new int[]{0, 0}; - ((GridBagLayout) panel3.getLayout()).columnWeights = new double[]{0.0, 1.0E-4}; - ((GridBagLayout) panel3.getLayout()).rowWeights = new double[]{1.0, 1.0E-4}; - - txtDeckList.setMinimumSize(new Dimension(250, 400)); - txtDeckList.setPreferredSize(new Dimension(550, 400)); - txtDeckList.setText(FORMAT_TEXT); - JScrollPane txtScrollableDeckList = new JScrollPane(txtDeckList); - panel3.add(txtScrollableDeckList, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, - GridBagConstraints.CENTER, GridBagConstraints.BOTH, - new Insets(0, 0, 0, 0), 0, 0)); - } - contentPane.add(panel3, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, - GridBagConstraints.CENTER, GridBagConstraints.BOTH, - new Insets(0, 0, 5, 0), 0, 0)); - } - } -}