View Javadoc

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.IOException;
7   import java.util.ArrayList;
8   import java.util.Collections;
9   import java.util.Iterator;
10  import java.util.List;
11  
12  import org.apache.commons.io.FileUtils;
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  
16  import com.jgoodies.binding.beans.Model;
17  import com.jgoodies.uif.application.Application;
18  import com.melloware.jukes.db.orm.Artist;
19  import com.melloware.jukes.db.orm.Disc;
20  import com.melloware.jukes.db.orm.Track;
21  import com.melloware.jukes.gui.tool.Resources;
22  import com.melloware.jukes.gui.view.MainFrame;
23  import com.melloware.jukes.util.MessageUtil;
24  
25  /**
26   * The disclist of the application. The list can also be saved as file.
27   * <p>
28   * Copyright (c) 2006 Melloware, Inc. <http://www.melloware.com>
29   * @author Emil A. Lefkof III <info@melloware.com>
30   * @version 4.0 AZ Development 2009
31   */
32  @SuppressWarnings("unchecked")
33  public final class Disclist extends Model implements PropertyChangeListener {
34  
35     private static final Log LOG = LogFactory.getLog(Disclist.class);
36     public static final String PROPERTYNAME_DISC_LIST = "discList";
37     private final List discList;
38     private Disc currentDisc;
39  
40     /**
41      * Default constructor constructs a list of 100 items.
42      */
43     public Disclist() {
44        super();
45        LOG.debug("Disclist created.");
46        this.discList = new ArrayList();
47     }
48  
49     /**
50      * Gets the discList.
51      * <p>
52      * @return Returns the discList.
53      */
54     public List getDiscList() {
55        return this.discList;
56     }
57  
58     /**
59      * Gets the currentDisc.
60      * <p>
61      * @return Returns the currentDisc.
62      */
63     public Disc getCurrentDisc() {
64        synchronized (this) {
65           return this.currentDisc;
66        }
67     }
68  
69     /**
70      * Sets the currentDisc.
71      * <p>
72      * @return Returns the currentDisc.
73      */
74     public Disc setCurrentDisc(Disc aDisc) {
75        synchronized (this) {
76           this.currentDisc = aDisc;
77           return this.currentDisc;
78        }
79     }
80  
81     /**
82      * Delete the currentDisc.
83      * <p>
84      * @return Returns null for the currentDisc.
85      */
86     public Disc removeCurrentDisc() {
87        synchronized (this) {
88           this.currentDisc = null;
89           return this.currentDisc;
90        }
91     }
92  
93     /**
94      * Checks and answer if there's a next element.
95      * @return true if there's a next element
96      */
97     public boolean hasNext() {
98        synchronized (this) {
99           if (discList.size() != 0) {
100             if (discList.indexOf(this.currentDisc) < discList.size() - 1) {
101                return (true);
102             } else {
103                return (false);
104             }
105          } else
106             return (false);
107       }
108    }
109 
110    /**
111     * Checks and answer if there's a next element.
112     * @return true if there's a next element
113     */
114    public boolean hasPrevious() {
115       synchronized (this) {
116          if (discList.size() != 0) {
117             if (discList.indexOf(this.currentDisc) > 0) {
118                return (true);
119             } else {
120                return (false);
121             }
122          } else
123             return (false);
124       }
125    }
126 
127    /**
128     * Returns the next element
129     * @return the next element
130     */
131    public Object getNext() {
132       Object next = null;
133       next = ((hasNext()) ? discList.get(discList.indexOf(this.currentDisc) + 1) : null);
134       if (next != null) {
135          synchronized (this) {
136             currentDisc = (Disc) next;
137          }
138          updateState();
139       }
140       return next;
141    }
142 
143    /**
144     * Returns the previous element
145     * @return the previous element
146     */
147    public Object getPrevious() {
148       Object prev = null;
149       prev = ((hasPrevious()) ? discList.get(discList.indexOf(this.currentDisc) - 1) : null);
150       if (prev != null) {
151          synchronized (this) {
152             currentDisc = (Disc) prev;
153          }
154          updateState();
155       }
156       return prev;
157    }
158 
159    /**
160     * Returns an <code>Iterator</code> for the available next elements.
161     * @return an iterator that iterates over the available next elements
162     */
163    public Iterator getNextIterator() {
164       return discList.iterator();
165    }
166 
167    /**
168     * Move an item down on the list.
169     * <p>
170     * @param index the index to move
171     */
172    public void moveDown(final int index) {
173       synchronized (this) {
174          final Object temp = getDiscList().remove(index);
175          getDiscList().add(index + 1, temp);
176       }
177    }
178 
179    /**
180     * Move an item up on the list.
181     * <p>
182     * @param index the index to move
183     */
184    public void moveUp(final int index) {
185       synchronized (this) {
186          final Object temp = getDiscList().remove(index);
187          getDiscList().add(index - 1, temp);
188       }
189    }
190 
191    /**
192     * The Catalog has changed.
193     * @param evt describes the property change
194     */
195    public void propertyChange(final PropertyChangeEvent evt) {
196       // final String propertyName = evt.getPropertyName();
197    }
198 
199    /**
200     * Removes an item from the list.
201     * <p>
202     * @param index the index to remove from the list
203     */
204    public void remove(final int index) {
205       synchronized (this) {
206          if (index < getDiscList().size()) {
207             getDiscList().remove(index);
208          }
209       }
210    }
211 
212    /**
213     * Saves a disclist to a file.
214     * <p>
215     * @param aFile the file to save.
216     * @throws Exception if any error occurs
217     */
218    public void save(final File aFile) throws Exception {
219 
220       saveDiscList(aFile);
221    }
222 
223    /**
224     * Returns the size of the correct list.
225     * @return the size of the correct list
226     */
227    public int size() {
228       return discList.size();
229    }
230 
231    @Override
232    public String toString() {
233       return "(Current disclist: " + discList.size() + ")";
234    }
235 
236    /**
237     * Updates the state
238     */
239    public void updateState() {
240       firePropertyChange(PROPERTYNAME_DISC_LIST, null, discList);
241    }
242 
243    /**
244     * Saves a disclist to file.
245     * <p>
246     * @param aFile the file to save
247     * @throws IOException if any error occurs writing file
248     */
249    private void saveDiscList(final File aFile) throws IOException {
250       final ArrayList results = new ArrayList();
251 
252       final StringBuffer sb = new StringBuffer();
253 
254       // loop through the discs adding them
255       for (final Iterator iter = getDiscList().iterator(); iter.hasNext();) {
256          final Disc disc = (Disc) iter.next();
257          sb.delete(0, sb.length());
258          sb.append(disc.getArtist().getName());
259          sb.append(" - ");
260          sb.append(disc.getName());
261          sb.append(" - ");
262          sb.append(disc.getYear());
263          results.add(sb.toString());
264          results.add(disc.getLocation());
265       }
266 
267       FileUtils.writeLines(aFile, null, results);
268    }
269 
270    /**
271     * Adds an element to the disclist.
272     * @param o the object to add
273     */
274    public void add(final Object o) {
275       if (LOG.isDebugEnabled()) {
276          LOG.debug("Adding to disclist bottom: " + o);
277       }
278       final MainFrame mainFrame = (MainFrame) Application.getDefaultParentFrame();
279       synchronized (this) {
280          if (o instanceof Artist) {
281             final Artist artist = (Artist) o;
282             final ArrayList discs = new ArrayList();
283             discs.addAll(artist.getDiscs());
284             Collections.sort(discs);
285             for (final Iterator iter = discs.iterator(); iter.hasNext();) {
286                final Disc disc = (Disc) iter.next();
287                discList.add(disc);
288             }
289          } else if (o instanceof Disc) {
290             final Disc disc = (Disc) o;
291             discList.add(disc);
292          } else if (o instanceof Track) {
293             MessageUtil.showError(mainFrame, Resources.getString("messages.SelectArtistOrDisc"));
294             return;
295          }
296          if (currentDisc == null) {
297             currentDisc = (Disc) discList.get(0);
298          }
299          updateState();
300       }
301    }
302 
303 }