Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Commit

Permalink
Removed JSON preview dialog. #95
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzthief81 committed Mar 11, 2015
1 parent 917c4d3 commit 510d2be
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 168 deletions.
91 changes: 91 additions & 0 deletions src/main/java/org/worldcubeassociation/ui/ExportJSONAction.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
166 changes: 0 additions & 166 deletions src/main/java/org/worldcubeassociation/ui/GenerateJSONAction.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 510d2be

Please # to comment.