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.Graphics;
28  import java.awt.Graphics2D;
29  import java.awt.event.MouseEvent;
30  import java.awt.event.MouseListener;
31  import java.awt.event.MouseMotionListener;
32  
33  import javax.swing.JPanel;
34  
35  import org.diyefi.openlogviewer.OpenLogViewerApp;
36  import org.diyefi.openlogviewer.genericlog.GenericLog;
37  
38  /**
39   * @author Bryan Harris and Ben Fenner
40   */
41  public class InfoPanel extends JPanel implements MouseMotionListener, MouseListener {
42  
43      public InfoPanel() {
44      	genLog = new GenericLog();
45          xMouseCoord = -100;
46          yMouseCoord = -100;
47          mouseOver = false;
48          this.setOpaque(false);
49      }
50  
51      @Override
52      public void paint(Graphics g) { // override paint because there will be no components in this pane
53          builtTime += System.currentTimeMillis() - currentTime;
54          currentTime = System.currentTimeMillis();
55          if (builtTime <= 1000) {
56              FPScounter++;
57          } else {
58              FPS = FPScounter;
59              if (FPScounter != 0) {
60                  FPS += (1000 % FPScounter) * 0.001;
61              }
62              FPScounter = 0;
63              builtTime = 0;
64          }
65  
66          if (!this.getSize().equals(this.getParent().getSize())) {
67              this.setSize(this.getParent().getSize());
68          }
69          if (genLog.getLogStatus() == GenericLog.LOG_NOT_LOADED) {
70              g.setColor(Color.RED);
71              g.drawString("No log loaded, please select a log from the file menu.", 20, 20);
72          } else if (genLog.getLogStatus() == GenericLog.LOG_LOADING) {
73              g.setColor(Color.red);
74              g.drawString("Loading log, please wait...", 20, 20);
75          } else if (genLog.getLogStatus() == GenericLog.LOG_LOADED) {
76              Dimension d = this.getSize();
77              int center = d.width / 2;
78              MultiGraphLayeredPane multigGraph = OpenLogViewerApp.getInstance().getMultiGraphLayeredPane();
79              Graphics2D g2d = (Graphics2D) g;
80              g2d.drawString("FPS: " + Double.toString(FPS), 30, 60);
81              if (mouseOver) {
82              	int snappedDataPosition = OpenLogViewerApp.getInstance().getEntireGraphingPanel().getGraphPositionPanel().getBestSnappingPosition(xMouseCoord);
83                  g2d.setColor(vertBar);
84                  g2d.drawLine(d.width / 2, 0, d.width / 2, d.height);  //center position line
85                  g2d.drawLine(snappedDataPosition, 0, snappedDataPosition, d.height);  //mouse cursor line
86                  for (int i = 0; i < multigGraph.getComponentCount(); i++) {
87                      if (multigGraph.getComponent(i) instanceof SingleGraphPanel) {
88                          SingleGraphPanel singleGraph = (SingleGraphPanel) multigGraph.getComponent(i);
89                          g2d.setColor(textBackground);
90                          String mouseDataString = singleGraph.getMouseInfo(snappedDataPosition - center).toString();
91                          g2d.fillRect(snappedDataPosition, yMouseCoord + 2 + (15 * i), mouseDataString.length() * 8, 15);
92                          g2d.setColor(singleGraph.getColor());
93                          g2d.drawString(mouseDataString, snappedDataPosition + 2, yMouseCoord + 15 + (15 * i));
94                      }
95                  }
96              }
97          }
98      }
99  
100     public void setLog(GenericLog log) {
101         genLog = log;
102         repaint();
103     }
104 
105 
106 	@Override
107 	public void mouseEntered(MouseEvent e) {
108 		mouseOver = true;
109 	}
110 
111 	@Override
112 	public void mouseExited(MouseEvent e) {
113 		mouseOver = false;
114 		repaint();
115 	}
116 
117 	@Override
118 	public void mouseMoved(MouseEvent e) {
119 		xMouseCoord = e.getX();
120 		yMouseCoord = e.getY();
121 		repaint();
122 	}
123 
124 	@Override
125 	public void mouseClicked(MouseEvent arg0) {
126 	}
127 
128 	@Override
129 	public void mousePressed(MouseEvent e) {
130 	}
131 
132 	@Override
133 	public void mouseReleased(MouseEvent e) {
134 	}
135 
136 	@Override
137 	public void mouseDragged(MouseEvent e) {
138 
139 	}
140 
141     int FPScounter = 0;
142     int FPS = 0;
143     private long currentTime;
144     private long builtTime;
145     private GenericLog genLog;
146     private Color vertBar = new Color(255, 255, 255, 100);
147     private Color textBackground = new Color(0, 0, 0, 170);
148     private int xMouseCoord;
149     private int yMouseCoord;
150     boolean mouseOver;
151     private static final long serialVersionUID = -6657156551430700622L;
152 
153 }