From 510d2beed6e78dfd4335b59c0e6ce83ae775a767 Mon Sep 17 00:00:00 2001 From: jazzthief81 Date: Wed, 11 Mar 2015 22:36:16 +0100 Subject: [PATCH] Removed JSON preview dialog. #95 --- .../ui/ExportJSONAction.java | 91 ++++++++++ .../ui/GenerateJSONAction.java | 166 ------------------ .../ui/WorkbookAssistantFrame.java | 4 +- 3 files changed, 93 insertions(+), 168 deletions(-) create mode 100644 src/main/java/org/worldcubeassociation/ui/ExportJSONAction.java delete mode 100644 src/main/java/org/worldcubeassociation/ui/GenerateJSONAction.java diff --git a/src/main/java/org/worldcubeassociation/ui/ExportJSONAction.java b/src/main/java/org/worldcubeassociation/ui/ExportJSONAction.java new file mode 100644 index 0000000..d27f307 --- /dev/null +++ b/src/main/java/org/worldcubeassociation/ui/ExportJSONAction.java @@ -0,0 +1,91 @@ +package org.worldcubeassociation.ui; + +import java.awt.event.ActionEvent; +import java.beans.PropertyChangeListener; +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Arrays; + +import javax.swing.JFileChooser; +import javax.swing.JOptionPane; + +import org.worldcubeassociation.WorkbookAssistantEnv; +import org.worldcubeassociation.workbook.JSONGenerator; +import org.worldcubeassociation.workbook.SheetType; + +/** + * @author Lars Vandenbergh + */ +public class ExportJSONAction extends AbstractGenerateAction implements PropertyChangeListener { + + private JFileChooser fc; + private ExtensionFileFilter jsonFileFilter; + + public ExportJSONAction(WorkbookAssistantEnv aEnv) { + super("Export results JSON...", aEnv); + + initUI(); + updateEnabledState(); + } + + private void initUI() { + fc = new JFileChooser(); + fc.setCurrentDirectory(getEnv().getWorkingDirectory()); + fc.setDialogTitle("Export results JSON"); + jsonFileFilter = new ExtensionFileFilter("Results JSON", ".json"); + fc.setFileFilter(jsonFileFilter); + } + + @Override + public void actionPerformed(ActionEvent aActionEvent) { + boolean approved = warnForErrors(Arrays.asList(SheetType.values())); + if (!approved) { + return; + } + + try { + String jsonFileName = "Results for " + getEnv().getCompetitionId() + ".json"; + fc.setSelectedFile(new File(jsonFileName)); + + int returnVal = fc.showSaveDialog(getEnv().getTopLevelComponent()); + getEnv().setWorkingDirectory(fc.getCurrentDirectory()); + if (returnVal == JFileChooser.APPROVE_OPTION) { + File f = fc.getSelectedFile(); + if (fc.getFileFilter() == jsonFileFilter) { + // Only append the .json extension when the user chose to save + // the file as .json. + if (!f.getPath().toLowerCase().endsWith(".json")) { + f = new File(f.getPath() + ".json"); + } + } + PrintWriter pw = null; + try { + pw = new PrintWriter(f, "UTF-8"); + String scripts = JSONGenerator.generateJSON(getEnv().getMatchedWorkbook(), getEnv().getCompetitionId(), getEnv().getScrambles()); + pw.write(scripts); + } + catch (IOException e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(getEnv().getTopLevelComponent(), + "An error occurred while trying to write to " + f.getAbsolutePath() + "!", + "Export results JSON", + JOptionPane.ERROR_MESSAGE); + } + finally { + if (pw != null) { + pw.close(); + } + } + } + } + catch (Exception e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(getEnv().getTopLevelComponent(), + "An unexpected validation error occurred in one of the sheets!", + "Export results JSON", + JOptionPane.ERROR_MESSAGE); + } + } + +} diff --git a/src/main/java/org/worldcubeassociation/ui/GenerateJSONAction.java b/src/main/java/org/worldcubeassociation/ui/GenerateJSONAction.java deleted file mode 100644 index 2a89add..0000000 --- a/src/main/java/org/worldcubeassociation/ui/GenerateJSONAction.java +++ /dev/null @@ -1,166 +0,0 @@ -package org.worldcubeassociation.ui; - -import java.awt.Dialog; -import java.awt.Font; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Toolkit; -import java.awt.datatransfer.StringSelection; -import java.awt.event.ActionEvent; -import java.beans.PropertyChangeListener; -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.Arrays; - -import javax.swing.AbstractAction; -import javax.swing.JButton; -import javax.swing.JDialog; -import javax.swing.JFileChooser; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JTextArea; - -import org.worldcubeassociation.WorkbookAssistantEnv; -import org.worldcubeassociation.workbook.JSONGenerator; -import org.worldcubeassociation.workbook.SheetType; - -/** - * @author Lars Vandenbergh - */ -public class GenerateJSONAction extends AbstractGenerateAction implements PropertyChangeListener { - - private JDialog fDialog; - private JTextArea fTextArea; - - public GenerateJSONAction(WorkbookAssistantEnv aEnv) { - super("Generate competition JSON...", aEnv); - - initUI(); - updateEnabledState(); - } - - private void initUI() { - fDialog = new NicelySizedJDialog(getEnv().getTopLevelComponent(), "Generate competition JSON", Dialog.ModalityType.APPLICATION_MODAL); - fDialog.getContentPane().setLayout(new GridBagLayout()); - - GridBagConstraints c = new GridBagConstraints(); - - c.insets.top = 4; - c.insets.right = 4; - c.insets.left = 4; - c.insets.bottom = 4; - c.fill = GridBagConstraints.BOTH; - c.weightx = 1; - c.weighty = 1; - c.gridx = GridBagConstraints.REMAINDER; - c.anchor = GridBagConstraints.CENTER; - fTextArea = new JTextArea(50, 150); - fTextArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); - fTextArea.setLineWrap(true); - fTextArea.setWrapStyleWord(true); - fTextArea.setEditable(false); - JScrollPane scrollPane = new JScrollPane(fTextArea, - JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, - JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); - fDialog.getContentPane().add(scrollPane, c); - - c.weighty = 0; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.LAST_LINE_END; - JPanel buttons = new JPanel(); - fDialog.getContentPane().add(buttons, c); - buttons.add(new JButton(new CopyAction())); - buttons.add(new JButton(new SaveAction())); - - fDialog.pack(); - } - - @Override - public void actionPerformed(ActionEvent aActionEvent) { - boolean approved = warnForErrors(Arrays.asList(SheetType.values())); - if (!approved) { - return; - } - - try { - String scripts = JSONGenerator.generateJSON(getEnv().getMatchedWorkbook(), getEnv().getCompetitionId(), getEnv().getScrambles()); - fTextArea.setText(scripts); - - fDialog.setLocationRelativeTo(getEnv().getTopLevelComponent()); - fDialog.setVisible(true); - } - catch (Exception e) { - e.printStackTrace(); - JOptionPane.showMessageDialog(getEnv().getTopLevelComponent(), - "An unexpected validation error occurred in one of the sheets!", - "Generate competition JSON", - JOptionPane.ERROR_MESSAGE); - } - } - - private class SaveAction extends AbstractAction { - - private JFileChooser fc = new JFileChooser(); - - private SaveAction() { - super("Save as..."); - } - - @Override - public void actionPerformed(ActionEvent aActionEvent) { - fc.setCurrentDirectory(getEnv().getWorkingDirectory()); - fc.setDialogTitle("Save competition JSON"); - ExtensionFileFilter jsonFileFilter = new ExtensionFileFilter("Competition JSON", ".json"); - fc.setFileFilter(jsonFileFilter); - String jsonFileName = "Results for " + getEnv().getCompetitionId() + ".json"; - fc.setSelectedFile(new File(jsonFileName)); - - int returnVal = fc.showSaveDialog(getEnv().getTopLevelComponent()); - getEnv().setWorkingDirectory(fc.getCurrentDirectory()); - if(returnVal == JFileChooser.APPROVE_OPTION) { - File f = fc.getSelectedFile(); - if(fc.getFileFilter() == jsonFileFilter) { - // Only append the .json extension when the user chose to save - // the file as .json. - if(!f.getPath().toLowerCase().endsWith(".json")) { - f = new File(f.getPath() + ".json"); - } - } - PrintWriter pw = null; - try { - pw = new PrintWriter(f, "UTF-8"); - pw.write(fTextArea.getText()); - } catch (IOException e) { - e.printStackTrace(); - JOptionPane.showMessageDialog(getEnv().getTopLevelComponent(), - "An error occurred while trying to write to " + f.getAbsolutePath() + "!", - "Save competition JSON", - JOptionPane.ERROR_MESSAGE); - } finally { - if(pw != null) { - pw.close(); - } - } - } - } - - } - - private class CopyAction extends AbstractAction { - - private CopyAction() { - super("Copy"); - } - - @Override - public void actionPerformed(ActionEvent aActionEvent) { - Toolkit.getDefaultToolkit(). - getSystemClipboard(). - setContents(new StringSelection(fTextArea.getText()), null); - } - - } - -} diff --git a/src/main/java/org/worldcubeassociation/ui/WorkbookAssistantFrame.java b/src/main/java/org/worldcubeassociation/ui/WorkbookAssistantFrame.java index d8c6c0f..55fcc42 100644 --- a/src/main/java/org/worldcubeassociation/ui/WorkbookAssistantFrame.java +++ b/src/main/java/org/worldcubeassociation/ui/WorkbookAssistantFrame.java @@ -80,7 +80,7 @@ public void run() { addScramblesAction.open(TEST_SCRAMBLE_FILES); } if (TEST_JSON_EXPORT) { - new GenerateJSONAction(fEnv).actionPerformed(null); + new ExportJSONAction(fEnv).actionPerformed(null); } } }); @@ -323,7 +323,7 @@ public void actionPerformed(ActionEvent aActionEvent) { c.weightx = 0; c.anchor = GridBagConstraints.EAST; c.insets.right = 4; - panel.add(new JButton(new GenerateJSONAction(fEnv)), c); + panel.add(new JButton(new ExportJSONAction(fEnv)), c); return panel; }