summaryrefslogtreecommitdiff
path: root/TAO/examples/Simulator/Sim_Display/Graph_Panel.java
blob: 2eb6519f7c37b0bf481941d979bbf4d0c2a03be4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// $Id$ 

import java.awt.*;
import java.awt.event.*;
import java.util.*;

class Graph_Panel
  extends Panel	  
  implements ComponentListener,
	     ContainerListener
{
  private final static int VPAD = 4, HPAD = 4,
    MIN_WIDTH = 100,
    SUG_WIDTH = 175;
  private final static Random RAND = new Random (System.currentTimeMillis());
  private static float hue_ = RAND.nextFloat();
  
  private Hashtable graphs_ = new Hashtable();
  
  public Graph_Panel()
    {
      addComponentListener (this);
      addContainerListener (this);
    }  
  
  public void addGraph(String name, int max)
    {
      int index = name.lastIndexOf('/');
      String short_name;      
      
      if (index != -1)
	short_name = name.substring(index+1);
      else
	short_name = name;
      
      Graph graph = constructGraph(short_name, max);
      graphs_.put(name, graph);
    }

  public void updateGraph(String name, float new_value)
    {
      Graph graph = (Graph)graphs_.get(name);

      if(graph != null)
	graph.addNewData(new_value);
    }

  public Graph getGraph(String name)
    {
      return (Graph)((Container)graphs_.get(name)).getComponent(0);
    }
  
  public void removeGraph(String name)
    {
      graphs_.remove(name);

      if (graphs_.size() > 0)
	layoutGraphs();
      else
	removeAll();
    }

  public int numGraphs()
    {
      return graphs_.size();
    }

  public void clear()
    {
      removeAll();
    }

  public void componentResized(ComponentEvent e)
    {
      System.out.println ("Component resized.");
      removeContainerListener (this);
      removeComponentListener (this);
      layoutGraphs ();
      addComponentListener (this);
      addContainerListener (this);
    }

  public void componentMoved(ComponentEvent e) {}
  public void componentShown(ComponentEvent e) {}
  public void componentHidden(ComponentEvent e) {}

  public void componentAdded(ContainerEvent e)
    {
      System.out.println ("component added.");
      removeContainerListener (this);
      removeComponentListener (this);
      layoutGraphs ();
      addComponentListener (this);
      addContainerListener (this);
    }
  
  public void componentRemoved(ContainerEvent e)
    {
      System.out.println ("component removed.");
    }
  
  private void layoutGraphs ()
    {
      Dimension current_size = getSize ();
      int total_area = current_size.width * current_size.height;
      int num_rows, graphs_per_row, min_width = MIN_WIDTH;
      int graph_area = graphs_.size () *
	(SUG_WIDTH + HPAD) *
	(SUG_WIDTH + VPAD);
      Enumeration graph_enum = graphs_.elements ();

      removeAll ();

      System.out.println ("Total area: " + total_area + " Graph area: " + graph_area);
      
      if ((total_area < graph_area) ||
	  (current_size.width < MIN_WIDTH) ||
	  (current_size.height < MIN_WIDTH))
	{
	  System.out.println ("adjusting graph size");
	  min_width = total_area / graphs_.size ();
	  min_width = (int) Math.round (Math.sqrt (min_width));

	  if (min_width > current_size.height)
      	    min_width = current_size.height;
	  if (min_width > current_size.width)
	    min_width = current_size.width;

	  // Ah, hopeless. Too narrow to draw graphs in this panel.
	  if (min_width < MIN_WIDTH)
	    {
	      System.err.println ("Graph_Panel.layoutGraphs: Giving up!");
	      return;
	    }
	}

      graphs_per_row = current_size.width / (min_width + HPAD);
      num_rows = (graphs_.size () / graphs_per_row) + 1;

      setLayout (new GridLayout (num_rows, 1, 0, VPAD));
      for (int i = 0; i < num_rows; i++)
	{
	  Panel row = new Panel ();
	  row.setLayout (new GridLayout (1, graphs_per_row, HPAD, 0));
	  
	  for (int j = 0;
	       j < graphs_per_row && graph_enum.hasMoreElements ();
	       j++)
	    {
	      Component graph = (Component) graph_enum.nextElement ();		  
	      row.add (graph);
	    }

	  add (row);
	}

      validate ();
    }
  
  private final static Graph constructGraph(String label, int max)
    {
      Graph graph = new Graph(label, max);
      
      float brightness = RAND.nextFloat();

      hue_ += .075;

      if (hue_ > 1.0)
	hue_ -= 1.0;
      
      if (brightness > 0.75)
	brightness -= 0.25;
      
      Color new_color = Color.getHSBColor(hue_, 1, brightness);
      
      graph.setBackground(new_color);
      graph.setForeground(Color.white);
      
      return graph;
    }
}