1 package org.opentrafficsim.swing.graphs;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.util.LinkedHashMap;
6 import java.util.Map;
7
8 import javax.swing.ButtonGroup;
9 import javax.swing.JCheckBoxMenuItem;
10 import javax.swing.JMenu;
11 import javax.swing.JPopupMenu;
12 import javax.swing.JRadioButtonMenuItem;
13
14 import org.opentrafficsim.draw.graphs.AbstractContourPlot;
15 import org.opentrafficsim.draw.graphs.AbstractSpaceTimePlot;
16 import org.opentrafficsim.draw.graphs.ContourDataSource.Dimension;
17
18
19
20
21
22
23
24
25
26
27
28 public class SwingContourPlot extends SwingSpaceTimePlot
29 {
30
31 private static final long serialVersionUID = 20190823L;
32
33
34 private Map<JRadioButtonMenuItem, Double> timeGranularityButtons;
35
36
37 private Map<JRadioButtonMenuItem, Double> spaceGranularityButtons;
38
39
40 private JCheckBoxMenuItem smoothCheckBox;
41
42
43 private JCheckBoxMenuItem interpolateCheckBox;
44
45
46
47
48
49 public SwingContourPlot(final AbstractSpaceTimePlot plot)
50 {
51 super(plot);
52 }
53
54
55 @Override
56 protected void addPopUpMenuItems(final JPopupMenu popupMenu)
57 {
58 this.timeGranularityButtons = new LinkedHashMap<>();
59 this.spaceGranularityButtons = new LinkedHashMap<>();
60 super.addPopUpMenuItems(popupMenu);
61 JMenu spaceGranularityMenu = buildMenu("Distance granularity", "%.0f m", 1000, "%.0f km", "setSpaceGranularity",
62 getPlot().getDataPool().getGranularities(Dimension.DISTANCE),
63 getPlot().getDataPool().getGranularity(Dimension.DISTANCE), this.spaceGranularityButtons);
64 popupMenu.insert(spaceGranularityMenu, 0);
65 JMenu timeGranularityMenu = buildMenu("Time granularity", "%.0f s", 60.0, "%.0f min", "setTimeGranularity",
66 getPlot().getDataPool().getGranularities(Dimension.TIME),
67 getPlot().getDataPool().getGranularity(Dimension.TIME), this.timeGranularityButtons);
68 popupMenu.insert(timeGranularityMenu, 1);
69 this.smoothCheckBox = new JCheckBoxMenuItem("Adaptive smoothing method", false);
70 this.smoothCheckBox.addActionListener(new ActionListener()
71 {
72
73 @Override
74 public void actionPerformed(final ActionEvent e)
75 {
76 getPlot().getDataPool().setSmooth(((JCheckBoxMenuItem) e.getSource()).isSelected());
77 getPlot().notifyPlotChange();
78 }
79 });
80 popupMenu.insert(this.smoothCheckBox, 2);
81 this.interpolateCheckBox = new JCheckBoxMenuItem("Bilinear interpolation", true);
82 this.interpolateCheckBox.addActionListener(new ActionListener()
83 {
84
85 @Override
86 public void actionPerformed(final ActionEvent e)
87 {
88 boolean interpolate = ((JCheckBoxMenuItem) e.getSource()).isSelected();
89 getPlot().getBlockRenderer().setInterpolate(interpolate);
90 getPlot().getDataPool().setInterpolate(interpolate);
91 getPlot().notifyPlotChange();
92 }
93 });
94 popupMenu.insert(this.interpolateCheckBox, 3);
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108
109 private JMenu buildMenu(final String menuName, final String format1, final double formatValue, final String format2,
110 final String command, final double[] values, final double initialValue,
111 final Map<JRadioButtonMenuItem, Double> granularityButtons)
112 {
113 JMenu result = new JMenu(menuName);
114 ButtonGroup group = new ButtonGroup();
115 for (double value : values)
116 {
117 JRadioButtonMenuItem item = new JRadioButtonMenuItem(
118 String.format(value < formatValue ? format1 : format2, value < formatValue ? value : value / formatValue));
119 granularityButtons.put(item, value);
120 item.setSelected(value == initialValue);
121 item.setActionCommand(command);
122 item.addActionListener(new ActionListener()
123 {
124
125 @Override
126 public void actionPerformed(final ActionEvent actionEvent)
127 {
128 if (command.equalsIgnoreCase("setSpaceGranularity"))
129 {
130 double granularity = SwingContourPlot.this.spaceGranularityButtons.get(actionEvent.getSource());
131 getPlot().getDataPool().setGranularity(Dimension.DISTANCE, granularity);
132 }
133 else if (command.equalsIgnoreCase("setTimeGranularity"))
134 {
135 double granularity = SwingContourPlot.this.timeGranularityButtons.get(actionEvent.getSource());
136 getPlot().getDataPool().setGranularity(Dimension.TIME, granularity);
137 }
138 else
139 {
140 throw new RuntimeException("Unknown ActionEvent");
141 }
142 }
143 });
144 result.add(item);
145 group.add(item);
146 }
147 return result;
148 }
149
150
151 @Override
152 public AbstractContourPlot<?> getPlot()
153 {
154 return (AbstractContourPlot<?>) super.getPlot();
155 }
156
157
158
159
160
161
162 protected final void setSpaceGranularityRadioButton(final double granularity)
163 {
164 getPlot().setSpaceGranularity(granularity);
165 for (JRadioButtonMenuItem button : this.spaceGranularityButtons.keySet())
166 {
167 button.setSelected(this.spaceGranularityButtons.get(button) == granularity);
168 }
169 }
170
171
172
173
174
175
176 protected final void setTimeGranularityRadioButton(final double granularity)
177 {
178 getPlot().setTimeGranularity(granularity);
179 for (JRadioButtonMenuItem button : this.timeGranularityButtons.keySet())
180 {
181 button.setSelected(this.timeGranularityButtons.get(button) == granularity);
182 }
183 }
184
185
186
187
188
189 protected final void setSmoothing(final boolean smooth)
190 {
191 this.smoothCheckBox.setSelected(smooth);
192 }
193
194
195
196
197
198
199 protected final void setInterpolation(final boolean interpolate)
200 {
201 getPlot().setInterpolation(interpolate);
202 this.interpolateCheckBox.setSelected(interpolate);
203 }
204
205 }