2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 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 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import jalview.bin.Cache;
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.awt.Dimension;
26 import java.awt.Rectangle;
27 import java.awt.Dialog.ModalExclusionType;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.KeyEvent;
31 import java.awt.event.MouseEvent;
32 import java.awt.event.WindowAdapter;
33 import java.awt.event.WindowEvent;
34 import java.beans.PropertyChangeListener;
35 import java.text.DateFormat;
36 import java.text.SimpleDateFormat;
37 import java.util.ArrayList;
38 import java.util.Calendar;
39 import java.util.Collections;
40 import java.util.Iterator;
41 import java.util.List;
44 import javax.swing.AbstractAction;
45 import javax.swing.AbstractButton;
46 import javax.swing.Action;
47 import javax.swing.DefaultListCellRenderer;
48 import javax.swing.DefaultListModel;
49 import javax.swing.Icon;
50 import javax.swing.ImageIcon;
51 import javax.swing.JButton;
52 import javax.swing.JFrame;
53 import javax.swing.JLabel;
54 import javax.swing.JList;
55 import javax.swing.JMenuItem;
56 import javax.swing.JPanel;
57 import javax.swing.JPopupMenu;
58 import javax.swing.JScrollPane;
59 import javax.swing.JSplitPane;
60 import javax.swing.JToolBar;
61 import javax.swing.ListSelectionModel;
62 import javax.swing.SwingUtilities;
63 import javax.swing.event.HyperlinkEvent;
64 import javax.swing.event.HyperlinkListener;
65 import javax.swing.event.ListSelectionEvent;
66 import javax.swing.event.ListSelectionListener;
68 import org.robsite.jswingreader.action.MarkChannelAsRead;
69 import org.robsite.jswingreader.action.MarkChannelAsUnread;
70 import org.robsite.jswingreader.action.MarkItemAsRead;
71 import org.robsite.jswingreader.action.MarkItemAsUnread;
72 import org.robsite.jswingreader.action.UpdatableAction;
73 import org.robsite.jswingreader.model.Channel;
74 import org.robsite.jswingreader.model.ChannelListModel;
75 import org.robsite.jswingreader.model.Item;
76 import org.robsite.jswingreader.model.SimpleRSSParser;
77 import org.robsite.jswingreader.ui.BlogContentPane;
78 import org.robsite.jswingreader.ui.ItemReadTimer;
79 import org.robsite.jswingreader.ui.Main;
80 import org.robsite.jswingreader.ui.util.ContextMenuMouseAdapter;
83 * Blog reading window, adapted from JSwingReader's
84 * org.robsite.jswingreader.ui.MainWindow class
87 public class BlogReader extends JPanel
89 private JButton buttonRefresh = new JButton();
91 private JToolBar toolBar = new JToolBar();
93 private JLabel statusBar = new JLabel();
95 private JPanel panelMain = new JPanel();
97 private BorderLayout layoutMain = new BorderLayout();
99 private BorderLayout borderLayout1 = new BorderLayout();
101 private JPanel topPanel = new JPanel();
103 private JPanel bottomPanel = new JPanel();
105 private JSplitPane topBottomSplitPane = new JSplitPane();
107 private JList listItems = new JList(new DefaultListModel());
109 // SWITCH IN JALVIEW HTML VIEWER PANE HERE
110 private BlogContentPane textDescription = new BlogContentPane();
112 // ADD IN JALVIEW BANNER FOR PRETTINESS
113 private BorderLayout borderLayout4 = new BorderLayout();
115 private BorderLayout borderLayout5 = new BorderLayout();
117 private ChannelListModel _channelModel = null;
119 private JList listChannels = new JList();
121 private Action exitAction = new Action()
125 public void actionPerformed(ActionEvent arg0)
135 parent.showNews(false);
141 public void setEnabled(boolean arg0)
147 public void removePropertyChangeListener(PropertyChangeListener arg0)
149 // TODO Auto-generated method stub
154 public void putValue(String arg0, Object arg1)
156 // TODO Auto-generated method stub
161 public boolean isEnabled()
163 // TODO Auto-generated method stub
168 public Object getValue(String arg0)
170 // TODO Auto-generated method stub
175 public void addPropertyChangeListener(PropertyChangeListener arg0)
177 // TODO Auto-generated method stub
182 private JFrame xf = null;
184 private JalviewDialog jd = null;
186 private JalviewDialog createDialog()
189 return jd = new JalviewDialog()
193 protected void raiseClosed()
197 Cache.log.info("News window closed.");
199 parent.showNews(false);
204 protected void okPressed()
206 // TODO Auto-generated method stub
211 protected void cancelPressed()
213 // TODO Auto-generated method stub
219 private JLabel lblChannels = new JLabel();
221 private List _updatableActions = new ArrayList();
223 private ItemReadTimer _itemTimer = null;
225 private JPopupMenu _popupItems = null;
227 private JPopupMenu _popupChannels = null;
229 private String lastm = "";
231 private boolean newsnew = false;
233 private Desktop parent = null;
240 // should we ignore fake gui events
241 private boolean updating = false;
243 public BlogReader(Desktop desktop)
245 Cache.log.info("Constructing news reader.");
248 _channelModel = new ChannelListModel();
249 // Construct our jalview news channel
250 Channel chan = new Channel();
251 chan.setURL(jalview.bin.Cache.getDefault(
253 jalview.bin.Cache.getDefault("www.jalview.org",
254 "http://www.jalview.org") + "/feeds/desktop/rss"));
256 _channelModel.addChannel(chan);
262 } catch (Exception e)
269 boolean setvisible = checkForNew(chan, true);
274 Cache.log.info("Will show jalview news automatically");
277 Cache.log.info("Completed construction of reader.");
282 * check if the news panel's container is visible
284 public boolean isVisible()
288 return xf != null && xf.isVisible();
290 return jd != null && jd.isVisible();
294 * display the container for the news panel
296 public void showNews()
298 final BlogReader me = this;
299 SwingUtilities.invokeLater(new Runnable()
304 Rectangle bounds = new Rectangle(5, 5, 550, 350);
308 xf.setContentPane(me);
309 xf.addWindowListener(new WindowAdapter()
311 public void windowClosing(WindowEvent e)
313 ActionEvent actionEvent = new ActionEvent(this,
314 ActionEvent.ACTION_FIRST, (String) exitAction
315 .getValue(Action.NAME));
316 exitAction.actionPerformed(actionEvent);
319 public void windowOpened(WindowEvent e)
323 me.setSize(new Dimension(550, 350));
329 bounds = new Rectangle(5, 5, 550, 350);
330 jd.initDialogFrame(me, false, false, "News from www.jalview.org",
331 bounds.width, bounds.height);
332 jd.frame.setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
333 Cache.log.info("Displaying news.");
341 * update hasnew flag and mark all new messages as unread.
343 private boolean checkForNew(Channel chan, boolean updateItems)
346 if (!updating || updateItems)
350 java.util.Date earliest = null;
353 earliest = new SimpleDateFormat("YYYY-MM-DD").parse(chan
354 .getHTTPLastModified());
355 } catch (Exception x)
359 if (chan != null && chan.getItems() != null)
361 Cache.log.debug("Scanning news items: newsnew=" + newsnew
362 + " and lastDate is " + lastDate);
363 for (Item i : (List<Item>) chan.getItems())
365 boolean isread = lastDate == null ? false
366 : (i.getPublishDate() != null && !lastDate.before(i
369 if (!updating || updateItems)
377 if (i.getPublishDate() != null && !i.isRead())
379 if (earliest == null || earliest.after(i.getPublishDate()))
381 earliest = i.getPublishDate();
386 if (!updateItems && !updating && lastDate == null)
393 java.util.Date lastDate = null;
395 private void loadLastM()
397 lastDate = Cache.getDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED");
400 private void saveLastM(Item item)
404 if (item.getPublishDate() != null)
406 if (lastDate == null || item.getPublishDate().after(lastDate))
408 lastDate = item.getPublishDate();
412 if (_channelModel.getElementAt(0) != null)
414 checkForNew((Channel) _channelModel.getElementAt(0), false);
416 if (lastDate != null)
418 jalview.bin.Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED",
420 jalview.bin.Cache.log.info("Saved last read date as "
421 + jalview.bin.Cache.date_format.format(lastDate));
427 private void jbInit() throws Exception
429 setLayout(layoutMain);
430 panelMain.setLayout(borderLayout1);
431 topPanel.setLayout(borderLayout5);
432 bottomPanel.setLayout(borderLayout4);
433 topBottomSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
434 topBottomSplitPane.setDividerLocation(100);
435 topBottomSplitPane.setTopComponent(topPanel);
436 topBottomSplitPane.setBottomComponent(bottomPanel);
437 JScrollPane spTextDescription = new JScrollPane(textDescription);
438 textDescription.setText("");
439 statusBar.setText(" [Status] ");
440 buttonRefresh.addActionListener(new ActionListener()
444 public void actionPerformed(ActionEvent e)
449 add(statusBar, BorderLayout.SOUTH);
450 toolBar.add(buttonRefresh);
451 toolBar.addSeparator();
452 JLabel about = new JLabel(
453 "brought to you by JSwingReader (jswingreader.sourceforge.net)");
455 toolBar.setFloatable(false);
456 add(toolBar, BorderLayout.NORTH);
457 panelMain.add(topBottomSplitPane, BorderLayout.CENTER);
458 add(panelMain, BorderLayout.CENTER);
459 JScrollPane spListItems = new JScrollPane(listItems);
461 .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
462 topPanel.add(spListItems, BorderLayout.CENTER);
463 bottomPanel.add(spTextDescription, BorderLayout.CENTER);
464 listChannels.setModel(_channelModel);
466 listItems.addMouseListener(new java.awt.event.MouseAdapter()
468 public void mouseClicked(MouseEvent e)
470 listItems_mouseClicked(e);
473 _popupItems = _buildItemsPopupMenu();
474 _popupChannels = _buildChannelsPopupMenu();
475 ContextMenuMouseAdapter popupAdapter = new ContextMenuMouseAdapter(
477 ContextMenuMouseAdapter popupChannelsAdapter = new ContextMenuMouseAdapter(
479 listItems.addMouseListener(popupAdapter);
480 listItems.setCellRenderer(new ItemsRenderer());
481 lblChannels.setText("Channels");
484 private void postInit()
486 // clear the default hyperlink listener and replace with our own.
487 for (HyperlinkListener hll : textDescription.getHyperlinkListeners())
489 textDescription.removeHyperlinkListener(hll);
491 textDescription.addHyperlinkListener(new HyperlinkListener()
493 public void hyperlinkUpdate(HyperlinkEvent e)
495 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
497 Desktop.showUrl(e.getURL().toExternalForm());
502 listItems.addListSelectionListener(new ListSelectionListener()
504 public void valueChanged(ListSelectionEvent e)
506 if (e.getValueIsAdjusting() == false)
508 _itemsValueChanged(listItems);
512 listChannels.setSelectedIndex(1);
514 _updateToolbarButtons();
516 _itemTimer = new ItemReadTimer(listChannels, listItems);
517 _itemsValueChanged(listItems);
520 public class LaunchJvBrowserOnItem extends AbstractAction implements
523 JList _listItems = null;
525 public LaunchJvBrowserOnItem(JList listItems)
527 super("Open in Browser");
528 this.putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_O));
529 this.putValue(Action.LONG_DESCRIPTION, "Open in Browser");
530 _listItems = listItems;
533 public void actionPerformed(ActionEvent e)
535 Object o = _listItems.getSelectedValue();
536 if (o instanceof Item)
538 Item item = (Item) o;
540 _listItems.repaint();
542 Desktop.showUrl(item.getLink());
546 public void update(Object o)
549 if (_listItems == null || _listItems.getModel().getSize() == 0)
553 else if (_listItems.getSelectedIndex() == -1)
561 private JPopupMenu _buildItemsPopupMenu()
563 JPopupMenu popup = new JPopupMenu();
564 popup.add(new JMenuItem(new LaunchJvBrowserOnItem(listItems)));
565 popup.addSeparator();
566 popup.add(new JMenuItem(new MarkItemAsRead(listItems)));
567 popup.add(new JMenuItem(new MarkItemAsUnread(listItems)));
571 private JPopupMenu _buildChannelsPopupMenu()
573 JPopupMenu popup = new JPopupMenu();
574 popup.add(new JMenuItem(new MarkChannelAsRead(listChannels, listItems)));
575 popup.add(new JMenuItem(
576 new MarkChannelAsUnread(listChannels, listItems)));
580 private void initItems(Channel channel)
584 channel = new Channel();
586 if (!channel.isOpen() && channel.getURL() != null)
590 SimpleRSSParser.parse(channel);
591 } catch (Exception ex)
593 ex.printStackTrace();
596 DefaultListModel itemsModel = (DefaultListModel) listItems.getModel();
598 Iterator iter = (channel.getItems() != null) ? channel.getItems()
599 .iterator() : Collections.EMPTY_LIST.iterator();
600 while (iter.hasNext())
602 itemsModel.addElement(iter.next());
604 if (itemsModel.getSize() > 0)
606 listItems.setSelectedIndex(0);
607 _itemsValueChanged(listItems);
609 setStatusBarText(channel.getURL());
613 private void _itemsValueChanged(JList itemList)
615 Item item = (Item) itemList.getSelectedValue();
618 if (itemList.getModel().getSize() > 0)
620 item = (Item) itemList.getModel().getElementAt(0);
628 itemList.setSelectedIndex(0);
632 if (_itemTimer != null)
634 // prefer a shorter delay than 5s
635 _itemTimer.setDelay(300);
637 _itemTimer.setLastItem(item);
638 final Item lastitem = item;
639 _itemTimer.addActionListener(new ActionListener()
643 public void actionPerformed(ActionEvent e)
650 setStatusBarText(item.getLink());
651 textDescription.setBlogText(item);
655 public void setStatusBarText(String text)
657 statusBar.setText(text);
660 private void _updateAllActions()
662 Iterator iter = _updatableActions.iterator();
663 while (iter.hasNext())
665 UpdatableAction action = (UpdatableAction) iter.next();
670 private void _updateToolbarButtons()
672 Map general = (Map) Main.getPreferences().get("general");
678 Component[] components = toolBar.getComponents();
679 for (int i = 0; i < components.length; i++)
681 Component component = components[i];
682 if (component instanceof JButton)
684 JButton button = (JButton) component;
685 if (Boolean.toString(false).equals(general.get("useToolBarText")))
687 // Remove the text if preferences state no toolbar text
690 if (Boolean.toString(true).equals(general.get("radioTextBelow")))
692 button.setVerticalTextPosition(AbstractButton.BOTTOM);
693 button.setHorizontalTextPosition(AbstractButton.CENTER);
695 else if (Boolean.toString(true).equals(
696 general.get("radioTextRight")))
698 button.setVerticalTextPosition(AbstractButton.CENTER);
699 button.setHorizontalTextPosition(AbstractButton.RIGHT);
705 private void listItems_mouseClicked(MouseEvent e)
707 if (e.getClickCount() == 2 && e.getModifiersEx() == MouseEvent.NOBUTTON)
709 Item item = (Item) listItems.getSelectedValue();
712 if (_itemTimer != null)
717 Action action = new LaunchJvBrowserOnItem(listItems);
718 ActionEvent event = new ActionEvent(this,
719 ActionEvent.ACTION_PERFORMED, "LaunchBrowserOnItem");
720 action.actionPerformed(event);
725 * force the news panel to refresh
727 public void refreshNews()
731 initItems((Channel) _channelModel.getElementAt(0));
733 } catch (Exception x)
738 public static void main(String args[])
740 // this tests the detection of new news based on the last read date stored
741 // in jalview properties
742 jalview.bin.Cache.loadProperties(null);
743 jalview.bin.Cache.initLogger();
744 // test will advance read date each time
745 Calendar today = Calendar.getInstance(), lastread = Calendar
747 lastread.set(1983, 01, 01);
748 while (lastread.before(today))
750 Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED",
752 BlogReader me = new BlogReader();
753 System.out.println("Set last date to "
754 + jalview.bin.Cache.date_format.format(lastread.getTime()));
757 Cache.log.info("There is news to read.");
761 Cache.log.info("There is no new news.");
762 me.xf.setTitle("Testing : Last read is " + me.lastDate);
766 Cache.log.info("Waiting for closure.");
772 } catch (InterruptedException x)
776 } while (me.isVisible());
780 Cache.log.info("Still new news after reader displayed.");
782 if (lastread.getTime().before(me.lastDate))
784 Cache.log.info("The news was read.");
785 lastread.setTime(me.lastDate);
789 lastread.add(Calendar.MONTH, 1);
801 class ChannelsRenderer extends DefaultListCellRenderer
803 private final static Icon _icon = new ImageIcon(
804 Main.class.getResource("image/ComposeMail16.gif"));
806 public Component getListCellRendererComponent(JList list, Object value,
807 int index, boolean isSelected, boolean cellHasFocus)
809 JLabel component = (JLabel) super.getListCellRendererComponent(list,
810 value, index, isSelected, cellHasFocus);
811 component.setIcon(ChannelsRenderer._icon);
812 if (value instanceof Channel)
814 Channel channel = (Channel) value;
815 component.setText(channel.getTitle() + " ("
816 + channel.getUnreadItemCount() + ")");
817 component.setToolTipText(channel.getURL());
823 class ItemsRenderer extends DefaultListCellRenderer
825 private final static Icon _icon = new ImageIcon(
826 Main.class.getResource("image/ComposeMail16.gif"));
828 public Component getListCellRendererComponent(JList list, Object value,
829 int index, boolean isSelected, boolean cellHasFocus)
831 JLabel component = (JLabel) super.getListCellRendererComponent(list,
832 value, index, isSelected, cellHasFocus);
833 component.setIcon(ItemsRenderer._icon);
834 if (value instanceof Item)
836 Item item = (Item) value;
837 if (item.getPublishDate() != null)
839 component.setText(DateFormat.getDateInstance().format(
840 item.getPublishDate())
841 + " " + item.getTitle());
843 component.setToolTipText(item.getLink());
846 component.setFont(component.getFont().deriveFont(Font.BOLD));
850 component.setFont(component.getFont().deriveFont(Font.PLAIN));