Added basic import and export functionality

This commit is contained in:
Andrew Lalis 2017-04-24 08:39:53 +02:00
parent 5545ca7548
commit fbba8b2550
3 changed files with 106 additions and 12 deletions

View File

@ -0,0 +1,18 @@
package net.agspace;
import java.io.File;
class Utils {
static String getExtension(File f) {
String ext = null;
String s = f.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
ext = s.substring(i+1).toLowerCase();
}
return ext;
}
}

View File

@ -18,7 +18,7 @@
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children> <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"> <grid id="9f3fe" binding="tengwarPanelTop" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <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"> <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">
@ -30,13 +30,32 @@
<children> <children>
<component id="3d5ee" class="javax.swing.JLabel" binding="tengwarPanelLabel"> <component id="3d5ee" class="javax.swing.JLabel" binding="tengwarPanelLabel">
<constraints> <constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/> <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties>
<font name="Tengwar Annatar" size="18" style="0"/> <font name="Tengwar Annatar" size="18" style="0"/>
<text value="1b$y6D"/> <text value="1b$y6D"/>
</properties> </properties>
</component> </component>
<grid id="f21a7" 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"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="48468" class="javax.swing.JButton" binding="saveButton" custom-create="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Save"/>
<toolTipText value="Saves the tengwar text to a file."/>
</properties>
</component>
</children>
</grid>
</children> </children>
</grid> </grid>
<scrollpane id="ad3c8"> <scrollpane id="ad3c8">
@ -81,7 +100,7 @@
<children> <children>
<component id="32988" class="javax.swing.JLabel" binding="inputPanelLabel"> <component id="32988" class="javax.swing.JLabel" binding="inputPanelLabel">
<constraints> <constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="7" anchor="0" fill="0" indent="0" use-parent-layout="false"/> <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"/>
</constraints> </constraints>
<properties> <properties>
<text value="English"/> <text value="English"/>

View File

@ -5,17 +5,27 @@ import net.agspace.translate.Translator;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener; import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.io.IOException; 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.ArrayList;
/** /**
* Created by Andrew's Computer on 22-Apr-17. * Created by Andrew's Computer on 22-Apr-17.
*/ */
public class Window { public class Window {
public static final List<String> OPEN_FILE_EXTENSIONS = new ArrayList<String>();
static {
OPEN_FILE_EXTENSIONS.add("txt");
}
private JPanel mainPanel; private JPanel mainPanel;
private JPanel tengwarPanel; private JPanel tengwarPanel;
private JPanel inputPanel; private JPanel inputPanel;
@ -31,6 +41,7 @@ public class Window {
private JCheckBox liveCheckBox; private JCheckBox liveCheckBox;
private JButton clearButton; private JButton clearButton;
private JButton importButton; private JButton importButton;
private JButton saveButton;
private boolean isLive = false; private boolean isLive = false;
@ -86,6 +97,13 @@ public class Window {
onImportClicked(); onImportClicked();
} }
}); });
//Save text to a file.
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
onSaveClicked();
}
});
} }
private void createUIComponents() { private void createUIComponents() {
@ -114,6 +132,7 @@ public class Window {
this.clearButton = new JButton(); this.clearButton = new JButton();
this.importButton = new JButton(); this.importButton = new JButton();
this.inputTextArea.requestFocus(); this.inputTextArea.requestFocus();
this.saveButton = new JButton();
} }
public JPanel getMainPanel(){ public JPanel getMainPanel(){
@ -129,15 +148,53 @@ public class Window {
* What to do if the user clicks the 'import' button. * What to do if the user clicks the 'import' button.
*/ */
private void onImportClicked(){ private void onImportClicked(){
if (Desktop.isDesktopSupported()){ JFileChooser fileChooser = new JFileChooser(System.getProperty("user.documents"));
if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)){ fileChooser.setFileFilter(new FileFilter() {
try { @Override
Desktop.getDesktop().browse(new URI("https://www.youtube.com/watch?v=lsJLLEwUYZM")); public boolean accept(File f) {
} catch (IOException e1) { if (f.isDirectory())
e1.printStackTrace(); return true;
} catch (URISyntaxException e1) { String extension = Utils.getExtension(f);
e1.printStackTrace(); if (extension != null)
return OPEN_FILE_EXTENSIONS.contains(extension.toLowerCase());
else
return false;
}
@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.
*/
private 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();
} }
} }
} }