Skip to content

Commit

Permalink
#4,#9 Enforce some reasonable file extension upon save / No reuse of
Browse files Browse the repository at this point in the history
filenames on "export as JSON" and "save"

- enforcing ".wikitax", ".json" in ApplicationView and ".csv" in 
TableView
- "export as JSON" uses its own file chooser to prevent accidentally
overwriting. There is also a warning shown if the file to write to
already exists.
  • Loading branch information
Dominik Mosen committed Jun 10, 2013
1 parent 3f1c82a commit ca42ded
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 58 deletions.
124 changes: 83 additions & 41 deletions wiki-analysis/src/visualisation/view/ApplicationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import schemas.categoryschema.CategoryGraph;
import schemas.categoryschema.CategorySchema;
Expand Down Expand Up @@ -41,14 +42,17 @@
*/
public class ApplicationView {

private final String JSON_SUFFIX = ".json";
private final String WIKITAX_SUFFIX = ".wikitax";
private final String FILE = "data/simple_graph.tg";

private Controller controller;
private CategoryTreeModel model;
private CategoryGraph graph;

private JFrame frmCategoryTreeExtraction;
private JFileChooser fileChooser;
private JFileChooser saveFileChooser;
private JFileChooser exportJSONFileChooser;

private JMenuItem mntmLoad;
private JMenuItem mntmExtract;
Expand Down Expand Up @@ -109,7 +113,39 @@ private void initialize() {
controller.setModel(model);
this.controller = controller;

fileChooser = new JFileChooser();
exportJSONFileChooser = new JFileChooser();
exportJSONFileChooser.setFileFilter(new FileFilter() {

@Override
public String getDescription() {
return "JSON files";
}

@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
return f.getName().toLowerCase().endsWith(JSON_SUFFIX);
}
});

saveFileChooser = new JFileChooser();
saveFileChooser.setFileFilter(new FileFilter() {

@Override
public String getDescription() {
return "Wikitax files";
}

@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
return f.getName().toLowerCase().endsWith(WIKITAX_SUFFIX);
}
});

frmCategoryTreeExtraction = new JFrame();
frmCategoryTreeExtraction.setTitle("WikiTax");
Expand Down Expand Up @@ -183,7 +219,7 @@ public void actionPerformed(ActionEvent e) {

@Override
public void actionPerformed(ActionEvent e) {
showSaveGraphDialog();
saveFile(saveFileChooser, WIKITAX_SUFFIX);
}
});

