Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PingHistoryPanel.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.lagdisplay;
  18. import com.dmdirc.addons.ui_swing.interfaces.ActiveFrameManager;
  19. import com.dmdirc.util.collections.RollingList;
  20. import java.awt.Color;
  21. import java.awt.Dimension;
  22. import java.awt.Graphics;
  23. import java.awt.Graphics2D;
  24. import java.awt.Rectangle;
  25. import java.awt.geom.Rectangle2D;
  26. import java.util.ArrayList;
  27. import java.util.Collection;
  28. import java.util.List;
  29. import javax.swing.JPanel;
  30. /**
  31. * Shows a ping history graph for the current server.
  32. */
  33. public class PingHistoryPanel extends JPanel {
  34. /** A version number for this class. */
  35. private static final long serialVersionUID = 1;
  36. /** The manager that this panel is for. */
  37. protected final LagDisplayManager manager;
  38. /** The history that we're graphing. */
  39. protected final RollingList<Long> history;
  40. /** The maximum ping value. */
  41. protected long maximum;
  42. /**
  43. * Creates a new history panel for the specified plugin.
  44. *
  45. * @param manager The manager that owns this panel
  46. * @param activeFrameManager Active frame manager.
  47. */
  48. public PingHistoryPanel(final LagDisplayManager manager,
  49. final ActiveFrameManager activeFrameManager) {
  50. setMinimumSize(new Dimension(50, 100));
  51. setMaximumSize(new Dimension(Integer.MAX_VALUE, 100));
  52. setOpaque(false);
  53. this.manager = manager;
  54. if (activeFrameManager.getActiveFrame().isPresent()) {
  55. history = manager.getHistory(activeFrameManager.getActiveFrame().get().getContainer()
  56. .getConnection().orElse(null));
  57. } else {
  58. history = manager.getHistory(null);
  59. }
  60. for (Long value : history.getList()) {
  61. maximum = Math.max(value, maximum);
  62. }
  63. }
  64. @Override
  65. public void paint(final Graphics g) {
  66. super.paint(g);
  67. g.setColor(Color.DARK_GRAY);
  68. g.drawLine(2, 1, 2, getHeight() - 1);
  69. g.drawLine(1, getHeight() - 2, getWidth() - 1, getHeight() - 2);
  70. g.setFont(g.getFont().deriveFont(10f));
  71. final float pixelsperpointX = (getWidth() - 3)
  72. / (float) (history.getList().size() == 1 ? 1
  73. : history.getList().size() - 1);
  74. final float pixelsperpointY = (getHeight() - 10) / (float) maximum;
  75. if (history.isEmpty()) {
  76. g.drawString("No data", getWidth() / 2 - 25, getHeight() / 2 + 5);
  77. }
  78. long last1 = -1;
  79. long last2 = -1;
  80. final List<Long> list = history.getList();
  81. final Collection<Rectangle> rects = new ArrayList<>();
  82. float lastX = -1;
  83. float lastY = -1;
  84. for (int i = 0; i < list.size(); i++) {
  85. final Long value = list.get(i);
  86. final float x = lastX == -1 ? 2 : lastX + pixelsperpointX;
  87. final float y = getHeight() - 5 - value * pixelsperpointY;
  88. if (lastX > -1) {
  89. g.drawLine((int) lastX, (int) lastY, (int) x, (int) y);
  90. }
  91. g.drawRect((int) x - 1, (int) y - 1, 2, 2);
  92. if (manager.shouldShowLabels() && last1 > -1 && (last2 <= last1 || last1 >= value)) {
  93. final String text = manager.formatTime(last1);
  94. final Rectangle2D rect = g.getFont().getStringBounds(text,
  95. ((Graphics2D) g).getFontRenderContext());
  96. final int width = 10 + (int) rect.getWidth();
  97. final int points = (int) Math.ceil(width / pixelsperpointX);
  98. final int diffy = (int) (last1 - (10 + rect.getHeight()) / pixelsperpointY);
  99. float posX = lastX - width + 7;
  100. final float posY = (float) (lastY + rect.getHeight() / 2) - 1;
  101. boolean failed = posX < 0;
  102. // Check left
  103. for (int j = Math.max(0, i - points); j < i - 1; j++) {
  104. if (list.get(j) > diffy) {
  105. failed = true;
  106. break;
  107. }
  108. }
  109. if (failed) {
  110. posX = lastX + 3;
  111. failed = posX + width > getWidth();
  112. // Check right
  113. for (int j = i; j < Math.min(list.size(), i + points); j++) {
  114. if (list.get(j) > diffy) {
  115. failed = true;
  116. break;
  117. }
  118. }
  119. }
  120. if (!failed) {
  121. final Rectangle myrect = new Rectangle((int) posX - 2,
  122. (int) lastY - 7, 5 + (int) rect.getWidth(),
  123. 3 + (int) rect.getHeight());
  124. for (Rectangle test : rects) {
  125. if (test.intersects(myrect)) {
  126. failed = true;
  127. }
  128. }
  129. if (!failed) {
  130. g.drawString(text, (int) posX, (int) posY);
  131. rects.add(myrect);
  132. }
  133. }
  134. }
  135. lastX = x;
  136. lastY = y;
  137. last2 = last1;
  138. last1 = value;
  139. }
  140. }
  141. }