2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.bin.Cache;
24 import jalview.util.MessageManager;
26 import java.awt.BorderLayout;
27 import java.awt.Component;
28 import java.awt.Dialog.ModalExclusionType;
29 import java.awt.Dimension;
31 import java.awt.Rectangle;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.KeyEvent;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.WindowAdapter;
37 import java.awt.event.WindowEvent;
38 import java.beans.PropertyChangeListener;
39 import java.text.DateFormat;
40 import java.text.SimpleDateFormat;
41 import java.util.ArrayList;
42 import java.util.Calendar;
43 import java.util.Collections;
44 import java.util.Iterator;
45 import java.util.List;
48 import javax.swing.AbstractAction;
49 import javax.swing.AbstractButton;
50 import javax.swing.Action;
51 import javax.swing.DefaultListCellRenderer;
52 import javax.swing.DefaultListModel;
53 import javax.swing.Icon;
54 import javax.swing.ImageIcon;
55 import javax.swing.JButton;
56 import javax.swing.JFrame;
57 import javax.swing.JLabel;
58 import javax.swing.JList;
59 import javax.swing.JMenuItem;
60 import javax.swing.JPanel;
61 import javax.swing.JPopupMenu;
62 import javax.swing.JScrollPane;
63 import javax.swing.JSplitPane;
64 import javax.swing.JToolBar;
65 import javax.swing.ListSelectionModel;
66 import javax.swing.SwingUtilities;
67 import javax.swing.event.HyperlinkEvent;
68 import javax.swing.event.HyperlinkListener;
69 import javax.swing.event.ListSelectionEvent;
70 import javax.swing.event.ListSelectionListener;
72 import org.robsite.jswingreader.action.MarkChannelAsRead;
73 import org.robsite.jswingreader.action.MarkChannelAsUnread;
74 import org.robsite.jswingreader.action.MarkItemAsRead;
75 import org.robsite.jswingreader.action.MarkItemAsUnread;
76 import org.robsite.jswingreader.action.UpdatableAction;
77 import org.robsite.jswingreader.model.Channel;
78 import org.robsite.jswingreader.model.ChannelListModel;
79 import org.robsite.jswingreader.model.Item;
80 import org.robsite.jswingreader.model.SimpleRSSParser;
81 import org.robsite.jswingreader.ui.BlogContentPane;
82 import org.robsite.jswingreader.ui.ItemReadTimer;
83 import org.robsite.jswingreader.ui.Main;
84 import org.robsite.jswingreader.ui.util.ContextMenuMouseAdapter;
87 * Blog reading window, adapted from JSwingReader's
88 * org.robsite.jswingreader.ui.MainWindow class
91 public class BlogReader extends JPanel
93 private JButton buttonRefresh = new JButton();
95 private JToolBar toolBar = new JToolBar();
97 private JLabel statusBar = new JLabel();
99 private JPanel panelMain = new JPanel();
101 private BorderLayout layoutMain = new BorderLayout();
103 private BorderLayout borderLayout1 = new BorderLayout();
105 private JPanel topPanel = new JPanel();
107 private JPanel bottomPanel = new JPanel();
109 private JSplitPane topBottomSplitPane = new JSplitPane();
111 private JList listItems = new JList(new DefaultListModel());
113 // SWITCH IN JALVIEW HTML VIEWER PANE HERE
114 private BlogContentPane textDescription = new BlogContentPane();
116 // ADD IN JALVIEW BANNER FOR PRETTINESS
117 private BorderLayout borderLayout4 = new BorderLayout();
119 private BorderLayout borderLayout5 = new BorderLayout();
121 private ChannelListModel _channelModel = null;
123 private JList listChannels = new JList();
125 private Action exitAction = new Action()
129 public void actionPerformed(ActionEvent arg0)
139 parent.showNews(false);
145 public void setEnabled(boolean arg0)
151 public void removePropertyChangeListener(PropertyChangeListener arg0)
153 // TODO Auto-generated method stub
158 public void putValue(String arg0, Object arg1)
160 // TODO Auto-generated method stub
165 public boolean isEnabled()
167 // TODO Auto-generated method stub
172 public Object getValue(String arg0)
174 // TODO Auto-generated method stub
179 public void addPropertyChangeListener(PropertyChangeListener arg0)
181 // TODO Auto-generated method stub
186 private JFrame xf = null;
188 private JalviewDialog jd = null;
190 private JalviewDialog createDialog()
193 return jd = new JalviewDialog()
197 protected void raiseClosed()
201 Cache.log.debug("News window closed.");
203 parent.showNews(false);
208 protected void okPressed()
210 // TODO Auto-generated method stub
215 protected void cancelPressed()
217 // TODO Auto-generated method stub
223 private JLabel lblChannels = new JLabel();
225 private List _updatableActions = new ArrayList();
227 private ItemReadTimer _itemTimer = null;
229 private JPopupMenu _popupItems = null;
231 private JPopupMenu _popupChannels = null;
233 private String lastm = "";
235 private boolean newsnew = false;
237 private Desktop parent = null;
244 // should we ignore fake gui events
245 private boolean updating = false;
247 public BlogReader(Desktop desktop)
249 Cache.log.debug("Constructing news reader.");
252 _channelModel = new ChannelListModel();
253 // Construct our jalview news channel
254 Channel chan = new Channel();
255 chan.setURL(jalview.bin.Cache.getDefault(
257 jalview.bin.Cache.getDefault("www.jalview.org",
258 "http://www.jalview.org") + "/feeds/desktop/rss"));
260 _channelModel.addChannel(chan);
266 } catch (Exception e)
273 boolean setvisible = checkForNew(chan, true);
278 Cache.log.debug("Will show jalview news automatically");
281 Cache.log.debug("Completed construction of reader.");
286 * check if the news panel's container is visible
288 public boolean isVisible()
292 return xf != null && xf.isVisible();
294 return jd != null && jd.isVisible();
298 * display the container for the news panel
300 public void showNews()
302 final BlogReader me = this;
303 SwingUtilities.invokeLater(new Runnable()
308 Rectangle bounds = new Rectangle(5, 5, 550, 350);
312 xf.setContentPane(me);
313 xf.addWindowListener(new WindowAdapter()
315 public void windowClosing(WindowEvent e)
317 ActionEvent actionEvent = new ActionEvent(this,
318 ActionEvent.ACTION_FIRST, (String) exitAction
319 .getValue(Action.NAME));
320 exitAction.actionPerformed(actionEvent);
323 public void windowOpened(WindowEvent e)
327 me.setSize(new Dimension(550, 350));
333 bounds = new Rectangle(5, 5, 550, 350);
334 jd.initDialogFrame(me, false, false,
335 MessageManager.getString("label.news_from_jalview"),
336 bounds.width, bounds.height);
337 jd.frame.setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
338 Cache.log.debug("Displaying news.");
346 * update hasnew flag and mark all new messages as unread.
348 private boolean checkForNew(Channel chan, boolean updateItems)
351 if (!updating || updateItems)
355 java.util.Date earliest = null;
358 earliest = new SimpleDateFormat("YYYY-MM-DD").parse(chan
359 .getHTTPLastModified());
360 } catch (Exception x)
364 if (chan != null && chan.getItems() != null)
366 Cache.log.debug("Scanning news items: newsnew=" + newsnew
367 + " and lastDate is " + lastDate);
368 for (Item i : (List<Item>) chan.getItems())
370 boolean isread = lastDate == null ? false
371 : (i.getPublishDate() != null && !lastDate.before(i
374 if (!updating || updateItems)
382 if (i.getPublishDate() != null && !i.isRead())
384 if (earliest == null || earliest.after(i.getPublishDate()))
386 earliest = i.getPublishDate();
391 if (!updateItems && !updating && lastDate == null)
398 java.util.Date lastDate = null;
400 private void loadLastM()
402 lastDate = Cache.getDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED");
405 private void saveLastM(Item item)
409 if (item.getPublishDate() != null)
411 if (lastDate == null || item.getPublishDate().after(lastDate))
413 lastDate = item.getPublishDate();
417 if (_channelModel.getElementAt(0) != null)
419 checkForNew((Channel) _channelModel.getElementAt(0), false);
421 if (lastDate != null)
423 jalview.bin.Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED",
425 jalview.bin.Cache.log.debug("Saved last read date as "
426 + jalview.bin.Cache.date_format.format(lastDate));
432 private void jbInit() throws Exception
434 setLayout(layoutMain);
435 panelMain.setLayout(borderLayout1);
436 topPanel.setLayout(borderLayout5);
437 bottomPanel.setLayout(borderLayout4);
438 topBottomSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
439 topBottomSplitPane.setDividerLocation(100);
440 topBottomSplitPane.setTopComponent(topPanel);
441 topBottomSplitPane.setBottomComponent(bottomPanel);
442 JScrollPane spTextDescription = new JScrollPane(textDescription);
443 textDescription.setText("");
444 statusBar.setText(new StringBuffer("[")
445 .append(MessageManager.getString("label.status")).append("]")
447 buttonRefresh.addActionListener(new ActionListener()
451 public void actionPerformed(ActionEvent e)
456 add(statusBar, BorderLayout.SOUTH);
457 toolBar.add(buttonRefresh);
458 toolBar.addSeparator();
459 JLabel about = new JLabel(
460 "brought to you by JSwingReader (jswingreader.sourceforge.net)");
462 toolBar.setFloatable(false);
463 add(toolBar, BorderLayout.NORTH);
464 panelMain.add(topBottomSplitPane, BorderLayout.CENTER);
465 add(panelMain, BorderLayout.CENTER);
466 JScrollPane spListItems = new JScrollPane(listItems);
468 .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
469 topPanel.add(spListItems, BorderLayout.CENTER);
470 bottomPanel.add(spTextDescription, BorderLayout.CENTER);
471 listChannels.setModel(_channelModel);
473 listItems.addMouseListener(new java.awt.event.MouseAdapter()
475 public void mouseClicked(MouseEvent e)
477 listItems_mouseClicked(e);
480 _popupItems = _buildItemsPopupMenu();
481 _popupChannels = _buildChannelsPopupMenu();
482 ContextMenuMouseAdapter popupAdapter = new ContextMenuMouseAdapter(
484 ContextMenuMouseAdapter popupChannelsAdapter = new ContextMenuMouseAdapter(
486 listItems.addMouseListener(popupAdapter);
487 listItems.setCellRenderer(new ItemsRenderer());
488 lblChannels.setText(MessageManager.getString("label.channels"));
491 private void postInit()
493 // clear the default hyperlink listener and replace with our own.
494 for (HyperlinkListener hll : textDescription.getHyperlinkListeners())
496 textDescription.removeHyperlinkListener(hll);
498 textDescription.addHyperlinkListener(new HyperlinkListener()
500 public void hyperlinkUpdate(HyperlinkEvent e)
502 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
504 Desktop.showUrl(e.getURL().toExternalForm());
509 listItems.addListSelectionListener(new ListSelectionListener()
511 public void valueChanged(ListSelectionEvent e)
513 if (e.getValueIsAdjusting() == false)
515 _itemsValueChanged(listItems);
519 listChannels.setSelectedIndex(1);
521 _updateToolbarButtons();
523 _itemTimer = new ItemReadTimer(listChannels, listItems);
524 _itemsValueChanged(listItems);
527 public class LaunchJvBrowserOnItem extends AbstractAction implements
530 JList _listItems = null;
532 public LaunchJvBrowserOnItem(JList listItems)
534 super("Open in Browser");
535 this.putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_O));
536 this.putValue(Action.LONG_DESCRIPTION, "Open in Browser");
537 _listItems = listItems;
540 public void actionPerformed(ActionEvent e)
542 Object o = _listItems.getSelectedValue();
543 if (o instanceof Item)
545 Item item = (Item) o;
547 _listItems.repaint();
549 Desktop.showUrl(item.getLink());
553 public void update(Object o)
556 if (_listItems == null || _listItems.getModel().getSize() == 0)
560 else if (_listItems.getSelectedIndex() == -1)
568 private JPopupMenu _buildItemsPopupMenu()
570 JPopupMenu popup = new JPopupMenu();
571 popup.add(new JMenuItem(new LaunchJvBrowserOnItem(listItems)));
572 popup.addSeparator();
573 popup.add(new JMenuItem(new MarkItemAsRead(listItems)));
574 popup.add(new JMenuItem(new MarkItemAsUnread(listItems)));
578 private JPopupMenu _buildChannelsPopupMenu()
580 JPopupMenu popup = new JPopupMenu();
581 popup.add(new JMenuItem(new MarkChannelAsRead(listChannels, listItems)));
582 popup.add(new JMenuItem(
583 new MarkChannelAsUnread(listChannels, listItems)));
587 private void initItems(Channel channel)
591 channel = new Channel();
593 if (!channel.isOpen() && channel.getURL() != null)
597 SimpleRSSParser.parse(channel);
598 } catch (Exception ex)
600 ex.printStackTrace();
603 DefaultListModel itemsModel = (DefaultListModel) listItems.getModel();
605 Iterator iter = (channel.getItems() != null) ? channel.getItems()
606 .iterator() : Collections.EMPTY_LIST.iterator();
607 while (iter.hasNext())
609 itemsModel.addElement(iter.next());
611 if (itemsModel.getSize() > 0)
613 listItems.setSelectedIndex(0);
614 _itemsValueChanged(listItems);
616 setStatusBarText(channel.getURL());
620 private void _itemsValueChanged(JList itemList)
622 Item item = (Item) itemList.getSelectedValue();
625 if (itemList.getModel().getSize() > 0)
627 item = (Item) itemList.getModel().getElementAt(0);
635 itemList.setSelectedIndex(0);
639 if (_itemTimer != null)
641 // prefer a shorter delay than 5s
642 _itemTimer.setDelay(300);
644 _itemTimer.setLastItem(item);
645 final Item lastitem = item;
646 _itemTimer.addActionListener(new ActionListener()
650 public void actionPerformed(ActionEvent e)
657 setStatusBarText(item.getLink());
658 textDescription.setBlogText(item);
662 public void setStatusBarText(String text)
664 statusBar.setText(text);
667 private void _updateAllActions()
669 Iterator iter = _updatableActions.iterator();
670 while (iter.hasNext())
672 UpdatableAction action = (UpdatableAction) iter.next();
677 private void _updateToolbarButtons()
679 Map general = (Map) Main.getPreferences().get("general");
685 Component[] components = toolBar.getComponents();
686 for (int i = 0; i < components.length; i++)
688 Component component = components[i];
689 if (component instanceof JButton)
691 JButton button = (JButton) component;
692 if (Boolean.toString(false).equals(general.get("useToolBarText")))
694 // Remove the text if preferences state no toolbar text
697 if (Boolean.toString(true).equals(general.get("radioTextBelow")))
699 button.setVerticalTextPosition(AbstractButton.BOTTOM);
700 button.setHorizontalTextPosition(AbstractButton.CENTER);
702 else if (Boolean.toString(true).equals(
703 general.get("radioTextRight")))
705 button.setVerticalTextPosition(AbstractButton.CENTER);
706 button.setHorizontalTextPosition(AbstractButton.RIGHT);
712 private void listItems_mouseClicked(MouseEvent e)
714 if (e.getClickCount() == 2 && e.getModifiersEx() == MouseEvent.NOBUTTON)
716 Item item = (Item) listItems.getSelectedValue();
719 if (_itemTimer != null)
724 Action action = new LaunchJvBrowserOnItem(listItems);
725 ActionEvent event = new ActionEvent(this,
726 ActionEvent.ACTION_PERFORMED, "LaunchBrowserOnItem");
727 action.actionPerformed(event);
732 * force the news panel to refresh
734 public void refreshNews()
738 initItems((Channel) _channelModel.getElementAt(0));
740 } catch (Exception x)
745 public static void main(String args[])
747 // this tests the detection of new news based on the last read date stored
748 // in jalview properties
749 jalview.bin.Cache.loadProperties(null);
750 jalview.bin.Cache.initLogger();
751 // test will advance read date each time
752 Calendar today = Calendar.getInstance(), lastread = Calendar
754 lastread.set(1983, 01, 01);
755 while (lastread.before(today))
757 Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED",
759 BlogReader me = new BlogReader();
760 System.out.println("Set last date to "
761 + jalview.bin.Cache.date_format.format(lastread.getTime()));
764 Cache.log.debug("There is news to read.");
768 Cache.log.debug("There is no new news.");
769 me.xf.setTitle("Testing : Last read is " + me.lastDate);
773 Cache.log.debug("Waiting for closure.");
779 } catch (InterruptedException x)
783 } while (me.isVisible());
787 Cache.log.debug("Still new news after reader displayed.");
789 if (lastread.getTime().before(me.lastDate))
791 Cache.log.debug("The news was read.");
792 lastread.setTime(me.lastDate);
796 lastread.add(Calendar.MONTH, 1);
808 class ChannelsRenderer extends DefaultListCellRenderer
810 private final static Icon _icon = new ImageIcon(
811 Main.class.getResource("image/ComposeMail16.gif"));
813 public Component getListCellRendererComponent(JList list, Object value,
814 int index, boolean isSelected, boolean cellHasFocus)
816 JLabel component = (JLabel) super.getListCellRendererComponent(list,
817 value, index, isSelected, cellHasFocus);
818 component.setIcon(ChannelsRenderer._icon);
819 if (value instanceof Channel)
821 Channel channel = (Channel) value;
823 .setText(MessageManager.formatMessage(
824 "label.channel_title_item_count",
827 Integer.valueOf(channel.getUnreadItemCount())
829 component.setToolTipText(channel.getURL());
835 class ItemsRenderer extends DefaultListCellRenderer
837 private final static Icon _icon = new ImageIcon(
838 Main.class.getResource("image/ComposeMail16.gif"));
840 public Component getListCellRendererComponent(JList list, Object value,
841 int index, boolean isSelected, boolean cellHasFocus)
843 JLabel component = (JLabel) super.getListCellRendererComponent(list,
844 value, index, isSelected, cellHasFocus);
845 component.setIcon(ItemsRenderer._icon);
846 if (value instanceof Item)
848 Item item = (Item) value;
849 if (item.getPublishDate() != null)
851 component.setText(MessageManager.formatMessage(
852 "label.blog_item_published_on_date",
855 .getDateInstance(DateFormat.LONG,
856 MessageManager.getLocale())
857 .format(item.getPublishDate()).toString(),
860 component.setToolTipText(item.getLink());
863 component.setFont(component.getFont().deriveFont(Font.BOLD));
867 component.setFont(component.getFont().deriveFont(Font.PLAIN));