1 package tim.prune.function;
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;
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;
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;
33 * Function to set the tile server for the map backgrounds
35 public class SetMapBgFunction extends GenericFunction
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;
44 private AddMapSourceDialog _addDialog = null;
49 * @param inApp app object
51 public SetMapBgFunction(App inApp)
56 /** Get the name key */
57 public String getNameKey() {
58 return "function.setmapbg";
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());
77 _dialog.setVisible(true);
82 * Create dialog components
83 * @return Panel containing all gui elements in dialog
85 private Component makeDialogComponents()
87 JPanel dialogPanel = new JPanel();
88 dialogPanel.setLayout(new BorderLayout(8, 8));
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);
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) {
103 _list.addKeyListener(new KeyAdapter() {
104 public void keyReleased(KeyEvent e) {
105 super.keyReleased(e);
106 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
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) {
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());
128 rightPanel.add(_deleteButton);
129 dialogPanel.add(rightPanel, BorderLayout.EAST);
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)
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)
150 buttonPanel.add(cancelButton);
151 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
157 * Get the initial values from the Config and set gui values accordingly
159 private void initValues()
162 // Get selected value from config
163 int currSource = Config.getConfigInt(Config.KEY_MAPSOURCE_INDEX);
164 if (currSource < 0 || currSource >= _listModel.getSize()) {
167 _initialSource = _listModel.getSource(currSource).getSiteStrings();
168 _list.setSelectedIndex(currSource);
172 * @return index of selected server, or -1 if none
174 private int getSelectedServer()
176 return _list.getSelectedIndex();
180 * Enable or disable the buttons according to the selection
182 private void enableButtons()
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());
192 * Start the dialog to add a new map source to the list
194 private void addNewSource()
196 if (_addDialog == null) {
197 _addDialog = new AddMapSourceDialog(_dialog, this);
199 _addDialog.showDialog();
203 * Delete the selected map source so it is no longer available
204 * @param inIndex index within list
206 private void deleteMapSource(int inIndex)
208 MapSourceLibrary.deleteSource(inIndex);
214 * use the library to update the current list, after add or delete
216 public void updateList()
218 _listModel.fireChanged();
219 Config.setConfigString(Config.KEY_MAPSOURCE_LIST, MapSourceLibrary.getConfigString());
223 * Finish the dialog when OK pressed
225 private void finish()
227 int serverNum = getSelectedServer();
228 if (serverNum < 0) {serverNum = 0;}
229 Config.setConfigInt(Config.KEY_MAPSOURCE_INDEX, serverNum);
230 UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);