-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.java
148 lines (128 loc) · 4.63 KB
/
Model.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// src: https://git.uwaterloo.ca/j2avery/cs349_f18_examples/blob/master/java/3-4-Undo/ShapeUndo/Model.java
// fileChooser: https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html
// save&load: https://blog.csdn.net/cc_fys/article/details/78501136
import java.awt.geom.*;
import java.awt.*;
import javax.swing.undo.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Vector;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Model {
private ArrayList<Observer> observers;
private Vector<Sprite> sprites;
public static String FILE_EXT = ".json";
public boolean isSaved = false;
// Observer setup
Model() {
this.observers = new ArrayList<Observer>();
this.sprites = new Vector<Sprite>();
}
public void addObserver(Observer observer) {
this.observers.add(observer);
}
public void removeObserver(Observer observer) {
this.observers.remove(observer);
}
public void notifyObservers() {
for (Observer observer: this.observers)
observer.update(this);
}
// TODO: may need to change it to the blog way, since I don't
// want to use the Serializable package on both FSM and Image layer
// Menubar implementations
public void save() {
// fileChooser dialog
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Save (ctrl-S)");
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("JSON files", "json"));
int returnVal = fileChooser.showSaveDialog(null);
if ( returnVal == JFileChooser.APPROVE_OPTION ) {
File file = fileChooser.getSelectedFile();
String filename = file.toString();
if ( !filename.endsWith(FILE_EXT) ) {
filename += FILE_EXT;
file = new File(filename);
}
// save as JSON file
try {
file.createNewFile();
FileOutputStream fileOut = new FileOutputStream(file, false);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this.sprites);
out.close();
fileOut.close();
this.isSaved = true;
notifyObservers();
} catch(Exception e) {
e.printStackTrace();
}
} // if
}
@SuppressWarnings("unchecked")
public void load() {
// check if user want to save the current changes
if (!isSaved) {
int returnVal = JOptionPane.showConfirmDialog(null, "Will lose current changes! Save now?",
"Warning!", JOptionPane.YES_NO_CANCEL_OPTION);
if ( returnVal == JOptionPane.CANCEL_OPTION ) return;
if ( returnVal == JOptionPane.YES_OPTION ) {
this.save();
this.clearScreen();
} else if ( returnVal == JOptionPane.NO_OPTION ) {
this.clearScreen();
this.load();
}
} else {
this.clearScreen();
}
// fileChooser dialog
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Load (ctrl-L)");
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("JSON files", "json"));
int returnVal = fileChooser.showSaveDialog(null);
if ( returnVal == JFileChooser.APPROVE_OPTION ) {
File file = fileChooser.getSelectedFile();
// load the given JSON file
try {
FileInputStream loadInputFile = new FileInputStream(file);
ObjectInputStream load = new ObjectInputStream(loadInputFile);
this.sprites = ((Vector<Sprite>) load.readObject());
load.close();
this.isSaved = true;
notifyObservers();
} catch(Exception e) {
e.printStackTrace();
}
} // if
}
// FSM related functions
// TODO BY ZOE: rm this later
public void spriteModified() {
this.isSaved = false;
}
public Vector<Sprite> getSprites() {
return sprites;
}
public void clearScreen() {
this.sprites.clear();
notifyObservers();
}
public void reset() {
this.sprites.clear();
try {
sprites.add(Main.makeSprite());
} catch (IOException e) {
e.printStackTrace();
}
notifyObservers();
}
} // model