]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/settings/SetMapBgFunction.java
Version 18.6, December 2016
[GpsPrune.git] / tim / prune / function / settings / SetMapBgFunction.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 import java.awt.event.KeyAdapter;
9 import java.awt.event.KeyEvent;
10
11 import javax.swing.BorderFactory;
12 import javax.swing.Box;
13 import javax.swing.BoxLayout;
14 import javax.swing.JButton;
15 import javax.swing.JDialog;
16 import javax.swing.JLabel;
17 import javax.swing.JList;
18 import javax.swing.JPanel;
19 import javax.swing.JScrollPane;
20 import javax.swing.ListSelectionModel;
21 import javax.swing.event.ListSelectionEvent;
22 import javax.swing.event.ListSelectionListener;
23
24 import tim.prune.App;
25 import tim.prune.DataSubscriber;
26 import tim.prune.GenericFunction;
27 import tim.prune.I18nManager;
28 import tim.prune.UpdateMessageBroker;
29 import tim.prune.config.Config;
30 import tim.prune.gui.map.MapSource;
31 import tim.prune.gui.map.MapSourceLibrary;
32
33 /**
34  * Function to set the tile server for the map backgrounds
35  * Also allows call to add/edit/delete functions
36  */
37 public class SetMapBgFunction extends GenericFunction
38 {
39         private JDialog _dialog = null;
40         private JList<String> _list = null;
41         private MapSourceListModel _listModel = null;
42         private String _initialSource = null;
43         private JButton _okButton = null, _cancelButton = null;
44         private JButton _deleteButton = null, _editButton = null;
45         // Add dialog
46         private AddMapSourceDialog _addDialog = null;
47         // Flags for what has been edited
48         private boolean _sourcesEdited = false;
49
50
51         /**
52          * Constructor
53          * @param inApp app object
54          */
55         public SetMapBgFunction(App inApp)
56         {
57                 super(inApp);
58         }
59
60         /** Get the name key */
61         public String getNameKey() {
62                 return "function.setmapbg";
63         }
64
65         /**
66          * Begin the function
67          */
68         public void begin()
69         {
70                 // Make dialog window
71                 if (_dialog == null)
72                 {
73                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
74                         _dialog.setLocationRelativeTo(_parentFrame);
75                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
76                         _dialog.getContentPane().add(makeDialogComponents());
77                         _dialog.pack();
78                 }
79                 initValues();
80                 enableButtons();
81                 _dialog.setVisible(true);
82         }
83
84
85         /**
86          * Create dialog components
87          * @return Panel containing all gui elements in dialog
88          */
89         private Component makeDialogComponents()
90         {
91                 JPanel dialogPanel = new JPanel();
92                 dialogPanel.setLayout(new BorderLayout(8, 8));
93                 // intro label
94                 JLabel introLabel = new JLabel(I18nManager.getText("dialog.setmapbg.intro"));
95                 introLabel.setBorder(BorderFactory.createEmptyBorder(5, 4, 1, 4));
96                 dialogPanel.add(introLabel, BorderLayout.NORTH);
97                 // list box
98                 _listModel = new MapSourceListModel();
99                 _list = new JList<String>(_listModel);
100                 _list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
101                 dialogPanel.add(new JScrollPane(_list), BorderLayout.CENTER);
102                 _list.addListSelectionListener(new ListSelectionListener() {
103                         public void valueChanged(ListSelectionEvent arg0) {
104                                 enableButtons();
105                         }
106                 });
107                 _list.addKeyListener(new KeyAdapter() {
108                         public void keyReleased(KeyEvent e) {
109                                 super.keyReleased(e);
110                                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
111                                         _dialog.dispose();
112                                 }
113                         }
114                 });
115                 // button panel on right
116                 JPanel rightPanel = new JPanel();
117                 rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
118                 JButton addButton = new JButton(I18nManager.getText("button.addnew"));
119                 addButton.addActionListener(new ActionListener() {
120                         public void actionPerformed(ActionEvent arg0) {
121                                 addNewSource();
122                         }
123                 });
124                 rightPanel.add(addButton);
125                 rightPanel.add(Box.createVerticalStrut(5));
126                 // edit
127                 _editButton = new JButton(I18nManager.getText("button.edit"));
128                 _editButton.addActionListener(new ActionListener() {
129                         public void actionPerformed(ActionEvent e) {
130                                 editMapSource();
131                         }
132                 });
133                 rightPanel.add(_editButton);
134                 rightPanel.add(Box.createVerticalStrut(5));
135                 // delete
136                 _deleteButton = new JButton(I18nManager.getText("button.delete"));
137                 _deleteButton.addActionListener(new ActionListener() {
138                         public void actionPerformed(ActionEvent arg0) {
139                                 deleteMapSource();
140                         }
141                 });
142                 rightPanel.add(_deleteButton);
143                 dialogPanel.add(rightPanel, BorderLayout.EAST);
144
145                 // button panel at bottom
146                 JPanel buttonPanel = new JPanel();
147                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
148                 _okButton = new JButton(I18nManager.getText("button.ok"));
149                 ActionListener okListener = new ActionListener() {
150                         public void actionPerformed(ActionEvent e)
151                         {
152                                 finish();
153                         }
154                 };
155                 _okButton.addActionListener(okListener);
156                 buttonPanel.add(_okButton);
157                 _cancelButton = new JButton(I18nManager.getText("button.cancel"));
158                 _cancelButton.addActionListener(new ActionListener() {
159                         public void actionPerformed(ActionEvent e)
160                         {
161                                 _dialog.dispose();
162                         }
163                 });
164                 buttonPanel.add(_cancelButton);
165                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
166                 return dialogPanel;
167         }
168
169
170         /**
171          * Get the initial values from the Config and set gui values accordingly
172          */
173         private void initValues()
174         {
175                 updateList();
176                 // Get selected value from config
177                 int currSource = Config.getConfigInt(Config.KEY_MAPSOURCE_INDEX);
178                 if (currSource < 0 || currSource >= _listModel.getSize()) {
179                         currSource = 0;
180                 }
181                 _initialSource = _listModel.getSource(currSource).getSiteStrings();
182                 _list.setSelectedIndex(currSource);
183                 // Scroll down to see selected index
184                 _list.ensureIndexIsVisible(currSource);
185                 _sourcesEdited = false;
186         }
187
188         /**
189          * @return index of selected server, or -1 if none
190          */
191         private int getSelectedServer()
192         {
193                 return _list.getSelectedIndex();
194         }
195
196         /**
197          * Enable or disable the buttons according to the selection
198          */
199         private void enableButtons()
200         {
201                 int serverNum = getSelectedServer();
202                 _okButton.setEnabled(serverNum >= 0 && serverNum < _listModel.getSize()
203                         && (_sourcesEdited || !_listModel.getSource(serverNum).getSiteStrings().equals(_initialSource)));
204                 boolean hasCustomSource = serverNum >= MapSourceLibrary.getNumFixedSources()
205                         && serverNum < _listModel.getSize();
206                 _editButton.setEnabled(hasCustomSource);
207                 _deleteButton.setEnabled(hasCustomSource);
208                 _cancelButton.setEnabled(!_sourcesEdited);
209         }
210
211         /**
212          * Start the dialog to add a new map source to the list
213          */
214         private void addNewSource()
215         {
216                 addSource(null);
217         }
218
219         /**
220          * Start the dialog to either add or edit a map source
221          * @param inSource a current source to edit, or null to add a new one
222          */
223         private void addSource(MapSource inSource)
224         {
225                 if (_addDialog == null) {
226                         _addDialog = new AddMapSourceDialog(_dialog, this);
227                 }
228                 _addDialog.showDialog(inSource);
229         }
230
231         /**
232          * Delete the selected map source so it is no longer available
233          */
234         private void deleteMapSource()
235         {
236                 int serverNum = getSelectedServer();
237                 MapSourceLibrary.deleteSource(serverNum);
238                 updateList();
239                 enableButtons();
240         }
241
242         /**
243          * Open the dialog to edit the selected map source
244          */
245         private void editMapSource()
246         {
247                 addSource(_listModel.getSource(getSelectedServer()));
248         }
249
250         /**
251          * use the library to update the current list, after add or edit or delete
252          */
253         public void updateList()
254         {
255                 _listModel.fireChanged();
256                 _sourcesEdited = true;
257                 Config.setConfigString(Config.KEY_MAPSOURCE_LIST, MapSourceLibrary.getConfigString());
258                 enableButtons();
259         }
260
261         /**
262          * Finish the dialog when OK pressed
263          */
264         private void finish()
265         {
266                 int serverNum = getSelectedServer();
267                 if (serverNum < 0) {serverNum = 0;}
268                 Config.setConfigInt(Config.KEY_MAPSOURCE_INDEX, serverNum);
269                 UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);
270                 _dialog.dispose();
271         }
272 }