TengwarTranslator/src/net/agspace/Window.java

237 lines
7.7 KiB
Java

package net.agspace;
import net.agspace.translate.Translator;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.ArrayList;
public class Window {
//List of files that can be imported.
private static final List<String> OPEN_FILE_EXTENSIONS = new ArrayList<>();
static {
OPEN_FILE_EXTENSIONS.add("txt");
}
private JPanel mainPanel;
private JPanel tengwarPanel;
private JPanel tengwarPanelTop;
private JLabel tengwarPanelLabel;
private JTextArea tengwarTextArea;
private JPanel inputPanel;
private JPanel inputPanelTop;
private JLabel inputPanelLabel;
private JTextArea inputTextArea;
private JPanel translateButtonPanel;
private JButton toTengwarButton;
private JButton toEnglishButton;
private JButton clearButton;
private boolean isLive = false;
Window() {
//Load the font into the system.
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, this.getClass().getClassLoader().getResourceAsStream("resources/tngan.ttf")));
} catch (FontFormatException e) {
System.err.println("Font format exception!");
} catch (IOException e) {
System.err.println("IOException!");
}
//Explicit translation from english to tengwar.
toTengwarButton.addActionListener(e -> tengwarTextArea.setText(Translator.translateToTengwar(inputTextArea.getText())));
//Clear both text areas.
clearButton.addActionListener(e -> {
tengwarTextArea.setText(null);
inputTextArea.setText(null);
});
//Translate tengwar to english.
toEnglishButton.addActionListener(e -> inputTextArea.setText(Translator.translateToEnglish(tengwarTextArea.getText())));
}
private void createUIComponents() {
mainPanel = new JPanel();
this.inputTextArea = new JTextArea();
inputTextArea.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
onEnglishTextChanged();
}
@Override
public void removeUpdate(DocumentEvent e) {
onEnglishTextChanged();
}
@Override
public void changedUpdate(DocumentEvent e) {
onEnglishTextChanged();
}
});
this.tengwarTextArea = new JTextArea();
tengwarTextArea.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
onTengwarTextChanged();
}
@Override
public void removeUpdate(DocumentEvent e) {
onTengwarTextChanged();
}
@Override
public void changedUpdate(DocumentEvent e) {
onTengwarTextChanged();
}
});
this.toTengwarButton = new JButton();
this.toEnglishButton = new JButton();
this.clearButton = new JButton();
this.inputTextArea.requestFocus();
}
JPanel getMainPanel(){
return this.mainPanel;
}
/**
* If the english text is changed, and live is selected, then update the tengwar text.
*/
private void onEnglishTextChanged(){
if (isLive && inputTextArea.hasFocus())
tengwarTextArea.setText(Translator.translateToTengwar(inputTextArea.getText()));
}
/**
* If tengwar text is changed, and live is selected, then update the english text.
*/
private void onTengwarTextChanged(){
if (isLive && tengwarTextArea.hasFocus())
inputTextArea.setText(Translator.translateToEnglish(tengwarTextArea.getText()));
}
/**
* Toggle the live button and disable translate buttons if needed.
* @param e event generated by the check box.
*/
void onLiveToggled(ActionEvent e){
if (((JCheckBoxMenuItem) e.getSource()).getState()){
//Deactivate buttons, set live to true.
toTengwarButton.setEnabled(false);
toEnglishButton.setEnabled(false);
isLive = true;
} else {
//Activate buttons, set live to false.
toTengwarButton.setEnabled(true);
toEnglishButton.setEnabled(true);
isLive = false;
}
}
/**
* What to do if the user clicks the 'import' button.
*/
void onImportClicked(){
JFileChooser fileChooser = new JFileChooser(System.getProperty("user.documents"));
fileChooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory())
return true;
String extension = Utils.getExtension(f);
return extension != null && OPEN_FILE_EXTENSIONS.contains(extension.toLowerCase());
}
@Override
public String getDescription() {
return "Plain text files.";
}
});
int result = fileChooser.showOpenDialog(this.mainPanel);
if (result == JFileChooser.APPROVE_OPTION){
try (BufferedReader br = new BufferedReader(new FileReader(fileChooser.getSelectedFile()))){
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null){
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
inputTextArea.setText(sb.toString());
tengwarTextArea.setText(Translator.translateToTengwar(inputTextArea.getText()));
} catch (IOException e){
e.printStackTrace();
}
}
}
/**
* What to do when the user wants to save their document.
*/
void onSaveClicked(){
JFileChooser fileChooser = new JFileChooser(System.getProperty("user.documents"));
int result = fileChooser.showSaveDialog(this.mainPanel);
if (result == JFileChooser.APPROVE_OPTION){
try (PrintStream ps = new PrintStream(fileChooser.getSelectedFile())){
ps.println(tengwarTextArea.getText());
} catch (IOException e){
e.printStackTrace();
}
}
}
/**
* Attempt to open the pdf in the browser.
*/
void onEnglishModeAboutClicked(){
if (Desktop.isDesktopSupported()){
try{
Desktop.getDesktop().browse(new URI("https://github.com/andrewlalis/TengwarTranslator/blob/master/src/resources/EnglishOneToOneTengwarV2-1.pdf"));
} catch (URISyntaxException | IOException e){
e.printStackTrace();
}
}
}
/**
* Attempt to open Tengwar wikipedia page.
*/
void onTengwarAboutClicked(){
if (Desktop.isDesktopSupported()){
try{
Desktop.getDesktop().browse(new URI("https://en.wikipedia.org/wiki/Tengwar"));
} catch (URISyntaxException | IOException e){
e.printStackTrace();
}
}
}
/**
* Attempt to open my github page.
*/
void onAboutMeClicked(){
if (Desktop.isDesktopSupported()){
try{
Desktop.getDesktop().browse(new URI("https://github.com/andrewlalis"));
} catch (URISyntaxException | IOException e){
e.printStackTrace();
}
}
}
}