Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PingHistoryPanel.java 6.1KB

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