This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
917c4d3
commit 510d2be
Showing
3 changed files
with
93 additions
and
168 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
src/main/java/org/worldcubeassociation/ui/ExportJSONAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
166
src/main/java/org/worldcubeassociation/ui/GenerateJSONAction.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters