1 package tim.prune.function.settings;
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;
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;
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;
34 * Function to set the tile server for the map backgrounds
35 * Also allows call to add/edit/delete functions
37 public class SetMapBgFunction extends GenericFunction
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;
46 private AddMapSourceDialog _addDialog = null;
47 // Flags for what has been edited
48 private boolean _sourcesEdited = false;
53 * @param inApp app object
55 public SetMapBgFunction(App inApp)
60 /** Get the name key */
61 public String getNameKey() {
62 return "function.setmapbg";
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());
81 _dialog.setVisible(true);
86 * Create dialog components
87 * @return Panel containing all gui elements in dialog
89 private Component makeDialogComponents()
91 JPanel dialogPanel = new JPanel();
92 dialogPanel.setLayout(new BorderLayout(8, 8));
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);
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) {
107 _list.addKeyListener(new KeyAdapter() {
108 public void keyReleased(KeyEvent e) {
109 super.keyReleased(e);
110 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
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) {
124 rightPanel.add(addButton);
125 rightPanel.add(Box.createVerticalStrut(5));
127 _editButton = new JButton(I18nManager.getText("button.edit"));
128 _editButton.addActionListener(new ActionListener() {
129 public void actionPerformed(ActionEvent e) {
133 rightPanel.add(_editButton);
134 rightPanel.add(Box.createVerticalStrut(5));
136 _deleteButton = new JButton(I18nManager.getText("button.delete"));
137 _deleteButton.addActionListener(new ActionListener() {
138 public void actionPerformed(ActionEvent arg0) {
142 rightPanel.add(_deleteButton);
143 dialogPanel.add(rightPanel, BorderLayout.EAST);
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)
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)
164 buttonPanel.add(_cancelButton);
165 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
171 * Get the initial values from the Config and set gui values accordingly
173 private void initValues()
176 // Get selected value from config
177 int currSource = Config.getConfigInt(Config.KEY_MAPSOURCE_INDEX);
178 if (currSource < 0 || currSource >= _listModel.getSize()) {
181 _initialSource = _listModel.getSource(currSource).getSiteStrings();
182 _list.setSelectedIndex(currSource);
183 // Scroll down to see selected index
184 _list.ensureIndexIsVisible(currSource);
185 _sourcesEdited = false;
189 * @return index of selected server, or -1 if none
191 private int getSelectedServer()
193 return _list.getSelectedIndex();
197 * Enable or disable the buttons according to the selection
199 private void enableButtons()
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);
212 * Start the dialog to add a new map source to the list
214 private void addNewSource()
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
223 private void addSource(MapSource inSource)
225 if (_addDialog == null) {
226 _addDialog = new AddMapSourceDialog(_dialog, this);
228 _addDialog.showDialog(inSource);
232 * Delete the selected map source so it is no longer available
234 private void deleteMapSource()
236 int serverNum = getSelectedServer();
237 MapSourceLibrary.deleteSource(serverNum);
243 * Open the dialog to edit the selected map source
245 private void editMapSource()
247 addSource(_listModel.getSource(getSelectedServer()));
251 * use the library to update the current list, after add or edit or delete
253 public void updateList()
255 _listModel.fireChanged();
256 _sourcesEdited = true;
257 Config.setConfigString(Config.KEY_MAPSOURCE_LIST, MapSourceLibrary.getConfigString());
262 * Finish the dialog when OK pressed
264 private void finish()
266 int serverNum = getSelectedServer();
267 if (serverNum < 0) {serverNum = 0;}
268 Config.setConfigInt(Config.KEY_MAPSOURCE_INDEX, serverNum);
269 UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);