Expand All @@ -204,31 +240,7 @@ public void actionPerformed(ActionEvent e) {

@Override
public void actionPerformed(ActionEvent e) {
fileChooser.setDialogTitle("Save JSON graph");
int value = fileChooser
.showSaveDialog(frmCategoryTreeExtraction);

if (value == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();

try {
WikipediaAnalysis.saveGraphAsJSON(graph, file);
JOptionPane.showMessageDialog(
frmCategoryTreeExtraction,
"Saved to " + file.getAbsolutePath() + ".",
"Success", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e1) {
JOptionPane.showMessageDialog(
frmCategoryTreeExtraction,
"Error on writing to file "
+ file.getAbsolutePath() + ".",
"Error", JOptionPane.ERROR_MESSAGE);
} catch (AlgorithmTerminatedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

saveFile(exportJSONFileChooser, JSON_SUFFIX);
}
});

Expand All @@ -245,10 +257,10 @@ public void actionPerformed(ActionEvent e) {
}

private void loadGraph() {
int value = fileChooser.showOpenDialog(frmCategoryTreeExtraction);
int value = saveFileChooser.showOpenDialog(frmCategoryTreeExtraction);

if (value == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
File file = saveFileChooser.getSelectedFile();

CategoryGraph tGraph = null;
try {
Expand All @@ -271,24 +283,54 @@ private void loadGraph() {
}
}

private void showSaveGraphDialog() {
private void saveFile(JFileChooser fileChooser, String suffix) {
fileChooser.setDialogTitle("Save graph");
int value = fileChooser.showSaveDialog(frmCategoryTreeExtraction);

if (value == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();

try {
GraphIO.saveGraphToFile(graph, file.getAbsolutePath(), null);
JOptionPane.showMessageDialog(frmCategoryTreeExtraction,
"Saved to " + file.getAbsolutePath() + ".", "Success",
JOptionPane.INFORMATION_MESSAGE);
} catch (GraphIOException e1) {
JOptionPane.showMessageDialog(frmCategoryTreeExtraction,
"Error on writing to file " + file.getAbsolutePath()
+ ".", "Error", JOptionPane.ERROR_MESSAGE);
if (!file.getPath().toLowerCase().endsWith(suffix)) {
file = new File(file.getPath() + suffix);
}

int override = JOptionPane.YES_OPTION;
if (file.exists()) {
override = JOptionPane.showConfirmDialog(
frmCategoryTreeExtraction, "\"" + file.getPath()
+ "\" already exists. Overwrite?", "File exists",
JOptionPane.YES_NO_OPTION);
}

if (override == JOptionPane.YES_OPTION) {
try {
if (suffix == WIKITAX_SUFFIX) {
GraphIO.saveGraphToFile(graph, file.getPath(), null);
} else if (suffix == JSON_SUFFIX) {
WikipediaAnalysis.saveGraphAsJSON(graph, file);
}
JOptionPane.showMessageDialog(frmCategoryTreeExtraction,
"Saved to \"" + file.getAbsolutePath() + "\".",
"Success", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e1) {
JOptionPane.showMessageDialog(
frmCategoryTreeExtraction,
"Error on writing to file "
+ file.getAbsolutePath() + ".", "Error",
JOptionPane.ERROR_MESSAGE);
} catch (GraphIOException e) {
JOptionPane
.showMessageDialog(
frmCategoryTreeExtraction,
"Error on loading file "
+ new File(FILE).getAbsolutePath()
+ ".\nMake sure it is readable and has the correct format.",
"Error", JOptionPane.ERROR_MESSAGE);
} catch (AlgorithmTerminatedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}
72 changes: 55 additions & 17 deletions wiki-analysis/src/visualisation/view/table/TableDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.TableModel;
import javax.swing.text.BadLocationException;

Expand All @@ -44,6 +45,7 @@
@SuppressWarnings("serial")
public class TableDialog extends JDialog {

private final String CSV_SUFFIX = ".csv";
private final JPanel contentPanel = new JPanel();
private JTable table;
CategoryTableModel model;
Expand Down Expand Up @@ -139,6 +141,23 @@ public TableDialog(CategoryGraph graph) {
}
buttonPane.add(okButton, "6, 2, left, top");
}

fc.setFileFilter(new FileFilter() {

@Override
public String getDescription() {
return "CSV files";
}

@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
return f.getName().toLowerCase().endsWith(CSV_SUFFIX);
}
});

addComponentListeners();
}

Expand Down Expand Up @@ -200,23 +219,7 @@ public void actionPerformed(ActionEvent e) {

@Override
public void actionPerformed(ActionEvent e) {
int value = fc.showSaveDialog(TableDialog.this);

if (value == JFileChooser.APPROVE_OPTION) {
try {
writeCSVFile(fc.getSelectedFile());
JOptionPane.showMessageDialog(TableDialog.this,
"Saved to " + fc.getSelectedFile() + ".",
"Success", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e1) {
JOptionPane.showMessageDialog(
TableDialog.this,
"Error on writing to file "
+ fc.getSelectedFile() + ".", "Error",
JOptionPane.ERROR_MESSAGE);
}

}
saveCSVFile();
}
});
}
Expand Down Expand Up @@ -258,4 +261,39 @@ public boolean showDialog() {
return model.isBlacklistingChanged();
}

private void saveCSVFile() {
fc.setDialogTitle("Save CSV");
int value = fc.showSaveDialog(this);

if (value == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();

if (!file.getPath().toLowerCase().endsWith(CSV_SUFFIX)) {
file = new File(file.getPath() + CSV_SUFFIX);
}

int override = JOptionPane.YES_OPTION;
if (file.exists()) {
override = JOptionPane.showConfirmDialog(this, "\"" + file.getPath()
+ "\" already exists. Ovewrite?", "File exists",
JOptionPane.YES_NO_OPTION);
}

if (override == JOptionPane.YES_OPTION) {
try {
writeCSVFile(file);

JOptionPane.showMessageDialog(this,
"Saved to \"" + file.getAbsolutePath() + "\".",
"Success", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(
this,
"Error on writing to file "
+ file.getAbsolutePath() + ".", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
}

0 comments on commit ca42ded

Please sign in to comment.