File Source: PingHistoryPanel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.lagdisplay.PingHistoryPanel__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.lagdisplay;
    24  
    25  import com.dmdirc.Main;
    26  import com.dmdirc.ServerManager;
    27  import com.dmdirc.util.RollingList;
    28  import java.awt.Color;
    29  import java.awt.Dimension;
    30  import java.awt.Graphics;
    31  import java.awt.Graphics2D;
    32  import java.awt.Rectangle;
    33  import java.awt.geom.Rectangle2D;
    34  import java.util.ArrayList;
    35  import java.util.List;
    36  import javax.swing.JPanel;
    37  
    38  /**
    39   * Shows a ping history graph for the current server.
    40   *
    41   * @author chris
    42   */
    43  public class PingHistoryPanel extends JPanel {
    44  
    45      /**
    46       * A version number for this class. It should be changed whenever the class
    47       * structure is changed (or anything else that would prevent serialized
    48       * objects being unserialized with the new class).
    49       */
    50      private static final long serialVersionUID = 1;
    51  
    52      /** The plugin that this panel is for. */
    53      protected final LagDisplayPlugin plugin;
    54  
    55      /** The history that we're graphing. */
    56      protected final RollingList<Long> history;
    57  
    58      /** The maximum ping value. */
    59      protected long maximum = 0l;
    60  
    61      /**
    62       * Creates a new history panel for the specified plugin.
    63       *
    64       * @param plugin The plugin that owns this panel
    65       */
    66      public PingHistoryPanel(final LagDisplayPlugin plugin) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.lagdisplay.PingHistoryPanel(LagDisplayPlugin)
                  * 
                  *  Preconditions:
                  *    plugin != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.Main:getUI(...)@74 != null
                  *    com.dmdirc.ServerManager:getServerManager(...)@74 != null
                  *    com.dmdirc.addons.lagdisplay.LagDisplayPlugin:getHistory(...)@74 != null
                  *    com.dmdirc.util.RollingList:getList(...)@77 != null
                  *    java.util.Iterator:next(...)@77 != null
                  * 
                  *  Postconditions:
                  *    this.history != null
                  *    init'ed(this.maximum)
                  *    this.plugin == plugin
                  *    this.plugin != null
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@77: {0}, {1}
                  */
    67          super();
    68  
    69          setMinimumSize(new Dimension(50, 100));
    70          setMaximumSize(new Dimension(Integer.MAX_VALUE, 100));
    71          setOpaque(false);
    72          
    73          this.plugin = plugin;
    74          this.history = plugin.getHistory(ServerManager.getServerManager()
    75                  .getServerFromFrame(Main.getUI().getActiveWindow()));
    76  
    77          for (Long value : history.getList()) {
    78              maximum = Math.max(value, maximum);
    79          }
    80      }
    81  
    82      /** {@inheritDoc} */
    83      @Override
    84      public void paint(final Graphics g) {
                 /* 
    P/P           *  Method: void paint(Graphics)
                  * 
                  *  Preconditions:
                  *    (float) (this.maximum) != +0
                  *    g != null
                  *    this.history != null
                  *    init'ed(this.maximum)
                  *    (soft) this.plugin != null
                  * 
                  *  Presumptions:
                  *    (float) (com.dmdirc.addons.lagdisplay.PingHistoryPanel:getHeight(...)@95 - 10)/(float) (this.maximum) != +0
                  *    (float) (com.dmdirc.addons.lagdisplay.PingHistoryPanel:getWidth(...)@93 - 3)/(float) (1) != +0
                  *    (int) ((java.awt.geom.Rectangle2D:getHeight(...)@126/2 + lastY) - 1) in {-231..232-1}
                  *    (int) (-((float) (java.lang.Long:longValue(...)@109)*((float) (com.dmdirc.addons.lagdisplay.PingHistoryPanel:getHeight(...)@95 - 10)/(float) (this.maximum)) - (float) (com.dmdirc.addons.lagdisplay.PingHistoryPanel:getHeight(...)@109 - 5))) in {-231+1..232-1}
                  *    (int) (-((java.awt.geom.Rectangle2D:getHeight(...)@123 + 10)/((float) (com.dmdirc.addons.lagdisplay.PingHistoryPanel:getHeight(...)@95 - 10)/(float) (this.maximum)) - (float) (last1))) in {-231..232-1}
                  *    ...
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.addons.lagdisplay.LagDisplayPlugin:shouldShowLabels(...)@117: {0}, {1}
                  *    com.dmdirc.util.RollingList:isEmpty(...)@97: {0}, {1}
                  *    java.awt.Rectangle:intersects(...)@156: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@155: {0}, {1}
                  */
    85          super.paint(g);
    86          
    87          g.setColor(Color.DARK_GRAY);
    88          g.drawLine(2, 1, 2, getHeight() - 1);
    89          g.drawLine(1, getHeight() - 2, getWidth() - 1, getHeight() - 2);
    90          g.setFont(g.getFont().deriveFont(10f));
    91  
    92          float lastX = -1, lastY = -1;
    93          float pixelsperpointX = (getWidth() - 3) / (float) (history.getList().size() == 1 ? 1
    94                  : history.getList().size() - 1);
    95          float pixelsperpointY = (getHeight() - 10) / (float) maximum;
    96  
    97          if (history.isEmpty()) {
    98              g.drawString("No data", getWidth() / 2 - 25, getHeight() / 2 + 5);
    99          }
   100  
   101          long last1 = -1, last2 = -1;
   102          final List<Long> list = history.getList();
   103          final List<Rectangle> rects = new ArrayList<Rectangle>();
   104  
   105          for (int i = 0; i < list.size(); i++) {
   106              final Long value = list.get(i);
   107             
   108              float x = lastX == -1 ? 2 : lastX + pixelsperpointX;
   109              float y = getHeight() - 5 - value * pixelsperpointY;
   110  
   111              if (lastX > -1) {
   112                  g.drawLine((int) lastX, (int) lastY, (int) x, (int) y);
   113              }
   114  
   115              g.drawRect((int) x - 1, (int) y - 1, 2, 2);
   116  
   117              if (plugin.shouldShowLabels() && last1 > -1 && (last2 <= last1 || last1 >= value)) {
   118                  final String text = plugin.formatTime(last1);
   119                  final Rectangle2D rect = g.getFont().getStringBounds(text,
   120                          ((Graphics2D) g).getFontRenderContext());
   121                  final int width = 10 + (int) rect.getWidth();
   122                  final int points = (int) Math.ceil(width / pixelsperpointX);
   123                  final int diffy = (int) (last1 - (10 + rect.getHeight()) / pixelsperpointY);
   124  
   125                  float posX = lastX - width + 7;
   126                  float posY = (float) (lastY + rect.getHeight() / 2) - 1;
   127                  boolean failed = posX < 0;
   128  
   129                  // Check left
   130                  for (int j = Math.max(0, i - points); j < i - 1; j++) {
   131                      if (list.get(j) > diffy) {
   132                          failed = true;
   133                          break;
   134                      }
   135                  }
   136  
   137                  if (failed) {
   138                      posX = lastX + 3;
   139                      failed = posX + width > getWidth();
   140  
   141                      // Check right
   142                      for (int j = i; j < Math.min(list.size(), i + points); j++) {
   143                          if (list.get(j) > diffy) {
   144                              failed = true;
   145                              break;
   146                          }
   147                      }
   148                  }
   149  
   150                  if (!failed) {
   151                      final Rectangle myrect = new Rectangle((int) posX - 2,
   152                              (int) lastY - 7, 5 + (int) rect.getWidth(),
   153                              3 + (int) rect.getHeight());
   154  
   155                      for (Rectangle test : rects) {
   156                          if (test.intersects(myrect)) {
   157                              failed = true;
   158                          }
   159                      }
   160  
   161                      if (!failed) {
   162                          g.drawString(text, (int) posX, (int) posY);
   163                          rects.add(myrect);
   164                      }
   165                  }
   166              }
   167  
   168              lastX = x;
   169              lastY = y;
   170  
   171              last2 = last1;
   172              last1 = value;
   173          }
   174      }
   175  
   176  }








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