File Source: OsdWindow.java

         /* 
    P/P   *  Method: com.dmdirc.addons.osd.OsdWindow$1__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.addons.osd;
    24  
    25  import com.dmdirc.Main;
    26  import com.dmdirc.addons.ui_swing.MainFrame;
    27  import com.dmdirc.config.IdentityManager;
    28  import com.dmdirc.ui.messages.ColourManager;
    29  
    30  import java.awt.Color;
    31  import java.awt.Point;
    32  import java.awt.event.MouseEvent;
    33  import java.awt.event.MouseListener;
    34  import java.awt.event.MouseMotionListener;
    35  import java.util.ArrayList;
    36  import java.util.List;
    37  import java.util.Timer;
    38  import java.util.TimerTask;
    39  
    40  import javax.swing.JDialog;
    41  import javax.swing.JLabel;
    42  import javax.swing.JPanel;
    43  import javax.swing.SwingConstants;
    44  import javax.swing.WindowConstants;
    45  import javax.swing.border.LineBorder;
    46  
    47  import net.miginfocom.swing.MigLayout;
    48  
    49  /**
    50   * The OSD Window is an always-on-top window designed to convey information
    51   * about events to the user.
    52   * @author chris
    53   */
    54  public final class OsdWindow extends JDialog implements MouseListener,
    55          MouseMotionListener {
    56      
    57      /**
    58       * A version number for this class. It should be changed whenever the class
    59       * structure is changed (or anything else that would prevent serialized
    60       * objects being unserialized with the new class).
    61       */
    62      private static final long serialVersionUID = 2;
    63      
    64      /** Gap between vertically stacked windows. */
    65      private static final int WINDOW_GAP = 5;
    66      
    67      /** A list of open OSD windows. */
             /* 
    P/P       *  Method: com.dmdirc.addons.osd.OsdWindow__static_init
              * 
              *  Postconditions:
              *    windows == &new ArrayList(OsdWindow__static_init#1)
              *    new ArrayList(OsdWindow__static_init#1) num objects == 1
              */
    68      private static List<OsdWindow> windows = new ArrayList<OsdWindow>();
    69  
    70      /** Plugin this window belongs to. */
    71      private final OsdPlugin plugin;
    72      
    73      /** OSD Label. */
    74      private final JLabel label;
    75      
    76      /** OSD Panel. */
    77      private final JPanel panel;
    78      
    79      /** Starting positions of the mouse. */
    80      private int startX, startY;
    81      
    82      /** Is this a config instance? */
    83      private final boolean config;
    84  
    85      /**
    86       * Creates a new instance of OsdWindow.
    87       * 
    88       * @param text The text to be displayed in the OSD window
    89       * @param config Is the window being configured (should it timeout and
    90       * allow itself to be moved)
    91       * @param plugin The plugin that owns this window
    92       */    
    93      public OsdWindow(final String text, final boolean config, final OsdPlugin plugin) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.osd.OsdWindow(String, bool, OsdPlugin)
                  * 
                  *  Preconditions:
                  *    plugin != null
                  *    (soft) windows != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@94 != null
                  * 
                  *  Postconditions:
                  *    this.config == config
                  *    init'ed(this.config)
                  *    this.label == &amp;new JLabel(OsdWindow#5)
                  *    this.panel == &amp;new JPanel(OsdWindow#1)
                  *    this.plugin == plugin
                  *    this.plugin != null
                  *    new JLabel(OsdWindow#5) num objects == 1
                  *    new JPanel(OsdWindow#1) num objects == 1
                  */
    94          this(text, config, IdentityManager.getGlobalConfig()
    95                  .getOptionInt(plugin.getDomain(), "locationX"), getYPosition(plugin), plugin);
    96      }
    97  
    98      /**
    99       * Creates a new instance of OsdWindow.
   100       *
   101       * @param text The text to be displayed in the OSD window
   102       * @param config Is the window being configured (should it timeout and
   103       * allow itself to be moved)
   104       * @param x The x-axis position for the OSD Window
   105       * @param y The y-axis position for the OSD window
   106       * @param plugin Parent OSD Plugin
   107       */
   108      public OsdWindow(final String text, final boolean config, final int x,
   109              final int y, final OsdPlugin plugin) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.osd.OsdWindow(String, bool, int, int, OsdPlugin)
                  * 
                  *  Preconditions:
                  *    plugin != null
                  *    (soft) windows != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Main:getUI(...)@110 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@126 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@133 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@135 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@149 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    this.config == config
                  *    init'ed(this.config)
                  *    this.label == &amp;new JLabel(OsdWindow#5)
                  *    this.panel == &amp;new JPanel(OsdWindow#1)
                  *    this.plugin == plugin
                  *    this.plugin != null
                  *    new JLabel(OsdWindow#5) num objects == 1
                  *    new JPanel(OsdWindow#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    config: {0}, {1}
                  */
   110          super((MainFrame) Main.getUI().getMainWindow(), false);
   111  
   112          this.plugin = plugin;
   113          this.config = config;
   114  
   115          setFocusableWindowState(false);
   116          setAlwaysOnTop(true);
   117          setResizable(false);
   118          setUndecorated(true);
   119  
   120          setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
   121  
   122          setLocation(x, y);
   123  
   124          panel = new JPanel();
   125          panel.setBorder(new LineBorder(Color.BLACK));
   126          panel.setBackground(IdentityManager.getGlobalConfig().getOptionColour(plugin.getDomain(),
   127                  "bgcolour"));
   128  
   129          setContentPane(panel);
   130          setLayout(new MigLayout("wmin 500, wmax 500, ins rel, fill"));
   131  
   132          label = new JLabel(text);
   133          label.setForeground(IdentityManager.getGlobalConfig().getOptionColour(plugin.getDomain(),
   134                  "fgcolour"));
   135          label.setFont(label.getFont().deriveFont(
   136                  (float) IdentityManager.getGlobalConfig().getOptionInt(plugin.getDomain(),
   137                  "fontSize")));
   138          label.setHorizontalAlignment(SwingConstants.CENTER);
   139              add(label, "alignx center");
   140  
   141          setVisible(true);
   142          pack();
   143  
   144          if (config) {
   145              this.addMouseMotionListener(this);
   146              this.addMouseListener(this);
   147          } else {
   148              addMouseListener(this);
                     /* 
    P/P               *  Method: void com.dmdirc.addons.osd.OsdWindow$1(OsdWindow)
                      */
   149              new Timer("OSD Display Timer").schedule(new TimerTask() {
   150                  
   151                  /** {@inheritDoc} */
   152                  @Override
   153                  public void run() {
                             /* 
    P/P                       *  Method: void run()
                              * 
                              *  Preconditions:
                              *    (soft) com/dmdirc/addons/osd/OsdWindow.windows != null
                              */
   154                      setVisible(false);
   155                      dispose();
   156                  }
   157              }, IdentityManager.getGlobalConfig().getOptionInt(plugin.getDomain(),
   158                      "timeout") * 1000);
   159          }
   160      }
   161      
   162      /**
   163       * Retrieves the y-axis position for a new OSD window, based on the user's
   164       * configured policy.
   165       * 
   166       * @return The y-axis position for a new OSD window.
   167       */
   168      private static int getYPosition(final OsdPlugin plugin) {
                 /* 
    P/P           *  Method: int getYPosition(OsdPlugin)
                  * 
                  *  Preconditions:
                  *    plugin != null
                  *    (soft) windows != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.osd.OsdWindow:getHeight(...)@185 - com.dmdirc.addons.osd.OsdWindow:getY(...)@185 in {-4_294_967_300..231-5}
                  *    com.dmdirc.addons.osd.OsdWindow:getY(...)@178 + com.dmdirc.addons.osd.OsdWindow:getHeight(...)@178 in {-2_147_483_653..232-6}
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@169 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@171 != null
                  *    java.util.ArrayList:iterator(...)@176 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.addons.osd.OsdWindow:isVisible(...)@177: {0}, {1}
                  *    com.dmdirc.addons.osd.OsdWindow:isVisible(...)@184: {0}, {1}
                  *    java.lang.String:equals(...)@174: {0}, {1}
                  *    java.lang.String:equals(...)@181: {0}, {1}
                  *    java.lang.String:equals(...)@188: {0}, {1}
                  */
   169          final String policy = IdentityManager.getGlobalConfig()
   170                  .getOption(plugin.getDomain(), "newbehaviour");
   171          int y = IdentityManager.getGlobalConfig().getOptionInt(plugin.getDomain(),
   172                  "locationY");
   173          
   174          if ("down".equals(policy)) {
   175              // Place our new window below old windows
   176              for (OsdWindow window : new ArrayList<OsdWindow>(windows)) {
   177                  if (window.isVisible()) {
   178                      y = Math.max(y, window.getY() + window.getHeight() + WINDOW_GAP);
   179                  }
   180              }
   181          } else if ("up".equals(policy)) {
   182              // Place our new window above old windows
   183              for (OsdWindow window : new ArrayList<OsdWindow>(windows)) {
   184                  if (window.isVisible()) {
   185                      y = Math.min(y, window.getY() - window.getHeight() - WINDOW_GAP);
   186                  }
   187              }            
   188          } else if ("close".equals(policy)) {
   189              // Close existing windows and use their place
   190              closeAll();
   191          }        
   192          
   193          return y;
   194      }
   195      
   196      /**
   197       * Closes all open OSD windows.
   198       */
   199      protected static void closeAll() {
                 /* 
    P/P           *  Method: void closeAll()
                  * 
                  *  Preconditions:
                  *    (soft) windows != null
                  * 
                  *  Presumptions:
                  *    java.util.ArrayList:iterator(...)@200 != null
                  *    java.util.Iterator:next(...)@200 != null
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@200: {0}, {1}
                  */
   200          for (OsdWindow window : new ArrayList<OsdWindow>(windows)) {
   201              window.setVisible(false);
   202              window.dispose();
   203          }        
   204      }
   205      
   206      /** 
   207       * {@inheritDoc}
   208       * 
   209       * @param e Mouse event
   210       */
   211      @Override
   212      public void mouseClicked(final MouseEvent e) {
                 /* 
    P/P           *  Method: void mouseClicked(MouseEvent)
                  * 
                  *  Preconditions:
                  *    (soft) windows != null
                  * 
                  *  Test Vectors:
                  *    this.config: {1}, {0}
                  */
   213          if (!config) {
   214              setVisible(false);
   215              dispose();
   216          }
   217      }
   218      
   219      /** 
   220       * {@inheritDoc}
   221       * 
   222       * @param e Mouse event
   223       */
   224      @Override
   225      public void mousePressed(final MouseEvent e) {
                 /* 
    P/P           *  Method: void mousePressed(MouseEvent)
                  * 
                  *  Preconditions:
                  *    (soft) e != null
                  * 
                  *  Presumptions:
                  *    java.awt.event.MouseEvent:getPoint(...)@227 != null
                  *    java.awt.event.MouseEvent:getPoint(...)@228 != null
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.startX)
                  *    possibly_updated(this.startY)
                  * 
                  *  Test Vectors:
                  *    this.config: {0}, {1}
                  */
   226          if (config) {
   227              startX = e.getPoint().x;
   228              startY = e.getPoint().y;
   229          }
   230      }
   231      
   232      /** 
   233       * {@inheritDoc}
   234       * 
   235       * @param e Mouse event
   236       */
   237      @Override
   238      public void mouseReleased(final MouseEvent e) {
   239          // Do nothing
             /* 
    P/P       *  Method: void mouseReleased(MouseEvent)
              */
   240      }
   241      
   242      /** 
   243       * {@inheritDoc}
   244       * 
   245       * @param e Mouse event
   246       */
   247      @Override
   248      public void mouseEntered(final MouseEvent e) {
   249          // Do nothing
             /* 
    P/P       *  Method: void mouseEntered(MouseEvent)
              */
   250      }
   251      
   252      /** 
   253       * {@inheritDoc}
   254       * 
   255       * @param e Mouse event
   256       */
   257      @Override
   258      public void mouseExited(final MouseEvent e) {
   259          // Do nothing
             /* 
    P/P       *  Method: void mouseExited(MouseEvent)
              */
   260      }
   261      
   262      /** 
   263       * {@inheritDoc}
   264       * 
   265       * @param e Mouse event
   266       */
   267      @Override
   268      public void mouseDragged(final MouseEvent e) {
                 /* 
    P/P           *  Method: void mouseDragged(MouseEvent)
                  * 
                  *  Preconditions:
                  *    e != null
                  *    this.startX <= 231
                  *    this.startY <= 231
                  * 
                  *  Presumptions:
                  *    java.awt.event.MouseEvent:getLocationOnScreen(...)@269 != null
                  */
   269          final Point p = e.getLocationOnScreen();
   270          p.translate(-1 * startX, -1 * startY);
   271          setLocation(p);
   272      }
   273      
   274      /** 
   275       * {@inheritDoc}
   276       * 
   277       * @param e Mouse event
   278       */
   279      @Override
   280      public void mouseMoved(final MouseEvent e) {
   281          // Do nothing
             /* 
    P/P       *  Method: void mouseMoved(MouseEvent)
              */
   282      }
   283      
   284      /**
   285       * Sets the font size that this OSD uses.
   286       * 
   287       * @param size The new size of the font
   288       */
   289      public void setFontSize(final int size) {
                 /* 
    P/P           *  Method: void setFontSize(int)
                  * 
                  *  Preconditions:
                  *    this.label != null
                  * 
                  *  Presumptions:
                  *    javax.swing.JLabel:getFont(...)@290 != null
                  */
   290          label.setFont(label.getFont().deriveFont((float) size));
   291      }
   292  
   293      /**
   294       * Sets the background colour for this OSD.
   295       * 
   296       * @param colour The background colour to use
   297       */
   298      public void setBackgroundColour(final String colour) {
                 /* 
    P/P           *  Method: void setBackgroundColour(String)
                  * 
                  *  Preconditions:
                  *    this.panel != null
                  */
   299          panel.setBackground(ColourManager.parseColour(colour));
   300      }
   301  
   302      /**
   303       * Sets the foreground colour for this OSD.
   304       * 
   305       * @param colour The foreground colour to use
   306       */
   307      public void setForegroundColour(final String colour) {
                 /* 
    P/P           *  Method: void setForegroundColour(String)
                  * 
                  *  Preconditions:
                  *    this.label != null
                  */
   308          label.setForeground(ColourManager.parseColour(colour));
   309      }
   310  
   311      /** {@inheritDoc} */
   312      @Override
   313      public void setVisible(final boolean b) {
                 /* 
    P/P           *  Method: void setVisible(bool)
                  * 
                  *  Preconditions:
                  *    (soft) windows != null
                  * 
                  *  Test Vectors:
                  *    b: {0}, {1}
                  */
   314          super.setVisible(b);
   315          
   316          if (b) {
   317              windows.add(this);
   318              transferFocusBackward();
   319          }
   320      }
   321  
   322      /** {@inheritDoc} */
   323      @Override
   324      public void dispose() {
                 /* 
    P/P           *  Method: void dispose()
                  * 
                  *  Preconditions:
                  *    windows != null
                  */
   325          super.dispose();
   326          
   327          windows.remove(this);
   328      }
   329  }








SofCheck Inspector Build Version : 2.17854
OsdWindow.java 2009-Jun-25 01:54:24
OsdWindow.class 2009-Sep-02 17:04:15
OsdWindow$1.class 2009-Sep-02 17:04:15