Added basic import and export functionality
This commit is contained in:
parent
5545ca7548
commit
fbba8b2550
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -18,7 +18,7 @@
|
|||
<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">
|
||||
<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"/>
|
||||
<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">
|
||||
|
@ -30,13 +30,32 @@
|
|||
<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="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>
|
||||
<properties>
|
||||
<font name="Tengwar Annatar" size="18" style="0"/>
|
||||
<text value="1b$y6D"/>
|
||||
</properties>
|
||||
</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>
|
||||
</grid>
|
||||
<scrollpane id="ad3c8">
|
||||
|
@ -81,7 +100,7 @@
|
|||
<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="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>
|
||||
<properties>
|
||||
<text value="English"/>
|
||||
|
|
|
@ -5,17 +5,27 @@ 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.IOException;
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
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.
|
||||
*/
|
||||
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 tengwarPanel;
|
||||
private JPanel inputPanel;
|
||||
|
@ -31,6 +41,7 @@ public class Window {
|
|||
private JCheckBox liveCheckBox;
|
||||
private JButton clearButton;
|
||||
private JButton importButton;
|
||||
private JButton saveButton;
|
||||
|
||||
private boolean isLive = false;
|
||||
|
||||
|
@ -86,6 +97,13 @@ public class Window {
|
|||
onImportClicked();
|
||||
}
|
||||
});
|
||||
//Save text to a file.
|
||||
saveButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
onSaveClicked();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void createUIComponents() {
|
||||
|
@ -114,6 +132,7 @@ public class Window {
|
|||
this.clearButton = new JButton();
|
||||
this.importButton = new JButton();
|
||||
this.inputTextArea.requestFocus();
|
||||
this.saveButton = new JButton();
|
||||
}
|
||||
|
||||
public JPanel getMainPanel(){
|
||||
|
@ -129,15 +148,53 @@ public class Window {
|
|||
* What to do if the user clicks the 'import' button.
|
||||
*/
|
||||
private void onImportClicked(){
|
||||
if (Desktop.isDesktopSupported()){
|
||||
if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)){
|
||||
try {
|
||||
Desktop.getDesktop().browse(new URI("https://www.youtube.com/watch?v=lsJLLEwUYZM"));
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
} catch (URISyntaxException e1) {
|
||||
e1.printStackTrace();
|
||||
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);
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue