Source code: mas_gui/MA_ScrollPane.java
1 /* Copyright 1998 - 2003: Jim Cochrane - see file forum.txt */
2
3 package mas_gui;
4
5 import java.awt.*;
6 import graph.*;
7 import support.Configuration;
8 import java.util.Vector;
9 import java.util.Properties;
10 import java.awt.event.ItemListener;
11 import java.awt.event.ItemEvent;
12 import common.*;
13
14 /** Scroll pane that holds the Market Analysis graph and buttons */
15 public class MA_ScrollPane extends ScrollPane implements NetworkProtocol
16 {
17 static Properties print_properties;
18 static { print_properties = new Properties(); }
19
20 public MA_ScrollPane(Vector period_types, int scrollbarDisplayPolicy,
21 Chart parent_chart, Properties print_props)
22 {
23 super(scrollbarDisplayPolicy);
24
25 if (print_props != null) print_properties = print_props;
26 Configuration config = Configuration.instance();
27 chart = parent_chart;
28 _main_graph = new MainGraph();
29 _indicator_graph = new LowerGraph(_main_graph);
30 GridBagLayout gblayout = new GridBagLayout();
31 GridBagConstraints gbconstraints = new GridBagConstraints();
32
33 main_panel = new Panel(new BorderLayout());
34 add(main_panel, "Center");
35 graph_panel = new Panel(gblayout);
36 main_panel.add (graph_panel, "Center");
37
38 // Set GridBagLayout constraints such that the main graph is at
39 // the top and takes about 2/3 of available height and the
40 // indicator graph is below the main graph and takes the remaining
41 // 1/3 of the height and both graph panels grow/shrink when the
42 // window is resized.
43 gbconstraints.gridx = 0; gbconstraints.gridy = 0;
44 gbconstraints.gridwidth = 9; gbconstraints.gridheight = 6;
45 gbconstraints.weightx = 1; gbconstraints.weighty = 1;
46 gbconstraints.fill = GridBagConstraints.BOTH;
47 gblayout.setConstraints(_main_graph, gbconstraints);
48 graph_panel.add(_main_graph);
49 gbconstraints.gridx = 0; gbconstraints.gridy = 6;
50 gbconstraints.gridwidth = 9; gbconstraints.gridheight = 3;
51 gbconstraints.weightx = 1; gbconstraints.weighty = 1;
52 gbconstraints.fill = GridBagConstraints.BOTH;
53 gblayout.setConstraints(_indicator_graph, gbconstraints);
54 graph_panel.add(_indicator_graph);
55 _main_graph.set_framecolor(new Color(0,0,0));
56 _main_graph.set_borderTop(0);
57 _main_graph.set_borderBottom(1);
58 _main_graph.set_borderLeft(0);
59 _main_graph.set_borderRight(1);
60 _main_graph.setGraphBackground(config.background_color());
61 _main_graph.setSize(400, 310);
62
63 _indicator_graph.set_framecolor(new Color(0,0,0));
64 _indicator_graph.set_borderTop(0);
65 _indicator_graph.set_borderBottom(1);
66 _indicator_graph.set_borderLeft(0);
67 _indicator_graph.set_borderRight(1);
68 _indicator_graph.setGraphBackground(config.background_color());
69 _indicator_graph.setSize(400, 150);
70 }
71
72 // The main graph - where the principal market data is displayed
73 public Graph main_graph() {
74 return _main_graph;
75 }
76
77 // The indicator graph - where the indicator data is displayed
78 public Graph indicator_graph() {
79 return _indicator_graph;
80 }
81
82 // Delete all data from the main graph.
83 public void clear_main_graph() {
84 _main_graph.detachDataSets();
85 main_graph_changed = true;
86 }
87
88 // Delete all data from the indicator graph.
89 public void clear_indicator_graph() {
90 _indicator_graph.detachDataSets();
91 }
92
93 // Add a data set to the main graph.
94 public void add_main_data_set(DataSet d) {
95 _main_graph.attachDataSet(d);
96 main_graph_changed = true;
97 }
98
99 // Add a data set to the indicator graph.
100 public void add_indicator_data_set(DataSet d) {
101 _indicator_graph.attachDataSet(d);
102 }
103
104 // Repaint the graphs.
105 public void repaint_graphs() {
106 // The order matters - first repaint the main graph, then
107 // the indicator graph.
108 if (main_graph_changed) {
109 _main_graph.repaint();
110 main_graph_changed = false;
111 }
112 _indicator_graph.repaint();
113 }
114
115 //If all is true, loop through all selections in chart, asking it to
116 //grab the data and print each one.
117 public void print(boolean all) {
118 Toolkit tk = getToolkit();
119 PrintJob pj = tk.getPrintJob(chart,
120 all? "All charts": chart.current_tradable, print_properties);
121 if (pj != null) {
122 if (all) {
123 Vector selections = chart.market_selections.selections();
124 for (int i = 0; i < selections.size(); ++i) {
125 chart.request_data((String) selections.elementAt(i));
126 if (chart.request_result() == OK) {
127 print_current_chart(pj);
128 }
129 }
130 }
131 else {
132 print_current_chart(pj);
133 }
134 pj.end();
135 }
136 }
137
138 private void print_current_chart(PrintJob pj) {
139 Graphics page = pj.getGraphics();
140 Dimension size = graph_panel.getSize();
141 Dimension pagesize = pj.getPageDimension();
142 _main_graph.set_symbol(chart.current_tradable.toUpperCase());
143 if (size.width <= pagesize.width) {
144 // Center the output on the page.
145 page.translate((pagesize.width - size.width)/2,
146 (pagesize.height - size.height)/2);
147 } else {
148 // The graph size is wider than a page, so print first
149 // the left side, then the right side. Assumption - it is
150 // not larger than two pages.
151 page.translate(15,
152 (pagesize.height - size.height)/2);
153 graph_panel.printAll(page);
154 page.dispose();
155 page = pj.getGraphics();
156 page.translate(pagesize.width - size.width - 13,
157 (pagesize.height - size.height)/2);
158 }
159 graph_panel.printAll(page);
160 page.dispose();
161 _main_graph.set_symbol(null);
162 }
163
164 // Implementation
165
166 private MainGraph _main_graph;
167 private LowerGraph _indicator_graph;
168 private Panel main_panel;
169 private Panel graph_panel;
170 // Did _main_graph change since it was last repainted?
171 boolean main_graph_changed;
172 Choice period_type_choice;
173 Chart chart;
174 String _last_period_type;
175 }