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