1 package net.sourceforge.phpeclipse.news;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6 import java.util.Timer;
7 import java.util.TimerTask;
9 import net.sourceforge.phpeclipse.news.pref.ChannelStore;
10 import net.sourceforge.phpeclipse.news.pref.ListEncoder;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.IPluginDescriptor;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.resource.ImageRegistry;
18 import org.eclipse.ui.IWorkbenchPage;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.plugin.AbstractUIPlugin;
22 public class Plugin extends AbstractUIPlugin {
24 public static final String BACKENDS_SECTION = "backends";
26 public static final String REFRESH_INTERVAL_PREFERENCE = "net.sourceforge.phpeclipse.news.refreshinterval";
29 public static final String BACKENDS_PREFERENCE = "net.sourceforge.phpeclipse.news.backends";
31 public static final String BROWSER_PREFERENCE = "net.sourceforge.phpeclipse.news.browser";
33 public static final String BANNED_ITEMS_PREFERENCE = "net.sourceforge.phpeclipse.news.banneditems";
35 public static final String FORCE_CACHE_PREFERENCE = "net.sourceforge.phpeclipse.news.forcecache";
38 public static final int DEFAULT_REFRESH_INTERVAL = 60;
40 public static final String DEFAULT_BROWSER = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";
42 public static final String DEFAULT_BANNED_ITEMS = "";
44 public static final boolean DEFAULT_FORCE_CACHE = false;
47 public static String userAgent;
49 protected UpdateThread updateThread;
51 protected ArrayList views = new ArrayList();
53 protected Timer timer;
55 protected ArrayList channelList;
57 protected Object channelLock = new Object();
59 protected ArrayList banList = new ArrayList();
62 * List of RssListeners to notify
64 private ArrayList rssListeners = new ArrayList();
67 * Constructor for Plugin
69 public Plugin(IPluginDescriptor descriptor) {
72 //set the user-agent ID
73 StringBuffer buffer = new StringBuffer();
74 buffer.append("AllTheNews/").append(descriptor.getVersionIdentifier()).append(" (").append(System.getProperty("os.name"))
75 .append("; ").append(System.getProperty("os.arch")).append("; http://www.jnegre.org/)");
76 userAgent = buffer.toString();
80 //init the channel store
81 ChannelStore.init(this);
88 protected static Plugin singleton;
90 public static Plugin getDefault() {
94 public static void logError(String message, Throwable t) {
96 .log(new Status(IStatus.ERROR, getDefault().getDescriptor().getUniqueIdentifier(), IStatus.OK, message, t));
99 public static void logInfo(String message, Throwable t) {
100 getDefault().getLog().log(new Status(IStatus.INFO, getDefault().getDescriptor().getUniqueIdentifier(), IStatus.OK, message, t));
104 * @see AbstractUIPlugin#initializeDefaultPreferences(IPreferenceStore)
106 protected void initializeDefaultPreferences(IPreferenceStore store) {
107 store.setDefault(REFRESH_INTERVAL_PREFERENCE, DEFAULT_REFRESH_INTERVAL);
108 File file = new File(DEFAULT_BROWSER);
110 store.setDefault(BROWSER_PREFERENCE, DEFAULT_BROWSER);
112 store.setDefault(BANNED_ITEMS_PREFERENCE, DEFAULT_BANNED_ITEMS);
113 store.setDefault(FORCE_CACHE_PREFERENCE, DEFAULT_FORCE_CACHE);
116 protected ImageRegistry createImageRegistry() {
117 ImageRegistry registry = super.createImageRegistry();
118 IconManager.populateImageRegistry(registry);
122 public void addRssListener(RssListener listener) {
123 synchronized (rssListeners) {
124 rssListeners.add(listener);
128 public void removeRssListener(RssListener listener) {
129 synchronized (rssListeners) {
130 rssListeners.remove(listener);
134 public void notifyChannelListChanged(RssListener source) {
135 Iterator iterator = rssListeners.iterator();
136 ArrayList channels = getChannelList();
137 while (iterator.hasNext()) {
138 RssListener listener = (RssListener) iterator.next();
139 if (listener != source) {
140 listener.onChannelListChanged(channels);
145 public void notifyChannelStatusChanged(Channel channel, RssListener source) {
146 Iterator iterator = rssListeners.iterator();
147 while (iterator.hasNext()) {
148 RssListener listener = (RssListener) iterator.next();
149 if (listener != source) {
150 listener.onChannelStatusChanged(channel);
155 public void notifyChannelSelected(Channel channel, RssListener source) {
156 Iterator iterator = rssListeners.iterator();
157 while (iterator.hasNext()) {
158 RssListener listener = (RssListener) iterator.next();
159 if (listener != source) {
160 listener.onChannelSelected(channel);
165 public void notifyItemSelected(Item item, RssListener source) {
166 Iterator iterator = rssListeners.iterator();
167 while (iterator.hasNext()) {
168 RssListener listener = (RssListener) iterator.next();
169 if (listener != source) {
170 listener.onItemSelected(item);
175 public void notifyItemStatusChanged(Item item, RssListener source) {
176 Iterator iterator = rssListeners.iterator();
177 while (iterator.hasNext()) {
178 RssListener listener = (RssListener) iterator.next();
179 if (listener != source) {
180 listener.onItemStatusChanged(item);
185 public void setTimer() {
189 long period = getPreferenceStore().getInt(Plugin.REFRESH_INTERVAL_PREFERENCE) * 60000l;
191 timer = new Timer(true);
192 timer.schedule(new UpdateTimer(), 0, period);
196 public void updateBanList() {
197 synchronized (banList) {
200 String banned = this.getPreferenceStore().getString(Plugin.BANNED_ITEMS_PREFERENCE);
201 String[] bannedTitles = ListEncoder.decode(banned);
202 for (int i = 0; i < bannedTitles.length; i++) {
203 banList.add(bannedTitles[i]);
208 public boolean isBannedTitle(String title) {
209 synchronized (banList) {
210 return banList.contains(title);
214 public void updateChannelList() {
215 synchronized (channelLock) {
216 channelList = ChannelStore.getChannels();
218 notifyChannelListChanged(null);
221 public ArrayList getChannelList() {
222 synchronized (channelLock) {
223 return new ArrayList(channelList);
227 public void update() {
228 if (updateThread == null) {
229 updateThread = new UpdateThread();
230 updateThread.start();
234 protected class UpdateTimer extends TimerTask {
240 public void shutdown() throws CoreException {
241 ChannelStore.saveReadStatus(getChannelList());
245 public IWorkbenchPage getActivePage() {
246 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
248 return window.getActivePage();