KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > rssreader > gui > MainFrame


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.applications.rssreader.gui;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.net.ConnectException JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import javax.swing.*;
32 import javax.swing.event.*;
33
34 import org.jperdian.rss2.RssException;
35 import org.jperdian.rss2.dom.RssChannel;
36 import org.jperdian.rss2.dom.RssItem;
37 import org.lucane.applications.rssreader.RssReader;
38 import org.lucane.applications.rssreader.rss.ChannelInfo;
39 import org.lucane.applications.rssreader.rss.RssItemComparator;
40 import org.lucane.client.Client;
41 import org.lucane.client.Plugin;
42 import org.lucane.client.PluginManager;
43 import org.lucane.client.util.WidgetState;
44 import org.lucane.client.widgets.DialogBox;
45 import org.lucane.client.widgets.ManagedWindow;
46 import org.lucane.common.ConnectInfo;
47
48 public class MainFrame extends ManagedWindow
49 implements ListSelectionListener, ActionListener, MouseListener
50 {
51     private JButton btnRefresh;
52     private JButton btnAggregate;
53     private JButton btnAddChannel;
54     private JButton btnRemoveChannel;
55     private JButton btnHelp;
56     
57     private JSplitPane split;
58     private JList channels;
59     private JList items;
60     private DefaultListModel rssChannels;
61     private DefaultListModel rssItems;
62     private JProgressBar status;
63     private RssReader plugin;
64     
65     public MainFrame(RssReader plugin)
66     {
67         super(plugin, plugin.getTitle());
68         this.setName("mainFrame");
69         
70         this.plugin = plugin;
71         
72         this.rssChannels = new DefaultListModel();
73         this.rssItems = new DefaultListModel();
74         
75         btnRefresh = new JButton(Client.getImageIcon("refresh.png"));
76         btnRefresh.setToolTipText(plugin.tr("btn.refresh"));
77         btnRefresh.addActionListener(this);
78         btnAggregate = new JButton(plugin.getImageIcon("aggregate.png"));
79         btnAggregate.setToolTipText(plugin.tr("btn.aggregate"));
80         btnAggregate.addActionListener(this);
81         btnAddChannel = new JButton(Client.getImageIcon("add.png"));
82         btnAddChannel.setToolTipText(plugin.tr("btn.addChannel"));
83         btnAddChannel.addActionListener(this);
84         btnRemoveChannel = new JButton(Client.getImageIcon("delete.png"));
85         btnRemoveChannel.setToolTipText(plugin.tr("btn.removeChannel"));
86         btnRemoveChannel.addActionListener(this);
87         btnHelp = new JButton(Client.getImageIcon("help.png"));
88         btnHelp.setToolTipText(plugin.tr("btn.help"));
89         btnHelp.addActionListener(this);
90         
91         JPanel buttonPanel = new JPanel(new BorderLayout());
92         JPanel buttons = new JPanel(new GridLayout(1, 5));
93         buttons.add(btnRefresh);
94         buttons.add(btnAggregate);
95         buttons.add(btnAddChannel);
96         buttons.add(btnRemoveChannel);
97         buttons.add(btnHelp);
98         buttonPanel.add(buttons, BorderLayout.WEST);
99         
100         JPanel channelPanel = new JPanel(new BorderLayout());
101         channels = new JList(rssChannels);
102         channels.addListSelectionListener(this);
103         status = new JProgressBar();
104         channelPanel.add(new JScrollPane(channels), BorderLayout.CENTER);
105         channelPanel.add(status, BorderLayout.SOUTH);
106         
107         items = new JList(rssItems);
108         items.setCellRenderer(new ItemRenderer());
109         items.addMouseListener(this);
110         
111         
112         split = new JSplitPane();
113         split.setName("split");
114         split.setTopComponent(channelPanel);
115         split.setBottomComponent(new JScrollPane(items));
116         split.setOneTouchExpandable(true);
117         split.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
118         split.setDividerLocation(120);
119         
120         getContentPane().add(buttonPanel, BorderLayout.NORTH);
121         getContentPane().add(split, BorderLayout.CENTER);
122         
123         setExitPluginOnClose(true);
124     }
125     
126     public void saveState()
127     {
128         WidgetState.save(plugin.getLocalConfig(), split);
129     }
130     
131     public void restoreState()
132     {
133         WidgetState.restore(plugin.getLocalConfig(), split);
134     }
135     
136     public void actionPerformed(ActionEvent ae)
137     {
138         if(ae.getSource().equals(btnRefresh))
139             valueChanged(null);
140         else if(ae.getSource().equals(btnAggregate))
141             showAllChannels();
142         else if(ae.getSource().equals(btnRemoveChannel))
143         {
144             ChannelInfo channel = (ChannelInfo)channels.getSelectedValue();
145             if(channel == null)
146                 return;
147             
148             if(DialogBox.question(plugin.getTitle(), plugin.tr("msg.removeChannel")))
149             {
150                 plugin.removeChannel(channel);
151                 rssItems.clear();
152             }
153         }
154         else if(ae.getSource().equals(btnAddChannel))
155         {
156             new ChannelDialog(this, plugin).show();
157         }
158         else if(ae.getSource().equals(btnHelp))
159         {
160             try {
161                 Plugin help = PluginManager.getInstance().newPluginInstance(
162                         "org.lucane.applications.helpbrowser", new ConnectInfo[0], true);
163                 help.run();
164                 help.invoke("showHelp", new Class JavaDoc[]{Plugin.class}, new Object JavaDoc[]{plugin});
165                 help.invoke("hidePluginList", new Class JavaDoc[0], new Object JavaDoc[0]);
166             } catch (Exception JavaDoc ex) {
167                 DialogBox.error(plugin.tr("err.unableToLoadHelp"));
168                 ex.printStackTrace();
169             }
170         }
171     }
172     
173     public void valueChanged(ListSelectionEvent lse)
174     {
175         ChannelInfo channel = (ChannelInfo)channels.getSelectedValue();
176         if(channel == null)
177             return;
178         
179         showChannel(channel);
180     }
181     
182     public void refreshChannelList()
183     {
184         rssChannels.clear();
185         Iterator JavaDoc i = plugin.getChannels().iterator();
186         while(i.hasNext())
187             rssChannels.addElement(i.next());
188     }
189     
190     public void showChannel(final ChannelInfo channel)
191     {
192         Runnable JavaDoc refresh = new Runnable JavaDoc() {
193             public void run() {
194                 try {
195                     rssItems.clear();
196                     status.setIndeterminate(true);
197                     RssChannel rss = channel.getChannel();
198                     
199                     Iterator JavaDoc i = rss.getItemList().iterator();
200                     while (i.hasNext())
201                         rssItems.addElement(i.next());
202                 } catch (MalformedURLException JavaDoc mue) {
203                     DialogBox.error(plugin.tr("err.wrongUrl")+ mue);
204                 } catch (RssException re) {
205                     //call proxy config if unable to connect
206
if(re.getCause() != null && re.getCause() instanceof ConnectException JavaDoc)
207                     {
208                         if(DialogBox.question(plugin.tr("err.connect"), plugin.tr("msg.setupProxy")))
209                             new ProxyDialog(plugin).show();
210                     }
211                     else
212                         DialogBox.error(plugin.tr("err.rssError") + re);
213                 } finally {
214                     status.setIndeterminate(false);
215                 }
216             }
217         };
218         
219         new Thread JavaDoc(refresh).start();
220     }
221     
222     public void showAllChannels()
223     {
224         Runnable JavaDoc refresh = new Runnable JavaDoc() {
225             public void run() {
226                 try {
227                     channels.setSelectedValue(null, false);
228                     rssItems.clear();
229                     status.setIndeterminate(true);
230                     
231                     Iterator JavaDoc channels = plugin.getChannels().iterator();
232                     ArrayList JavaDoc allItems = new ArrayList JavaDoc();
233                     while(channels.hasNext())
234                     {
235                         ChannelInfo channel = (ChannelInfo)channels.next();
236                         Collection JavaDoc items = channel.getChannel().getItemList();
237                         allItems.addAll(items);
238                     }
239                     
240                     Collections.sort(allItems, new RssItemComparator());
241                     
242                     Iterator JavaDoc i = allItems.iterator();
243                     while (i.hasNext())
244                         rssItems.addElement(i.next());
245                     
246                 } catch (MalformedURLException JavaDoc mue) {
247                     DialogBox.error(plugin.tr("err.wrongUrl")+ mue);
248                 } catch (RssException re) {
249                     //call proxy config if unable to connect
250
if(re.getCause() != null && re.getCause() instanceof ConnectException JavaDoc)
251                     {
252                         if(DialogBox.question(plugin.tr("err.connect"), plugin.tr("msg.setupProxy")))
253                             new ProxyDialog(plugin).show();
254                     }
255                     else
256                         DialogBox.error(plugin.tr("err.rssError") + re);
257                 } finally {
258                     status.setIndeterminate(false);
259                 }
260             }
261         };
262         new Thread JavaDoc(refresh).start();
263     }
264     
265     public void mousePressed(MouseEvent me) {}
266     public void mouseReleased(MouseEvent me) {}
267     public void mouseEntered(MouseEvent me) {}
268     public void mouseExited(MouseEvent me) {}
269     public void mouseClicked(MouseEvent me)
270     {
271         if(me.getButton() == MouseEvent.BUTTON1 && me.getClickCount() == 2)
272         {
273             RssItem item = (RssItem)items.getSelectedValue();
274             if(item == null)
275                 return;
276             
277             plugin.openUrl(item.getLink());
278         }
279     }
280 }
Popular Tags