1515import java .awt .event .WindowEvent ;
1616import java .awt .event .WindowListener ;
1717import java .io .BufferedReader ;
18+ import java .io .BufferedWriter ;
19+ import java .io .File ;
1820import java .io .FileReader ;
21+ import java .io .FileWriter ;
1922import java .io .IOException ;
20- import java .io .InputStream ;
2123
2224import javax .swing .BorderFactory ;
25+ import javax .swing .JButton ;
2326import javax .swing .JFileChooser ;
2427import javax .swing .JFrame ;
2528import javax .swing .JMenu ;
@@ -57,6 +60,7 @@ public class Window {
5760 volatile int inputStart = 0 ;
5861
5962 private static Font font ;
63+ private File openedFile = null ; //Null means, creating a new file when saving.
6064
6165 public Window () {
6266 try {
@@ -85,17 +89,21 @@ public void componentHidden(ComponentEvent e) {}
8589 p .addOutput (new Output () {
8690 public void warning (String message ) {
8791 console .append ("[WARN]" + message + "\n " );
92+ consolePane .getVerticalScrollBar ().setValue (consolePane .getVerticalScrollBar ().getMaximum ());
8893 }
8994 public void log (String message , boolean newline ) {
9095 console .append (message + (newline ? "\n " : "" ));
96+ consolePane .getVerticalScrollBar ().setValue (consolePane .getVerticalScrollBar ().getMaximum ());
9197 }
9298 public void error (String message ) {
9399 console .append (message + "\n " );
100+ consolePane .getVerticalScrollBar ().setValue (consolePane .getVerticalScrollBar ().getMaximum ());
94101 }
95102 });
96103 input = new ApplicationInput () {
97104 @ Override
98105 public void awaitInput () {
106+ System .out .println ("Waiting for input..." );
99107 waitForEnter = true ;
100108 console .setEnabled (true );
101109 inputStart = console .getText ().length ();
@@ -106,7 +114,7 @@ public void awaitInput() {
106114 p .setInput (input );
107115 p .setApplicationListener (new ApplicationListener () {
108116 public void done (int exitCode ) {
109- runWindow .setTitle ("Done ( Exit Code " + exitCode + ")" );
117+ runWindow .setTitle ("Finished with Exit Code " + exitCode );
110118 window .setEnabled (true );
111119 console .setEnabled (false );
112120 }
@@ -129,14 +137,17 @@ public boolean dispatchKeyEvent(KeyEvent e) {
129137 JMenu m = new JMenu ("File" );
130138
131139 JMenuItem newFile = new JMenuItem ("New" );
140+ newFile .setAccelerator (KeyStroke .getKeyStroke ("control N" ));
132141 newFile .addActionListener (new ActionListener () {
133142 public void actionPerformed (ActionEvent e ) {
134143 textArea .setText ("" );
144+ openedFile = null ;
135145 }
136146 });
137147 m .add (newFile );
138148
139- JMenuItem loadFile = new JMenuItem ("Load File" );
149+ JMenuItem loadFile = new JMenuItem (getFormattedBarText ("Open..." ));
150+ loadFile .setAccelerator (KeyStroke .getKeyStroke ("control O" ));
140151 loadFile .addActionListener (new ActionListener () {
141152 public void actionPerformed (ActionEvent e ) {
142153 window .setEnabled (false );
@@ -148,11 +159,12 @@ public void actionPerformed(ActionEvent e) {
148159 BufferedReader reader = new BufferedReader (new FileReader (chooser .getSelectedFile ()));
149160 String line = reader .readLine ();
150161 while (line != null ) {
151- appendToPane (textArea , "\n " + line , Color .black );
162+ appendToPane (textArea , line + "\n " , Color .black );
152163 line = reader .readLine ();
153164 }
154165 reader .close ();
155166 window .setEnabled (true );
167+ openedFile = chooser .getSelectedFile ();
156168 } catch (Exception e1 ) {
157169 e1 .printStackTrace ();
158170 window .setEnabled (true );
@@ -164,14 +176,37 @@ public void actionPerformed(ActionEvent e) {
164176 });
165177 m .add (loadFile );
166178 JMenuItem saveFile = new JMenuItem ("Save File" );
179+ saveFile .setAccelerator (KeyStroke .getKeyStroke ("control S" ));
180+ saveFile .addActionListener (new ActionListener () {
181+ public void actionPerformed (ActionEvent e ) {
182+ if (openedFile == null ) {
183+ if (textArea .getText ().isEmpty ()) return ;
184+ window .setEnabled (false );
185+ JFileChooser chooser = new JFileChooser ();
186+ chooser .setDialogTitle ("Save new file" );
187+ int res = chooser .showSaveDialog (new JFrame ());
188+ if (res == JFileChooser .APPROVE_OPTION ) {
189+ BufferedWriter writer = new BufferedWriter (new FileWriter (chooser .getSelectedFile ()));
190+ try {
191+ writer .write (textArea .getText ());
192+ writer .close ();
193+ } catch (IOException e1 ) {
194+ e1 .printStackTrace ();
195+ }
196+ }
197+ window .setEnabled (true );
198+ } else System .out .println ("Overwriging file..." );
199+
200+ }
201+ });
167202 m .add (saveFile );
168203 bar .add (m );
169204
170205 JMenu m2 = new JMenu ("Edit" );
171206 bar .add (m2 );
172207
173208 JMenu m3 = new JMenu ("Run" );
174- JMenuItem run = new JMenuItem (getFormattedBarText ("Run in shell " ));
209+ JMenuItem run = new JMenuItem (getFormattedBarText ("Run in Shell " ));
175210 run .setAccelerator (KeyStroke .getKeyStroke (KeyEvent .VK_F5 , 0 ));
176211 run .addActionListener (new ActionListener () {
177212 public void actionPerformed (ActionEvent e ) {
@@ -182,7 +217,6 @@ public void actionPerformed(ActionEvent e) {
182217 runWindow .setVisible (true );
183218 window .setEnabled (false );
184219 p .execute (textArea .getText (), true );
185- System .out .println ("Done" );
186220 }
187221 });
188222 m3 .add (run );
@@ -202,6 +236,14 @@ public void actionPerformed(ActionEvent e) {
202236 window .setSize (500 , 500 );
203237 }
204238
239+ JFrame saveDialog ;
240+ JButton approveSave ;
241+ JButton cancelSave ;
242+
243+ public void initSaveNotification () {
244+ saveDialog = new JFrame ("Save changes to " );
245+ }
246+
205247 JFrame runWindow ;
206248 JTextArea console ;
207249 JScrollPane consolePane ;
@@ -244,7 +286,6 @@ public void componentHidden(ComponentEvent e) {}
244286 console .addCaretListener (new CaretListener () {
245287 @ Override
246288 public void caretUpdate (CaretEvent e ) {
247- System .out .println ("Updating caret..." );
248289 System .out .println (console .getCaretPosition () + " " + inputStart );
249290 if (input .inputRequested () && console .getCaretPosition () < inputStart ) {
250291 console .setCaretPosition (inputStart );
@@ -268,6 +309,7 @@ public void keyPressed(KeyEvent e) {
268309 consolePane = new JScrollPane (console , JScrollPane .VERTICAL_SCROLLBAR_AS_NEEDED , JScrollPane .HORIZONTAL_SCROLLBAR_AS_NEEDED );
269310 consolePane .setBounds (5 , 5 , runWindow .getRootPane ().getWidth ()-10 , runWindow .getRootPane ().getHeight ()-10 );
270311 consolePane .setBorder (BorderFactory .createLineBorder (Color .BLACK ));
312+ consolePane .setAutoscrolls (true );
271313 runWindow .add (consolePane );
272314
273315 runWindow .pack ();
0 commit comments