1 package com.melloware.jukes.file;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.io.File;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.sql.Timestamp;
9 import java.text.MessageFormat;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Random;
15 import javax.swing.JComponent;
16
17 import org.apache.commons.io.FileUtils;
18 import org.apache.commons.io.FilenameUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.dom4j.Document;
23 import org.dom4j.DocumentHelper;
24 import org.dom4j.io.OutputFormat;
25 import org.dom4j.io.XMLWriter;
26
27 import com.jgoodies.binding.beans.Model;
28 import com.jgoodies.uif.action.ActionManager;
29 import com.jgoodies.uif.application.Application;
30 import com.jgoodies.uif.util.ResourceUtils;
31 import com.melloware.jspiff.jaxp.XspfPlaylist;
32 import com.melloware.jspiff.jaxp.XspfPlaylistTrackList;
33 import com.melloware.jspiff.jaxp.XspfTrack;
34 import com.melloware.jukes.db.HibernateDao;
35 import com.melloware.jukes.db.orm.Artist;
36 import com.melloware.jukes.db.orm.Catalog;
37 import com.melloware.jukes.db.orm.Disc;
38 import com.melloware.jukes.db.orm.Track;
39 import com.melloware.jukes.file.filter.M3uFilter;
40 import com.melloware.jukes.file.filter.XspfFilter;
41 import com.melloware.jukes.gui.tool.Actions;
42 import com.melloware.jukes.gui.tool.MainModule;
43 import com.melloware.jukes.gui.view.MainFrame;
44 import com.melloware.jukes.util.TimeSpan;
45 import com.melloware.jukes.util.MessageUtil;
46 import com.melloware.jukes.gui.tool.Resources;
47
48
49
50
51
52
53
54
55
56
57
58 @SuppressWarnings("unchecked")
59 public final class Disclist extends Model implements PropertyChangeListener {
60
61 private static final Log LOG = LogFactory.getLog(Disclist.class);
62 public static final String PROPERTYNAME_DISC_LIST = "discList";
63 private Catalog catalog = null;
64 private final List discList;
65 private Disc currentDisc;
66
67
68
69
70 public Disclist() {
71 super();
72 LOG.debug("Disclist created.");
73 this.discList = new ArrayList();
74 }
75
76
77
78
79
80
81 public List getDiscList() {
82 return this.discList;
83 }
84
85
86
87
88
89
90 public Disc getCurrentDisc() {
91 synchronized (this) {
92 return this.currentDisc;
93 }
94 }
95
96
97
98
99
100
101 public Disc setCurrentDisc(Disc aDisc) {
102 synchronized (this) {
103 this.currentDisc = aDisc;
104 return this.currentDisc;
105 }
106 }
107
108
109
110
111
112
113 public Disc removeCurrentDisc() {
114 synchronized (this) {
115 this.currentDisc = null;
116 return this.currentDisc;
117 }
118 }
119
120
121
122
123
124 public boolean hasNext() {
125 synchronized (this) {
126 if (discList.size() != 0) {
127 if (discList.indexOf(this.currentDisc) < discList.size()-1) {
128 return (true);
129 } else {
130 return (false);
131 }
132 }
133 else return (false);
134 }
135 }
136
137
138
139
140
141 public boolean hasPrevious() {
142 synchronized (this) {
143 if (discList.size() != 0) {
144 if (discList.indexOf(this.currentDisc) > 0) {
145 return (true);
146 } else {
147 return (false);
148 }
149 }
150 else return (false);
151 }
152 }
153
154
155
156
157
158 public Object getNext() {
159 Object next = null;
160 next = ((hasNext()) ? discList.get(discList.indexOf(this.currentDisc) + 1) : null);
161 if (next != null) {
162 synchronized (this) {
163 currentDisc = (Disc) next;
164 }
165 updateState();
166 }
167 return next;
168 }
169
170
171
172
173
174 public Object getPrevious() {
175 Object prev = null;
176 prev = ((hasPrevious()) ? discList.get(discList.indexOf(this.currentDisc) - 1) : null);
177 if (prev != null) {
178 synchronized (this) {
179 currentDisc = (Disc) prev;
180 }
181 updateState();
182 }
183 return prev;
184 }
185
186
187
188
189
190 public Iterator getNextIterator() {
191 return discList.iterator();
192 }
193
194
195
196
197
198
199
200
201 public void moveDown(final int index) {
202 synchronized (this) {
203 final Object temp = getDiscList().remove(index);
204 getDiscList().add(index + 1, temp);
205 }
206 }
207
208
209
210
211
212
213
214 public void moveUp(final int index) {
215 synchronized (this) {
216 final Object temp = getDiscList().remove(index);
217 getDiscList().add(index - 1, temp);
218 }
219 }
220
221
222
223
224
225
226 public void propertyChange(final PropertyChangeEvent evt) {
227 final String propertyName = evt.getPropertyName();
228 if (MainModule.PROPERTYNAME_CATALOG.equals(propertyName)) {
229 catalog = ((Catalog) evt.getNewValue());
230 }
231 }
232
233
234
235
236
237
238 public void remove(final int index) {
239 synchronized (this) {
240 if (index < getDiscList().size()) {
241 getDiscList().remove(index);
242 }
243 }
244 }
245
246
247
248
249
250
251
252 public void save(final File aFile) throws Exception {
253
254 saveDiscList(aFile);
255 }
256
257
258
259
260
261 public int size() {
262 return discList.size();
263 }
264
265
266 public String toString() {
267 return "(Current disclist: " + discList.size() + ")";
268 }
269
270
271
272
273 public void updateState() {
274 firePropertyChange(PROPERTYNAME_DISC_LIST, null, discList);
275 }
276
277
278
279
280
281
282
283 private void saveDiscList(final File aFile) throws IOException {
284 final ArrayList results = new ArrayList();
285
286 final StringBuffer sb = new StringBuffer();
287
288
289
290 for (final Iterator iter = getDiscList().iterator(); iter.hasNext();) {
291 final Disc disc = (Disc) iter.next();
292 sb.delete(0, sb.length());
293 sb.append(disc.getArtist().getName());
294 sb.append(" - ");
295 sb.append(disc.getName());
296 sb.append(" - ");
297 sb.append(disc.getYear());
298 results.add(sb.toString());
299 results.add(disc.getLocation());
300 }
301
302 FileUtils.writeLines(aFile, null, results);
303 }
304
305
306
307
308
309 public void add(final Object o) {
310 if (LOG.isDebugEnabled()) {
311 LOG.debug("Adding to disclist bottom: " + o);
312 }
313 final MainFrame mainFrame = (MainFrame) Application.getDefaultParentFrame();
314 synchronized (this) {
315 if (o instanceof Artist) {
316 final Artist artist = (Artist) o;
317 final ArrayList discs = new ArrayList();
318 discs.addAll(artist.getDiscs());
319 Collections.sort(discs);
320 for (final Iterator iter = discs.iterator(); iter.hasNext();) {
321 final Disc disc = (Disc) iter.next();
322 discList.add(disc);
323 }
324 } else if (o instanceof Disc) {
325 final Disc disc = (Disc) o;
326 discList.add(disc);
327 }
328 else if (o instanceof Track) {
329 MessageUtil.showError(mainFrame, Resources.getString("messages.SelectArtistOrDisc"));
330 return;
331 }
332 if (currentDisc == null) {
333 currentDisc = (Disc) discList.get(0);
334 }
335 updateState();
336 }
337 }
338
339 }