Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/lilacsoftware/orca/Playlist.java


1   /*
2    *  ____________________________________________________________________________
3    *  Orca - Audio System
4    *  Copyright (C) 2001 Tom Wadzinski <orca_twadzins@yahoo.com>
5    *  This program is free software; you can redistribute it and/or modify
6    *  it under the terms of the GNU General Public License as published by
7    *  the Free Software Foundation; either version 2 of the License, or
8    *  (at your option) any later version.
9    *  This program is distributed in the hope that it will be useful,
10   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *  GNU General Public License for more details.
13   *  You should have received a copy of the GNU General Public License
14   *  along with this program; if not, write to the Free Software
15   *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *  ____________________________________________________________________________
17   */
18  package com.lilacsoftware.orca;
19  import com.lilacsoftware.orca.TrackInfoStruct;
20  import java.io.*;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.Vector;
25  
26  /*
27   *  The beast here is making the access and updating of the list thread-safe.
28   *  ArrayList's are not thread-safe, and the wrapper implementation making it
29   */
30  /**
31   *  Description of the Class
32   *
33   *@author     user
34   *@created    November 15, 2001
35   */
36  public class Playlist extends Vector
37  {
38  
39      int onDeckCursor = 0;
40      boolean repeatMode = true;
41      private int recentCursor;
42  
43  
44      /**
45       *  Sets the recent attribute of the Playlist object
46       *
47       *@param  recentCursor  The new recentCursor value
48       *@since
49       */
50      public void setRecentCursor( int recentCursor )
51      {
52          //Go back a few positions so that html table will show a few tracks before selected track
53          recentCursor = recentCursor - 4;//make that a property
54  
55          if ( recentCursor < 0 ) {
56              recentCursor = 0;
57          }
58          this.recentCursor = recentCursor;
59      }
60  
61  
62      /**
63       *  Sets the onDeckCursor attribute of the Playlist object
64       *
65       *@since
66       */
67      public void setOnDeckCursor() { }
68  
69  
70      /**
71       *  Sets the onDeckCursor attribute of the Playlist object
72       *
73       *@param  cursor  The new onDeckCursor value
74       *@since
75       */
76      public void setOnDeckCursor( int cursor )
77      {
78          if ( ( cursor > 0 ) && ( cursor < super.size() ) ) {
79              this.onDeckCursor = cursor;
80          }
81  
82      }
83  
84  
85  
86      /**
87       *  Gets the recent attribute of the Playlist object
88       *
89       *@return    The recent value
90       *@since
91       */
92  
93      public int getRecentCursor()
94      {
95          return recentCursor;
96      }
97  
98  
99  
100     /**
101      *  getOnDeckTrack retrieves the on deck object from the playlist, and moves
102      *  the on deck cursor to the next track in the playlist. It returns null if
103      *  at the end of the playlist and repeat mode is disabled. If the repeat
104      *  mode is enabled, and cursor is at the end of the playlist, then the
105      *  cursor is moved to the beginning of the playlist.
106      *
107      *@return    The onDeckTrack value
108      *@since
109      */
110     public Object getOnDeckTrack()
111     {
112         if ( onDeckCursor >= super.size() ) {
113             //Only should occur when not in repeat mode or when queue is empty.
114             onDeckCursor = 0;
115 
116             return new TrackInfoStruct( -1,
117                 0,
118                 "",
119                 "",
120                 "",
121                 new File( "" ),
122                 0,
123                 Constants.VORBISFORMAT,
124                 0,
125                 0,
126                 0,
127                 0,
128                 false,
129                 true );
130         }
131         return super.get( onDeckCursor );
132     }
133 
134 
135     /**
136      *  Gets the onDeckCursor attribute of the Playlist object
137      *
138      *@return    The onDeckCursor value
139      *@since
140      */
141     public int getOnDeckCursor()
142     {
143         return onDeckCursor;
144     }
145 
146 
147     //List list = Collections.synchronizedCollection(new ArrayList());
148 
149     /**
150      *  Description of the Method
151      *
152      *@param  index    Description of Parameter
153      *@param  element  Description of Parameter
154      *@since
155      */
156     public void add( int index, Object element )
157     {
158         if ( index < onDeckCursor ) {
159             incrementOnDeckCursor();
160         }
161         super.add( index, element );
162     }
163 
164 
165     /**
166      *  Description of the Method
167      *
168      *@param  index  Description of Parameter
169      *@return        Description of the Returned Value
170      *@since
171      */
172     public Object remove( int index )
173     {
174         if ( ( index < 0 ) || ( index >= super.size() ) ) {
175             return null;
176         }
177         if ( index < onDeckCursor ) {
178             decrementOnDeckCursor();
179         }
180         Object o = super.remove( index );
181         //if (isEmpty()) {
182         //    //Enter null track
183         //   add(new TrackInfoStruct());
184         //}
185         return o;
186     }
187 
188 
189     /**
190      *  Add a track into the on deck position
191      *
192      *@param  element  The feature to be added to the ToOnDeck attribute
193      *@since
194      */
195     public void addToOnDeck( Object element )
196     {
197         add( onDeckCursor, element );
198         //No need to move ondeckcursor because it still points to the same element
199         //while each other element's index will change
200         //Thought about using a linked list but bad outweighs good, methinks, but must revisit
201     }
202 
203 
204     /**
205      *  returns the on deck track and moves the on deck cursor to the next
206      *  element
207      *
208      *@return    Description of the Returned Value
209      *@since
210      */
211     public Object moveOnDeckTrack()
212     {
213         Object element = getOnDeckTrack();
214         incrementOnDeckCursor();
215         return element;
216     }
217 
218 
219     /**
220      *  Description of the Method
221      *
222      *@since
223      */
224     public synchronized void incrementOnDeckCursor()
225     {
226         onDeckCursor++;
227         if ( ( onDeckCursor >= super.size() ) && ( repeatMode ) ) {
228             onDeckCursor = 0;
229         }
230     }
231 
232 
233     /**
234      *  Description of the Method
235      *
236      *@return    Description of the Returned Value
237      *@since
238      */
239     public synchronized boolean decrementOnDeckCursor()
240     {
241         onDeckCursor--;
242         if ( onDeckCursor < 0 ) {
243             return false;
244         }
245         else {
246             return true;
247         }
248     }
249 }
250 
251