-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainFrame.java
More file actions
372 lines (318 loc) · 11.5 KB
/
MainFrame.java
File metadata and controls
372 lines (318 loc) · 11.5 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*******************************************************************************
* Copyright (c) 2013 Yannic Remmet, Maximilian Berger.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* Contributors:
* Yannic Remmet - initial API and implementation
* Maximilian Berger - initial API and implementation
******************************************************************************/
package gui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
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.JSeparator;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import player.PodcastPlayerException;
import player.mp3.MP3PodcastPlayer;
import com.google.common.collect.ImmutableList;
import data.Episode;
import data.Podcast;
import data.PodcastLibrary;
public class MainFrame extends JFrame implements Observer {
private final JFrame frame;
private static final long serialVersionUID = -6325374892958547616L;
private final JScrollPane jsp_left;
private final JScrollPane jsp_right;
private final JPanel panel_left;
private final JPanel panel_right;
private final JLabel label_podcast;
private final JLabel label_episode;
private final JSeparator sep_upper;
private final JSeparator sep_lower;
private final JList<Podcast> list_podcast;
private final DefaultListModel<Podcast> listmodel_podcast;
private final JList<Episode> list_episode;
private final DefaultListModel<Episode> listmodel_episode;
//
private final JButton btn_addPodcast;
private final JButton btn_removePodcast;
private final JButton btn_updatePodcast;
private final JButton btn_playEpisode;
private final JButton btn_pauseEpisode;
private final JButton btn_toggleFav;
private final JButton btn_togglePlayed;
private PodcastLibrary library;
private MP3PodcastPlayer player;
public MainFrame() {
super("Radio-Activity");
setIconImage(new ImageIcon("icon/radio-activity.png").getImage());
GridBagLayout gbl = new GridBagLayout();
try {
library = PodcastLibrary.importLibray();
} catch (ClassNotFoundException
| IOException e) {
library = PodcastLibrary.getLibrary();
}
frame = this;
library.addObserver(this);
player = MP3PodcastPlayer.getInstance();
library.updatePodcasts();
// List
listmodel_podcast = new DefaultListModel<Podcast>();
listmodel_episode = new DefaultListModel<Episode>();
list_podcast = new JList<Podcast>(listmodel_podcast);
list_episode = new JList<Episode>(listmodel_episode);
for(Podcast p : library.getPodcasts())
listmodel_podcast.addElement(p);
list_podcast.setCellRenderer(new PodcastCellRenderer());
list_episode.setCellRenderer(new EpisodeCellRenderer());
// Label
label_podcast = new JLabel("Podcasts");
label_episode = new JLabel("Episoden");
//Seperators
sep_upper = new JSeparator(JSeparator.VERTICAL);
sep_lower = new JSeparator(JSeparator.VERTICAL);
// ScrollPane
panel_left = new JPanel();
panel_left.setLayout(new BorderLayout());
panel_left.add(list_podcast, BorderLayout.CENTER);
jsp_left = new JScrollPane(panel_left);
panel_right = new JPanel();
panel_right.setLayout(new BorderLayout());
panel_right.add(list_episode, BorderLayout.CENTER);
jsp_right = new JScrollPane(panel_right);
// Buttons Left
// Get current classloader
ClassLoader cl = this.getClass().getClassLoader();
btn_addPodcast = new JButton(new ImageIcon(cl.getResource("icon/plus.png")));
btn_addPodcast.setToolTipText("Add a Podcast");
btn_removePodcast = new JButton(new ImageIcon(cl.getResource("icon/minus.png")));
btn_removePodcast.setToolTipText("Remove Selected Podcast");
btn_updatePodcast =new JButton(new ImageIcon(cl.getResource("icon/reload.png")));
btn_updatePodcast.setToolTipText("Update Selected Podcast");
btn_toggleFav = new JButton(new ImageIcon(cl.getResource("icon/heart_stroke_big.png")));
btn_toggleFav.setToolTipText("Toggle Selected Episode Fav-status");
btn_togglePlayed = new JButton(new ImageIcon(cl.getResource("icon/played_big.png")));
btn_togglePlayed.setToolTipText("Toggle Selected Episode Played-status");
// Buttons Right
btn_playEpisode = new JButton(new ImageIcon(cl.getResource("icon/play.png")));
btn_pauseEpisode = new JButton(new ImageIcon(cl.getResource("icon/pause.png")));
this.setLayout(gbl);
addComp(this, gbl, label_podcast, 1, 1, 1, 1, 0, 0);
addComp(this, gbl, jsp_left, 1, 2, 7, 16, 1, 1);
addComp(this , gbl, btn_updatePodcast, 1, 18, 1, 1, 0, 0);
addComp(this , gbl, btn_addPodcast, 6, 18, 1, 1, 0, 0);
addComp(this , gbl, btn_removePodcast, 5, 18, 1,1, 0, 0);
addComp(this, gbl, sep_lower, 8, 18, 1, 1, 0, 0);
addComp(this, gbl, sep_upper, 8, 1, 1, 1, 0, 0);
addComp(this, gbl, label_episode, 9, 1, 1, 1, 0, 0);
addComp(this, gbl, jsp_right, 8, 2, 8, 16, 1, 1);
addComp(this, gbl, btn_playEpisode, 9, 18, 1, 1, 0, 0);
addComp(this, gbl, btn_pauseEpisode, 9, 18, 1, 1, 0, 0);
addComp(this, gbl, btn_toggleFav, 10, 18, 1, 1, 0, 0);
addComp(this, gbl, btn_togglePlayed, 11, 18, 1, 1, 0, 0);
addListeners();
setVisible(true);
btn_pauseEpisode.setVisible(false);
setMinimumSize(new Dimension(640,480));
pack();
}
private void updateEpisodeList(){
listmodel_episode.setSize(0);
Podcast selected = list_podcast.getSelectedValue();
if(selected != null){
int sel = list_episode.getSelectedIndex();
ImmutableList<Episode> episodes = selected.getEpisodes();
for(Episode e : episodes)
listmodel_episode.addElement(e);
list_episode.setSelectedIndex(sel);
}
}
private void updatePodcastList(){
int sel = list_podcast.getSelectedIndex();
listmodel_podcast.setSize(0);
ImmutableList<Podcast> podcasts = library.getPodcasts();
for(Podcast p : podcasts)
listmodel_podcast.addElement(p);
list_podcast.setSelectedIndex(sel);
updateEpisodeList();
}
private void addListeners() {
btn_playEpisode.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Episode episodeToBePlayed = list_episode.getSelectedValue();
if(episodeToBePlayed != null){
try {
if(episodeToBePlayed != player.getCurrentEpisode()){
player.setEpisode(episodeToBePlayed);
}
player.play();
btn_playEpisode.setVisible(false);
btn_pauseEpisode.setVisible(true);
} catch (PodcastPlayerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
btn_pauseEpisode.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
player.pause();
} catch (PodcastPlayerException e) {
e.printStackTrace();
}
btn_playEpisode.setVisible(true);
btn_pauseEpisode.setVisible(false);
}
});
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosed(e);
try {
library.exportLibrary();
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
}
});
btn_removePodcast.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Podcast podcastToBeRemoved = list_podcast.getSelectedValue();
if(podcastToBeRemoved != null){
library.removePodcast(podcastToBeRemoved.getTitle());
listmodel_podcast.removeElement(podcastToBeRemoved);
}
}
});
btn_updatePodcast.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
updatePodcastList();
}
});
list_podcast.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent arg0) {
updateEpisodeList();
}
});
list_episode.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if(list_episode.getSelectedValue() != null){
if(list_episode.getSelectedValue().isFavorit())
btn_toggleFav.setIcon(new ImageIcon("icon/heart_fill_big.png"));
else
btn_toggleFav.setIcon(new ImageIcon("icon/heart_stroke_big.png"));
if(list_episode.getSelectedValue().isPlayed())
btn_togglePlayed.setIcon(new ImageIcon("icon/played_big.png"));
else
btn_togglePlayed.setIcon(new ImageIcon("icon/unplayed_big.png"));
}
}
});
btn_addPodcast.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
String res = JOptionPane.showInputDialog("Please input new podcast feed URL");
if(res != null)
library.putPodcast(new URL(res));
} catch (HeadlessException | MalformedURLException e) {
JOptionPane.showMessageDialog(frame, "Please put in a valid url!",
"Invalid URL!", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
});
btn_toggleFav.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Episode selectedEpisode = list_episode.getSelectedValue();
int index = list_episode.getSelectedIndex();
if(selectedEpisode != null){
if(selectedEpisode.isFavorit())
selectedEpisode.setFavorit(false);
else
selectedEpisode.setFavorit(true);
updateEpisodeList();
list_episode.setSelectedIndex(index);
}
}
});
btn_togglePlayed.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Episode selectedEpisode = list_episode.getSelectedValue();
int index = list_episode.getSelectedIndex();
if(selectedEpisode != null){
if(selectedEpisode.isPlayed())
selectedEpisode.setPlayedTime(0);
else
selectedEpisode.setPlayedTime(600000000);
updatePodcastList();
list_episode.setSelectedIndex(index);
}
}
});
}
public static void main(String[] args) {
new MainFrame();
}
private void addComp(final Container co, final GridBagLayout gbl,
final Component c, final int x, final int y, final int width,
final int height, final double wx, final double wy) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill =GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx= x; // Zeile
gbc.gridy= y; // Spalte
gbc.gridwidth =width; // Anzahl an Kasten in Spalte
gbc.gridheight = height; // Anzahl an Kasten in Reihe
gbc.weightx = wx; // Prioritat Platz verwendung x achse
gbc.weighty = wy; // Prioritat Platz verwendung y achse
gbl.setConstraints(c, gbc);
co.add(c);
}
@Override
public void update(Observable o, Object arg) {
System.out.println(arg);
updatePodcastList();
repaint();
}
}