Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PingHistoryPanel.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.ui.WindowManager;
  26. import com.dmdirc.util.RollingList;
  27. import java.awt.Color;
  28. import java.awt.Dimension;
  29. import java.awt.Graphics;
  30. import java.awt.Graphics2D;
  31. import java.awt.Rectangle;
  32. import java.awt.geom.Rectangle2D;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import javax.swing.JPanel;
  36. /**
  37. * Shows a ping history graph for the current server.
  38. *
  39. * @author chris
  40. */
  41. public class PingHistoryPanel extends JPanel {
  42. /**
  43. * A version number for this class. It should be changed whenever the class
  44. * structure is changed (or anything else that would prevent serialized
  45. * objects being unserialized with the new class).
  46. */
  47. private static final long serialVersionUID = 1;
  48. /** The plugin that this panel is for. */
  49. protected final LagDisplayPlugin plugin;
  50. /** The history that we're graphing. */
  51. protected final RollingList<Long> history;
  52. /** The maximum ping value. */
  53. protected long maximum = 0l;
  54. /**
  55. * Creates a new history panel for the specified plugin.
  56. *
  57. * @param plugin The plugin that owns this panel
  58. */
  59. public PingHistoryPanel(final LagDisplayPlugin plugin) {
  60. super();
  61. setMinimumSize(new Dimension(50, 100));
  62. setMaximumSize(new Dimension(Integer.MAX_VALUE, 100));
  63. setOpaque(false);
  64. this.plugin = plugin;
  65. this.history = plugin.getHistory(ServerManager.getServerManager()
  66. .getServerFromFrame(WindowManager.getActiveWindow()));
  67. for (Long value : history.getList()) {
  68. maximum = Math.max(value, maximum);
  69. }
  70. }
  71. /** {@inheritDoc} */
  72. @Override
  73. public void paint(final Graphics g) {
  74. super.paint(g);
  75. g.setColor(Color.DARK_GRAY);
  76. g.drawLine(2, 1, 2, getHeight() - 1);
  77. g.drawLine(1, getHeight() - 2, getWidth() - 1, getHeight() - 2);
  78. g.setFont(g.getFont().deriveFont(10f));
  79. float lastX = -1, lastY = -1;
  80. float pixelsperpointX = (getWidth() - 3) / (float) (history.getList().size() == 1 ? 1
  81. : history.getList().size() - 1);
  82. float pixelsperpointY = (getHeight() - 10) / (float) maximum;
  83. if (history.isEmpty()) {
  84. g.drawString("No data", getWidth() / 2 - 25, getHeight() / 2 + 5);
  85. }
  86. long last1 = -1, last2 = -1;
  87. final List<Long> list = history.getList();
  88. final List<Rectangle> rects = new ArrayList<Rectangle>();
  89. for (int i = 0; i < list.size(); i++) {
  90. final Long value = list.get(i);
  91. float x = lastX == -1 ? 2 : lastX + pixelsperpointX;
  92. 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 (plugin.shouldShowLabels() && last1 > -1 && (last2 <= last1 || last1 >= value)) {
  98. final String text = plugin.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. 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. }