Source code: mas_gui/IndicatorColors.java
1 /* Copyright 1998 - 2003: Jim Cochrane - see file forum.txt */
2
3 package mas_gui;
4
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.util.*;
8 import support.*;
9
10 public class IndicatorColors extends MA_Dialog {
11
12 public IndicatorColors(Chart c) {
13 super(c);
14 panel = new Panel();
15 gblayout = new GridBagLayout();
16 panel.setLayout(gblayout);
17 //panel.setSize(350, 350);
18 add(panel);
19 current_entries = new Hashtable();
20
21 // Add a key listener to close the selection dialog if the
22 // escape key is pressed while the focus is in the selection list.
23 panel.addKeyListener(new KeyAdapter() {
24 public void keyPressed(KeyEvent e) {
25 if (e.getKeyCode() == e.VK_ESCAPE) {
26 dialog.setVisible(false);
27 }
28 }});
29 add_close_listener();
30 }
31
32 public void actionPerformed(ActionEvent e) {
33 Label l;
34 GridBagConstraints gbconstraints = new GridBagConstraints();
35 String s, cmd;
36 int old_size = current_entries.size();
37 cmd = e.getActionCommand();
38 boolean cmd_is_indicator = cmd != null && ! cmd.equals(getTitle());
39 // If the newly chosen item is already in the current list, there
40 // is nothing to do.
41 if (cmd_is_indicator && current_entries.containsKey(cmd) &&
42 ! chart.replace_indicators) {
43 return;
44 }
45 int j = 0, ilength, longest_ilength = 30;
46 final int Length_increment = 30, Width_increment = 10,
47 Dialog_xborder = 40, Dialog_yborder = 30;
48 gbconstraints.gridx = 0; gbconstraints.gridy = gbconstraints.RELATIVE;
49 panel.removeAll();
50 current_entries.clear();
51 for (int i = 0; i < chart.current_upper_indicators.size(); ++i, ++j) {
52 s = (String) chart.current_upper_indicators.elementAt(i);
53 add_indicator(s, true, j);
54 ilength = s.length();
55 if (ilength > longest_ilength) longest_ilength = ilength;
56 }
57 for (int i = 0; i < chart.current_lower_indicators.size(); ++i, ++j) {
58 s = (String) chart.current_lower_indicators.elementAt(i);
59 add_indicator(s, false, j);
60 ilength = s.length();
61 if (ilength > longest_ilength) longest_ilength = ilength;
62 }
63 // Don't set to visible if the event was caused by an update to
64 // the indicator selection list.
65 if (! cmd_is_indicator) {
66 setVisible(true);
67 }
68 if (old_size != current_entries.size()) {
69 setSize(longest_ilength * Width_increment + Dialog_xborder,
70 j*Length_increment + Dialog_yborder);
71 } else if (isVisible()) {
72 // Ugly kludge to prevent the dialog window from becoming blank.
73 Dimension original_size = getSize();
74 Point original_pos = getLocation();
75 setSize(longest_ilength * Width_increment + Dialog_xborder,
76 original_size.height + inc);
77 setLocation(original_pos.x + 1, original_pos.y + 1);
78 inc = -inc;
79 }
80 }
81
82 protected String title() { return "Indicator Colors"; }
83
84 // Is indicator `s' in the list of currently selected indicators?
85 boolean is_in_current_indicator_list(String s) {
86 boolean result = false;
87 int i;
88 for (i = 0; ! result && i < chart.current_upper_indicators.size(); ++i)
89 {
90 if (s.equals(chart.current_upper_indicators.elementAt(i))) {
91 result = true;
92 }
93 }
94 for (i = 0; ! result && i < chart.current_lower_indicators.size(); ++i)
95 {
96 if (s.equals(chart.current_lower_indicators.elementAt(i))) {
97 result = true;
98 }
99 }
100 return result;
101 }
102
103 private void add_indicator(String i, boolean upper, int position) {
104 GridBagConstraints gbconstraints = new GridBagConstraints();
105 Label l;
106 Button bar;
107 Color color;
108 final int Length_increment = 20, Dialog_border = 30;
109 Panel p = new Panel(new BorderLayout());
110 Panel insidep = new Panel(new BorderLayout());
111 gbconstraints.gridx = 0; gbconstraints.gridy = gbconstraints.RELATIVE;
112 l = new Label(i);
113 p.add(l, "West");
114 color = indicator_color(i, upper);
115 l = new Label(Configuration.instance().color_name(color));
116 p.add(insidep, "East");
117 insidep.add(l, "West");
118 bar = new Button(" ");
119 bar.setBackground(color);
120
121 // Add a key listener to close the selection dialog if the
122 // escape key is pressed while the focus is in the selection list.
123 bar.addKeyListener(new KeyAdapter() {
124 public void keyPressed(KeyEvent e) {
125 if (e.getKeyCode() == e.VK_ESCAPE) {
126 dialog.setVisible(false);
127 }
128 }});
129
130 insidep.add(bar, "East");
131 gbconstraints.fill = GridBagConstraints.BOTH;
132 gblayout.setConstraints(p, gbconstraints);
133 panel.add(p);
134 current_entries.put(i, new Integer(position));
135 }
136
137 private Color indicator_color(String i, boolean upper) {
138 Color result;
139 Configuration conf = Configuration.instance();
140 result = conf.indicator_color(i, upper);
141 if (result == null) {
142 if (i.equals(chart.Volume)) result = conf.bar_color();
143 else result = conf.line_color();
144 }
145
146 return result;
147 }
148
149 Panel panel;
150 Hashtable current_entries;
151 GridBagLayout gblayout;
152 // Needed for ugly kludge in actionPerformed:
153 static int inc = 1;
154 }