6
6
import java .io .FileInputStream ;
7
7
import java .io .FileOutputStream ;
8
8
import 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 ;
11
11
12
12
import javax .swing .JFileChooser ;
13
13
import javax .swing .JOptionPane ;
14
14
15
+ import jotepad .model .Config ;
15
16
import jotepad .view .MainWindow ;
16
17
17
18
/**
@@ -22,14 +23,14 @@ public class FileManager {
22
23
private static final String USER_HOME = System .getProperty ("user.home" );
23
24
private static final String BACKUP_PATH = String .format ("%s/.jotepad/backups/" , USER_HOME );
24
25
26
+ private static final File CONFIG_FILE = new File ("config" );
27
+
25
28
private File file , backupFile ;
26
- private final Object lock ;
27
29
private final MainWindow view ;
28
30
29
31
public FileManager (MainWindow view ) {
30
32
this .file = null ;
31
33
this .backupFile = null ;
32
- this .lock = new Object ();
33
34
this .view = view ;
34
35
35
36
File backupFolder = new File (BACKUP_PATH );
@@ -39,41 +40,42 @@ public FileManager(MainWindow view) {
39
40
}
40
41
}
41
42
42
- public void saveFile () {
43
+ public boolean saveFile () {
43
44
if (file == null ) {
44
- saveFileAs ();
45
+ return saveFileAs ();
45
46
} else {
46
47
byte [] buffer = textToBuffer ();
47
48
writeContentInFile (buffer , file );
48
49
writeContentInFile (buffer , backupFile );
50
+ return true ;
49
51
}
50
52
}
51
53
52
- public void saveFileAs () {
54
+ public boolean saveFileAs () {
53
55
int fileAnswer = view .getFileChooser ().showSaveDialog (view );
56
+ int answer = JOptionPane .NO_OPTION ;
54
57
55
58
if (fileAnswer != JFileChooser .APPROVE_OPTION ) {
56
- return ;
59
+ return false ;
57
60
}
58
61
59
62
file = new File (view .getFileChooser ().getSelectedFile ().getAbsolutePath ());
60
63
backupFile = new File (createBackupFileName ());
61
64
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 ()) {
63
71
writeContentInFile (textToBuffer (), file );
64
72
writeContentInFile (textToBuffer (), backupFile );
65
73
view .changeTitle (file .getAbsolutePath ());
74
+ return true ;
75
+ }
66
76
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 ;
75
78
76
- }
77
79
}
78
80
79
81
public void openFile () {
@@ -101,32 +103,8 @@ private byte[] textToBuffer() {
101
103
String value = view .getTextArea ().getText ();
102
104
byte [] buffer = new byte [value .length ()];
103
105
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 );
130
108
}
131
109
132
110
return buffer ;
@@ -167,4 +145,38 @@ private String createBackupFileName() {
167
145
return String .format ("%s%s" , BACKUP_PATH , file .getName ());
168
146
}
169
147
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
+
170
182
}
0 commit comments