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