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.bin.Console;
25 import jalview.util.MessageManager;
27 import java.awt.BorderLayout;
28 import java.awt.Component;
29 import java.awt.Dialog.ModalExclusionType;
30 import java.awt.Dimension;
32 import java.awt.Rectangle;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.WindowAdapter;
38 import java.awt.event.WindowEvent;
39 import java.beans.PropertyChangeListener;
40 import java.text.DateFormat;
41 import java.text.SimpleDateFormat;
42 import java.util.ArrayList;
43 import java.util.Calendar;
44 import java.util.Collections;
45 import java.util.Date;
46 import java.util.Iterator;
47 import java.util.List;
50 import javax.swing.AbstractAction;
51 import javax.swing.AbstractButton;
52 import javax.swing.Action;
53 import javax.swing.DefaultListCellRenderer;
54 import javax.swing.DefaultListModel;
55 import javax.swing.Icon;
56 import javax.swing.ImageIcon;
57 import javax.swing.JButton;
58 import javax.swing.JFrame;
59 import javax.swing.JLabel;
60 import javax.swing.JList;
61 import javax.swing.JMenuItem;
62 import javax.swing.JPanel;
63 import javax.swing.JPopupMenu;
64 import javax.swing.JScrollPane;
65 import javax.swing.JSplitPane;
66 import javax.swing.JToolBar;
67 import javax.swing.ListSelectionModel;
68 import javax.swing.SwingUtilities;
69 import javax.swing.event.HyperlinkEvent;
70 import javax.swing.event.HyperlinkListener;
71 import javax.swing.event.ListSelectionEvent;
72 import javax.swing.event.ListSelectionListener;
74 import org.robsite.jswingreader.action.MarkChannelAsRead;
75 import org.robsite.jswingreader.action.MarkChannelAsUnread;
76 import org.robsite.jswingreader.action.MarkItemAsRead;
77 import org.robsite.jswingreader.action.MarkItemAsUnread;
78 import org.robsite.jswingreader.action.UpdatableAction;
79 import org.robsite.jswingreader.model.Channel;
80 import org.robsite.jswingreader.model.ChannelListModel;
81 import org.robsite.jswingreader.model.Item;
82 import org.robsite.jswingreader.model.SimpleRSSParser;
83 import org.robsite.jswingreader.ui.BlogContentPane;
84 import org.robsite.jswingreader.ui.ItemReadTimer;
85 import org.robsite.jswingreader.ui.Main;
86 import org.robsite.jswingreader.ui.util.ContextMenuMouseAdapter;
89 * Blog reading window, adapted from JSwingReader's
90 * org.robsite.jswingreader.ui.MainWindow class
93 public class BlogReader extends JPanel
95 private JButton buttonRefresh = new JButton();
97 private JToolBar toolBar = new JToolBar();
99 private JLabel statusBar = new JLabel();
101 private JPanel panelMain = new JPanel();
103 private BorderLayout layoutMain = new BorderLayout();
105 private BorderLayout borderLayout1 = new BorderLayout();
107 private JPanel topPanel = new JPanel();
109 private JPanel bottomPanel = new JPanel();
111 private JSplitPane topBottomSplitPane = new JSplitPane();
113 private JList listItems = new JList(new DefaultListModel());
115 // SWITCH IN JALVIEW HTML VIEWER PANE HERE
116 private BlogContentPane textDescription = new BlogContentPane();
118 // ADD IN JALVIEW BANNER FOR PRETTINESS
119 private BorderLayout borderLayout4 = new BorderLayout();
121 private BorderLayout borderLayout5 = new BorderLayout();
123 private ChannelListModel _channelModel = null;
125 private JList listChannels = new JList();
127 private Action exitAction = new Action()
131 public void actionPerformed(ActionEvent arg0)
141 parent.showNews(false);
147 public void setEnabled(boolean arg0)
153 public void removePropertyChangeListener(PropertyChangeListener arg0)
155 // TODO Auto-generated method stub
160 public void putValue(String arg0, Object arg1)
162 // TODO Auto-generated method stub
167 public boolean isEnabled()
169 // TODO Auto-generated method stub
174 public Object getValue(String arg0)
176 // TODO Auto-generated method stub
181 public void addPropertyChangeListener(PropertyChangeListener arg0)
183 // TODO Auto-generated method stub
188 private JFrame xf = null;
190 private JalviewDialog jd = null;
192 private JalviewDialog createDialog()
195 return jd = new JalviewDialog()
199 protected void raiseClosed()
203 Console.debug("News window closed.");
205 parent.showNews(false);
210 protected void okPressed()
212 // TODO Auto-generated method stub
217 protected void cancelPressed()
219 // TODO Auto-generated method stub
225 private JLabel lblChannels = new JLabel();
227 private List _updatableActions = new ArrayList();
229 private ItemReadTimer _itemTimer = null;
231 private JPopupMenu _popupItems = null;
233 private JPopupMenu _popupChannels = null;
235 private String lastm = "";
237 private boolean newsnew = false;
239 private Desktop parent = null;
246 // should we ignore fake gui events
247 private boolean updating = false;
249 public BlogReader(Desktop desktop)
251 Console.debug("Constructing news reader.");
254 _channelModel = new ChannelListModel();
255 // Construct our jalview news channel
256 Channel chan = new Channel();
258 Cache.getDefault("JALVIEW_NEWS_RSS",
259 Cache.getDefault("www.jalview.org",
260 "https://www.jalview.org")
261 + "/feeds/desktop/rss"));
263 _channelModel.addChannel(chan);
269 } catch (Exception e)
276 if (!Cache.getDefault("NONEWS", false))
278 boolean setvisible = checkForNew(chan, true);
283 Console.debug("Will show jalview news automatically");
287 Console.debug("Completed construction of reader.");
292 * check if the news panel's container is visible
295 public boolean isVisible()
299 return xf != null && xf.isVisible();
301 return jd != null && jd.isVisible();
305 * display the container for the news panel
307 public void showNews()
309 final BlogReader me = this;
310 SwingUtilities.invokeLater(new Runnable()
315 Rectangle bounds = new Rectangle(5, 5, 550, 350);
319 xf.setContentPane(me);
320 xf.addWindowListener(new WindowAdapter()
323 public void windowClosing(WindowEvent e)
325 ActionEvent actionEvent = new ActionEvent(this,
326 ActionEvent.ACTION_FIRST,
327 (String) exitAction.getValue(Action.NAME));
328 exitAction.actionPerformed(actionEvent);
332 public void windowOpened(WindowEvent e)
336 me.setSize(new Dimension(550, 350));
342 bounds = new Rectangle(5, 5, 550, 350);
343 jd.initDialogFrame(me, false, false,
344 MessageManager.getString("label.news_from_jalview"),
345 bounds.width, bounds.height);
346 jd.frame.setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
347 Console.debug("Displaying news.");
355 * update hasnew flag and mark all new messages as unread.
357 private boolean checkForNew(Channel chan, boolean updateItems)
360 if (!updating || updateItems)
364 java.util.Date earliest = null;
367 earliest = new SimpleDateFormat("YYYY-MM-DD")
368 .parse(chan.getHTTPLastModified());
369 } catch (Exception x)
372 if (chan != null && chan.getItems() != null)
374 Console.debug("Scanning news items: newsnew=" + newsnew
375 + " and lastDate is " + lastDate);
376 for (Item i : (List<Item>) chan.getItems())
378 Date published = i.getPublishDate();
379 boolean isread = lastDate == null ? false
380 : (published != null && !lastDate.before(published));
382 if (!updating || updateItems)
390 if (published != null && !i.isRead())
392 if (earliest == null || earliest.after(published))
394 earliest = published;
399 if (!updateItems && !updating && lastDate == null)
406 java.util.Date lastDate = null;
408 private void loadLastM()
410 lastDate = Cache.getDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED");
413 private void saveLastM(Item item)
417 if (item.getPublishDate() != null)
419 if (lastDate == null || item.getPublishDate().after(lastDate))
421 lastDate = item.getPublishDate();
425 if (_channelModel.getElementAt(0) != null)
427 checkForNew((Channel) _channelModel.getElementAt(0), false);
429 if (lastDate != null)
431 String formatted = Cache
432 .setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", lastDate);
433 Console.debug("Saved last read date as " + formatted);
438 private void jbInit() throws Exception
440 setLayout(layoutMain);
441 panelMain.setLayout(borderLayout1);
442 topPanel.setLayout(borderLayout5);
443 bottomPanel.setLayout(borderLayout4);
444 topBottomSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
445 topBottomSplitPane.setDividerLocation(100);
446 topBottomSplitPane.setTopComponent(topPanel);
447 topBottomSplitPane.setBottomComponent(bottomPanel);
448 JScrollPane spTextDescription = new JScrollPane(textDescription);
449 textDescription.setText("");
450 statusBar.setText(new StringBuffer("[")
451 .append(MessageManager.getString("label.status")).append("]")
453 buttonRefresh.addActionListener(new ActionListener()
457 public void actionPerformed(ActionEvent e)
462 add(statusBar, BorderLayout.SOUTH);
463 toolBar.add(buttonRefresh);
464 toolBar.addSeparator();
465 JLabel about = new JLabel(
466 "brought to you by JSwingReader (jswingreader.sourceforge.net)");
468 toolBar.setFloatable(false);
469 add(toolBar, BorderLayout.NORTH);
470 panelMain.add(topBottomSplitPane, BorderLayout.CENTER);
471 add(panelMain, BorderLayout.CENTER);
472 JScrollPane spListItems = new JScrollPane(listItems);
474 .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
475 topPanel.add(spListItems, BorderLayout.CENTER);
476 bottomPanel.add(spTextDescription, BorderLayout.CENTER);
477 listChannels.setModel(_channelModel);
479 listItems.addMouseListener(new java.awt.event.MouseAdapter()
482 public void mouseClicked(MouseEvent e)
484 listItems_mouseClicked(e);
487 _popupItems = _buildItemsPopupMenu();
488 _popupChannels = _buildChannelsPopupMenu();
489 ContextMenuMouseAdapter popupAdapter = new ContextMenuMouseAdapter(
491 ContextMenuMouseAdapter popupChannelsAdapter = new ContextMenuMouseAdapter(
493 listItems.addMouseListener(popupAdapter);
494 listItems.setCellRenderer(new ItemsRenderer());
495 lblChannels.setText(MessageManager.getString("label.channels"));
498 private void postInit()
500 // clear the default hyperlink listener and replace with our own.
501 for (HyperlinkListener hll : textDescription.getHyperlinkListeners())
503 textDescription.removeHyperlinkListener(hll);
505 textDescription.addHyperlinkListener(new HyperlinkListener()
508 public void hyperlinkUpdate(HyperlinkEvent e)
510 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
512 Desktop.showUrl(e.getURL().toExternalForm());
517 listItems.addListSelectionListener(new ListSelectionListener()
520 public void valueChanged(ListSelectionEvent e)
522 if (e.getValueIsAdjusting() == false)
524 _itemsValueChanged(listItems);
528 listChannels.setSelectedIndex(1);
530 _updateToolbarButtons();
532 _itemTimer = new ItemReadTimer(listChannels, listItems);
533 _itemsValueChanged(listItems);
536 public class LaunchJvBrowserOnItem extends AbstractAction
537 implements UpdatableAction
539 JList _listItems = null;
541 public LaunchJvBrowserOnItem(JList listItems)
543 super("Open in Browser");
544 this.putValue(MNEMONIC_KEY, Integer.valueOf(KeyEvent.VK_O));
545 this.putValue(Action.LONG_DESCRIPTION, "Open in Browser");
546 _listItems = listItems;
550 public void actionPerformed(ActionEvent e)
552 Object o = _listItems.getSelectedValue();
553 if (o instanceof Item)
555 Item item = (Item) o;
557 _listItems.repaint();
559 Desktop.showUrl(item.getLink());
564 public void update(Object o)
567 if (_listItems == null || _listItems.getModel().getSize() == 0)
571 else if (_listItems.getSelectedIndex() == -1)
579 private JPopupMenu _buildItemsPopupMenu()
581 JPopupMenu popup = new JPopupMenu();
582 popup.add(new JMenuItem(new LaunchJvBrowserOnItem(listItems)));
583 popup.addSeparator();
584 popup.add(new JMenuItem(new MarkItemAsRead(listItems)));
585 popup.add(new JMenuItem(new MarkItemAsUnread(listItems)));
589 private JPopupMenu _buildChannelsPopupMenu()
591 JPopupMenu popup = new JPopupMenu();
593 new JMenuItem(new MarkChannelAsRead(listChannels, listItems)));
594 popup.add(new JMenuItem(
595 new MarkChannelAsUnread(listChannels, listItems)));
599 private void initItems(Channel channel)
603 channel = new Channel();
605 if (!channel.isOpen() && channel.getURL() != null)
609 SimpleRSSParser.parse(channel);
610 } catch (Exception ex)
612 ex.printStackTrace();
615 DefaultListModel itemsModel = (DefaultListModel) listItems.getModel();
617 Iterator iter = (channel.getItems() != null)
618 ? channel.getItems().iterator()
619 : Collections.EMPTY_LIST.iterator();
620 while (iter.hasNext())
622 itemsModel.addElement(iter.next());
624 if (itemsModel.getSize() > 0)
626 listItems.setSelectedIndex(0);
627 _itemsValueChanged(listItems);
629 setStatusBarText(channel.getURL());
633 private void _itemsValueChanged(JList itemList)
635 Item item = (Item) itemList.getSelectedValue();
638 if (itemList.getModel().getSize() > 0)
640 item = (Item) itemList.getModel().getElementAt(0);
648 itemList.setSelectedIndex(0);
652 if (_itemTimer != null)
654 // prefer a shorter delay than 5s
655 _itemTimer.setDelay(300);
657 _itemTimer.setLastItem(item);
658 final Item lastitem = item;
659 _itemTimer.addActionListener(new ActionListener()
663 public void actionPerformed(ActionEvent e)
670 setStatusBarText(item.getLink());
671 textDescription.setBlogText(item);
675 public void setStatusBarText(String text)
677 statusBar.setText(text);
680 private void _updateAllActions()
682 Iterator iter = _updatableActions.iterator();
683 while (iter.hasNext())
685 UpdatableAction action = (UpdatableAction) iter.next();
690 private void _updateToolbarButtons()
692 Map general = (Map) Main.getPreferences().get("general");
698 Component[] components = toolBar.getComponents();
699 for (int i = 0; i < components.length; i++)
701 Component component = components[i];
702 if (component instanceof JButton)
704 JButton button = (JButton) component;
705 if (Boolean.toString(false).equals(general.get("useToolBarText")))
707 // Remove the text if preferences state no toolbar text
710 if (Boolean.toString(true).equals(general.get("radioTextBelow")))
712 button.setVerticalTextPosition(AbstractButton.BOTTOM);
713 button.setHorizontalTextPosition(AbstractButton.CENTER);
715 else if (Boolean.toString(true)
716 .equals(general.get("radioTextRight")))
718 button.setVerticalTextPosition(AbstractButton.CENTER);
719 button.setHorizontalTextPosition(AbstractButton.RIGHT);
725 private void listItems_mouseClicked(MouseEvent e)
727 if (e.getClickCount() == 2 && e.getModifiersEx() == MouseEvent.NOBUTTON)
729 Item item = (Item) listItems.getSelectedValue();
732 if (_itemTimer != null)
737 Action action = new LaunchJvBrowserOnItem(listItems);
738 ActionEvent event = new ActionEvent(this,
739 ActionEvent.ACTION_PERFORMED, "LaunchBrowserOnItem");
740 action.actionPerformed(event);
745 * force the news panel to refresh
747 public void refreshNews()
751 initItems((Channel) _channelModel.getElementAt(0));
753 } catch (Exception x)
762 public static void main(String args[])
764 // this tests the detection of new news based on the last read date stored
765 // in jalview properties
766 Cache.loadProperties(null);
767 Console.initLogger();
768 // test will advance read date each time
769 Calendar today = Calendar.getInstance(),
770 lastread = Calendar.getInstance();
771 lastread.set(1983, 01, 01);
772 while (lastread.before(today))
774 String formattedDate = Cache.setDateProperty(
775 "JALVIEW_NEWS_RSS_LASTMODIFIED", lastread.getTime());
776 BlogReader me = new BlogReader();
777 System.out.println("Set last date to " + formattedDate);
780 Console.debug("There is news to read.");
784 Console.debug("There is no new news.");
785 me.xf.setTitle("Testing : Last read is " + me.lastDate);
789 Console.debug("Waiting for closure.");
795 } catch (InterruptedException x)
798 } while (me.isVisible());
802 Console.debug("Still new news after reader displayed.");
804 if (lastread.getTime().before(me.lastDate))
806 Console.debug("The news was read.");
807 lastread.setTime(me.lastDate);
811 lastread.add(Calendar.MONTH, 1);
823 class ChannelsRenderer extends DefaultListCellRenderer
825 private final static Icon _icon = new ImageIcon(
826 Main.class.getResource("image/ComposeMail16.gif"));
829 public Component getListCellRendererComponent(JList list, Object value,
830 int index, boolean isSelected, boolean cellHasFocus)
832 JLabel component = (JLabel) super.getListCellRendererComponent(list,
833 value, index, isSelected, cellHasFocus);
834 component.setIcon(ChannelsRenderer._icon);
835 if (value instanceof Channel)
837 Channel channel = (Channel) value;
838 component.setText(MessageManager
839 .formatMessage("label.channel_title_item_count", new String[]
840 { channel.getTitle(), Integer
841 .valueOf(channel.getUnreadItemCount()).toString() }));
842 component.setToolTipText(channel.getURL());
848 class ItemsRenderer extends DefaultListCellRenderer
850 private final static Icon _icon = new ImageIcon(
851 Main.class.getResource("image/ComposeMail16.gif"));
854 public Component getListCellRendererComponent(JList list, Object value,
855 int index, boolean isSelected, boolean cellHasFocus)
857 JLabel component = (JLabel) super.getListCellRendererComponent(list,
858 value, index, isSelected, cellHasFocus);
859 component.setIcon(ItemsRenderer._icon);
860 if (value instanceof Item)
862 Item item = (Item) value;
863 if (item.getPublishDate() != null)
865 component.setText(MessageManager.formatMessage(
866 "label.blog_item_published_on_date", new String[]
868 .getDateInstance(DateFormat.LONG,
869 MessageManager.getLocale())
870 .format(item.getPublishDate()).toString(),
873 component.setToolTipText(item.getLink());
876 component.setFont(component.getFont().deriveFont(Font.BOLD));
880 component.setFont(component.getFont().deriveFont(Font.PLAIN));