1 package org.robsite.extension.rss.model;
2
3
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Collections;
9 import java.util.Date;
10 import java.util.Iterator;
11 import java.util.List;
12 import javax.swing.SwingUtilities;
13 import org.xml.sax.SAXException;
14
15 public class Channel
16 {
17 private String _title;
18 private String _link;
19 private String _description;
20 private List _items;
21 private boolean _open;
22 private String _url;
23
24
25
26 private String _httpLastModified;
27 private String _httpEtag;
28
29 /***
30 * Future flag to indicate a channel added by the user. These won't be
31 * automatically deleted when we synch with the channel directory.
32 */
33 private boolean _isUserChannel;
34
35 private ArrayList _channelListeners = new ArrayList( 1 );
36
37
38 public Channel()
39 {
40 _open = false;
41 }
42
43
44 public void setHTTPLastModified( String httpLastModified )
45 {
46 _httpLastModified = httpLastModified;
47 }
48
49 public String getHTTPLastModified()
50 {
51 return _httpLastModified;
52 }
53
54 public void setHTTPEtag( String httpEtag )
55 {
56 _httpEtag = httpEtag;
57 }
58
59 public String getHTTPEtag()
60 {
61 return _httpEtag;
62 }
63
64 public boolean isUserChannel()
65 {
66 return _isUserChannel;
67 }
68
69 public void setUserChannel( boolean isUserChannel )
70 {
71 _isUserChannel = isUserChannel;
72 }
73
74 public boolean equals( Object that )
75 {
76 if ( that instanceof Channel )
77 {
78 String thatURL = ((Channel)that).getURL();
79 if ( thatURL != null && this._url != null)
80 {
81 return this._url.equals( thatURL );
82 }
83 else
84 {
85 return this._url == null && thatURL == null;
86 }
87 }
88
89 return false;
90 }
91
92 public int hashCode()
93 {
94 if ( _url != null )
95 {
96 return _url.hashCode();
97 }
98 return super.hashCode();
99 }
100
101 public void addChannelListener( ChannelListener cl )
102 {
103 _channelListeners.add( cl );
104 }
105
106 public void removeChannelListener( ChannelListener cl )
107 {
108 _channelListeners.remove( cl );
109 }
110
111 public void fireItemChanged( Item item )
112 {
113 final int index = _items.indexOf( item );
114 if ( index >= 0 )
115 {
116 fireItemChanged( index );
117 }
118 }
119
120
121 public void fireItemChanged( final int index )
122 {
123
124 SwingUtilities.invokeLater( new Runnable() {
125 public void run()
126 {
127
128 int count = _channelListeners.size();
129 if ( count > 0 )
130 {
131 ChannelListener[] copy = new ChannelListener[ count ];
132 _channelListeners.toArray( copy );
133
134 ChannelEvent event = new ChannelEvent( Channel.this, index );
135
136 for ( int i=0; i < count; i++ )
137 {
138 copy[ i ].itemsChanged( event );
139 }
140 }
141
142 }
143 });
144 }
145
146 public void fireItemsChanged( Collection items )
147 {
148
149 for ( Iterator i=items.iterator(); i.hasNext(); )
150 {
151 Item item = (Item) i.next();
152 fireItemChanged( item );
153 }
154 }
155
156
157 public void fireItemsChanged( )
158 {
159
160 SwingUtilities.invokeLater( new Runnable() {
161 public void run()
162 {
163
164 int count = _channelListeners.size();
165 if ( count > 0 )
166 {
167 ChannelListener[] copy = new ChannelListener[ count ];
168 _channelListeners.toArray( copy );
169
170 ChannelEvent event = new ChannelEvent( Channel.this );
171
172 for ( int i=0; i < count; i++ )
173 {
174 copy[ i ].itemsChanged( event );
175 }
176 }
177
178 }
179 });
180 }
181
182
183 public void setTitle( String title )
184 {
185 _title = title;
186 }
187
188
189 public String getTitle()
190 {
191 return _title;
192 }
193
194
195 public void setLink( String link )
196 {
197 _link = link;
198 }
199
200
201 public String getLink()
202 {
203 return _link;
204 }
205
206
207 public void setDescription( String description )
208 {
209 _description = description;
210 }
211
212
213 public String getDescription()
214 {
215 return _description;
216 }
217
218
219 public void setItems( List items )
220 {
221 _items = items;
222 }
223
224
225 public List getItems()
226 {
227 return _items;
228 }
229
230
231 public int getUnreadItemCount()
232 {
233 if ( _items == null )
234 {
235 return 0;
236 }
237
238 int unreadCount = 0;
239 Iterator iter = _items.iterator();
240 while( iter.hasNext() )
241 {
242 Item item = ( Item ) iter.next();
243 if ( !item.isRead() )
244 {
245 unreadCount ++;
246 }
247 }
248 return unreadCount;
249 }
250
251
252 public void addItem( Item item, boolean overwrite )
253 {
254 if ( _items == null )
255 {
256 _items = new ArrayList( 10 );
257 }
258 if ( overwrite )
259 {
260 if ( _items.contains( item ) )
261 {
262 _items.remove( item );
263 }
264 _items.add( item );
265 }
266 else
267 {
268 if ( !_items.contains( item ) )
269 {
270 _items.add( item );
271 }
272 }
273 }
274
275
276 public void addAllItems( Channel fromChannel )
277 {
278 Iterator iter = fromChannel.getItems().iterator();
279 while( iter.hasNext() )
280 {
281 addItem( ( Item ) iter.next(), true );
282 }
283 }
284
285
286 public String dumpString()
287 {
288 StringBuffer b = new StringBuffer(1000);
289 b.append( "[Channel] {Title: " + _title + "} {Link: " + _link + "} {Description: " + _description + "}\n" );
290 Iterator i = ( _items != null ) ? _items.iterator() : Collections.EMPTY_LIST.iterator();
291 while ( i.hasNext() )
292 {
293 Item item = ( Item ) i.next();
294 b.append( " " );
295 b.append( item.dumpString() );
296 }
297 return b.toString();
298 }
299
300
301 public String toString()
302 {
303 if ( _title == null || _title.length() == 0 )
304 {
305 if ( _description == null || _description.length() == 0 )
306 {
307 return getURL();
308 }
309 return _description.substring( 0, 60 );
310 }
311 return _title;
312 }
313
314
315 public void setOpen( boolean open )
316 {
317 _open = open;
318 }
319
320 public boolean isOpen()
321 {
322 return _open;
323 }
324
325
326 public void setURL( String url )
327 {
328 _url = url;
329 }
330
331
332 public String getURL()
333 {
334 return _url;
335 }
336
337
338 }