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.util.ResourceBundle;
27  
28  import javax.swing.JLayeredPane;
29  
30  import org.diyefi.openlogviewer.Keys;
31  import org.diyefi.openlogviewer.OpenLogViewer;
32  import org.diyefi.openlogviewer.genericlog.GenericDataElement;
33  import org.diyefi.openlogviewer.genericlog.GenericLog;
34  
35  public class MultiGraphLayeredPane extends JLayeredPane {
36  	private static final long serialVersionUID = 1L;
37  	private static final int BOTTOM_LAYER = 999;  // Not really the bottom, but plenty far enough
38  	private static final int PANEL_WIDTH = 600;
39  	private static final int PANEL_HEIGHT = 400;
40  
41  	private GenericLog genLog;
42  	private final InfoPanel infoPanel;
43  	private int trackCount;
44  	private int layer;
45  
46  	public MultiGraphLayeredPane(final ResourceBundle labels) {
47  		setOpaque(true);
48  		setLayout(null);
49  		setBackground(Color.BLACK);
50  
51  		layer = BOTTOM_LAYER;
52  		trackCount = 1;
53  
54  		infoPanel = new InfoPanel(labels);
55  		infoPanel.setSize(PANEL_WIDTH, PANEL_HEIGHT);
56  		setLayer(infoPanel, layer);
57  		layer--;
58  		add(infoPanel);
59  	}
60  
61  	public final void addGraph(final String header) {
62  		final boolean p = OpenLogViewer.getInstance().getEntireGraphingPanel().isPlaying();
63  		if (p) {
64  			OpenLogViewer.getInstance().getEntireGraphingPanel().pause();
65  		}
66  		boolean found = false;
67  		for (int i = 0; i < this.getComponentCount() && !found; i++) {
68  			if (this.getComponent(i) instanceof SingleGraphPanel) {
69  				final SingleGraphPanel gl = (SingleGraphPanel) this.getComponent(i);
70  				if (gl.getName().equals(header)) {
71  					found = true;
72  				}
73  			}
74  		}
75  
76  		if (!found) {
77  			final SingleGraphPanel graph = new SingleGraphPanel();
78  			graph.setSize(this.getSize());
79  			graph.setName(header);
80  			this.setLayer(graph, layer);
81  			layer--;
82  			this.add(graph);
83  			this.addHierarchyBoundsListener(graph); // updates graph size automatically
84  			genLog.get(header).addPropertyChangeListener(Keys.SPLIT, graph);
85  			graph.setData(genLog.get(header));
86  			graph.repaint();
87  		}
88  
89  		this.revalidate();
90  		this.repaint();
91  
92  		if (p) {
93  			OpenLogViewer.getInstance().getEntireGraphingPanel().play();
94  		}
95  	}
96  
97  	public final void removeGraph(final String header) {
98  		final GenericDataElement temp = genLog.get(header);
99  		for (int i = 0; i < this.getComponentCount(); i++) {
100 			if (this.getComponent(i) instanceof SingleGraphPanel) {
101 				final SingleGraphPanel t = (SingleGraphPanel) this.getComponent(i);
102 				if (t.getData() == temp) {
103 					this.remove(t);
104 					this.removeHierarchyBoundsListener(t);
105 					this.revalidate();
106 					this.repaint();
107 				}
108 			}
109 		}
110 	}
111 
112 	private void removeAllGraphs() {
113 		int componentIndex = 0;
114 		while (this.getComponentCount() > 1) {  // Leave InfoPanel in component count
115 			if (this.getComponent(componentIndex) instanceof SingleGraphPanel) {
116 				final SingleGraphPanel sgp = (SingleGraphPanel) getComponent(componentIndex);
117 				this.removeHierarchyBoundsListener(sgp);
118 				sgp.getData().setDisplayColor(null);
119 				this.remove(sgp);
120 			} else {
121 				componentIndex++;
122 			}
123 		}
124 		repaint();
125 	}
126 
127 	public final void setLog(final GenericLog log) {
128 		removeAllGraphs();
129 		genLog = log;
130 		infoPanel.setLog(genLog);
131 		repaint();
132 	}
133 
134 	public final InfoPanel getInfoPanel() {
135 		return infoPanel;
136 	}
137 
138 	public final int getTrackCount() {
139 		return trackCount;
140 	}
141 
142 	public final void setTrackCount(final int newTrackCount) {
143 		trackCount = newTrackCount;
144 		for (int i = 0; i < getComponentCount(); i++) {
145 			if (getComponent(i) instanceof SingleGraphPanel) {
146 				final SingleGraphPanel gl = (SingleGraphPanel) getComponent(i);
147 				gl.sizeGraph();
148 			}
149 		}
150 	}
151 
152 	/**
153 	 * Graph total size
154 	 * @return GDE.size()
155 	 */
156 	public final int graphSize() {
157 		int availableData = 0;
158 		for (int i = 0; i < getComponentCount(); i++) {
159 			if (getComponent(i) instanceof SingleGraphPanel) {
160 				final SingleGraphPanel singleGraph = (SingleGraphPanel) getComponent(i);
161 				availableData = singleGraph.graphSize();
162 			}
163 		}
164 		return availableData;
165 	}
166 }