JAL-3949 - refactor logging from jalview.bin.Cache to jalview.bin.Console
[jalview.git] / src / jalview / gui / BlogReader.java
index 989d6c9..3dc51ce 100644 (file)
@@ -1,10 +1,32 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.gui;
 
 import jalview.bin.Cache;
-import jalview.jbgui.GBlogReader;
+import jalview.bin.Console;
+import jalview.util.MessageManager;
 
 import java.awt.BorderLayout;
 import java.awt.Component;
+import java.awt.Dialog.ModalExclusionType;
 import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.Rectangle;
@@ -20,6 +42,7 @@ import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
+import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -32,6 +55,7 @@ import javax.swing.DefaultListModel;
 import javax.swing.Icon;
 import javax.swing.ImageIcon;
 import javax.swing.JButton;
+import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JList;
 import javax.swing.JMenuItem;
@@ -66,7 +90,7 @@ import org.robsite.jswingreader.ui.util.ContextMenuMouseAdapter;
  * org.robsite.jswingreader.ui.MainWindow class
  */
 
-public class BlogReader extends GBlogReader
+public class BlogReader extends JPanel
 {
   private JButton buttonRefresh = new JButton();
 
@@ -106,7 +130,12 @@ public class BlogReader extends GBlogReader
     @Override
     public void actionPerformed(ActionEvent arg0)
     {
-      setVisible(false);
+      if (xf != null)
+      {
+        xf.dispose();
+      }
+      xf = null;
+      jd = null;
       if (parent != null)
       {
         parent.showNews(false);
@@ -156,6 +185,43 @@ public class BlogReader extends GBlogReader
     }
   };
 
+  private JFrame xf = null;
+
+  private JalviewDialog jd = null;
+
+  private JalviewDialog createDialog()
+  {
+
+    return jd = new JalviewDialog()
+    {
+
+      @Override
+      protected void raiseClosed()
+      {
+        if (parent != null)
+        {
+          Console.debug("News window closed.");
+          jd = null;
+          parent.showNews(false);
+        }
+      }
+
+      @Override
+      protected void okPressed()
+      {
+        // TODO Auto-generated method stub
+
+      }
+
+      @Override
+      protected void cancelPressed()
+      {
+        // TODO Auto-generated method stub
+
+      }
+    };
+  }
+
   private JLabel lblChannels = new JLabel();
 
   private List _updatableActions = new ArrayList();
@@ -182,31 +248,17 @@ public class BlogReader extends GBlogReader
 
   public BlogReader(Desktop desktop)
   {
+    Console.debug("Constructing news reader.");
+
     parent = desktop;
-    if (parent == null)
-    {
-      this.setSize(new Dimension(550, 350));
-    }
-    else
-    {
-      Rectangle bounds = parent
-              .getLastKnownDimensions("JALVIEW_RSS_WINDOW_");
-      if (bounds == null)
-      {
-        setBounds(parent.getX(), parent.getY(), 550, 350);
-      }
-      else
-      {
-        setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
-      }
-    }
     _channelModel = new ChannelListModel();
     // Construct our jalview news channel
     Channel chan = new Channel();
-    chan.setURL(jalview.bin.Cache.getDefault(
-            "JALVIEW_NEWS_RSS",
-            jalview.bin.Cache.getDefault("www.jalview.org",
-                    "http://www.jalview.org") + "/feeds/desktop/rss"));
+    chan.setURL(
+            Cache.getDefault("JALVIEW_NEWS_RSS",
+                    Cache.getDefault("www.jalview.org",
+                            "https://www.jalview.org")
+                            + "/feeds/desktop/rss"));
     loadLastM();
     _channelModel.addChannel(chan);
     updating = true;
@@ -222,25 +274,78 @@ public class BlogReader extends GBlogReader
     initItems(chan);
     updating = false;
     boolean setvisible = checkForNew(chan, true);
+
     if (setvisible)
     {
-      Cache.log.info("Will show jalview news automatically");
-      SwingUtilities.invokeLater(new Runnable()
+
+      Console.debug("Will show jalview news automatically");
+      showNews();
+    }
+    Console.debug("Completed construction of reader.");
+
+  }
+
+  /**
+   * check if the news panel's container is visible
+   */
+  @Override
+  public boolean isVisible()
+  {
+    if (parent == null)
+    {
+      return xf != null && xf.isVisible();
+    }
+    return jd != null && jd.isVisible();
+  }
+
+  /**
+   * display the container for the news panel
+   */
+  public void showNews()
+  {
+    final BlogReader me = this;
+    SwingUtilities.invokeLater(new Runnable()
+    {
+      @Override
+      public void run()
       {
-        @Override
-        public void run()
+        Rectangle bounds = new Rectangle(5, 5, 550, 350);
+        if (parent == null)
         {
-          if (parent != null)
-          {
-            parent.showNews(true);
-          }
-          else
+          xf = new JFrame();
+          xf.setContentPane(me);
+          xf.addWindowListener(new WindowAdapter()
           {
-            setVisible(true);
-          }
+            @Override
+            public void windowClosing(WindowEvent e)
+            {
+              ActionEvent actionEvent = new ActionEvent(this,
+                      ActionEvent.ACTION_FIRST,
+                      (String) exitAction.getValue(Action.NAME));
+              exitAction.actionPerformed(actionEvent);
+            }
+
+            @Override
+            public void windowOpened(WindowEvent e)
+            {
+            }
+          });
+          me.setSize(new Dimension(550, 350));
+          xf.setVisible(true);
         }
-      });
-    }
+        else
+        {
+          createDialog();
+          bounds = new Rectangle(5, 5, 550, 350);
+          jd.initDialogFrame(me, false, false,
+                  MessageManager.getString("label.news_from_jalview"),
+                  bounds.width, bounds.height);
+          jd.frame.setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
+          Console.debug("Displaying news.");
+          jd.waitForInput();
+        }
+      }
+    });
   }
 
   /**
@@ -248,22 +353,28 @@ public class BlogReader extends GBlogReader
    */
   private boolean checkForNew(Channel chan, boolean updateItems)
   {
-    
+
     if (!updating || updateItems)
     {
       newsnew = false;
     }
-    java.util.Date earliest=null;
-    try {
-      earliest = new SimpleDateFormat("YYYY-MM-DD").parse(chan.getHTTPLastModified());
-    } catch (Exception x) {};
+    java.util.Date earliest = null;
+    try
+    {
+      earliest = new SimpleDateFormat("YYYY-MM-DD")
+              .parse(chan.getHTTPLastModified());
+    } catch (Exception x)
+    {
+    }
     if (chan != null && chan.getItems() != null)
     {
-      Cache.log.debug("Scanning news items: newsnew="+newsnew+" and lastDate is "+lastDate);
+      Console.debug("Scanning news items: newsnew=" + newsnew
+              + " and lastDate is " + lastDate);
       for (Item i : (List<Item>) chan.getItems())
       {
-        boolean isread = lastDate==null ? false : (i.getPublishDate() != null && !lastDate
-                .before(i.getPublishDate()));
+        Date published = i.getPublishDate();
+        boolean isread = lastDate == null ? false
+                : (published != null && !lastDate.before(published));
 
         if (!updating || updateItems)
         {
@@ -273,18 +384,18 @@ public class BlogReader extends GBlogReader
         {
           i.setRead(isread);
         }
-        if (i.getPublishDate()!=null && !i.isRead())
+        if (published != null && !i.isRead())
         {
-          if (earliest==null || earliest.after(i.getPublishDate()))
+          if (earliest == null || earliest.after(published))
           {
-            earliest=i.getPublishDate();
+            earliest = published;
           }
         }
       }
     }
-    if (!updateItems && !updating && lastDate==null)
+    if (!updateItems && !updating && lastDate == null)
     {
-      lastDate=earliest;
+      lastDate = earliest;
     }
     return newsnew;
   }
@@ -302,31 +413,28 @@ public class BlogReader extends GBlogReader
     {
       if (item.getPublishDate() != null)
       {
-        if (lastDate==null || item.getPublishDate().after(lastDate))
+        if (lastDate == null || item.getPublishDate().after(lastDate))
         {
           lastDate = item.getPublishDate();
         }
       }
-      
+
       if (_channelModel.getElementAt(0) != null)
       {
         checkForNew((Channel) _channelModel.getElementAt(0), false);
       }
       if (lastDate != null)
       {
-        jalview.bin.Cache.setDateProperty(
-                "JALVIEW_NEWS_RSS_LASTMODIFIED", lastDate);
-        jalview.bin.Cache.log.info("Saved last read date as "
-                + jalview.bin.Cache.date_format.format(lastDate));
-
+        String formatted = Cache
+                .setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", lastDate);
+        Console.debug("Saved last read date as " + formatted);
       }
     }
   }
 
   private void jbInit() throws Exception
   {
-    this.setTitle("News from www.jalview.org");
-    this.getContentPane().setLayout(layoutMain);
+    setLayout(layoutMain);
     panelMain.setLayout(borderLayout1);
     topPanel.setLayout(borderLayout5);
     bottomPanel.setLayout(borderLayout4);
@@ -336,25 +444,28 @@ public class BlogReader extends GBlogReader
     topBottomSplitPane.setBottomComponent(bottomPanel);
     JScrollPane spTextDescription = new JScrollPane(textDescription);
     textDescription.setText("");
-    statusBar.setText(" [Status] ");
+    statusBar.setText(new StringBuffer("[")
+            .append(MessageManager.getString("label.status")).append("]")
+            .toString());
     buttonRefresh.addActionListener(new ActionListener()
     {
-      
+
       @Override
       public void actionPerformed(ActionEvent e)
       {
         refreshNews();
       }
     });
-    this.getContentPane().add(statusBar, BorderLayout.SOUTH);
+    add(statusBar, BorderLayout.SOUTH);
     toolBar.add(buttonRefresh);
     toolBar.addSeparator();
-    JLabel about=new JLabel("brought to you by JSwingReader (jswingreader.sourceforge.net)");
+    JLabel about = new JLabel(
+            "brought to you by JSwingReader (jswingreader.sourceforge.net)");
     toolBar.add(about);
     toolBar.setFloatable(false);
-    this.getContentPane().add(toolBar, BorderLayout.NORTH);
+    add(toolBar, BorderLayout.NORTH);
     panelMain.add(topBottomSplitPane, BorderLayout.CENTER);
-    this.getContentPane().add(panelMain, BorderLayout.CENTER);
+    add(panelMain, BorderLayout.CENTER);
     JScrollPane spListItems = new JScrollPane(listItems);
     listItems
             .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
@@ -364,6 +475,7 @@ public class BlogReader extends GBlogReader
 
     listItems.addMouseListener(new java.awt.event.MouseAdapter()
     {
+      @Override
       public void mouseClicked(MouseEvent e)
       {
         listItems_mouseClicked(e);
@@ -377,7 +489,7 @@ public class BlogReader extends GBlogReader
             _popupChannels);
     listItems.addMouseListener(popupAdapter);
     listItems.setCellRenderer(new ItemsRenderer());
-    lblChannels.setText("Channels");
+    lblChannels.setText(MessageManager.getString("label.channels"));
   }
 
   private void postInit()
@@ -389,6 +501,7 @@ public class BlogReader extends GBlogReader
     }
     textDescription.addHyperlinkListener(new HyperlinkListener()
     {
+      @Override
       public void hyperlinkUpdate(HyperlinkEvent e)
       {
         if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
@@ -398,23 +511,9 @@ public class BlogReader extends GBlogReader
       }
     });
 
-    this.addWindowListener(new WindowAdapter()
-    {
-      public void windowClosing(WindowEvent e)
-      {
-        ActionEvent actionEvent = new ActionEvent(this,
-                ActionEvent.ACTION_FIRST, (String) exitAction
-                        .getValue(Action.NAME));
-        exitAction.actionPerformed(actionEvent);
-      }
-
-      public void windowOpened(WindowEvent e)
-      {
-      }
-    });
-
     listItems.addListSelectionListener(new ListSelectionListener()
     {
+      @Override
       public void valueChanged(ListSelectionEvent e)
       {
         if (e.getValueIsAdjusting() == false)
@@ -431,19 +530,20 @@ public class BlogReader extends GBlogReader
     _itemsValueChanged(listItems);
   }
 
-  public class LaunchJvBrowserOnItem extends AbstractAction implements
-          UpdatableAction
+  public class LaunchJvBrowserOnItem extends AbstractAction
+          implements UpdatableAction
   {
     JList _listItems = null;
 
     public LaunchJvBrowserOnItem(JList listItems)
     {
       super("Open in Browser");
-      this.putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_O));
+      this.putValue(MNEMONIC_KEY, Integer.valueOf(KeyEvent.VK_O));
       this.putValue(Action.LONG_DESCRIPTION, "Open in Browser");
       _listItems = listItems;
     }
 
+    @Override
     public void actionPerformed(ActionEvent e)
     {
       Object o = _listItems.getSelectedValue();
@@ -457,6 +557,7 @@ public class BlogReader extends GBlogReader
       }
     }
 
+    @Override
     public void update(Object o)
     {
       setEnabled(true);
@@ -485,7 +586,8 @@ public class BlogReader extends GBlogReader
   private JPopupMenu _buildChannelsPopupMenu()
   {
     JPopupMenu popup = new JPopupMenu();
-    popup.add(new JMenuItem(new MarkChannelAsRead(listChannels, listItems)));
+    popup.add(
+            new JMenuItem(new MarkChannelAsRead(listChannels, listItems)));
     popup.add(new JMenuItem(
             new MarkChannelAsUnread(listChannels, listItems)));
     return popup;
@@ -509,8 +611,9 @@ public class BlogReader extends GBlogReader
     }
     DefaultListModel itemsModel = (DefaultListModel) listItems.getModel();
     itemsModel.clear();
-    Iterator iter = (channel.getItems() != null) ? channel.getItems()
-            .iterator() : Collections.EMPTY_LIST.iterator();
+    Iterator iter = (channel.getItems() != null)
+            ? channel.getItems().iterator()
+            : Collections.EMPTY_LIST.iterator();
     while (iter.hasNext())
     {
       itemsModel.addElement(iter.next());
@@ -606,8 +709,8 @@ public class BlogReader extends GBlogReader
           button.setVerticalTextPosition(AbstractButton.BOTTOM);
           button.setHorizontalTextPosition(AbstractButton.CENTER);
         }
-        else if (Boolean.toString(true).equals(
-                general.get("radioTextRight")))
+        else if (Boolean.toString(true)
+                .equals(general.get("radioTextRight")))
         {
           button.setVerticalTextPosition(AbstractButton.CENTER);
           button.setHorizontalTextPosition(AbstractButton.RIGHT);
@@ -640,65 +743,71 @@ public class BlogReader extends GBlogReader
    */
   public void refreshNews()
   {
-    try {
-      initItems((Channel)_channelModel.getElementAt(0));
-      
+    try
+    {
+      initItems((Channel) _channelModel.getElementAt(0));
+
     } catch (Exception x)
-    {}
+    {
+    }
   }
 
+  /**
+   * @j2sIgnore
+   * @param args
+   */
   public static void main(String args[])
   {
     // this tests the detection of new news based on the last read date stored
     // in jalview properties
-    jalview.bin.Cache.loadProperties(null);
-    jalview.bin.Cache.initLogger();
+    Cache.loadProperties(null);
+    Console.initLogger();
     // test will advance read date each time
-    Calendar today = Calendar.getInstance(), lastread = Calendar
-            .getInstance();
+    Calendar today = Calendar.getInstance(),
+            lastread = Calendar.getInstance();
     lastread.set(1983, 01, 01);
     while (lastread.before(today))
     {
-      Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED",
-              lastread.getTime());
+      String formattedDate = Cache.setDateProperty(
+              "JALVIEW_NEWS_RSS_LASTMODIFIED", lastread.getTime());
       BlogReader me = new BlogReader();
-      System.out.println("Set last date to "
-              + jalview.bin.Cache.date_format.format(lastread.getTime()));
+      System.out.println("Set last date to " + formattedDate);
       if (me.isNewsNew())
       {
-        Cache.log.info("There is news to read.");
+        Console.debug("There is news to read.");
       }
       else
       {
-        Cache.log.info("There is no new news.");
+        Console.debug("There is no new news.");
+        me.xf.setTitle("Testing : Last read is " + me.lastDate);
+        me.showNews();
+        me.xf.toFront();
       }
-      me.setTitle("Testing : Last read is " + me.lastDate);
-      me.setVisible(true);
-      me.toFront();
-      while (me.isVisible())
+      Console.debug("Waiting for closure.");
+      do
       {
         try
         {
-          Thread.sleep(100);
+          Thread.sleep(300);
         } catch (InterruptedException x)
         {
         }
-        ;
-      }
+      } while (me.isVisible());
+
       if (me.isNewsNew())
       {
-        Cache.log.info("Still new news after reader displayed.");
+        Console.debug("Still new news after reader displayed.");
       }
       if (lastread.getTime().before(me.lastDate))
       {
-        Cache.log.info("The news was read.");
+        Console.debug("The news was read.");
         lastread.setTime(me.lastDate);
       }
       else
       {
         lastread.add(Calendar.MONTH, 1);
       }
-      me.dispose();
+
     }
   }
 
@@ -713,6 +822,7 @@ class ChannelsRenderer extends DefaultListCellRenderer
   private final static Icon _icon = new ImageIcon(
           Main.class.getResource("image/ComposeMail16.gif"));
 
+  @Override
   public Component getListCellRendererComponent(JList list, Object value,
           int index, boolean isSelected, boolean cellHasFocus)
   {
@@ -722,8 +832,10 @@ class ChannelsRenderer extends DefaultListCellRenderer
     if (value instanceof Channel)
     {
       Channel channel = (Channel) value;
-      component.setText(channel.getTitle() + " ("
-              + channel.getUnreadItemCount() + ")");
+      component.setText(MessageManager
+              .formatMessage("label.channel_title_item_count", new String[]
+              { channel.getTitle(), Integer
+                      .valueOf(channel.getUnreadItemCount()).toString() }));
       component.setToolTipText(channel.getURL());
     }
     return component;
@@ -735,6 +847,7 @@ class ItemsRenderer extends DefaultListCellRenderer
   private final static Icon _icon = new ImageIcon(
           Main.class.getResource("image/ComposeMail16.gif"));
 
+  @Override
   public Component getListCellRendererComponent(JList list, Object value,
           int index, boolean isSelected, boolean cellHasFocus)
   {
@@ -746,9 +859,13 @@ class ItemsRenderer extends DefaultListCellRenderer
       Item item = (Item) value;
       if (item.getPublishDate() != null)
       {
-        component.setText(DateFormat.getDateInstance().format(
-                item.getPublishDate())
-                + " " + item.getTitle());
+        component.setText(MessageManager.formatMessage(
+                "label.blog_item_published_on_date", new String[]
+                { DateFormat
+                        .getDateInstance(DateFormat.LONG,
+                                MessageManager.getLocale())
+                        .format(item.getPublishDate()).toString(),
+                    item.getTitle() }));
       }
       component.setToolTipText(item.getLink());
       if (!item.isRead())