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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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.addons.ui_swing.MainFrame;
  24. import com.dmdirc.util.collections.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. public class PingHistoryPanel extends JPanel {
  38. /**
  39. * A version number for this class. It should be changed whenever the class structure is changed
  40. * (or anything else that would prevent serialized objects being unserialized with the new
  41. * class).
  42. */
  43. private static final long serialVersionUID = 1;
  44. /** The manager that this panel is for. */
  45. protected final LagDisplayManager manager;
  46. /** The history that we're graphing. */
  47. protected final RollingList<Long> history;
  48. /** The maximum ping value. */
  49. protected long maximum;
  50. /**
  51. * Creates a new history panel for the specified plugin.
  52. *
  53. * @param manager The manager that owns this panel
  54. * @param mainFrame Swing main frame
  55. */
  56. public PingHistoryPanel(final LagDisplayManager manager,
  57. final MainFrame mainFrame) {
  58. super();
  59. setMinimumSize(new Dimension(50, 100));
  60. setMaximumSize(new Dimension(Integer.MAX_VALUE, 100));
  61. setOpaque(false);
  62. this.manager = manager;
  63. history = manager.getHistory(mainFrame.getActiveFrame().getContainer().getConnection());
  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;
  77. float lastY = -1;
  78. final float pixelsperpointX = (getWidth() - 3)
  79. / (float) (history.getList().size() == 1 ? 1
  80. : history.getList().size() - 1);
  81. final float pixelsperpointY = (getHeight() - 10) / (float) maximum;
  82. if (history.isEmpty()) {
  83. g.drawString("No data", getWidth() / 2 - 25, getHeight() / 2 + 5);
  84. }
  85. long last1 = -1;
  86. long last2 = -1;
  87. final List<Long> list = history.getList();
  88. final List<Rectangle> rects = new ArrayList<>();
  89. for (int i = 0; i < list.size(); i++) {
  90. final Long value = list.get(i);
  91. final float x = lastX == -1 ? 2 : lastX + pixelsperpointX;
  92. final float y = getHeight() - 5 - value * pixelsperpointY;
  93. if (lastX > -1) {
  94. g.drawLine((int) lastX, (int) lastY, (int) x, (int) y);
  95. }
  96. g.drawRect((int) x - 1, (int) y - 1, 2, 2);
  97. if (manager.shouldShowLabels() && last1 > -1 && (last2 <= last1 || last1 >= value)) {
  98. final String text = manager.formatTime(last1);
  99. final Rectangle2D rect = g.getFont().getStringBounds(text,
  100. ((Graphics2D) g).getFontRenderContext());
  101. final int width = 10 + (int) rect.getWidth();
  102. final int points = (int) Math.ceil(width / pixelsperpointX);
  103. final int diffy = (int) (last1 - (10 + rect.getHeight()) / pixelsperpointY);
  104. float posX = lastX - width + 7;
  105. final float posY = (float) (lastY + rect.getHeight() / 2) - 1;
  106. boolean failed = posX < 0;
  107. // Check left
  108. for (int j = Math.max(0, i - points); j < i - 1; j++) {
  109. if (list.get(j) > diffy) {
  110. failed = true;
  111. break;
  112. }
  113. }
  114. if (failed) {
  115. posX = lastX + 3;
  116. failed = posX + width > getWidth();
  117. // Check right
  118. for (int j = i; j < Math.min(list.size(), i + points); j++) {
  119. if (list.get(j) > diffy) {
  120. failed = true;
  121. break;
  122. }
  123. }
  124. }
  125. if (!failed) {
  126. final Rectangle myrect = new Rectangle((int) posX - 2,
  127. (int) lastY - 7, 5 + (int) rect.getWidth(),
  128. 3 + (int) rect.getHeight());
  129. for (Rectangle test : rects) {
  130. if (test.intersects(myrect)) {
  131. failed = true;
  132. }
  133. }
  134. if (!failed) {
  135. g.drawString(text, (int) posX, (int) posY);
  136. rects.add(myrect);
  137. }
  138. }
  139. }
  140. lastX = x;
  141. lastY = y;
  142. last2 = last1;
  143. last1 = value;
  144. }
  145. }
  146. }