Skip to content

Commit

Permalink
Refactor open file to open source/destination file #21
Browse files Browse the repository at this point in the history
For HoI4 (also EUIV and Stellaris) files there no more equivalent between
a Translator Helper line and a file in the system.
So the action is splitted in 2 for the source and the destination.
  • Loading branch information
NicolasGrosjean committed Jul 14, 2018
1 parent 8428590 commit 4145397
Showing 1 changed file with 55 additions and 33 deletions.
88 changes: 55 additions & 33 deletions src/gui/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
import config.ConfigStorage;
import config.WorkingSession;
import parsing.CK2ParsedFile;
import parsing.HoI4ParsedFile;
import parsing.IParsedFile;
import parsing.Language;
import parsing.Parse;
import renderer.ButtonRenderer;
import renderer.ColoredInteger;
Expand Down Expand Up @@ -459,39 +461,13 @@ public void actionPerformed(ActionEvent e) {
});
contextMenu.add(checkLineItem);

JMenuItem openFileItem = new JMenuItem("Open file in the default software");
openFileItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRows()[0];
String directory = (ws.getDirectory().endsWith("/")) ? ws.getDirectory() : ws.getDirectory() + "/";
if ((table.getValueAt(row, FILE_COLUMN) instanceof CK2ParsedFile)) {
IParsedFile f = (IParsedFile) table.getValueAt(row, FILE_COLUMN);
try {
Desktop.getDesktop().open(new File(directory + f.getName()));
} catch (IllegalArgumentException exception) {
JOptionPane.showMessageDialog(null,
"Impossible to open the file " + f.getName() + ".\nThe file doesn't exist anymore.",
"ERROR", JOptionPane.ERROR_MESSAGE);
} catch (UnsupportedOperationException exception) {
JOptionPane.showMessageDialog(null,
"Impossible to open the file " + f.getName()
+ ".\nYour platform doesn't allow to open files.",
"ERROR", JOptionPane.ERROR_MESSAGE);
} catch (IOException exception) {
JOptionPane.showMessageDialog(null, "Impossible to open the file " + f.getName()
+ ".\nNo defined application to open this file or the application failed to launch.",
"ERROR", JOptionPane.ERROR_MESSAGE);
} catch (SecurityException exception) {
JOptionPane.showMessageDialog(null,
"Impossible to open the file " + f.getName()
+ ".\nInsuffisant permission to open this file. ",
"ERROR", JOptionPane.ERROR_MESSAGE);
}
}
}
});
contextMenu.add(openFileItem);
JMenuItem openSourceFileItem = new JMenuItem("Open source file in the default software");
openSourceFileItem.addActionListener(new OpenFile(ws.getSourceLanguage()));
contextMenu.add(openSourceFileItem);

JMenuItem openDestFileItem = new JMenuItem("Open destination file in the default software");
openDestFileItem.addActionListener(new OpenFile(ws.getSourceLanguage()));
contextMenu.add(openDestFileItem);

// Select a line by right clicking
contextMenu.addPopupMenuListener(new PopupMenuListener() {
Expand Down Expand Up @@ -666,4 +642,50 @@ public void actionPerformed(ActionEvent arg0) {
}
}
}

class OpenFile implements ActionListener {
private Language language;

OpenFile(Language language) {
this.language = language;
}

@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRows()[0];
String directory = (ws.getDirectory().endsWith("/")) ? ws.getDirectory() : ws.getDirectory() + "/";
Object file = table.getValueAt(row, FILE_COLUMN);
if (!(file instanceof IParsedFile)) {
System.err.println("The line is not a IParsedFile");
}
String filePath = "";
IParsedFile f = (IParsedFile) table.getValueAt(row, FILE_COLUMN);
if (file instanceof CK2ParsedFile) {
filePath = directory + f.getName();
} else if (table.getValueAt(row, FILE_COLUMN) instanceof HoI4ParsedFile) {
filePath = ((HoI4ParsedFile) table.getValueAt(row, FILE_COLUMN)).getFilePath(language);
}
try {
Desktop.getDesktop().open(new File(filePath));
} catch (IllegalArgumentException exception) {
JOptionPane.showMessageDialog(null,
"Impossible to open the file " + f.getName() + ".\nThe file doesn't exist anymore.",
"ERROR", JOptionPane.ERROR_MESSAGE);
} catch (UnsupportedOperationException exception) {
JOptionPane.showMessageDialog(null,
"Impossible to open the file " + f.getName()
+ ".\nYour platform doesn't allow to open files.",
"ERROR", JOptionPane.ERROR_MESSAGE);
} catch (IOException exception) {
JOptionPane.showMessageDialog(null, "Impossible to open the file " + f.getName()
+ ".\nNo defined application to open this file or the application failed to launch.",
"ERROR", JOptionPane.ERROR_MESSAGE);
} catch (SecurityException exception) {
JOptionPane.showMessageDialog(null,
"Impossible to open the file " + f.getName()
+ ".\nInsuffisant permission to open this file. ",
"ERROR", JOptionPane.ERROR_MESSAGE);
}
}
}
}

0 comments on commit 4145397

Please sign in to comment.