Fixed some issues and replaced implementations with Lambda expressions.

This commit is contained in:
Andrew Lalis 2017-05-02 11:47:08 +02:00
parent 8a71899904
commit 84a1cb36d4
3 changed files with 35 additions and 74 deletions

View File

@ -4,10 +4,8 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
/** /**
* @author Andrew Lalis * @author Andrew Lalis
@ -15,8 +13,8 @@ import java.io.IOException;
*/ */
public class Main { public class Main {
public static final String TITLE = "Tengwar Typewriter"; private static final String TITLE = "Tengwar Typewriter";
public static final String ICON_PATH = "resources/icon.png"; private static final String ICON_PATH = "resources/icon.png";
public static void main(String[] args){ public static void main(String[] args){
JFrame f = new JFrame(TITLE); JFrame f = new JFrame(TITLE);
@ -31,31 +29,16 @@ public class Main {
JMenu fileMenu = new JMenu("File"); JMenu fileMenu = new JMenu("File");
//Save Item. //Save Item.
JMenuItem saveItem = new JMenuItem("Save"); JMenuItem saveItem = new JMenuItem("Save");
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK)); saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
saveItem.addActionListener(new ActionListener() { saveItem.addActionListener(e -> window.onSaveClicked());
@Override
public void actionPerformed(ActionEvent e) {
window.onSaveClicked();
}
});
fileMenu.add(saveItem); fileMenu.add(saveItem);
//Import Item. //Import Item.
JMenuItem importItem = new JMenuItem("Import"); JMenuItem importItem = new JMenuItem("Import");
importItem.addActionListener(new ActionListener() { importItem.addActionListener(e -> window.onImportClicked());
@Override
public void actionPerformed(ActionEvent e) {
window.onImportClicked();
}
});
fileMenu.add(importItem); fileMenu.add(importItem);
//Exit Item. //Exit Item.
JMenuItem exitItem = new JMenuItem("Exit"); JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener() { exitItem.addActionListener(e -> f.dispose());
@Override
public void actionPerformed(ActionEvent e) {
f.dispose();
}
});
fileMenu.add(exitItem); fileMenu.add(exitItem);
menuBar.add(fileMenu); menuBar.add(fileMenu);
//Edit menu. //Edit menu.
@ -63,49 +46,29 @@ public class Main {
//Live checkbox //Live checkbox
JCheckBoxMenuItem liveCheckBox = new JCheckBoxMenuItem("Live"); JCheckBoxMenuItem liveCheckBox = new JCheckBoxMenuItem("Live");
liveCheckBox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK)); liveCheckBox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
liveCheckBox.addActionListener(new ActionListener() { liveCheckBox.addActionListener(e -> window.onLiveToggled(e));
@Override
public void actionPerformed(ActionEvent e) {
window.onLiveToggled(e);
}
});
editMenu.add(liveCheckBox); editMenu.add(liveCheckBox);
menuBar.add(editMenu); menuBar.add(editMenu);
//About Menu. //About Menu.
JMenu aboutMenu = new JMenu("About"); JMenu aboutMenu = new JMenu("About");
//Tengwar Item. //Tengwar Item.
JMenuItem tengwarItem = new JMenuItem("Tengwar"); JMenuItem tengwarItem = new JMenuItem("Tengwar");
tengwarItem.addActionListener(new ActionListener() { tengwarItem.addActionListener(e -> window.onTengwarAboutClicked());
@Override
public void actionPerformed(ActionEvent e) {
window.onTengwarAboutClicked();
}
});
aboutMenu.add(tengwarItem); aboutMenu.add(tengwarItem);
//English Mode. //English Mode.
JMenuItem englishModeItem = new JMenuItem("English Mode"); JMenuItem englishModeItem = new JMenuItem("English Mode");
englishModeItem.addActionListener(new ActionListener() { englishModeItem.addActionListener(e -> window.onEnglishModeAboutClicked());
@Override
public void actionPerformed(ActionEvent e) {
window.onEnglishModeAboutClicked();
}
});
aboutMenu.add(englishModeItem); aboutMenu.add(englishModeItem);
//About the author. //About the author.
JMenuItem aboutMeItem = new JMenuItem("About the Author"); JMenuItem aboutMeItem = new JMenuItem("About the Author");
aboutMeItem.addActionListener(new ActionListener() { aboutMeItem.addActionListener(e -> window.onAboutMeClicked());
@Override
public void actionPerformed(ActionEvent e) {
window.onAboutMeClicked();
}
});
aboutMenu.add(aboutMeItem); aboutMenu.add(aboutMeItem);
menuBar.add(aboutMenu); menuBar.add(aboutMenu);
f.setJMenuBar(menuBar); f.setJMenuBar(menuBar);
f.pack(); f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {

View File

@ -11,14 +11,13 @@ import java.awt.event.*;
import java.io.*; import java.io.*;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
public class Window { public class Window {
//List of files that can be imported. //List of files that can be imported.
public static final List<String> OPEN_FILE_EXTENSIONS = new ArrayList<>(); private static final List<String> OPEN_FILE_EXTENSIONS = new ArrayList<>();
static { static {
OPEN_FILE_EXTENSIONS.add("txt"); OPEN_FILE_EXTENSIONS.add("txt");
@ -52,29 +51,16 @@ public class Window {
} }
//Explicit translation from english to tengwar. //Explicit translation from english to tengwar.
toTengwarButton.addActionListener(new ActionListener() { toTengwarButton.addActionListener(e -> tengwarTextArea.setText(Translator.translateToTengwar(inputTextArea.getText())));
@Override
public void actionPerformed(ActionEvent e) {
tengwarTextArea.setText(Translator.translateToTengwar(inputTextArea.getText()));
}
});
//Clear both text areas. //Clear both text areas.
clearButton.addActionListener(new ActionListener() { clearButton.addActionListener(e -> {
@Override tengwarTextArea.setText(null);
public void actionPerformed(ActionEvent e) { inputTextArea.setText(null);
tengwarTextArea.setText(null);
inputTextArea.setText(null);
}
}); });
//Translate tengwar to english. //Translate tengwar to english.
toEnglishButton.addActionListener(new ActionListener() { toEnglishButton.addActionListener(e -> inputTextArea.setText(Translator.translateToEnglish(tengwarTextArea.getText())));
@Override
public void actionPerformed(ActionEvent e) {
inputTextArea.setText(Translator.translateToEnglish(tengwarTextArea.getText()));
}
});
} }
private void createUIComponents() { private void createUIComponents() {
@ -97,6 +83,22 @@ public class Window {
} }
}); });
this.tengwarTextArea = new JTextArea(); 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.toTengwarButton = new JButton();
this.toEnglishButton = new JButton(); this.toEnglishButton = new JButton();
this.clearButton = new JButton(); this.clearButton = new JButton();
@ -152,10 +154,7 @@ public class Window {
if (f.isDirectory()) if (f.isDirectory())
return true; return true;
String extension = Utils.getExtension(f); String extension = Utils.getExtension(f);
if (extension != null) return extension != null && OPEN_FILE_EXTENSIONS.contains(extension.toLowerCase());
return OPEN_FILE_EXTENSIONS.contains(extension.toLowerCase());
else
return false;
} }
@Override @Override

View File

@ -571,7 +571,6 @@ public class Translator {
//Do nothing, this is fine. //Do nothing, this is fine.
} }
String currentLiteral = getEnglishLiteral(currentChar); String currentLiteral = getEnglishLiteral(currentChar);
System.out.println("At: "+i+" Literal: "+currentLiteral);
//Check if the current character is a literal translation. //Check if the current character is a literal translation.
if (currentLiteral != null){ if (currentLiteral != null){
//Check if the next character is a vowel that should be placed before a character. //Check if the next character is a vowel that should be placed before a character.