66import java .io .FileInputStream ;
77import java .io .FileOutputStream ;
88import java .io .IOException ;
9- import java .util . logging . Level ;
10- import java .util . logging . Logger ;
9+ import java .io . ObjectInputStream ;
10+ import java .io . ObjectOutputStream ;
1111
1212import javax .swing .JFileChooser ;
1313import javax .swing .JOptionPane ;
1414
15+ import jotepad .model .Config ;
1516import jotepad .view .MainWindow ;
1617
1718/**
@@ -22,14 +23,14 @@ public class FileManager {
2223 private static final String USER_HOME = System .getProperty ("user.home" );
2324 private static final String BACKUP_PATH = String .format ("%s/.jotepad/backups/" , USER_HOME );
2425
26+ private static final File CONFIG_FILE = new File ("config" );
27+
2528 private File file , backupFile ;
26- private final Object lock ;
2729 private final MainWindow view ;
2830
2931 public FileManager (MainWindow view ) {
3032 this .file = null ;
3133 this .backupFile = null ;
32- this .lock = new Object ();
3334 this .view = view ;
3435
3536 File backupFolder = new File (BACKUP_PATH );
@@ -39,41 +40,42 @@ public FileManager(MainWindow view) {
3940 }
4041 }
4142
42- public void saveFile () {
43+ public boolean saveFile () {
4344 if (file == null ) {
44- saveFileAs ();
45+ return saveFileAs ();
4546 } else {
4647 byte [] buffer = textToBuffer ();
4748 writeContentInFile (buffer , file );
4849 writeContentInFile (buffer , backupFile );
50+ return true ;
4951 }
5052 }
5153
52- public void saveFileAs () {
54+ public boolean saveFileAs () {
5355 int fileAnswer = view .getFileChooser ().showSaveDialog (view );
56+ int answer = JOptionPane .NO_OPTION ;
5457
5558 if (fileAnswer != JFileChooser .APPROVE_OPTION ) {
56- return ;
59+ return false ;
5760 }
5861
5962 file = new File (view .getFileChooser ().getSelectedFile ().getAbsolutePath ());
6063 backupFile = new File (createBackupFileName ());
6164
62- if (!file .exists ()) {
65+ if (file .exists ()) {
66+ answer = JOptionPane .showConfirmDialog (view , "This file already exists, do you want overwrite it?" ,
67+ "Overwrite file?" , JOptionPane .YES_NO_OPTION , JOptionPane .INFORMATION_MESSAGE );
68+ }
69+
70+ if (answer == JOptionPane .YES_OPTION || !file .exists ()) {
6371 writeContentInFile (textToBuffer (), file );
6472 writeContentInFile (textToBuffer (), backupFile );
6573 view .changeTitle (file .getAbsolutePath ());
74+ return true ;
75+ }
6676
67- } else {
68- int answer = JOptionPane .showConfirmDialog (view , "This file already exists, do you want overwrite it?" ,
69- "Overwrite file?" , JOptionPane .YES_NO_OPTION , JOptionPane .INFORMATION_MESSAGE );
70-
71- if (answer == JOptionPane .YES_OPTION ) {
72- writeContentInFile (textToBuffer (), file );
73- writeContentInFile (textToBuffer (), backupFile );
74- }
77+ return false ;
7578
76- }
7779 }
7880
7981 public void openFile () {
@@ -101,32 +103,8 @@ private byte[] textToBuffer() {
101103 String value = view .getTextArea ().getText ();
102104 byte [] buffer = new byte [value .length ()];
103105
104- Thread thread1 = new Thread (() -> {
105- synchronized (lock ) {
106- for (int i = 0 ; i < value .length () / 2 ; i ++) {
107- buffer [i ] = (byte ) value .charAt (i );
108- }
109- lock .notify ();
110- }
111- });
112-
113- Thread thread2 = new Thread (() -> {
114- synchronized (lock ) {
115- for (int i = value .length () / 2 ; i < value .length (); i ++) {
116- buffer [i ] = (byte ) value .charAt (i );
117- }
118- lock .notify ();
119- }
120- });
121-
122- thread1 .start ();
123- thread2 .start ();
124-
125- try {
126- thread1 .join ();
127- thread2 .join ();
128- } catch (InterruptedException ex ) {
129- Logger .getLogger (MainWindow .class .getName ()).log (Level .SEVERE , null , ex );
106+ for (int i = 0 , len = value .length (); i < len ; i ++) {
107+ buffer [i ] = (byte ) value .charAt (i );
130108 }
131109
132110 return buffer ;
@@ -167,4 +145,38 @@ private String createBackupFileName() {
167145 return String .format ("%s%s" , BACKUP_PATH , file .getName ());
168146 }
169147
148+ public static Config readConfigFile () {
149+ if (!CONFIG_FILE .exists ()) {
150+ return null ;
151+ }
152+
153+ Config c = null ;
154+
155+ try (ObjectInputStream reader = new ObjectInputStream (
156+ new BufferedInputStream (new FileInputStream (CONFIG_FILE )))) {
157+ c = (Config ) reader .readObject ();
158+
159+ } catch (IOException ex ) {
160+ ex .printStackTrace ();
161+ } catch (ClassNotFoundException e ) {
162+ e .printStackTrace ();
163+ }
164+
165+ return c ;
166+ }
167+
168+ public static void saveConfigFile (Config c ) {
169+ try (ObjectOutputStream writer = new ObjectOutputStream (
170+ new BufferedOutputStream (new FileOutputStream (CONFIG_FILE )))) {
171+ if (!CONFIG_FILE .exists ()) {
172+ CONFIG_FILE .createNewFile ();
173+ }
174+
175+ writer .writeObject (c );
176+ } catch (IOException e ) {
177+ // TODO Auto-generated catch block
178+ e .printStackTrace ();
179+ }
180+ }
181+
170182}
0 commit comments