You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PingHistoryPanel.java 6.0KB

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