View Javadoc

1   package org.catacomb.graph.gui;
2   
3   import org.catacomb.interlish.structure.ModeSettable;
4   import org.catacomb.interlish.structure.RangeWatcher;
5   import org.catacomb.report.E;
6   
7   
8   import java.awt.BorderLayout;
9   import java.awt.Dimension;
10  
11  import javax.swing.JFrame;
12  
13  
14  import java.awt.Color;
15  
16  
17  public class DataDisplay extends BasePanel implements ModeSettable, RangeListener {
18  
19      static final long serialVersionUID = 1001;
20  
21      XAxisCanvas xAxisCanvas;
22      YAxisCanvas yAxisCanvas;
23      PickWorldCanvas pwCanvas;
24      CornerPanel cornerPanel;
25  
26      BasePanel controlPanel;
27  
28      AboveBelowSplitPanel ab1;
29      AboveBelowSplitPanel ab2;
30      LeftRightSplitPanel lr;
31  
32      GraphColors gcols;
33  
34      Dimension prefDim;
35  
36      RangeWatcher rangeWatcher;
37  
38      static boolean interactive = true;
39      public static void setBatch() {
40          interactive = false;
41      }
42  
43      public DataDisplay(int w, int h) {
44          super();
45  
46          gcols = new GraphColors();
47          int leftmargin = 64;
48          int bottommargin = 22;
49          xAxisCanvas = new XAxisCanvas(w - leftmargin, bottommargin);
50          yAxisCanvas = new YAxisCanvas(leftmargin, h - bottommargin);
51          pwCanvas = new PickWorldCanvas(w - leftmargin, h - bottommargin, interactive);
52  
53          cornerPanel = new CornerPanel(leftmargin, bottommargin, pwCanvas);
54  
55  
56          ab1 = new AboveBelowSplitPanel(yAxisCanvas, cornerPanel, gcols);
57  
58          ab2 = new AboveBelowSplitPanel(pwCanvas, xAxisCanvas, gcols);
59  
60  
61          ab1.setResizeWeight(1.0);
62          ab2.setResizeWeight(1.0);
63  
64  
65          setPrefSize(w, h);
66  
67          // NB calls setDividerLocation which has side effect of starting event thread
68          ab1.setSplitPanelFollower(ab2);
69          ab2.setSplitPanelFollower(ab1);
70  
71  
72          lr = new LeftRightSplitPanel(ab1, ab2, gcols);
73  
74          lr.setResizeWeight(0.1);
75  
76          setLayout(new BorderLayout(0, 0));
77          add("Center", lr);
78  
79  
80          pwCanvas.addRangeListener(xAxisCanvas);
81          pwCanvas.addRangeListener(yAxisCanvas);
82  
83      }
84  
85  
86      public void repaintAll() {
87          xAxisCanvas.invalidate();
88          yAxisCanvas.invalidate();
89          validateTree();
90          xAxisCanvas.repaint();
91          yAxisCanvas.repaint();
92          repaint();
93      }
94  
95      public void addRangeWatcher(RangeWatcher rw) {
96          if (rangeWatcher == null) {
97              pwCanvas.addRangeListener(this);
98              rangeWatcher = rw;
99          } else {
100             E.error("cant add another range watcher - already watching");
101         }
102     }
103 
104     public void rangeChanged(int mode, double[] newLimits) {
105         if (rangeWatcher != null) {
106             rangeWatcher.rangeChanged();
107         }
108     }
109 
110 
111 
112 
113     public Dimension getPreferredSize() {
114         return prefDim;
115     }
116 
117 
118     public void setBg(Color c) {
119         setDataBg(c);
120         setBorderBg(c.brighter());
121     }
122 
123     public void setDataBg(Color c) {
124         gcols.setGraphBg(c);
125         pwCanvas.setBg(c);
126     }
127 
128     public void setBorderBg(Color c) {
129         gcols.setBorderBg(c);
130 
131         xAxisCanvas.setBg(c);
132         yAxisCanvas.setBg(c);
133         cornerPanel.setBg(c);
134     }
135 
136 
137     public void setMode(String dom, String mod) {
138         pwCanvas.setMode(dom, mod);
139     }
140 
141 
142     public void setMode(String dom, boolean b) {
143         pwCanvas.setMode(dom, b);
144     }
145 
146 
147     public void setPaintInstructor(PaintInstructor pi) {
148         pwCanvas.setPaintInstructor(pi);
149     }
150 
151 
152     public void setBuildPaintInstructor(BuildPaintInstructor bpi) {
153         pwCanvas.setBuildPaintInstructor(bpi);
154     }
155 
156 
157     public void setPickListener(PickListener pl) {
158         pwCanvas.setPickListener(pl);
159     }
160 
161 
162     public void attach(Object obj) {
163         boolean done = false;
164 
165         if (obj instanceof BuildPaintInstructor) {
166             setBuildPaintInstructor((BuildPaintInstructor)obj);
167             done = true;
168 
169         } else if (obj instanceof PaintInstructor) {
170             setPaintInstructor((PaintInstructor)obj);
171             done = true;
172         }
173 
174         if (obj instanceof PickListener) {
175             setPickListener((PickListener)obj);
176             done = true;
177         }
178 
179         if (!done) {
180             E.error("cant attach " + obj + " to a data Display");
181         }
182     }
183 
184 
185     public void setXAxisLabel(String lab) {
186         xAxisCanvas.setLabel(lab);
187     }
188 
189     public void setYAxisLabel(String lab) {
190         yAxisCanvas.setLabel(lab);
191     }
192 
193     public void setXAxis(String lab, double min, double max) {
194         setXAxisLabel(lab);
195         setXRange(min, max);
196     }
197 
198 
199 
200 
201 
202     public void setYAxis(String lab, double min, double max) {
203         setYAxisLabel(lab);
204         setYRange(min, max);
205     }
206 
207     public void setYRange(double min, double max) {
208         pwCanvas.setYRange(min, max);
209     }
210 
211 
212 
213     public void setLimits(double[] xyxy) {
214         pwCanvas.setXRange(xyxy[0], xyxy[2]);
215         pwCanvas.setYRange(xyxy[1], xyxy[3]);
216         pwCanvas.requestRepaint();
217     }
218 
219 
220     public void setXRange(double low, double high) {
221         pwCanvas.setXRange(low, high);
222     }
223 
224 
225     public double[] getXRange() {
226         return pwCanvas.getXRange();
227     }
228 
229 
230     public double[] getYRange() {
231         return pwCanvas.getYRange();
232     }
233 
234 
235     public void setFixedAspectRatio(double ar) {
236         pwCanvas.setFixedAspectRatio(ar);
237     }
238 
239 
240     public void viewChanged() {
241         if (pwCanvas != null) {
242             pwCanvas.repaint();
243         }
244     }
245 
246 
247     public void reframe() {
248         pwCanvas.reframe();
249     }
250 
251 
252     public static void main(String[] argv) {
253         JFrame f = new JFrame();
254         DataDisplay wc = new DataDisplay(500, 300);
255         wc.setPaintInstructor(new Demo1());
256 
257         f.getContentPane().add(wc);
258         f.pack();
259         f.setVisible(true);
260     }
261 
262 
263     public void setColorRange(double cmin, double cmax) {
264         pwCanvas.setColorRange(cmin, cmax);
265     }
266 
267     public void setColorTable(Color[] ac) {
268         pwCanvas.setColorTable(ac);
269     }
270 
271 
272     public void syncSizes() {
273         pwCanvas.syncSize();
274     }
275 
276     public void setPrefSize(int w, int h) {
277 
278         prefDim = new Dimension(w, h);
279         setPreferredSize(prefDim);
280 
281         int leftmargin = 64;
282         int bottommargin = 22;
283         xAxisCanvas.setPreferredSize(w - leftmargin, bottommargin);
284         yAxisCanvas.setPreferredSize(leftmargin, h - bottommargin);
285         pwCanvas.setPreferredSize(w - leftmargin, h - bottommargin);
286         xAxisCanvas.setMinimumSize(new Dimension(100, bottommargin - 20));
287         yAxisCanvas.setMinimumSize(new Dimension(leftmargin - 20, 100));
288         cornerPanel.setMinimumSize(new Dimension(leftmargin - 20, bottommargin - 20));
289 
290 
291         // NB - these cause the AWT event thread to be started (which delays exit in batch mode)
292         ab1.setDividerLocation(h - leftmargin);
293         ab2.setDividerLocation(h - bottommargin);
294     }
295 
296 
297 }