1 package net.sourceforge.phpeclipse.news;
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.Timer;
6 import java.util.TimerTask;
8 import net.sourceforge.phpeclipse.news.pref.ChannelStore;
9 import net.sourceforge.phpeclipse.news.pref.ListEncoder;
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.core.runtime.IPluginDescriptor;
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.resource.ImageRegistry;
17 import org.eclipse.ui.IWorkbenchPage;
18 import org.eclipse.ui.IWorkbenchWindow;
19 import org.eclipse.ui.plugin.AbstractUIPlugin;
21 public class Plugin extends AbstractUIPlugin {
23 public static final String BACKENDS_SECTION = "backends";
25 public static final String REFRESH_INTERVAL_PREFERENCE = "net.sourceforge.phpeclipse.news.refreshinterval";
28 public static final String BACKENDS_PREFERENCE = "net.sourceforge.phpeclipse.news.backends";
30 public static final String BROWSER_PREFERENCE = "net.sourceforge.phpeclipse.news.browser";
32 public static final String BANNED_ITEMS_PREFERENCE = "net.sourceforge.phpeclipse.news.banneditems";
34 public static final String FORCE_CACHE_PREFERENCE = "net.sourceforge.phpeclipse.news.forcecache";
37 public static final int DEFAULT_REFRESH_INTERVAL = 60;
39 public static final String DEFAULT_BROWSER = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";
41 public static final String DEFAULT_BANNED_ITEMS = "";
43 public static final boolean DEFAULT_FORCE_CACHE = false;
46 public static String userAgent;
48 protected UpdateThread updateThread;
50 protected ArrayList views = new ArrayList();
52 protected Timer timer;
54 protected ArrayList channelList;
56 protected Object channelLock = new Object();
58 protected ArrayList banList = new ArrayList();
61 * List of RssListeners to notify
63 private ArrayList rssListeners = new ArrayList();
66 * Constructor for Plugin
68 public Plugin(IPluginDescriptor descriptor) {
71 //set the user-agent ID
72 StringBuffer buffer = new StringBuffer();
73 buffer.append("AllTheNews/").append(descriptor.getVersionIdentifier()).append(" (").append(System.getProperty("os.name"))
74 .append("; ").append(System.getProperty("os.arch")).append("; http://www.jnegre.org/)");
75 userAgent = buffer.toString();
79 //init the channel store
80 ChannelStore.init(this);
87 protected static Plugin singleton;
89 public static Plugin getDefault() {
93 public static void logError(String message, Throwable t) {
95 .log(new Status(IStatus.ERROR, getDefault().getDescriptor().getUniqueIdentifier(), IStatus.OK, message, t));
98 public static void logInfo(String message, Throwable t) {
99 getDefault().getLog().log(new Status(IStatus.INFO, getDefault().getDescriptor().getUniqueIdentifier(), IStatus.OK, message, t));
103 * @see AbstractUIPlugin#initializeDefaultPreferences(IPreferenceStore)
105 protected void initializeDefaultPreferences(IPreferenceStore store) {
106 store.setDefault(REFRESH_INTERVAL_PREFERENCE, DEFAULT_REFRESH_INTERVAL);
107 store.setDefault(BROWSER_PREFERENCE, DEFAULT_BROWSER);
108 store.setDefault(BANNED_ITEMS_PREFERENCE, DEFAULT_BANNED_ITEMS);
109 store.setDefault(FORCE_CACHE_PREFERENCE, DEFAULT_FORCE_CACHE);
112 protected ImageRegistry createImageRegistry() {
113 ImageRegistry registry = super.createImageRegistry();
114 IconManager.populateImageRegistry(registry);
118 public void addRssListener(RssListener listener) {
119 synchronized (rssListeners) {
120 rssListeners.add(listener);
124 public void removeRssListener(RssListener listener) {
125 synchronized (rssListeners) {
126 rssListeners.remove(listener);
130 public void notifyChannelListChanged(RssListener source) {
131 Iterator iterator = rssListeners.iterator();
132 ArrayList channels = getChannelList();
133 while (iterator.hasNext()) {
134 RssListener listener = (RssListener) iterator.next();
135 if (listener != source) {
136 listener.onChannelListChanged(channels);
141 public void notifyChannelStatusChanged(Channel channel, RssListener source) {
142 Iterator iterator = rssListeners.iterator();
143 while (iterator.hasNext()) {
144 RssListener listener = (RssListener) iterator.next();
145 if (listener != source) {
146 listener.onChannelStatusChanged(channel);
151 public void notifyChannelSelected(Channel channel, RssListener source) {
152 Iterator iterator = rssListeners.iterator();
153 while (iterator.hasNext()) {
154 RssListener listener = (RssListener) iterator.next();
155 if (listener != source) {
156 listener.onChannelSelected(channel);
161 public void notifyItemSelected(Item item, RssListener source) {
162 Iterator iterator = rssListeners.iterator();
163 while (iterator.hasNext()) {
164 RssListener listener = (RssListener) iterator.next();
165 if (listener != source) {
166 listener.onItemSelected(item);
171 public void notifyItemStatusChanged(Item item, RssListener source) {
172 Iterator iterator = rssListeners.iterator();
173 while (iterator.hasNext()) {
174 RssListener listener = (RssListener) iterator.next();
175 if (listener != source) {
176 listener.onItemStatusChanged(item);
181 public void setTimer() {
185 long period = getPreferenceStore().getInt(Plugin.REFRESH_INTERVAL_PREFERENCE) * 60000l;
187 timer = new Timer(true);
188 timer.schedule(new UpdateTimer(), 0, period);
192 public void updateBanList() {
193 synchronized (banList) {
196 String banned = this.getPreferenceStore().getString(Plugin.BANNED_ITEMS_PREFERENCE);
197 String[] bannedTitles = ListEncoder.decode(banned);
198 for (int i = 0; i < bannedTitles.length; i++) {
199 banList.add(bannedTitles[i]);
204 public boolean isBannedTitle(String title) {
205 synchronized (banList) {
206 return banList.contains(title);
210 public void updateChannelList() {
211 synchronized (channelLock) {
212 channelList = ChannelStore.getChannels();
214 notifyChannelListChanged(null);
217 public ArrayList getChannelList() {
218 synchronized (channelLock) {
219 return new ArrayList(channelList);
223 public void update() {
224 if (updateThread == null) {
225 updateThread = new UpdateThread();
226 updateThread.start();
230 protected class UpdateTimer extends TimerTask {
236 public void shutdown() throws CoreException {
237 ChannelStore.saveReadStatus(getChannelList());
241 public IWorkbenchPage getActivePage() {
242 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
244 return window.getActivePage();