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

View File

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

View File

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