1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 *******************************************************************************/
20 import jalview.bin.Cache;
21 import java.awt.BorderLayout;
22 import java.awt.Component;
23 import java.awt.Dimension;
25 import java.awt.Rectangle;
26 import java.awt.Dialog.ModalExclusionType;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.KeyEvent;
30 import java.awt.event.MouseEvent;
31 import java.awt.event.WindowAdapter;
32 import java.awt.event.WindowEvent;
33 import java.beans.PropertyChangeListener;
34 import java.text.DateFormat;
35 import java.text.SimpleDateFormat;
36 import java.util.ArrayList;
37 import java.util.Calendar;
38 import java.util.Collections;
39 import java.util.Iterator;
40 import java.util.List;
43 import javax.swing.AbstractAction;
44 import javax.swing.AbstractButton;
45 import javax.swing.Action;
46 import javax.swing.DefaultListCellRenderer;
47 import javax.swing.DefaultListModel;
48 import javax.swing.Icon;
49 import javax.swing.ImageIcon;
50 import javax.swing.JButton;
51 import javax.swing.JFrame;
52 import javax.swing.JLabel;
53 import javax.swing.JList;
54 import javax.swing.JMenuItem;
55 import javax.swing.JPanel;
56 import javax.swing.JPopupMenu;
57 import javax.swing.JScrollPane;
58 import javax.swing.JSplitPane;
59 import javax.swing.JToolBar;
60 import javax.swing.ListSelectionModel;
61 import javax.swing.SwingUtilities;
62 import javax.swing.event.HyperlinkEvent;
63 import javax.swing.event.HyperlinkListener;
64 import javax.swing.event.ListSelectionEvent;
65 import javax.swing.event.ListSelectionListener;
67 import org.robsite.jswingreader.action.MarkChannelAsRead;
68 import org.robsite.jswingreader.action.MarkChannelAsUnread;
69 import org.robsite.jswingreader.action.MarkItemAsRead;
70 import org.robsite.jswingreader.action.MarkItemAsUnread;
71 import org.robsite.jswingreader.action.UpdatableAction;
72 import org.robsite.jswingreader.model.Channel;
73 import org.robsite.jswingreader.model.ChannelListModel;
74 import org.robsite.jswingreader.model.Item;
75 import org.robsite.jswingreader.model.SimpleRSSParser;
76 import org.robsite.jswingreader.ui.BlogContentPane;
77 import org.robsite.jswingreader.ui.ItemReadTimer;
78 import org.robsite.jswingreader.ui.Main;
79 import org.robsite.jswingreader.ui.util.ContextMenuMouseAdapter;
82 * Blog reading window, adapted from JSwingReader's
83 * org.robsite.jswingreader.ui.MainWindow class
86 public class BlogReader extends JPanel
88 private JButton buttonRefresh = new JButton();
90 private JToolBar toolBar = new JToolBar();
92 private JLabel statusBar = new JLabel();
94 private JPanel panelMain = new JPanel();
96 private BorderLayout layoutMain = new BorderLayout();
98 private BorderLayout borderLayout1 = new BorderLayout();
100 private JPanel topPanel = new JPanel();
102 private JPanel bottomPanel = new JPanel();
104 private JSplitPane topBottomSplitPane = new JSplitPane();
106 private JList listItems = new JList(new DefaultListModel());
108 // SWITCH IN JALVIEW HTML VIEWER PANE HERE
109 private BlogContentPane textDescription = new BlogContentPane();
111 // ADD IN JALVIEW BANNER FOR PRETTINESS
112 private BorderLayout borderLayout4 = new BorderLayout();
114 private BorderLayout borderLayout5 = new BorderLayout();
116 private ChannelListModel _channelModel = null;
118 private JList listChannels = new JList();
120 private Action exitAction = new Action()
124 public void actionPerformed(ActionEvent arg0)
133 parent.showNews(false);
139 public void setEnabled(boolean arg0)
145 public void removePropertyChangeListener(PropertyChangeListener arg0)
147 // TODO Auto-generated method stub
152 public void putValue(String arg0, Object arg1)
154 // TODO Auto-generated method stub
159 public boolean isEnabled()
161 // TODO Auto-generated method stub
166 public Object getValue(String arg0)
168 // TODO Auto-generated method stub
173 public void addPropertyChangeListener(PropertyChangeListener arg0)
175 // TODO Auto-generated method stub
180 private JFrame xf = null;
182 private JalviewDialog jd = null;
184 private JalviewDialog createDialog()
187 return jd = new JalviewDialog()
191 protected void raiseClosed()
195 Cache.log.info("News window closed.");
197 parent.showNews(false);
202 protected void okPressed()
204 // TODO Auto-generated method stub
209 protected void cancelPressed()
211 // TODO Auto-generated method stub
217 private JLabel lblChannels = new JLabel();
219 private List _updatableActions = new ArrayList();
221 private ItemReadTimer _itemTimer = null;
223 private JPopupMenu _popupItems = null;
225 private JPopupMenu _popupChannels = null;
227 private String lastm = "";
229 private boolean newsnew = false;
231 private Desktop parent = null;
238 // should we ignore fake gui events
239 private boolean updating = false;
241 public BlogReader(Desktop desktop)
243 Cache.log.info("Constructing news reader.");
246 _channelModel = new ChannelListModel();
247 // Construct our jalview news channel
248 Channel chan = new Channel();
249 chan.setURL(jalview.bin.Cache.getDefault(
251 jalview.bin.Cache.getDefault("www.jalview.org",
252 "http://www.jalview.org") + "/feeds/desktop/rss"));
254 _channelModel.addChannel(chan);
260 } catch (Exception e)
267 boolean setvisible = checkForNew(chan, true);
272 Cache.log.info("Will show jalview news automatically");
275 Cache.log.info("Completed construction of reader.");
280 * check if the news panel's container is visible
282 public boolean isVisible()
286 return xf != null && xf.isVisible();
288 return jd != null && jd.isVisible();
292 * display the container for the news panel
294 public void showNews()
296 final BlogReader me = this;
297 SwingUtilities.invokeLater(new Runnable()
302 Rectangle bounds = new Rectangle(5, 5, 550, 350);
306 xf.setContentPane(me);
307 xf.addWindowListener(new WindowAdapter()
309 public void windowClosing(WindowEvent e)
311 ActionEvent actionEvent = new ActionEvent(this,
312 ActionEvent.ACTION_FIRST, (String) exitAction
313 .getValue(Action.NAME));
314 exitAction.actionPerformed(actionEvent);
317 public void windowOpened(WindowEvent e)
321 me.setSize(new Dimension(550, 350));
327 bounds = new Rectangle(5, 5, 550, 350);
328 jd.initDialogFrame(me, false, false, "News from www.jalview.org",
329 bounds.width, bounds.height);
330 jd.frame.setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
331 Cache.log.info("Displaying news.");
339 * update hasnew flag and mark all new messages as unread.
341 private boolean checkForNew(Channel chan, boolean updateItems)
344 if (!updating || updateItems)
348 java.util.Date earliest = null;
351 earliest = new SimpleDateFormat("YYYY-MM-DD").parse(chan
352 .getHTTPLastModified());
353 } catch (Exception x)
357 if (chan != null && chan.getItems() != null)
359 Cache.log.debug("Scanning news items: newsnew=" + newsnew
360 + " and lastDate is " + lastDate);
361 for (Item i : (List<Item>) chan.getItems())
363 boolean isread = lastDate == null ? false
364 : (i.getPublishDate() != null && !lastDate.before(i
367 if (!updating || updateItems)
375 if (i.getPublishDate() != null && !i.isRead())
377 if (earliest == null || earliest.after(i.getPublishDate()))
379 earliest = i.getPublishDate();
384 if (!updateItems && !updating && lastDate == null)
391 java.util.Date lastDate = null;
393 private void loadLastM()
395 lastDate = Cache.getDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED");
398 private void saveLastM(Item item)
402 if (item.getPublishDate() != null)
404 if (lastDate == null || item.getPublishDate().after(lastDate))
406 lastDate = item.getPublishDate();
410 if (_channelModel.getElementAt(0) != null)
412 checkForNew((Channel) _channelModel.getElementAt(0), false);
414 if (lastDate != null)
416 jalview.bin.Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED",
418 jalview.bin.Cache.log.info("Saved last read date as "
419 + jalview.bin.Cache.date_format.format(lastDate));
425 private void jbInit() throws Exception
427 setLayout(layoutMain);
428 panelMain.setLayout(borderLayout1);
429 topPanel.setLayout(borderLayout5);
430 bottomPanel.setLayout(borderLayout4);
431 topBottomSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
432 topBottomSplitPane.setDividerLocation(100);
433 topBottomSplitPane.setTopComponent(topPanel);
434 topBottomSplitPane.setBottomComponent(bottomPanel);
435 JScrollPane spTextDescription = new JScrollPane(textDescription);
436 textDescription.setText("");
437 statusBar.setText(" [Status] ");
438 buttonRefresh.addActionListener(new ActionListener()
442 public void actionPerformed(ActionEvent e)
447 add(statusBar, BorderLayout.SOUTH);
448 toolBar.add(buttonRefresh);
449 toolBar.addSeparator();
450 JLabel about = new JLabel(
451 "brought to you by JSwingReader (jswingreader.sourceforge.net)");
453 toolBar.setFloatable(false);
454 add(toolBar, BorderLayout.NORTH);
455 panelMain.add(topBottomSplitPane, BorderLayout.CENTER);
456 add(panelMain, BorderLayout.CENTER);
457 JScrollPane spListItems = new JScrollPane(listItems);
459 .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
460 topPanel.add(spListItems, BorderLayout.CENTER);
461 bottomPanel.add(spTextDescription, BorderLayout.CENTER);
462 listChannels.setModel(_channelModel);
464 listItems.addMouseListener(new java.awt.event.MouseAdapter()
466 public void mouseClicked(MouseEvent e)
468 listItems_mouseClicked(e);
471 _popupItems = _buildItemsPopupMenu();
472 _popupChannels = _buildChannelsPopupMenu();
473 ContextMenuMouseAdapter popupAdapter = new ContextMenuMouseAdapter(
475 ContextMenuMouseAdapter popupChannelsAdapter = new ContextMenuMouseAdapter(
477 listItems.addMouseListener(popupAdapter);
478 listItems.setCellRenderer(new ItemsRenderer());
479 lblChannels.setText("Channels");
482 private void postInit()
484 // clear the default hyperlink listener and replace with our own.
485 for (HyperlinkListener hll : textDescription.getHyperlinkListeners())
487 textDescription.removeHyperlinkListener(hll);
489 textDescription.addHyperlinkListener(new HyperlinkListener()
491 public void hyperlinkUpdate(HyperlinkEvent e)
493 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
495 Desktop.showUrl(e.getURL().toExternalForm());
500 listItems.addListSelectionListener(new ListSelectionListener()
502 public void valueChanged(ListSelectionEvent e)
504 if (e.getValueIsAdjusting() == false)
506 _itemsValueChanged(listItems);
510 listChannels.setSelectedIndex(1);
512 _updateToolbarButtons();
514 _itemTimer = new ItemReadTimer(listChannels, listItems);
515 _itemsValueChanged(listItems);
518 public class LaunchJvBrowserOnItem extends AbstractAction implements
521 JList _listItems = null;
523 public LaunchJvBrowserOnItem(JList listItems)
525 super("Open in Browser");
526 this.putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_O));
527 this.putValue(Action.LONG_DESCRIPTION, "Open in Browser");
528 _listItems = listItems;
531 public void actionPerformed(ActionEvent e)
533 Object o = _listItems.getSelectedValue();
534 if (o instanceof Item)
536 Item item = (Item) o;
538 _listItems.repaint();
540 Desktop.showUrl(item.getLink());
544 public void update(Object o)
547 if (_listItems == null || _listItems.getModel().getSize() == 0)
551 else if (_listItems.getSelectedIndex() == -1)
559 private JPopupMenu _buildItemsPopupMenu()
561 JPopupMenu popup = new JPopupMenu();
562 popup.add(new JMenuItem(new LaunchJvBrowserOnItem(listItems)));
563 popup.addSeparator();
564 popup.add(new JMenuItem(new MarkItemAsRead(listItems)));
565 popup.add(new JMenuItem(new MarkItemAsUnread(listItems)));
569 private JPopupMenu _buildChannelsPopupMenu()
571 JPopupMenu popup = new JPopupMenu();
572 popup.add(new JMenuItem(new MarkChannelAsRead(listChannels, listItems)));
573 popup.add(new JMenuItem(
574 new MarkChannelAsUnread(listChannels, listItems)));
578 private void initItems(Channel channel)
582 channel = new Channel();
584 if (!channel.isOpen() && channel.getURL() != null)
588 SimpleRSSParser.parse(channel);
589 } catch (Exception ex)
591 ex.printStackTrace();
594 DefaultListModel itemsModel = (DefaultListModel) listItems.getModel();
596 Iterator iter = (channel.getItems() != null) ? channel.getItems()
597 .iterator() : Collections.EMPTY_LIST.iterator();
598 while (iter.hasNext())
600 itemsModel.addElement(iter.next());
602 if (itemsModel.getSize() > 0)
604 listItems.setSelectedIndex(0);
605 _itemsValueChanged(listItems);
607 setStatusBarText(channel.getURL());
611 private void _itemsValueChanged(JList itemList)
613 Item item = (Item) itemList.getSelectedValue();
616 if (itemList.getModel().getSize() > 0)
618 item = (Item) itemList.getModel().getElementAt(0);
626 itemList.setSelectedIndex(0);
630 if (_itemTimer != null)
632 // prefer a shorter delay than 5s
633 _itemTimer.setDelay(300);
635 _itemTimer.setLastItem(item);
636 final Item lastitem = item;
637 _itemTimer.addActionListener(new ActionListener()
641 public void actionPerformed(ActionEvent e)
648 setStatusBarText(item.getLink());
649 textDescription.setBlogText(item);
653 public void setStatusBarText(String text)
655 statusBar.setText(text);
658 private void _updateAllActions()
660 Iterator iter = _updatableActions.iterator();
661 while (iter.hasNext())
663 UpdatableAction action = (UpdatableAction) iter.next();
668 private void _updateToolbarButtons()
670 Map general = (Map) Main.getPreferences().get("general");
676 Component[] components = toolBar.getComponents();
677 for (int i = 0; i < components.length; i++)
679 Component component = components[i];
680 if (component instanceof JButton)
682 JButton button = (JButton) component;
683 if (Boolean.toString(false).equals(general.get("useToolBarText")))
685 // Remove the text if preferences state no toolbar text
688 if (Boolean.toString(true).equals(general.get("radioTextBelow")))
690 button.setVerticalTextPosition(AbstractButton.BOTTOM);
691 button.setHorizontalTextPosition(AbstractButton.CENTER);
693 else if (Boolean.toString(true).equals(
694 general.get("radioTextRight")))
696 button.setVerticalTextPosition(AbstractButton.CENTER);
697 button.setHorizontalTextPosition(AbstractButton.RIGHT);
703 private void listItems_mouseClicked(MouseEvent e)
705 if (e.getClickCount() == 2 && e.getModifiersEx() == MouseEvent.NOBUTTON)
707 Item item = (Item) listItems.getSelectedValue();
710 if (_itemTimer != null)
715 Action action = new LaunchJvBrowserOnItem(listItems);
716 ActionEvent event = new ActionEvent(this,
717 ActionEvent.ACTION_PERFORMED, "LaunchBrowserOnItem");
718 action.actionPerformed(event);
723 * force the news panel to refresh
725 public void refreshNews()
729 initItems((Channel) _channelModel.getElementAt(0));
731 } catch (Exception x)
736 public static void main(String args[])
738 // this tests the detection of new news based on the last read date stored
739 // in jalview properties
740 jalview.bin.Cache.loadProperties(null);
741 jalview.bin.Cache.initLogger();
742 // test will advance read date each time
743 Calendar today = Calendar.getInstance(), lastread = Calendar
745 lastread.set(1983, 01, 01);
746 while (lastread.before(today))
748 Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED",
750 BlogReader me = new BlogReader();
751 System.out.println("Set last date to "
752 + jalview.bin.Cache.date_format.format(lastread.getTime()));
755 Cache.log.info("There is news to read.");
759 Cache.log.info("There is no new news.");
760 me.xf.setTitle("Testing : Last read is " + me.lastDate);
764 Cache.log.info("Waiting for closure.");
770 } catch (InterruptedException x)
774 } while (me.isVisible());
778 Cache.log.info("Still new news after reader displayed.");
780 if (lastread.getTime().before(me.lastDate))
782 Cache.log.info("The news was read.");
783 lastread.setTime(me.lastDate);
787 lastread.add(Calendar.MONTH, 1);
799 class ChannelsRenderer extends DefaultListCellRenderer
801 private final static Icon _icon = new ImageIcon(
802 Main.class.getResource("image/ComposeMail16.gif"));
804 public Component getListCellRendererComponent(JList list, Object value,
805 int index, boolean isSelected, boolean cellHasFocus)
807 JLabel component = (JLabel) super.getListCellRendererComponent(list,
808 value, index, isSelected, cellHasFocus);
809 component.setIcon(ChannelsRenderer._icon);
810 if (value instanceof Channel)
812 Channel channel = (Channel) value;
813 component.setText(channel.getTitle() + " ("
814 + channel.getUnreadItemCount() + ")");
815 component.setToolTipText(channel.getURL());
821 class ItemsRenderer extends DefaultListCellRenderer
823 private final static Icon _icon = new ImageIcon(
824 Main.class.getResource("image/ComposeMail16.gif"));
826 public Component getListCellRendererComponent(JList list, Object value,
827 int index, boolean isSelected, boolean cellHasFocus)
829 JLabel component = (JLabel) super.getListCellRendererComponent(list,
830 value, index, isSelected, cellHasFocus);
831 component.setIcon(ItemsRenderer._icon);
832 if (value instanceof Item)
834 Item item = (Item) value;
835 if (item.getPublishDate() != null)
837 component.setText(DateFormat.getDateInstance().format(
838 item.getPublishDate())
839 + " " + item.getTitle());
841 component.setToolTipText(item.getLink());
844 component.setFont(component.getFont().deriveFont(Font.BOLD));
848 component.setFont(component.getFont().deriveFont(Font.PLAIN));