]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/function/SetMapBgFunction.java
Version 13.4, May 2012
[GpsPrune.git] / tim / prune / function / SetMapBgFunction.java
index a683634383d30767fe17edf7638dbd3b3624dff2..a42b789ef2661cfe9ec716b8cb0a96343dc6a584 100644 (file)
@@ -8,33 +8,44 @@ import java.awt.event.ActionListener;
 import java.awt.event.KeyAdapter;
 import java.awt.event.KeyEvent;
 
+import javax.swing.BorderFactory;
+import javax.swing.Box;
 import javax.swing.BoxLayout;
-import javax.swing.ButtonGroup;
 import javax.swing.JButton;
 import javax.swing.JDialog;
 import javax.swing.JLabel;
+import javax.swing.JList;
 import javax.swing.JPanel;
-import javax.swing.JRadioButton;
-import javax.swing.JTextField;
+import javax.swing.JScrollPane;
+import javax.swing.ListSelectionModel;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
 
 import tim.prune.App;
-import tim.prune.Config;
 import tim.prune.DataSubscriber;
 import tim.prune.GenericFunction;
 import tim.prune.I18nManager;
 import tim.prune.UpdateMessageBroker;
+import tim.prune.config.Config;
+import tim.prune.gui.map.MapSource;
+import tim.prune.gui.map.MapSourceLibrary;
 
 /**
  * Function to set the tile server for the map backgrounds
+ * Also allows call to add/edit/delete functions
  */
 public class SetMapBgFunction extends GenericFunction
 {
        private JDialog _dialog = null;
-       private JButton _okButton = null;
-       private JRadioButton[] _serverRadios = null;
-       private JTextField _serverUrl = null;
-       /** Index of 'other' server with freeform url */
-       private static final int OTHER_SERVER_NUM = 3;
+       private JList _list = null;
+       private MapSourceListModel _listModel = null;
+       private String _initialSource = null;
+       private JButton _okButton = null, _cancelButton = null;
+       private JButton _deleteButton = null, _editButton = null;
+       // Add dialog
+       private AddMapSourceDialog _addDialog = null;
+       // Flags for what has been edited
+       private boolean _sourcesEdited = false;
 
 
        /**
@@ -63,10 +74,10 @@ public class SetMapBgFunction extends GenericFunction
                        _dialog.setLocationRelativeTo(_parentFrame);
                        _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                        _dialog.getContentPane().add(makeDialogComponents());
-                       initValues();
                        _dialog.pack();
                }
-               enableOK();
+               initValues();
+               enableButtons();
                _dialog.setVisible(true);
        }
 
@@ -78,39 +89,58 @@ public class SetMapBgFunction extends GenericFunction
        private Component makeDialogComponents()
        {
                JPanel dialogPanel = new JPanel();
-               dialogPanel.setLayout(new BorderLayout());
-               // Main panel
-               JPanel mainPanel = new JPanel();
-               mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
-               _serverRadios = new JRadioButton[4];
-               ButtonGroup serverRadioGroup = new ButtonGroup();
-               String[] serverKeys = {"dialog.setmapbg.mapnik", "dialog.setmapbg.osma",
-                       "dialog.setmapbg.cyclemap", "dialog.setmapbg.other"};
-               // action listener for radios
-               ActionListener changeListener = new ActionListener() {
-                       public void actionPerformed(ActionEvent arg0) {
-                               enableOK();
+               dialogPanel.setLayout(new BorderLayout(8, 8));
+               // intro label
+               JLabel introLabel = new JLabel(I18nManager.getText("dialog.setmapbg.intro"));
+               introLabel.setBorder(BorderFactory.createEmptyBorder(5, 4, 1, 4));
+               dialogPanel.add(introLabel, BorderLayout.NORTH);
+               // list box
+               _listModel = new MapSourceListModel();
+               _list = new JList(_listModel);
+               _list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+               dialogPanel.add(new JScrollPane(_list), BorderLayout.CENTER);
+               _list.addListSelectionListener(new ListSelectionListener() {
+                       public void valueChanged(ListSelectionEvent arg0) {
+                               enableButtons();
                        }
-               };
-               // Create four radio buttons
-               for (int i=0; i<4; i++)
-               {
-                       _serverRadios[i] = new JRadioButton(I18nManager.getText(serverKeys[i]));
-                       _serverRadios[i].addActionListener(changeListener);
-                       serverRadioGroup.add(_serverRadios[i]);
-                       mainPanel.add(_serverRadios[i]);
-               }
-               // entry field for other server urls
-               mainPanel.add(new JLabel(I18nManager.getText("dialog.setmapbg.server")));
-               _serverUrl = new JTextField("", 12);
-               _serverUrl.addKeyListener(new KeyAdapter() {
+               });
+               _list.addKeyListener(new KeyAdapter() {
                        public void keyReleased(KeyEvent e) {
                                super.keyReleased(e);
-                               enableOK();
+                               if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
+                                       _dialog.dispose();
+                               }
+                       }
+               });
+               // button panel on right
+               JPanel rightPanel = new JPanel();
+               rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
+               JButton addButton = new JButton(I18nManager.getText("button.addnew"));
+               addButton.addActionListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent arg0) {
+                               addNewSource();
+                       }
+               });
+               rightPanel.add(addButton);
+               rightPanel.add(Box.createVerticalStrut(5));
+               // edit
+               _editButton = new JButton(I18nManager.getText("button.edit"));
+               _editButton.addActionListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent e) {
+                               editMapSource();
                        }
                });
-               mainPanel.add(_serverUrl);
-               dialogPanel.add(mainPanel, BorderLayout.NORTH);
+               rightPanel.add(_editButton);
+               rightPanel.add(Box.createVerticalStrut(5));
+               // delete
+               _deleteButton = new JButton(I18nManager.getText("button.delete"));
+               _deleteButton.addActionListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent arg0) {
+                               deleteMapSource();
+                       }
+               });
+               rightPanel.add(_deleteButton);
+               dialogPanel.add(rightPanel, BorderLayout.EAST);
 
                // button panel at bottom
                JPanel buttonPanel = new JPanel();
@@ -124,14 +154,14 @@ public class SetMapBgFunction extends GenericFunction
                };
                _okButton.addActionListener(okListener);
                buttonPanel.add(_okButton);
-               JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
-               cancelButton.addActionListener(new ActionListener() {
+               _cancelButton = new JButton(I18nManager.getText("button.cancel"));
+               _cancelButton.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e)
                        {
                                _dialog.dispose();
                        }
                });
-               buttonPanel.add(cancelButton);
+               buttonPanel.add(_cancelButton);
                dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
                return dialogPanel;
        }
@@ -142,49 +172,90 @@ public class SetMapBgFunction extends GenericFunction
         */
        private void initValues()
        {
-               // Get values from config
-               try {
-                       _serverRadios[Config.getConfigInt(Config.KEY_MAPSERVERINDEX)].setSelected(true);
-               }
-               catch (ArrayIndexOutOfBoundsException e) {} // ignore
-               String url = Config.getConfigString(Config.KEY_MAPSERVERURL);
-               if (url != null) {_serverUrl.setText(url);}
-               // Choose default if none selected
-               if (getSelectedServer() < 0) {
-                       _serverRadios[0].setSelected(true);
+               updateList();
+               // Get selected value from config
+               int currSource = Config.getConfigInt(Config.KEY_MAPSOURCE_INDEX);
+               if (currSource < 0 || currSource >= _listModel.getSize()) {
+                       currSource = 0;
                }
+               _initialSource = _listModel.getSource(currSource).getSiteStrings();
+               _list.setSelectedIndex(currSource);
+               // Scroll down to see selected index
+               _list.ensureIndexIsVisible(currSource);
+               _sourcesEdited = false;
        }
 
        /**
-        * @return index of selected radio button, or -1 if none
+        * @return index of selected server, or -1 if none
         */
        private int getSelectedServer()
        {
-               // Loop over all four radios
-               for (int i=0; i<4; i++) {
-                       if (_serverRadios[i].isSelected()) {return i;}
-               }
-               // None selected
-               return -1;
+               return _list.getSelectedIndex();
        }
 
        /**
-        * Enable or disable the OK button according to the selection
+        * Enable or disable the buttons according to the selection
         */
-       private void enableOK()
+       private void enableButtons()
        {
                int serverNum = getSelectedServer();
-               _okButton.setEnabled(inputOK());
-               _serverUrl.setEnabled(serverNum == OTHER_SERVER_NUM);
+               _okButton.setEnabled(serverNum >= 0 && serverNum < _listModel.getSize()
+                       && (_sourcesEdited || !_listModel.getSource(serverNum).getSiteStrings().equals(_initialSource)));
+               boolean hasCustomSource = serverNum >= MapSourceLibrary.getNumFixedSources()
+                       && serverNum < _listModel.getSize();
+               _editButton.setEnabled(hasCustomSource);
+               _deleteButton.setEnabled(hasCustomSource);
+               _cancelButton.setEnabled(!_sourcesEdited);
+       }
+
+       /**
+        * Start the dialog to add a new map source to the list
+        */
+       private void addNewSource()
+       {
+               addSource(null);
        }
 
        /**
-        * @return true if inputs are ok
+        * Start the dialog to either add or edit a map source
+        * @param inSource a current source to edit, or null to add a new one
         */
-       private boolean inputOK()
+       private void addSource(MapSource inSource)
+       {
+               if (_addDialog == null) {
+                       _addDialog = new AddMapSourceDialog(_dialog, this);
+               }
+               _addDialog.showDialog(inSource);
+       }
+
+       /**
+        * Delete the selected map source so it is no longer available
+        */
+       private void deleteMapSource()
        {
                int serverNum = getSelectedServer();
-               return serverNum >= 0 && (serverNum != OTHER_SERVER_NUM || _serverUrl.getText().length() > 4);
+               MapSourceLibrary.deleteSource(serverNum);
+               updateList();
+               enableButtons();
+       }
+
+       /**
+        * Open the dialog to edit the selected map source
+        */
+       private void editMapSource()
+       {
+               addSource(_listModel.getSource(getSelectedServer()));
+       }
+
+       /**
+        * use the library to update the current list, after add or edit or delete
+        */
+       public void updateList()
+       {
+               _listModel.fireChanged();
+               _sourcesEdited = true;
+               Config.setConfigString(Config.KEY_MAPSOURCE_LIST, MapSourceLibrary.getConfigString());
+               enableButtons();
        }
 
        /**
@@ -193,9 +264,8 @@ public class SetMapBgFunction extends GenericFunction
        private void finish()
        {
                int serverNum = getSelectedServer();
-               if (!inputOK()) {serverNum = 0;}
-               Config.setConfigInt(Config.KEY_MAPSERVERINDEX, serverNum);
-               Config.setConfigString(Config.KEY_MAPSERVERURL, _serverUrl.getText());
+               if (serverNum < 0) {serverNum = 0;}
+               Config.setConfigInt(Config.KEY_MAPSOURCE_INDEX, serverNum);
                UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);
                _dialog.dispose();
        }