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