]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/settings/SetDisplaySettings.java
63286c5e86dcddce24736afe427a4bfc410bcb97
[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 JButton _okButton = null;
97
98
99         /**
100          * Constructor
101          * @param inApp app object
102          */
103         public SetDisplaySettings(App inApp)
104         {
105                 super(inApp);
106         }
107
108         /**
109          * Return the name key for this function
110          */
111         public String getNameKey()
112         {
113                 return "function.setdisplaysettings";
114         }
115
116         /**
117          * @return the contents of the window as a Component
118          */
119         private Component makeContents()
120         {
121                 JPanel mainPanel = new JPanel();
122                 mainPanel.setLayout(new BorderLayout(0, 5));
123                 JPanel midPanel = new JPanel();
124                 midPanel.setLayout(new BoxLayout(midPanel, BoxLayout.Y_AXIS));
125                 midPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
126
127                 JPanel linesPanel = new JPanel();
128                 linesPanel.setBorder(BorderFactory.createCompoundBorder(
129                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
130                 );
131                 GuiGridLayout grid = new GuiGridLayout(linesPanel);
132
133                 // line width
134                 JLabel lineWidthLabel = new JLabel(I18nManager.getText("dialog.displaysettings.linewidth"));
135                 grid.add(lineWidthLabel);
136                 _lineWidthField = new WholeNumberField(1);
137                 grid.add(_lineWidthField);
138                 // Antialiasing
139                 _antialiasCheckbox = new JCheckBox(I18nManager.getText("dialog.displaysettings.antialias"), false);
140                 grid.add(_antialiasCheckbox);
141                 grid.add(new JLabel(""));
142
143                 linesPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
144                 midPanel.add(linesPanel);
145                 midPanel.add(Box.createVerticalStrut(10));
146
147                 // Panel for waypoint icons
148                 JPanel waypointsPanel = new JPanel();
149                 waypointsPanel.setBorder(BorderFactory.createCompoundBorder(
150                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
151                 );
152                 waypointsPanel.setLayout(new BoxLayout(waypointsPanel, BoxLayout.Y_AXIS));
153                 // Select which waypoint icon to use
154                 JPanel iconPanel = new JPanel();
155                 GuiGridLayout iconGrid = new GuiGridLayout(iconPanel);
156                 JLabel headerLabel = new JLabel(I18nManager.getText("dialog.displaysettings.waypointicons"));
157                 headerLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
158                 iconGrid.add(headerLabel);
159                 _wpIconCombobox = new JComboBox<Integer>(new Integer[] {0, 1, 2, 3, 4});
160                 _wpIconCombobox.setRenderer(new IconComboRenderer());
161                 iconGrid.add(_wpIconCombobox);
162                 waypointsPanel.add(iconPanel);
163                 // Select size of waypoints
164                 JPanel sizePanel = new JPanel();
165                 sizePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
166                 _sizeRadioButtons = new JRadioButton[3];
167                 ButtonGroup sizeRadioGroup = new ButtonGroup();
168                 final String[] sizeKeys = {"small", "medium", "large"};
169                 for (int i=0; i<3; i++)
170                 {
171                         _sizeRadioButtons[i] = new JRadioButton(I18nManager.getText("dialog.displaysettings.size." + sizeKeys[i]));
172                         sizeRadioGroup.add(_sizeRadioButtons[i]);
173                         sizePanel.add(_sizeRadioButtons[i]);
174                 }
175                 waypointsPanel.add(sizePanel);
176                 waypointsPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
177                 midPanel.add(waypointsPanel);
178
179                 mainPanel.add(midPanel, BorderLayout.CENTER);
180
181                 // button panel at bottom
182                 JPanel buttonPanel = new JPanel();
183                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
184                 // OK button
185                 _okButton = new JButton(I18nManager.getText("button.ok"));
186                 _okButton.addActionListener(new ActionListener() {
187                         public void actionPerformed(ActionEvent e) {
188                                 finish();
189                         }
190                 });
191                 buttonPanel.add(_okButton);
192                 // Cancel button
193                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
194                 cancelButton.addActionListener(new ActionListener() {
195                         public void actionPerformed(ActionEvent e) {
196                                 _dialog.dispose();
197                         }
198                 });
199                 buttonPanel.add(cancelButton);
200                 mainPanel.add(buttonPanel, BorderLayout.SOUTH);
201                 return mainPanel;
202         }
203
204
205         /**
206          * Show window
207          */
208         public void begin()
209         {
210                 if (_dialog == null)
211                 {
212                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
213                         _dialog.setLocationRelativeTo(_parentFrame);
214                         _dialog.getContentPane().add(makeContents());
215                         _dialog.pack();
216                 }
217                 // Set values from config
218                 int lineWidth = Config.getConfigInt(Config.KEY_LINE_WIDTH);
219                 if (lineWidth < 1 || lineWidth > 4) {lineWidth = 2;}
220                 _lineWidthField.setValue(lineWidth);
221                 _antialiasCheckbox.setSelected(Config.getConfigBoolean(Config.KEY_ANTIALIAS));
222                 _wpIconCombobox.setSelectedIndex(Config.getConfigInt(Config.KEY_WAYPOINT_ICONS));
223                 selectIconSizeRadio(Config.getConfigInt(Config.KEY_WAYPOINT_ICON_SIZE));
224                 _dialog.setVisible(true);
225         }
226
227         /**
228          * Select the corresponding radio button according to the numeric value
229          * @param inValue numeric value saved in Config
230          */
231         private void selectIconSizeRadio(int inValue)
232         {
233                 if (inValue < 0 || inValue >= _sizeRadioButtons.length)
234                 {
235                         inValue = 1;
236                 }
237                 if (_sizeRadioButtons[inValue] != null)
238                 {
239                         _sizeRadioButtons[inValue].setSelected(true);
240                 }
241         }
242
243         /**
244          * @return numeric value of selected icon size according to radio buttons
245          */
246         private int getSelectedIconSize()
247         {
248                 for (int i=0; i<_sizeRadioButtons.length; i++)
249                 {
250                         if (_sizeRadioButtons[i] != null && _sizeRadioButtons[i].isSelected())
251                         {
252                                 return i;
253                         }
254                 }
255                 return 1; // default is medium
256         }
257
258         /**
259          * Save settings and close
260          */
261         public void finish()
262         {
263                 // update config
264                 int lineWidth = _lineWidthField.getValue();
265                 if (lineWidth < 1 || lineWidth > 4) {lineWidth = 2;}
266                 Config.setConfigInt(Config.KEY_LINE_WIDTH, lineWidth);
267                 Config.setConfigBoolean(Config.KEY_ANTIALIAS, _antialiasCheckbox.isSelected());
268                 Config.setConfigInt(Config.KEY_WAYPOINT_ICONS, _wpIconCombobox.getSelectedIndex());
269                 Config.setConfigInt(Config.KEY_WAYPOINT_ICON_SIZE, getSelectedIconSize());
270                 // refresh display
271                 UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);
272                 _dialog.dispose();
273         }
274 }