diff --git a/src/main/java/application/Settings.java b/src/main/java/application/Settings.java index 8687f50..a5e91e0 100644 --- a/src/main/java/application/Settings.java +++ b/src/main/java/application/Settings.java @@ -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; @@ -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(); } @@ -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(){ @@ -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; + } } diff --git a/src/main/java/application/entities/SettingsData.java b/src/main/java/application/entities/SettingsData.java index 5b23095..391d02f 100644 --- a/src/main/java/application/entities/SettingsData.java +++ b/src/main/java/application/entities/SettingsData.java @@ -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; diff --git a/src/main/java/application/views/AssemblerView.java b/src/main/java/application/views/AssemblerView.java index 22483e6..e895172 100644 --- a/src/main/java/application/views/AssemblerView.java +++ b/src/main/java/application/views/AssemblerView.java @@ -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; @@ -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(); @@ -89,6 +94,9 @@ public AssemblerView(GUI gui) { this.add(button); + setSaveToFileBtn(); + setExtractFromFileBtn(); + } public void panelActivate() { @@ -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 { @@ -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); + } + }