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
289 public boolean isVisible()
293 return xf != null && xf.isVisible();
295 return jd != null && jd.isVisible();
299 * display the container for the news panel
301 public void showNews()
303 final BlogReader me = this;
304 SwingUtilities.invokeLater(new Runnable()
309 Rectangle bounds = new Rectangle(5, 5, 550, 350);
313 xf.setContentPane(me);
314 xf.addWindowListener(new WindowAdapter()
317 public void windowClosing(WindowEvent e)
319 ActionEvent actionEvent = new ActionEvent(this,
320 ActionEvent.ACTION_FIRST, (String) exitAction
321 .getValue(Action.NAME));
322 exitAction.actionPerformed(actionEvent);
326 public void windowOpened(WindowEvent e)
330 me.setSize(new Dimension(550, 350));
336 bounds = new Rectangle(5, 5, 550, 350);
337 jd.initDialogFrame(me, false, false,
338 MessageManager.getString("label.news_from_jalview"),
339 bounds.width, bounds.height);
340 jd.frame.setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
341 Cache.log.debug("Displaying news.");
349 * update hasnew flag and mark all new messages as unread.
351 private boolean checkForNew(Channel chan, boolean updateItems)
354 if (!updating || updateItems)
358 java.util.Date earliest = null;
361 earliest = new SimpleDateFormat("YYYY-MM-DD").parse(chan
362 .getHTTPLastModified());
363 } catch (Exception x)
367 if (chan != null && chan.getItems() != null)
369 Cache.log.debug("Scanning news items: newsnew=" + newsnew
370 + " and lastDate is " + lastDate);
371 for (Item i : (List<Item>) chan.getItems())
373 boolean isread = lastDate == null ? false
374 : (i.getPublishDate() != null && !lastDate.before(i
377 if (!updating || updateItems)
385 if (i.getPublishDate() != null && !i.isRead())
387 if (earliest == null || earliest.after(i.getPublishDate()))
389 earliest = i.getPublishDate();
394 if (!updateItems && !updating && lastDate == null)
401 java.util.Date lastDate = null;
403 private void loadLastM()
405 lastDate = Cache.getDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED");
408 private void saveLastM(Item item)
412 if (item.getPublishDate() != null)
414 if (lastDate == null || item.getPublishDate().after(lastDate))
416 lastDate = item.getPublishDate();
420 if (_channelModel.getElementAt(0) != null)
422 checkForNew((Channel) _channelModel.getElementAt(0), false);
424 if (lastDate != null)
426 String formatted = Cache.setDateProperty(
427 "JALVIEW_NEWS_RSS_LASTMODIFIED", lastDate);
428 Cache.log.debug("Saved last read date as " + formatted);
433 private void jbInit() throws Exception
435 setLayout(layoutMain);
436 panelMain.setLayout(borderLayout1);
437 topPanel.setLayout(borderLayout5);
438 bottomPanel.setLayout(borderLayout4);
439 topBottomSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
440 topBottomSplitPane.setDividerLocation(100);
441 topBottomSplitPane.setTopComponent(topPanel);
442 topBottomSplitPane.setBottomComponent(bottomPanel);
443 JScrollPane spTextDescription = new JScrollPane(textDescription);
444 textDescription.setText("");
445 statusBar.setText(new StringBuffer("[")
446 .append(MessageManager.getString("label.status")).append("]")
448 buttonRefresh.addActionListener(new ActionListener()
452 public void actionPerformed(ActionEvent e)
457 add(statusBar, BorderLayout.SOUTH);
458 toolBar.add(buttonRefresh);
459 toolBar.addSeparator();
460 JLabel about = new JLabel(
461 "brought to you by JSwingReader (jswingreader.sourceforge.net)");
463 toolBar.setFloatable(false);
464 add(toolBar, BorderLayout.NORTH);
465 panelMain.add(topBottomSplitPane, BorderLayout.CENTER);
466 add(panelMain, BorderLayout.CENTER);
467 JScrollPane spListItems = new JScrollPane(listItems);
469 .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
470 topPanel.add(spListItems, BorderLayout.CENTER);
471 bottomPanel.add(spTextDescription, BorderLayout.CENTER);
472 listChannels.setModel(_channelModel);
474 listItems.addMouseListener(new java.awt.event.MouseAdapter()
477 public void mouseClicked(MouseEvent e)
479 listItems_mouseClicked(e);
482 _popupItems = _buildItemsPopupMenu();
483 _popupChannels = _buildChannelsPopupMenu();
484 ContextMenuMouseAdapter popupAdapter = new ContextMenuMouseAdapter(
486 ContextMenuMouseAdapter popupChannelsAdapter = new ContextMenuMouseAdapter(
488 listItems.addMouseListener(popupAdapter);
489 listItems.setCellRenderer(new ItemsRenderer());
490 lblChannels.setText(MessageManager.getString("label.channels"));
493 private void postInit()
495 // clear the default hyperlink listener and replace with our own.
496 for (HyperlinkListener hll : textDescription.getHyperlinkListeners())
498 textDescription.removeHyperlinkListener(hll);
500 textDescription.addHyperlinkListener(new HyperlinkListener()
503 public void hyperlinkUpdate(HyperlinkEvent e)
505 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
507 Desktop.showUrl(e.getURL().toExternalForm());
512 listItems.addListSelectionListener(new ListSelectionListener()
515 public void valueChanged(ListSelectionEvent e)
517 if (e.getValueIsAdjusting() == false)
519 _itemsValueChanged(listItems);
523 listChannels.setSelectedIndex(1);
525 _updateToolbarButtons();
527 _itemTimer = new ItemReadTimer(listChannels, listItems);
528 _itemsValueChanged(listItems);
531 public class LaunchJvBrowserOnItem extends AbstractAction implements
534 JList _listItems = null;
536 public LaunchJvBrowserOnItem(JList listItems)
538 super("Open in Browser");
539 this.putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_O));
540 this.putValue(Action.LONG_DESCRIPTION, "Open in Browser");
541 _listItems = listItems;
545 public void actionPerformed(ActionEvent e)
547 Object o = _listItems.getSelectedValue();
548 if (o instanceof Item)
550 Item item = (Item) o;
552 _listItems.repaint();
554 Desktop.showUrl(item.getLink());
559 public void update(Object o)
562 if (_listItems == null || _listItems.getModel().getSize() == 0)
566 else if (_listItems.getSelectedIndex() == -1)
574 private JPopupMenu _buildItemsPopupMenu()
576 JPopupMenu popup = new JPopupMenu();
577 popup.add(new JMenuItem(new LaunchJvBrowserOnItem(listItems)));
578 popup.addSeparator();
579 popup.add(new JMenuItem(new MarkItemAsRead(listItems)));
580 popup.add(new JMenuItem(new MarkItemAsUnread(listItems)));
584 private JPopupMenu _buildChannelsPopupMenu()
586 JPopupMenu popup = new JPopupMenu();
587 popup.add(new JMenuItem(new MarkChannelAsRead(listChannels, listItems)));
588 popup.add(new JMenuItem(
589 new MarkChannelAsUnread(listChannels, listItems)));
593 private void initItems(Channel channel)
597 channel = new Channel();
599 if (!channel.isOpen() && channel.getURL() != null)
603 SimpleRSSParser.parse(channel);
604 } catch (Exception ex)
606 ex.printStackTrace();
609 DefaultListModel itemsModel = (DefaultListModel) listItems.getModel();
611 Iterator iter = (channel.getItems() != null) ? channel.getItems()
612 .iterator() : Collections.EMPTY_LIST.iterator();
613 while (iter.hasNext())
615 itemsModel.addElement(iter.next());
617 if (itemsModel.getSize() > 0)
619 listItems.setSelectedIndex(0);
620 _itemsValueChanged(listItems);
622 setStatusBarText(channel.getURL());
626 private void _itemsValueChanged(JList itemList)
628 Item item = (Item) itemList.getSelectedValue();
631 if (itemList.getModel().getSize() > 0)
633 item = (Item) itemList.getModel().getElementAt(0);
641 itemList.setSelectedIndex(0);
645 if (_itemTimer != null)
647 // prefer a shorter delay than 5s
648 _itemTimer.setDelay(300);
650 _itemTimer.setLastItem(item);
651 final Item lastitem = item;
652 _itemTimer.addActionListener(new ActionListener()
656 public void actionPerformed(ActionEvent e)
663 setStatusBarText(item.getLink());
664 textDescription.setBlogText(item);
668 public void setStatusBarText(String text)
670 statusBar.setText(text);
673 private void _updateAllActions()
675 Iterator iter = _updatableActions.iterator();
676 while (iter.hasNext())
678 UpdatableAction action = (UpdatableAction) iter.next();
683 private void _updateToolbarButtons()
685 Map general = (Map) Main.getPreferences().get("general");
691 Component[] components = toolBar.getComponents();
692 for (int i = 0; i < components.length; i++)
694 Component component = components[i];
695 if (component instanceof JButton)
697 JButton button = (JButton) component;
698 if (Boolean.toString(false).equals(general.get("useToolBarText")))
700 // Remove the text if preferences state no toolbar text
703 if (Boolean.toString(true).equals(general.get("radioTextBelow")))
705 button.setVerticalTextPosition(AbstractButton.BOTTOM);
706 button.setHorizontalTextPosition(AbstractButton.CENTER);
708 else if (Boolean.toString(true).equals(
709 general.get("radioTextRight")))
711 button.setVerticalTextPosition(AbstractButton.CENTER);
712 button.setHorizontalTextPosition(AbstractButton.RIGHT);
718 private void listItems_mouseClicked(MouseEvent e)
720 if (e.getClickCount() == 2 && e.getModifiersEx() == MouseEvent.NOBUTTON)
722 Item item = (Item) listItems.getSelectedValue();
725 if (_itemTimer != null)
730 Action action = new LaunchJvBrowserOnItem(listItems);
731 ActionEvent event = new ActionEvent(this,
732 ActionEvent.ACTION_PERFORMED, "LaunchBrowserOnItem");
733 action.actionPerformed(event);
738 * force the news panel to refresh
740 public void refreshNews()
744 initItems((Channel) _channelModel.getElementAt(0));
746 } catch (Exception x)
751 public static void main(String args[])
753 // this tests the detection of new news based on the last read date stored
754 // in jalview properties
755 jalview.bin.Cache.loadProperties(null);
756 jalview.bin.Cache.initLogger();
757 // test will advance read date each time
758 Calendar today = Calendar.getInstance(), lastread = Calendar
760 lastread.set(1983, 01, 01);
761 while (lastread.before(today))
763 String formattedDate = Cache.setDateProperty(
764 "JALVIEW_NEWS_RSS_LASTMODIFIED", lastread.getTime());
765 BlogReader me = new BlogReader();
766 System.out.println("Set last date to " + formattedDate);
769 Cache.log.debug("There is news to read.");
773 Cache.log.debug("There is no new news.");
774 me.xf.setTitle("Testing : Last read is " + me.lastDate);
778 Cache.log.debug("Waiting for closure.");
784 } catch (InterruptedException x)
788 } while (me.isVisible());
792 Cache.log.debug("Still new news after reader displayed.");
794 if (lastread.getTime().before(me.lastDate))
796 Cache.log.debug("The news was read.");
797 lastread.setTime(me.lastDate);
801 lastread.add(Calendar.MONTH, 1);
813 class ChannelsRenderer extends DefaultListCellRenderer
815 private final static Icon _icon = new ImageIcon(
816 Main.class.getResource("image/ComposeMail16.gif"));
819 public Component getListCellRendererComponent(JList list, Object value,
820 int index, boolean isSelected, boolean cellHasFocus)
822 JLabel component = (JLabel) super.getListCellRendererComponent(list,
823 value, index, isSelected, cellHasFocus);
824 component.setIcon(ChannelsRenderer._icon);
825 if (value instanceof Channel)
827 Channel channel = (Channel) value;
829 .setText(MessageManager.formatMessage(
830 "label.channel_title_item_count",
833 Integer.valueOf(channel.getUnreadItemCount())
835 component.setToolTipText(channel.getURL());
841 class ItemsRenderer extends DefaultListCellRenderer
843 private final static Icon _icon = new ImageIcon(
844 Main.class.getResource("image/ComposeMail16.gif"));
847 public Component getListCellRendererComponent(JList list, Object value,
848 int index, boolean isSelected, boolean cellHasFocus)
850 JLabel component = (JLabel) super.getListCellRendererComponent(list,
851 value, index, isSelected, cellHasFocus);
852 component.setIcon(ItemsRenderer._icon);
853 if (value instanceof Item)
855 Item item = (Item) value;
856 if (item.getPublishDate() != null)
858 component.setText(MessageManager.formatMessage(
859 "label.blog_item_published_on_date",
862 .getDateInstance(DateFormat.LONG,
863 MessageManager.getLocale())
864 .format(item.getPublishDate()).toString(),
867 component.setToolTipText(item.getLink());
870 component.setFont(component.getFont().deriveFont(Font.BOLD));
874 component.setFont(component.getFont().deriveFont(Font.PLAIN));