Skip to content

Commit aa85806

Browse files
author
sirmigui
committed
Now if you have a font, theme and size in Jotepad, it will be saved and loaded
1 parent dc76d28 commit aa85806

File tree

4 files changed

+177
-98
lines changed

4 files changed

+177
-98
lines changed

src/jotepad/controller/FileManager.java

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import java.io.FileInputStream;
77
import java.io.FileOutputStream;
88
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;
1111

1212
import javax.swing.JFileChooser;
1313
import javax.swing.JOptionPane;
1414

15+
import jotepad.model.Config;
1516
import 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
}

src/jotepad/model/Config.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package jotepad.model;
2+
3+
import java.io.Serializable;
4+
5+
public class Config implements Serializable {
6+
private static final long serialVersionUID = 6066124863275515889L;
7+
8+
private String themeName;
9+
private String fontName;
10+
private int fontSize;
11+
12+
public Config(String themeName, String fontName, int fontSize) {
13+
this.themeName = themeName;
14+
this.fontName = fontName;
15+
this.fontSize = fontSize;
16+
}
17+
18+
public String getThemeName() {
19+
return themeName;
20+
}
21+
22+
public String getFontName() {
23+
return fontName;
24+
}
25+
26+
public int getFontSize() {
27+
return fontSize;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return String.format("%s %s %d", themeName, fontName, fontSize);
33+
}
34+
35+
}

src/jotepad/view/FontManager.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424
* @author sirmigui
2525
*/
2626
public class FontManager extends JDialog {
27+
private static final long serialVersionUID = -8162065733123645031L;
2728

2829
public static final int MAX_SIZE = 120;
29-
public static final int MIN_SIZE = 2;
30-
public static final int STEP_SIZE = 2;
31-
32-
private static final long serialVersionUID = -8162065733123645031L;
30+
public static final int MIN_SIZE = 6;
31+
public static final int STEP_SIZE = 4;
3332
private static final String TITLE = "Font selector";
3433

3534
private JComboBox<String> fontsCombo;
@@ -48,10 +47,12 @@ public FontManager(MainWindow view) {
4847
requestFocus();
4948
setAlwaysOnTop(true);
5049

50+
Font viewFont = view.getTextArea().getFont();
51+
5152
systemFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
5253

5354
fontsCombo = new JComboBox<>(systemFonts);
54-
fontsCombo.setSelectedItem(view.getTextArea().getFont().getFontName());
55+
fontsCombo.setSelectedItem(viewFont.getFontName());
5556
fontsCombo.addItemListener(new ItemListener() {
5657
@Override
5758
public void itemStateChanged(ItemEvent e) {
@@ -63,8 +64,9 @@ public void itemStateChanged(ItemEvent e) {
6364
});
6465

6566
sizesSpinner = new JSpinner();
66-
spinnerModel = new SpinnerNumberModel(view.getTextArea().getFont().getSize(), MIN_SIZE, MAX_SIZE, STEP_SIZE);
67-
spinnerModel.addChangeListener((e) -> {
67+
spinnerModel = new SpinnerNumberModel(Math.clamp(viewFont.getSize(), MIN_SIZE, MAX_SIZE), MIN_SIZE, MAX_SIZE,
68+
STEP_SIZE);
69+
spinnerModel.addChangeListener(e -> {
6870
Font f = new Font(fontsCombo.getItemAt(fontsCombo.getSelectedIndex()), Font.PLAIN,
6971
(int) spinnerModel.getValue());
7072
view.getTextArea().setFont(f);

0 commit comments

Comments
 (0)