Skip to content

Commit

Permalink
added file saving feature
Browse files Browse the repository at this point in the history
  • Loading branch information
AppLoidx committed May 26, 2019
1 parent 03caff3 commit 3810df4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/main/java/application/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public final class Settings {
private static transient volatile int tickFinishSleepTime = 6;
private static String backgroundPath;
private static String assemblerText;
private static Color busColor = DisplayStyles.COLOR_BUS;
private static Color activeBusColor = DisplayStyles.COLOR_ACTIVE;
private static Image backgroundImage;
Expand Down Expand Up @@ -61,6 +62,7 @@ public static void save(){
data.mainTextColor = Settings.getMainTextColor();
data.inputTitleColor = Settings.inputTitleColor;
data.inputBodyColor = Settings.getInputBodyColor();
data.assemblerText = Settings.getAssemblerText();
if (saveDebugMarks) saveDebugMarks(data);
data.save();
}
Expand All @@ -77,6 +79,7 @@ private static void init(SettingsData data){
Settings.setMainTextColor(data.mainTextColor);
Settings.setBorderColor(data.borderColor);
Settings.setBackgroundColor(data.backgroundColor);
Settings.setAssemblerText(data.assemblerText);
if (data.markedAddrs!=null) Debugger.markedAddrs = data.markedAddrs;
}
public static void init(){
Expand Down Expand Up @@ -150,4 +153,12 @@ public static Color getBackgroundColor() {
public static void setBackgroundColor(Color backgroundColor) {
Settings.backgroundColor = backgroundColor;
}

public static String getAssemblerText() {
return assemblerText;
}

public static void setAssemblerText(String assemblerText) {
Settings.assemblerText = assemblerText;
}
}
2 changes: 2 additions & 0 deletions src/main/java/application/entities/SettingsData.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class SettingsData implements Serializable {
public Color borderColor;
@SerializedName("background_color")
public Color backgroundColor;
@SerializedName("assembler_text")
public String assemblerText;

public transient Image backgroundImage;

Expand Down
95 changes: 92 additions & 3 deletions src/main/java/application/views/AssemblerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.io.InputStream;
import java.awt.event.WindowEvent;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -57,7 +59,10 @@ public AssemblerView(GUI gui) {
this.cpu = gui.getCPU();
this.cmanager = gui.getComponentManager();
this.text = new RSyntaxTextArea();
this.text.setText("ORG ADDR\n\nBEGIN:\n\t");

this.text.setText(Settings.getAssemblerText()!=null?Settings.getAssemblerText():"ORG ADDR\n\nBEGIN:\n\t");


this.asm = new Assembler(this.cpu.getInstructionSet(), this.text);
setTextArea();

Expand Down Expand Up @@ -89,6 +94,9 @@ public AssemblerView(GUI gui) {

this.add(button);

setSaveToFileBtn();
setExtractFromFileBtn();

}

public void panelActivate() {
Expand Down Expand Up @@ -207,6 +215,7 @@ public void keyTyped(KeyEvent e) {

@Override
public void keyPressed(KeyEvent e) {
Settings.setAssemblerText(text.getText());
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
text.removeAllLineHighlights();
try {
Expand All @@ -231,5 +240,85 @@ public void keyReleased(KeyEvent e) {
this.add(scroll);
}

private void setSaveToFileBtn(){
JButton btn = new JButton("Сохранить в файл");
btn.setBounds(650, 430, 170, 40);
btn.addActionListener((a) -> createFileChooseWindow(0));
this.add(btn);
}

private void setExtractFromFileBtn(){
JButton btn = new JButton("Загрузить с файла");
btn.setBounds(650, 480, 170, 40);
btn.addActionListener((a) -> createFileChooseWindow(1));
this.add(btn);
}

private void createFileChooseWindow(int operation){
JFrame frame = new JFrame("Choose file");
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();

JFileChooser fileChooser = new JFileChooser();
pane.add(fileChooser);
if (operation==0) fileChooser.setApproveButtonText("Сохранить");
else fileChooser.setApproveButtonText("Загрузить");
fileChooser.addActionListener(a -> {
if (a.getID() == ActionEvent.ACTION_PERFORMED && fileChooser.getSelectedFile()!=null) {

File file = fileChooser.getSelectedFile();
boolean status = false;
switch (operation){
case 0:
try {
if (!file.exists()) file.createNewFile();
if (file.canWrite() && file.canExecute()){

BufferedWriter bw = new BufferedWriter(new FileWriter(file));
String data = text.getText();
for (String l : data.split("\n")) {
bw.write(l);
bw.newLine();
}
bw.close();
status = true;

}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (status) frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
case 1:

if (file.canRead() && file.canExecute()){
try {
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String line;
while((line=br.readLine())!=null){
sb.append(line).append("\n");
}
text.setText(sb.toString());
status = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (status) frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
}
}

}
});

frame.getContentPane().add(pane, BorderLayout.CENTER);

frame.setSize(650, 450);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}

}

0 comments on commit 3810df4

Please sign in to comment.