-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramFrame.java
More file actions
246 lines (200 loc) · 10.6 KB
/
Copy pathProgramFrame.java
File metadata and controls
246 lines (200 loc) · 10.6 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package daw;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JFrame;
/**
*
* @author opdada01 Daniel Opdahl
* @author mantno01 Noah Manternach
* @author nteste01 Teboho Nteso
*
*/
//The ProgramFrame class is where our swing components are added and we hold "global" objects e.g.,
//current_track. These objects are needed by most components are thus are stored at the highest
//level, i.e., here.
public class ProgramFrame extends JFrame {
/* FIELDS */
private static final String our_title = "DAW";
public static ArrayList<File> tracks_list = new ArrayList<File>();
public static int current_track;
public static MainDisplayWindow our_main_display_window;
public static FileExplorerWindow our_file_explorer_window;
public static MenuBar our_menu_bar;
public static ToolBar our_tool_bar;
public static JLabel our_current_track_display;
public long position = 0;
/* CONSTRUCTOR */
public ProgramFrame() {
//First we check to see if there is a saved state of the application at the designated file
//path. If there is, we read in the saved objects (ArrayList<File>, long, int) and set them
//to be this ProgramFrame's tracks_list, position, and current_track, respectively.
File restore_state = new File("src/data/saveStates/state.ser");
if(restore_state.exists() && !restore_state.isDirectory()) {
try {
FileInputStream fileIn = new FileInputStream("src/data/saveStates/state.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
setTracksList((ArrayList<File>) in.readObject());
position = (long) in.readObject();
//System.out.println("position: " + position);
current_track = (int) in.readObject();
in.close();
fileIn.close();
}
catch (Exception could_not_open_restore_state) {}
}
//Here we set the title and layout style of our ProgramFrame.
setTitle(our_title);
setLayout(new GridBagLayout());
//This WindowListener is activated when the ProgramFrame is closed. First, we prompt the user
//if they would like to save the state of the application or not. If they select yes, we save
//the current state of the application (detailed below), if not, then we ask the user if they
//would like to delete the previous saved state (if there is one). If they select yes, we remove
//the previously saved state, if no, then we leave it.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
int save_reply = JOptionPane.showConfirmDialog(null, "Would you like to save the state of the application?", "Save?", JOptionPane.YES_NO_OPTION);
if (save_reply == JOptionPane.YES_OPTION) {
//Here, we create a FileOutputStream to our destination file, then we create a
//ObjectOutputStream to our FileOutputStream. Next, we write the track_list.
//Then, we attempt to write the position of the current clip, so the user can
//resume the same clip in the same place on restart. However, if that is not
//possible (e.g., there is no clip object in our_tool_bar), then we simply
//write a "0". Then, we write current_track and close our streams.
FileOutputStream destination_file = new FileOutputStream(new File("src/data/saveStates/state.ser"));
ObjectOutputStream obj_output_stream = new ObjectOutputStream(destination_file);
obj_output_stream.writeObject(getTracksList());
try {
obj_output_stream.writeObject(our_tool_bar.getClip().getMicrosecondPosition());
}
catch (Exception could_not_get_positon) {
obj_output_stream.writeObject(0L);
}
obj_output_stream.writeObject(getMainDisplayWindow().getCurrentTrack());
obj_output_stream.close();
destination_file.close();
}
else {
File previous_state = new File("src/data/saveStates/state.ser");
if(previous_state.exists() && !previous_state.isDirectory()) {
//If the user elected not to save the state of the application, we prompt
//them if they would like to remove the previously saved state.
int delete_response = JOptionPane.showConfirmDialog(null, "Delete previously saved state?", "Remove?", JOptionPane.YES_NO_OPTION);
if (delete_response == JOptionPane.YES_OPTION) {
//If the user elects to remove the previous state, we delete the file.
previous_state.delete();
}
}
}
}
//This catch clause catches all IOExceptions that may be throw while the streams are
//writing or opening.
catch (Exception could_not_save_state) {}
}
});
//Here we create the main components of our ProgramFrame.
our_main_display_window = new MainDisplayWindow(this, tracks_list, current_track);
our_file_explorer_window = new FileExplorerWindow(our_main_display_window, tracks_list, current_track);
our_menu_bar = new MenuBar(our_main_display_window, tracks_list);
our_tool_bar = new ToolBar(our_main_display_window, our_file_explorer_window, tracks_list, position);
try {
our_current_track_display = new JLabel("Current Track: " + getTracksList().get(current_track).getName());
}
//This catch clause catch catches any Exception that may be thrown while creating
//our_current_track_display, e.g., empty tracks_list, current_track does not exist.
catch (Exception our_current_track_display_generalExcpt) {
our_current_track_display = new JLabel("No Current Track");
}
//Here we create, specify, and add the GridBagConstraints for every component of our ProgramFrame.
GridBagConstraints menuBarConstraints = new GridBagConstraints();
GridBagConstraints toolBarConstraints = new GridBagConstraints();
GridBagConstraints audioDisplayWindowConstraints = new GridBagConstraints();
GridBagConstraints fileExplorerWindowConstraints = new GridBagConstraints();
GridBagConstraints currentTrackDisplayConstraints = new GridBagConstraints();
menuBarConstraints.gridx = 0;
menuBarConstraints.gridy = 0;
menuBarConstraints.fill = GridBagConstraints.HORIZONTAL;
menuBarConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
menuBarConstraints.weightx = 0;
menuBarConstraints.weighty = 0;
menuBarConstraints.gridwidth = 2;
toolBarConstraints.gridx = 0;
toolBarConstraints.gridy = 1;
toolBarConstraints.fill = GridBagConstraints.HORIZONTAL;
toolBarConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
toolBarConstraints.weightx = 0;
toolBarConstraints.weighty = 0;
toolBarConstraints.ipady = 40;
currentTrackDisplayConstraints.gridx = 0;
currentTrackDisplayConstraints.gridy = 2;
currentTrackDisplayConstraints.fill = GridBagConstraints.HORIZONTAL;
currentTrackDisplayConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
currentTrackDisplayConstraints.weightx = 0;
currentTrackDisplayConstraints.weighty = 0;
currentTrackDisplayConstraints.gridwidth = 2;
audioDisplayWindowConstraints.gridx = 0;
audioDisplayWindowConstraints.gridy = 3;
audioDisplayWindowConstraints.fill = GridBagConstraints.BOTH;
audioDisplayWindowConstraints.anchor = GridBagConstraints.CENTER;
audioDisplayWindowConstraints.weightx = 1;
audioDisplayWindowConstraints.weighty = 1;
fileExplorerWindowConstraints.gridx = 1;
fileExplorerWindowConstraints.gridy = 1;
fileExplorerWindowConstraints.gridheight = 3;
fileExplorerWindowConstraints.fill = GridBagConstraints.BOTH;
fileExplorerWindowConstraints.anchor = GridBagConstraints.CENTER;
fileExplorerWindowConstraints.weightx = 0;
fileExplorerWindowConstraints.weighty = 0;
getContentPane().add(our_menu_bar, menuBarConstraints);
getContentPane().add(our_tool_bar, toolBarConstraints);
getContentPane().add(our_current_track_display, currentTrackDisplayConstraints);
getContentPane().add(our_main_display_window, audioDisplayWindowConstraints);
getContentPane().add(our_file_explorer_window, fileExplorerWindowConstraints);
our_file_explorer_window.setVisible(true);
pack();
//This for loop loads all tracks in tracks_list into our_main_display_window, if there to be
//tracks already in tracks_list, i.e., tracks already loaded into tracks_list via a saved state.
for(int i = 0; i<getTracksList().size(); i++) {
getMainDisplayWindow().addAudioFile(i);
}
}
/* ACCESSORS */
ArrayList<File> getTracksList() {
return tracks_list;
}
MainDisplayWindow getMainDisplayWindow() {
return our_main_display_window;
}
int getCurrentTrack() {
return current_track;
}
/* MUTATORS */
void setTracksList(ArrayList<File> other) {
tracks_list = other;
}
void setMainDisplayWindow(MainDisplayWindow other) {
our_main_display_window = other;
}
void setCurrentTrack(int other) {
current_track = other;
try {
our_current_track_display.setText("Current Track: "+ getTracksList().get(current_track).getName());
}
//This catch clause catch catches any Exception that may be thrown while creating
//our_current_track_display, e.g., empty tracks_list, current_track does not exist.
//If such an error occurs, it means there is no current track, so we set the text of
//our_current_track_display accordingly.
catch (Exception no_current_track_to_display) {
our_current_track_display.setText("No Current Track");
}
our_current_track_display.revalidate();
our_current_track_display.repaint();
}
}