1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.diyefi.openlogviewer.graphing;
24
25 import java.awt.Color;
26 import java.awt.Dimension;
27 import java.awt.Font;
28 import java.awt.FontMetrics;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.event.MouseEvent;
32 import java.awt.event.MouseListener;
33 import java.awt.event.MouseMotionListener;
34
35 import javax.swing.JPanel;
36
37 import org.diyefi.openlogviewer.OpenLogViewer;
38 import org.diyefi.openlogviewer.genericlog.GenericLog;
39
40 public class InfoPanel extends JPanel implements MouseMotionListener, MouseListener {
41 private static final long serialVersionUID = 1L;
42
43 private static final int LEFT_MARGIN_OFFSET = 10;
44 private static final int ONE_TEXTUAL_HEIGHT = 20;
45 private int FPScounter = 0;
46 private int FPS = 0;
47 private long currentTime;
48 private long builtTime;
49 private GenericLog genLog;
50 private Color vertBar = new Color(255, 255, 255, 100);
51 private Color textBackground = new Color(0, 0, 0, 170);
52 private int xMouseCoord;
53 private int yMouseCoord;
54 private boolean mouseOver;
55
56 public InfoPanel() {
57 xMouseCoord = -100;
58 yMouseCoord = -100;
59 mouseOver = false;
60 this.setOpaque(false);
61 }
62
63 @Override
64 public final void paint(final Graphics g) {
65 builtTime += System.currentTimeMillis() - currentTime;
66 currentTime = System.currentTimeMillis();
67 if (builtTime <= 1000) {
68 FPScounter++;
69 } else {
70 FPS = FPScounter;
71 if (FPScounter != 0) {
72 FPS += (1000 % FPScounter) * 0.001;
73 }
74 FPScounter = 0;
75 builtTime = 0;
76 }
77
78 if (!this.getSize().equals(this.getParent().getSize())) {
79 this.setSize(this.getParent().getSize());
80 }
81
82 g.setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
83 if (genLog == null) {
84 g.setColor(Color.RED);
85 g.drawString("No log loaded, please select a log from the file menu.", LEFT_MARGIN_OFFSET, ONE_TEXTUAL_HEIGHT);
86 } else {
87 if (genLog.getLogStatus() == GenericLog.LogState.LOG_LOADING) {
88 g.setColor(Color.red);
89 g.drawString("Loading log, please wait...", LEFT_MARGIN_OFFSET, ONE_TEXTUAL_HEIGHT);
90 } else if (genLog.getLogStatus() == GenericLog.LogState.LOG_LOADED) {
91 int fpsHeight = 0;
92 if (genLog.getLogStatusMessage() != null) {
93 g.setColor(Color.RED);
94 g.drawString(":-( The very sad decoder crashed! Last words: ", LEFT_MARGIN_OFFSET, ONE_TEXTUAL_HEIGHT);
95 g.drawString(genLog.getLogStatusMessage(), LEFT_MARGIN_OFFSET, ONE_TEXTUAL_HEIGHT * 2);
96 g.drawString("Displaying what we managed to read in before the catastrophy anyway...", LEFT_MARGIN_OFFSET, ONE_TEXTUAL_HEIGHT * 3);
97 fpsHeight = ONE_TEXTUAL_HEIGHT * 4;
98 } else {
99 fpsHeight = ONE_TEXTUAL_HEIGHT;
100 }
101
102 final Dimension d = this.getSize();
103 final MultiGraphLayeredPane multigGraph = OpenLogViewer.getInstance().getMultiGraphLayeredPane();
104 final Graphics2D g2d = (Graphics2D) g;
105 g.setColor(Color.GRAY);
106 g2d.drawString("FPS: " + Double.toString(FPS), LEFT_MARGIN_OFFSET, fpsHeight);
107
108 if (mouseOver) {
109 FontMetrics fm = this.getFontMetrics(this.getFont());
110 final int fontHeight = fm.getHeight();
111 final GraphPositionPanel graphPositionPanel = OpenLogViewer.getInstance().getEntireGraphingPanel().getGraphPositionPanel();
112 final int zoom = OpenLogViewer.getInstance().getEntireGraphingPanel().getZoom();
113 final boolean zoomedOut = OpenLogViewer.getInstance().getEntireGraphingPanel().isZoomedOutBeyondOneToOne();
114 int snappedDataPosition = xMouseCoord;
115 if (!zoomedOut && zoom > 1) {
116 snappedDataPosition = graphPositionPanel.getBestSnappingPosition(xMouseCoord);
117 }
118 g2d.setColor(vertBar);
119 g2d.drawLine(d.width / 2, 0, d.width / 2, d.height);
120 g2d.drawLine(snappedDataPosition, 0, snappedDataPosition, d.height);
121
122 for (int i = 0; i < multigGraph.getComponentCount(); i++) {
123 if (multigGraph.getComponent(i) instanceof SingleGraphPanel) {
124 final SingleGraphPanel singleGraph = (SingleGraphPanel) multigGraph.getComponent(i);
125 g2d.setColor(textBackground);
126 String mouseDataString = singleGraph.getMouseInfo(snappedDataPosition);
127 final int stringWidth = fm.stringWidth(mouseDataString);
128 g2d.fillRect(snappedDataPosition + 1, yMouseCoord + 2 + (fontHeight * i), stringWidth + 3, fontHeight);
129 g2d.setColor(singleGraph.getColor());
130 g2d.drawString(mouseDataString, snappedDataPosition + 3, yMouseCoord + fontHeight + (fontHeight * i));
131 }
132 }
133 }
134 }
135 }
136 }
137
138 public final void setLog(final GenericLog log) {
139 genLog = log;
140 repaint();
141 }
142
143 @Override
144 public final void mouseEntered(final MouseEvent e) {
145 mouseOver = true;
146 }
147
148 @Override
149 public final void mouseExited(final MouseEvent e) {
150 mouseOver = false;
151 repaint();
152 }
153
154 @Override
155 public final void mouseMoved(final MouseEvent e) {
156 xMouseCoord = e.getX();
157 yMouseCoord = e.getY();
158 repaint();
159 }
160
161 @Override
162 public void mouseClicked(final MouseEvent e) {
163 }
164
165 @Override
166 public void mousePressed(final MouseEvent e) {
167 }
168
169 @Override
170 public void mouseReleased(final MouseEvent e) {
171 }
172
173 @Override
174 public void mouseDragged(final MouseEvent e) {
175
176 }
177 }