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.Graphics;
29  import java.awt.Graphics2D;
30  import java.awt.event.MouseEvent;
31  import java.awt.event.MouseListener;
32  import java.awt.event.MouseMotionListener;
33  
34  import javax.swing.JPanel;
35  
36  import org.diyefi.openlogviewer.OpenLogViewer;
37  import org.diyefi.openlogviewer.genericlog.GenericLog;
38  
39  public class InfoPanel extends JPanel implements MouseMotionListener, MouseListener {
40  	private static final long serialVersionUID = 1L;
41  
42  	private static final int LEFT_MARGIN_OFFSET = 10;
43  	private static final int ONE_TEXTUAL_HEIGHT = 20;
44  	private int FPScounter = 0;
45  	private int FPS = 0;
46  	private long currentTime;
47  	private long builtTime;
48  	private GenericLog genLog;
49  	private Color vertBar = new Color(255, 255, 255, 100);
50  	private Color textBackground = new Color(0, 0, 0, 170);
51  	private int xMouseCoord;
52  	private int yMouseCoord;
53  	private boolean mouseOver;
54  
55  	public InfoPanel() {
56  		xMouseCoord = -100;
57  		yMouseCoord = -100;
58  		mouseOver = false;
59  		this.setOpaque(false);
60  	}
61  
62  	@Override
63  	public final void paint(final Graphics g) { // override paint because there will be no components in this pane
64  		builtTime += System.currentTimeMillis() - currentTime;
65  		currentTime = System.currentTimeMillis();
66  		if (builtTime <= 1000) {
67  			FPScounter++;
68  		} else {
69  			FPS = FPScounter;
70  			if (FPScounter != 0) {
71  				FPS += (1000 % FPScounter) * 0.001;
72  			}
73  			FPScounter = 0;
74  			builtTime = 0;
75  		}
76  
77  		if (!this.getSize().equals(this.getParent().getSize())) {
78  			this.setSize(this.getParent().getSize());
79  		}
80  
81  		g.setFont(new Font(Font.DIALOG, Font.PLAIN, 12)); // Required to keep font consistent when using Mac L&F
82  		if (genLog == null) {
83  			g.setColor(Color.RED);
84  			g.drawString("No log loaded, please select a log from the file menu.", LEFT_MARGIN_OFFSET, ONE_TEXTUAL_HEIGHT);
85  		} else {
86  			if (genLog.getLogStatus() == GenericLog.LOG_LOADING) {
87  				g.setColor(Color.red);
88  				g.drawString("Loading log, please wait...", LEFT_MARGIN_OFFSET, ONE_TEXTUAL_HEIGHT);
89  			} else if (genLog.getLogStatus() == GenericLog.LOG_LOADED) {
90  				int fpsHeight = 0;
91  				if (genLog.getLogStatusMessage() != null) {
92  					g.setColor(Color.RED);
93  					g.drawString(":-( The very sad decoder crashed! Last words: ", LEFT_MARGIN_OFFSET, ONE_TEXTUAL_HEIGHT);
94  					g.drawString(genLog.getLogStatusMessage(), LEFT_MARGIN_OFFSET, ONE_TEXTUAL_HEIGHT * 2);
95  					g.drawString("Displaying what we managed to read in before the catastrophy anyway...", LEFT_MARGIN_OFFSET, ONE_TEXTUAL_HEIGHT * 3);
96  					fpsHeight = ONE_TEXTUAL_HEIGHT * 4;
97  				} else {
98  					fpsHeight = ONE_TEXTUAL_HEIGHT;
99  				}
100 
101 				final Dimension d = this.getSize();
102 				final MultiGraphLayeredPane multigGraph = OpenLogViewer.getInstance().getMultiGraphLayeredPane();
103 				final Graphics2D g2d = (Graphics2D) g;
104 				g.setColor(Color.GRAY);
105 				g2d.drawString("FPS: " + Double.toString(FPS), LEFT_MARGIN_OFFSET, fpsHeight);
106 
107 				if (mouseOver) {
108 					final GraphPositionPanel graphPositionPanel = OpenLogViewer.getInstance().getEntireGraphingPanel().getGraphPositionPanel();
109 					final int zoom = OpenLogViewer.getInstance().getEntireGraphingPanel().getZoom();
110 					final boolean zoomedOut = OpenLogViewer.getInstance().getEntireGraphingPanel().isZoomedOutBeyondOneToOne();
111 					int snappedDataPosition = xMouseCoord;
112 					if(!zoomedOut && zoom > 1){
113 						snappedDataPosition = graphPositionPanel.getBestSnappingPosition(xMouseCoord);
114 					}
115 					g2d.setColor(vertBar);
116 					g2d.drawLine(d.width / 2, 0, d.width / 2, d.height);  //center position line
117 					g2d.drawLine(snappedDataPosition, 0, snappedDataPosition, d.height);  //mouse cursor line
118 
119 					for (int i = 0; i < multigGraph.getComponentCount(); i++) {
120 						if (multigGraph.getComponent(i) instanceof SingleGraphPanel) {
121 							final SingleGraphPanel singleGraph = (SingleGraphPanel) multigGraph.getComponent(i);
122 							g2d.setColor(textBackground);
123 							final Double mouseData = singleGraph.getMouseInfo(snappedDataPosition);
124 							String mouseDataString = "-.-";
125 							if(mouseData != null){
126 								mouseDataString = mouseData.toString();
127 							}
128 							g2d.fillRect(snappedDataPosition, yMouseCoord + 2 + (15 * i), mouseDataString.length() * 8, 15);
129 							g2d.setColor(singleGraph.getColor());
130 							g2d.drawString(mouseDataString, snappedDataPosition + 2, yMouseCoord + 15 + (15 * 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 }