View Javadoc

1   /* OpenLogViewer
2    *
3    * Copyright 2011
4    *
5    * This file is part of the OpenLogViewer project.
6    *
7    * OpenLogViewer software is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * OpenLogViewer software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with any OpenLogViewer software.  If not, see http://www.gnu.org/licenses/
19   *
20   * I ask that if you make any changes to this file you fork the code on github.com!
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) { // override paint because there will be no components in this pane
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)); // Required to keep font consistent when using Mac L&F
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());  //For getting string width
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);  //center position line
120 					g2d.drawLine(snappedDataPosition, 0, snappedDataPosition, d.height);  //mouse cursor line
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 }