Removed local translator in favor of github project by myself, and #3

Merged
andrewlalis merged 1 commits from development into master 2017-07-23 10:20:46 +00:00
16 changed files with 506 additions and 257 deletions

View File

@ -1,5 +1,5 @@
# TengwarTranslator
![icon.png](/src/resources/icon.png "TengwarTranslator Icon")
![icon.png](/icon.png "TengwarTranslator Icon")
### About the Program
@ -11,7 +11,7 @@ Java's Built-in *Swing* library is used to provide the cross-platform user inter
### Translation
Since Tengwar is inherently a phonetic script, converting english to Tengwar would be almost impossible, and would require extremely advanced software to detect how individual words are pronounced. To avoid this, I created my [own Tengwar mode](/src/resources/EnglishOneToOneTengwarV2-1.pdf "English to Tengwar") which can be used to transcribe to and from English in a way that no information is lost in the transcription, as would be the case with normal Tengwar. I do this by including characters to represent every English letter, while still obeying the rules concerning placement of vowel diacritics and compound characters. The characters are shown below.
Since Tengwar is inherently a phonetic script, converting english to Tengwar would be almost impossible, and would require extremely advanced software to detect how individual words are pronounced. To avoid this, I created my [own Tengwar mode](/EnglishOneToOneTengwarV2-1.pdf "English to Tengwar") which can be used to transcribe to and from English in a way that no information is lost in the transcription, as would be the case with normal Tengwar. I do this by including characters to represent every English letter, while still obeying the rules concerning placement of vowel diacritics and compound characters. The characters are shown below.
![Tengwar](https://puu.sh/vtyfi/598ad704e1.png "Tengwar Characters")

63
pom.xml Normal file
View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.andrewlalis</groupId>
<artifactId>TengwarTypewriter</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>net.agspace.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.andrewlalis</groupId>
<artifactId>TengwarTranslatorLibrary</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</project>

View File

@ -3,7 +3,6 @@ package net.agspace;
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;
@ -14,7 +13,7 @@ import java.awt.event.KeyEvent;
public class Main {
private static final String TITLE = "Tengwar Typewriter";
private static final String ICON_PATH = "resources/icon.png";
private static final String ICON_PATH = "icon.png";
public static void main(String[] args){
JFrame f = new JFrame(TITLE);

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="net.agspace.Window">
<grid id="27dc6" binding="mainPanel" custom-create="true" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<grid id="27dc6" binding="mainPanel" custom-create="true" layout-manager="GridBagLayout">
<constraints>
<xy x="20" y="20" width="786" height="461"/>
</constraints>
@ -10,30 +9,34 @@
</properties>
<border type="none"/>
<children>
<grid id="7a294" binding="tengwarPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<grid id="7a294" binding="tengwarPanel" layout-manager="GridBagLayout">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="1.0"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="9f3fe" binding="tengwarPanelTop" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<grid id="9f3fe" binding="tengwarPanelTop" layout-manager="GridBagLayout">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="30"/>
</grid>
<gridbag weightx="1.0" weighty="0.0"/>
</constraints>
<properties/>
<properties>
<preferredSize width="54" height="30"/>
</properties>
<border type="none"/>
<children>
<component id="3d5ee" class="javax.swing.JLabel" binding="tengwarPanelLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="1.0"/>
</constraints>
<properties>
<font name="Tengwar Annatar" size="18" style="0"/>
<labelFor value="952a"/>
<text value="1b$y6D"/>
</properties>
</component>
@ -42,6 +45,7 @@
<scrollpane id="ad3c8">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="1.0"/>
</constraints>
<properties/>
<border type="none"/>
@ -58,10 +62,10 @@
</scrollpane>
</children>
</grid>
<grid id="5929f" binding="inputPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<grid id="5929f" binding="inputPanel" layout-manager="GridBagLayout">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="1.0"/>
</constraints>
<properties>
<enabled value="true"/>
@ -69,21 +73,28 @@
</properties>
<border type="none"/>
<children>
<grid id="f5158" binding="inputPanelTop" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<grid id="f5158" binding="inputPanelTop" layout-manager="GridBagLayout">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="30"/>
</grid>
<gridbag weightx="1.0" weighty="0.0"/>
</constraints>
<properties/>
<properties>
<enabled value="true"/>
<font/>
<preferredSize width="56" height="30"/>
</properties>
<border type="none"/>
<children>
<component id="32988" class="javax.swing.JLabel" binding="inputPanelLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="1.0"/>
</constraints>
<properties>
<font size="18" style="0"/>
<labelFor value="53965"/>
<text value="English"/>
</properties>
</component>
@ -92,6 +103,7 @@
<scrollpane id="36b2e">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="1.0"/>
</constraints>
<properties/>
<border type="none"/>
@ -108,10 +120,10 @@
</scrollpane>
</children>
</grid>
<grid id="5da94" binding="translateButtonPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="30" left="0" bottom="0" right="0"/>
<grid id="5da94" binding="translateButtonPanel" layout-manager="GridBagLayout">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="1" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
<gridbag top="50" left="0" bottom="0" right="0" weightx="0.0" weighty="1.0"/>
</constraints>
<properties/>
<border type="none"/>
@ -119,9 +131,10 @@
<component id="8f5c7" class="javax.swing.JButton" binding="toTengwarButton" custom-create="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="1" anchor="2" fill="1" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="1.0"/>
</constraints>
<properties>
<icon value="resources/toTengwar.png"/>
<icon value="toTengwar.png"/>
<label value=""/>
<text value=""/>
<toolTipText value="Translate to tengwar"/>
@ -130,9 +143,10 @@
<component id="1097" class="javax.swing.JButton" binding="toEnglishButton" custom-create="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="1" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="1.0"/>
</constraints>
<properties>
<icon value="resources/toEnglish.png"/>
<icon value="toEnglish.png"/>
<label value=""/>
<text value=""/>
<toolTipText value="Translate to english"/>
@ -141,9 +155,10 @@
<component id="59b28" class="javax.swing.JButton" binding="clearButton" custom-create="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="1" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="1.0"/>
</constraints>
<properties>
<icon value="resources/clear.png"/>
<icon value="clear.png"/>
<label value=""/>
<text value=""/>
</properties>

View File

@ -0,0 +1,408 @@
package net.agspace;
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.ActionEvent;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
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.
$$$setupUI$$$();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, this.getClass().getClassLoader().getResourceAsStream("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();
}
}
}
/**
* @noinspection ALL
*/
private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
if (currentFont == null) return null;
String resultName;
if (fontName == null) {
resultName = currentFont.getName();
} else {
Font testFont = new Font(fontName, Font.PLAIN, 10);
if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
resultName = fontName;
} else {
resultName = currentFont.getName();
}
}
return new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
}
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
*/
private void $$$setupUI$$$() {
createUIComponents();
mainPanel.setLayout(new GridBagLayout());
mainPanel.setPreferredSize(new Dimension(720, 480));
tengwarPanel = new JPanel();
tengwarPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc;
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(tengwarPanel, gbc);
tengwarPanelTop = new JPanel();
tengwarPanelTop.setLayout(new GridBagLayout());
tengwarPanelTop.setPreferredSize(new Dimension(54, 30));
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.BOTH;
tengwarPanel.add(tengwarPanelTop, gbc);
tengwarPanelLabel = new JLabel();
Font tengwarPanelLabelFont = this.$$$getFont$$$("Tengwar Annatar", Font.PLAIN, 18, tengwarPanelLabel.getFont());
if (tengwarPanelLabelFont != null) tengwarPanelLabel.setFont(tengwarPanelLabelFont);
tengwarPanelLabel.setText("1b$y6D");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
tengwarPanelTop.add(tengwarPanelLabel, gbc);
final JScrollPane scrollPane1 = new JScrollPane();
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
tengwarPanel.add(scrollPane1, gbc);
Font tengwarTextAreaFont = this.$$$getFont$$$("Tengwar Annatar", Font.PLAIN, 16, tengwarTextArea.getFont());
if (tengwarTextAreaFont != null) tengwarTextArea.setFont(tengwarTextAreaFont);
tengwarTextArea.setLineWrap(true);
tengwarTextArea.setWrapStyleWord(true);
scrollPane1.setViewportView(tengwarTextArea);
inputPanel = new JPanel();
inputPanel.setLayout(new GridBagLayout());
inputPanel.setEnabled(true);
inputPanel.setVisible(true);
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(inputPanel, gbc);
inputPanelTop = new JPanel();
inputPanelTop.setLayout(new GridBagLayout());
inputPanelTop.setEnabled(true);
Font inputPanelTopFont = this.$$$getFont$$$(null, -1, -1, inputPanelTop.getFont());
if (inputPanelTopFont != null) inputPanelTop.setFont(inputPanelTopFont);
inputPanelTop.setPreferredSize(new Dimension(56, 30));
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.BOTH;
inputPanel.add(inputPanelTop, gbc);
inputPanelLabel = new JLabel();
Font inputPanelLabelFont = this.$$$getFont$$$(null, Font.PLAIN, 18, inputPanelLabel.getFont());
if (inputPanelLabelFont != null) inputPanelLabel.setFont(inputPanelLabelFont);
inputPanelLabel.setText("English");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
inputPanelTop.add(inputPanelLabel, gbc);
final JScrollPane scrollPane2 = new JScrollPane();
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
inputPanel.add(scrollPane2, gbc);
inputTextArea.setEditable(true);
inputTextArea.setLineWrap(true);
inputTextArea.setWrapStyleWord(true);
scrollPane2.setViewportView(inputTextArea);
translateButtonPanel = new JPanel();
translateButtonPanel.setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(50, 0, 0, 0);
mainPanel.add(translateButtonPanel, gbc);
toTengwarButton.setIcon(new ImageIcon(getClass().getResource("/toTengwar.png")));
toTengwarButton.setLabel("");
toTengwarButton.setText("");
toTengwarButton.setToolTipText("Translate to tengwar");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.fill = GridBagConstraints.HORIZONTAL;
translateButtonPanel.add(toTengwarButton, gbc);
toEnglishButton.setIcon(new ImageIcon(getClass().getResource("/toEnglish.png")));
toEnglishButton.setLabel("");
toEnglishButton.setText("");
toEnglishButton.setToolTipText("Translate to english");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.HORIZONTAL;
translateButtonPanel.add(toEnglishButton, gbc);
clearButton.setIcon(new ImageIcon(getClass().getResource("/clear.png")));
clearButton.setLabel("");
clearButton.setText("");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
translateButtonPanel.add(clearButton, gbc);
tengwarPanelLabel.setLabelFor(tengwarTextArea);
inputPanelLabel.setLabelFor(inputTextArea);
}
/**
* @noinspection ALL
*/
public JComponent $$$getRootComponent$$$() {
return mainPanel;
}
}

View File

Before

Width:  |  Height:  |  Size: 298 B

After

Width:  |  Height:  |  Size: 298 B

View File

Before

Width:  |  Height:  |  Size: 6.2 KiB

After

Width:  |  Height:  |  Size: 6.2 KiB

View File

Before

Width:  |  Height:  |  Size: 414 B

After

Width:  |  Height:  |  Size: 414 B

View File

Before

Width:  |  Height:  |  Size: 426 B

After

Width:  |  Height:  |  Size: 426 B

View File

@ -1,236 +0,0 @@
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();
}
}
}
}