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