]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/settings/SetDisplaySettings.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / function / settings / SetDisplaySettings.java
1 package tim.prune.function.settings;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8
9 import javax.swing.BorderFactory;
10 import javax.swing.Box;
11 import javax.swing.BoxLayout;
12 import javax.swing.ButtonGroup;
13 import javax.swing.ImageIcon;
14 import javax.swing.JButton;
15 import javax.swing.JCheckBox;
16 import javax.swing.JComboBox;
17 import javax.swing.JDialog;
18 import javax.swing.JLabel;
19 import javax.swing.JList;
20 import javax.swing.JPanel;
21 import javax.swing.JRadioButton;
22 import javax.swing.ListCellRenderer;
23 import javax.swing.border.EtchedBorder;
24
25 import tim.prune.App;
26 import tim.prune.DataSubscriber;
27 import tim.prune.GenericFunction;
28 import tim.prune.I18nManager;
29 import tim.prune.UpdateMessageBroker;
30 import tim.prune.config.Config;
31 import tim.prune.gui.GuiGridLayout;
32 import tim.prune.gui.WholeNumberField;
33 import tim.prune.gui.map.WpIconLibrary;
34
35 /**
36  * Class to show the dialog for setting the display settings
37  * like line width, antialiasing, waypoint icons
38  */
39 public class SetDisplaySettings extends GenericFunction
40 {
41         /**
42          * Inner class to render waypoint icons
43          */
44         class IconComboRenderer extends JLabel implements ListCellRenderer<Integer>
45         {
46                 /** Cached icons for each waypoint type */
47                 private ImageIcon[] _icons = new ImageIcon[WpIconLibrary.WAYPT_NUMBER_OF_ICONS];
48
49                 /** Constructor */
50                 IconComboRenderer()
51                 {
52                         setOpaque(true);
53                 }
54
55                 /** Get the label text at the given index */
56                 private String getLabel(int inIndex)
57                 {
58                         return I18nManager.getText("dialog.displaysettings.wpicon." + WpIconLibrary.getIconName(inIndex));
59                 }
60
61                 /** Get the image icon at the given index */
62                 private ImageIcon getIcon(int inIndex)
63                 {
64                         if (_icons[inIndex] == null)
65                         {
66                                 _icons[inIndex] = WpIconLibrary.getIconDefinition(inIndex, 1).getImageIcon();
67                         }
68                         return _icons[inIndex];
69                 }
70
71                 /** @return a label to display the combo box entry */
72                 public Component getListCellRendererComponent(
73                         JList<? extends Integer> inList, Integer inValue, int inIndex,
74                         boolean inSelected, boolean inFocus)
75                 {
76                         if (inSelected) {
77                                 setBackground(inList.getSelectionBackground());
78                                 setForeground(inList.getSelectionForeground());
79                         } else {
80                                 setBackground(inList.getBackground());
81                                 setForeground(inList.getForeground());
82                         }
83                         setIcon(getIcon(inValue));
84                         setText(getLabel(inValue));
85                         return this;
86                 }
87         }
88
89
90         // Members of SetDisplaySettings
91         private JDialog _dialog = null;
92         private WholeNumberField _lineWidthField = null;
93         private JCheckBox _antialiasCheckbox = null;
94         private JComboBox<Integer> _wpIconCombobox = null;
95         private JRadioButton[] _sizeRadioButtons = null;
96         private JRadioButton[] _windowStyleRadios = null;
97         private JButton _okButton = null;
98
99         private static final String STYLEKEY_NIMBUS = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
100
101
102         /**
103          * Constructor
104          * @param inApp app object
105          */
106         public SetDisplaySettings(App inApp)
107         {
108                 super(inApp);
109         }
110
111         /**
112          * Return the name key for this function
113          */
114         public String getNameKey()
115         {
116                 return "function.setdisplaysettings";
117         }
118
119         /**
120          * @return the contents of the window as a Component
121          */
122         private Component makeContents()
123         {
124                 JPanel mainPanel = new JPanel();
125                 mainPanel.setLayout(new BorderLayout(0, 5));
126                 JPanel midPanel = new JPanel();
127                 midPanel.setLayout(new BoxLayout(midPanel, BoxLayout.Y_AXIS));
128                 midPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
129
130                 JPanel linesPanel = new JPanel();
131                 linesPanel.setBorder(BorderFactory.createCompoundBorder(
132                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
133                 );
134                 GuiGridLayout grid = new GuiGridLayout(linesPanel);
135
136                 // line width
137                 JLabel lineWidthLabel = new JLabel(I18nManager.getText("dialog.displaysettings.linewidth"));
138                 grid.add(lineWidthLabel);
139                 _lineWidthField = new WholeNumberField(1);
140                 grid.add(_lineWidthField);
141                 // Antialiasing
142                 _antialiasCheckbox = new JCheckBox(I18nManager.getText("dialog.displaysettings.antialias"), false);
143                 grid.add(_antialiasCheckbox);
144                 grid.add(new JLabel(""));
145
146                 linesPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
147                 midPanel.add(linesPanel);
148                 midPanel.add(Box.createVerticalStrut(10));
149
150                 // Panel for waypoint icons
151                 JPanel waypointsPanel = new JPanel();
152                 waypointsPanel.setBorder(BorderFactory.createCompoundBorder(
153                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
154                 );
155                 waypointsPanel.setLayout(new BoxLayout(waypointsPanel, BoxLayout.Y_AXIS));
156                 // Select which waypoint icon to use
157                 JPanel iconPanel = new JPanel();
158                 GuiGridLayout iconGrid = new GuiGridLayout(iconPanel);
159                 JLabel headerLabel = new JLabel(I18nManager.getText("dialog.displaysettings.waypointicons"));
160                 headerLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
161                 iconGrid.add(headerLabel);
162                 _wpIconCombobox = new JComboBox<Integer>(new Integer[] {0, 1, 2, 3, 4});
163                 _wpIconCombobox.setRenderer(new IconComboRenderer());
164                 iconGrid.add(_wpIconCombobox);
165                 waypointsPanel.add(iconPanel);
166                 // Select size of waypoints
167                 JPanel sizePanel = new JPanel();
168                 sizePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
169                 _sizeRadioButtons = new JRadioButton[3];
170                 ButtonGroup sizeRadioGroup = new ButtonGroup();
171                 final String[] sizeKeys = {"small", "medium", "large"};
172                 for (int i=0; i<3; i++)
173                 {
174                         _sizeRadioButtons[i] = new JRadioButton(I18nManager.getText("dialog.displaysettings.size." + sizeKeys[i]));
175                         sizeRadioGroup.add(_sizeRadioButtons[i]);
176                         sizePanel.add(_sizeRadioButtons[i]);
177                 }
178                 waypointsPanel.add(sizePanel);
179                 waypointsPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
180                 midPanel.add(waypointsPanel);
181
182                 // Panel for window style
183                 JPanel windowStylePanel = new JPanel();
184                 windowStylePanel.setBorder(BorderFactory.createCompoundBorder(
185                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
186                 );
187                 windowStylePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
188                 windowStylePanel.add(new JLabel(I18nManager.getText("dialog.displaysettings.windowstyle")));
189                 windowStylePanel.add(Box.createHorizontalStrut(10));
190                 ButtonGroup styleGroup = new ButtonGroup();
191                 final String[] styleKeys = {"default", "nimbus"};
192                 _windowStyleRadios = new JRadioButton[2];
193                 for (int i=0; i<2; i++)
194                 {
195                         _windowStyleRadios[i] = new JRadioButton(
196                                 I18nManager.getText("dialog.displaysettings.windowstyle." + styleKeys[i]));
197                         styleGroup.add(_windowStyleRadios[i]);
198                         windowStylePanel.add(_windowStyleRadios[i]);
199                 }
200                 midPanel.add(windowStylePanel);
201                 mainPanel.add(midPanel, BorderLayout.CENTER);
202
203                 // button panel at bottom
204                 JPanel buttonPanel = new JPanel();
205                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
206                 // OK button
207                 _okButton = new JButton(I18nManager.getText("button.ok"));
208                 _okButton.addActionListener(new ActionListener() {
209                         public void actionPerformed(ActionEvent e) {
210                                 finish();
211                         }
212                 });
213                 buttonPanel.add(_okButton);
214                 // Cancel button
215                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
216                 cancelButton.addActionListener(new ActionListener() {
217                         public void actionPerformed(ActionEvent e) {
218                                 _dialog.dispose();
219                         }
220                 });
221                 buttonPanel.add(cancelButton);
222                 mainPanel.add(buttonPanel, BorderLayout.SOUTH);
223                 return mainPanel;
224         }
225
226
227         /**
228          * Show window
229          */
230         public void begin()
231         {
232                 if (_dialog == null)
233                 {
234                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
235                         _dialog.setLocationRelativeTo(_parentFrame);
236                         _dialog.getContentPane().add(makeContents());
237                         _dialog.pack();
238                 }
239                 // Set values from config
240                 int lineWidth = Config.getConfigInt(Config.KEY_LINE_WIDTH);
241                 if (lineWidth < 1 || lineWidth > 4) {lineWidth = 2;}
242                 _lineWidthField.setValue(lineWidth);
243                 _antialiasCheckbox.setSelected(Config.getConfigBoolean(Config.KEY_ANTIALIAS));
244                 _wpIconCombobox.setSelectedIndex(Config.getConfigInt(Config.KEY_WAYPOINT_ICONS));
245                 selectIconSizeRadio(Config.getConfigInt(Config.KEY_WAYPOINT_ICON_SIZE));
246                 selectWindowStyleRadio(Config.getConfigString(Config.KEY_WINDOW_STYLE));
247                 _dialog.setVisible(true);
248         }
249
250         /**
251          * Select the corresponding radio button according to the numeric value
252          * @param inValue numeric value saved in Config
253          */
254         private void selectIconSizeRadio(int inValue)
255         {
256                 if (inValue < 0 || inValue >= _sizeRadioButtons.length)
257                 {
258                         inValue = 1;
259                 }
260                 if (_sizeRadioButtons[inValue] != null)
261                 {
262                         _sizeRadioButtons[inValue].setSelected(true);
263                 }
264         }
265
266         /**
267          * Select the corresponding radio button according to the selected style
268          * @param inValue style string saved in Config
269          */
270         private void selectWindowStyleRadio(String inValue)
271         {
272                 int selectedRadio = 0;
273                 if (inValue != null && inValue.equals(STYLEKEY_NIMBUS))
274                 {
275                         selectedRadio = 1;
276                 }
277                 _windowStyleRadios[selectedRadio].setSelected(true);
278         }
279
280         /**
281          * @return numeric value of selected icon size according to radio buttons
282          */
283         private int getSelectedIconSize()
284         {
285                 for (int i=0; i<_sizeRadioButtons.length; i++)
286                 {
287                         if (_sizeRadioButtons[i] != null && _sizeRadioButtons[i].isSelected())
288                         {
289                                 return i;
290                         }
291                 }
292                 return 1; // default is medium
293         }
294
295         /**
296          * Save settings and close
297          */
298         public void finish()
299         {
300                 // update config
301                 int lineWidth = _lineWidthField.getValue();
302                 if (lineWidth < 1 || lineWidth > 4) {lineWidth = 2;}
303                 Config.setConfigInt(Config.KEY_LINE_WIDTH, lineWidth);
304                 Config.setConfigBoolean(Config.KEY_ANTIALIAS, _antialiasCheckbox.isSelected());
305                 Config.setConfigInt(Config.KEY_WAYPOINT_ICONS, _wpIconCombobox.getSelectedIndex());
306                 Config.setConfigInt(Config.KEY_WAYPOINT_ICON_SIZE, getSelectedIconSize());
307                 final String styleString = (_windowStyleRadios[1].isSelected() ? STYLEKEY_NIMBUS : null);
308                 Config.setConfigString(Config.KEY_WINDOW_STYLE, styleString);
309                 // refresh display
310                 UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);
311                 _dialog.dispose();
312         }
313 }