-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppUI-bak
233 lines (197 loc) · 7.31 KB
/
AppUI-bak
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
package com.example;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;
import javafx.scene.control.Button;
import javafx.scene.media.Media;
import javafx.scene.media.MediaException;
import javafx.scene.media.MediaPlayer;
public class AppUI implements ActionListener {
private AudioPlayer audioPlayer;
private MediaPlayer mediaPlayer;
private DefaultListModel<String> listModel;
private JList<String> fileList;
JScrollPane scrollPane = new JScrollPane(fileList);
private JComboBox<String> sortBox;
public AppUI() {
audioPlayer = new AudioPlayer();
// Set the look and feel of the UI to system
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException
| IllegalAccessException e) {
e.printStackTrace();
}
// Create the list model and list objects
listModel = new DefaultListModel<>();
fileList = new JList<>(listModel);
// Create the frame object
JFrame frame = new JFrame("Java Exercise #6: Java Music Player");
// Add a window listener to the frame to handle closing events
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
int confirmed = JOptionPane.showConfirmDialog(null,
"Are you sure you want to exit the application?", "Exit Application Confirmation",
JOptionPane.YES_NO_OPTION);
if (confirmed == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
// Create the label object
JLabel label = new JLabel("Java Music Player");
// Create the main content panel with BorderLayout
JPanel contentPanel = new JPanel(new BorderLayout());
// Create open button object
JButton openButton = new JButton("Open");
openButton.setActionCommand("Open Explorer");
openButton.addActionListener(this);
// JButton playStopButton = new JButton("Play/Stop");
// playStopButton.setActionCommand("Play");
// playStopButton.addActionListener(this);
Button playPauseButton = new Button("Play/Pause");
playPauseButton.setOnAction(e -> playPauseSelectedFile());
JButton sortButton = new JButton("Sort");
sortButton.setActionCommand("Sort");
sortButton.addActionListener(this);
sortBox = new JComboBox<>();
sortBox.addItem("Ascending");
sortBox.addItem("Descending");
sortBox.addActionListener(this);
// Create a panel for the buttons with FlowLayout
JPanel buttonPanel = new JPanel(new FlowLayout());
// Add the label and file list to the top and center of the content panel
contentPanel.add(label, BorderLayout.NORTH);
contentPanel.add(fileList, BorderLayout.CENTER);
// Add the buttons to the button panel
buttonPanel.add(openButton);
// buttonPanel.add(playStopButton);
buttonPanel.add(sortBox);
// Add the button panel to the bottom of the content panel
contentPanel.add(buttonPanel, BorderLayout.SOUTH);
// Add the content panel to the frame
frame.add(contentPanel);
// Set the size and visibility of the frame
frame.setSize(600, 400);
frame.setLocationRelativeTo(null); // center window
frame.setVisible(true);
}
private void openFileExplorer() {
JFileChooser fileChooser = new JFileChooser();
setFileChooserDirectory(fileChooser);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Audio Files", "mp3", "wav", "aiff", "au", "snd", "mid", "rmi");
fileChooser.setFileFilter(filter);
fileChooser.setMultiSelectionEnabled(true);
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File[] selectedFiles = fileChooser.getSelectedFiles();
addFilesToList(selectedFiles);
}
}
private void playPauseSelectedFile() {
int selectedIndex = fileList.getSelectedIndex();
if (selectedIndex != -1) {
String filePath = listModel.getElementAt(selectedIndex);
if (mediaPlayer != null && mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) {
// Stop the media player if it's already playing
mediaPlayer.pause();
} else {
// Start playing the media file
Media media = new Media(new File(filePath).toURI().toString());
mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();
}
}
}
private void sortList() {
String selected = (String) sortBox.getSelectedItem();
List<String> list = new ArrayList<>();
for (int i = 0; i < listModel.getSize(); i++) {
list.add(listModel.get(i));
}
if (selected.equals("Ascending")) {
list.sort((s1, s2) -> s1.compareTo(s2));
} else if (selected.equals("Descending")) {
list.sort((s1, s2) -> s2.compareTo(s1));
}
listModel.clear();
for (String s : list) {
listModel.addElement(s);
}
}
// Maybe put the action triggers in a separate....class?
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
switch (actionCommand) {
case "Open Explorer":
openFileExplorer();
break;
case "Play":
playPauseSelectedFile();
break;
default:
// Handle the sortBox selection
if (e.getSource() == sortBox) {
sortList();
}
break;
// Add cases for other actions as needed
}
}
//the following could be sorted into file handling class
private void setFileChooserDirectory(JFileChooser fileChooser) {
File downloadsDir = new File(System.getProperty("user.home") + System.getProperty("file.separator") + "Downloads");
if (downloadsDir.exists() && downloadsDir.isDirectory()) {
fileChooser.setCurrentDirectory(downloadsDir);
} else {
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
}
}
private void addFilesToList(File[] files) {
for (File file : files) {
if (file.isFile()) {
try {
String filePath = file.getAbsolutePath();
listModel.addElement(filePath);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
// // // Un-implemented buttons
// // Create stop button object
// JButton stopButton = new JButton("Stop");
// stopButton.addActionListener(this);
// // Create prev button object
// JButton prevButton = new JButton("Prev");
// prevButton.addActionListener(this);
// // Create next button object
// JButton nextButton = new JButton("Next");
// nextButton.addActionListener(this);
// buttonPanel.add(stopButton);
// buttonPanel.add(prevButton);
// buttonPanel.add(nextButton);