]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/SetMapBgFunction.java
Version 7, February 2009
[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.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.BoxLayout;
12 import javax.swing.ButtonGroup;
13 import javax.swing.JButton;
14 import javax.swing.JDialog;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.JRadioButton;
18 import javax.swing.JTextField;
19
20 import tim.prune.App;
21 import tim.prune.Config;
22 import tim.prune.DataSubscriber;
23 import tim.prune.GenericFunction;
24 import tim.prune.I18nManager;
25 import tim.prune.UpdateMessageBroker;
26
27 /**
28  * Function to set the tile server for the map backgrounds
29  */
30 public class SetMapBgFunction extends GenericFunction
31 {
32         private JDialog _dialog = null;
33         private JButton _okButton = null;
34         private JRadioButton[] _serverRadios = null;
35         private JTextField _serverUrl = null;
36         /** Index of 'other' server with freeform url */
37         private static final int OTHER_SERVER_NUM = 3;
38
39
40         /**
41          * Constructor
42          * @param inApp app object
43          */
44         public SetMapBgFunction(App inApp)
45         {
46                 super(inApp);
47         }
48
49         /** Get the name key */
50         public String getNameKey() {
51                 return "function.setmapbg";
52         }
53
54         /**
55          * Begin the function
56          */
57         public void begin()
58         {
59                 // Make dialog window
60                 if (_dialog == null)
61                 {
62                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
63                         _dialog.setLocationRelativeTo(_parentFrame);
64                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
65                         _dialog.getContentPane().add(makeDialogComponents());
66                         initValues();
67                         _dialog.pack();
68                 }
69                 enableOK();
70                 _dialog.setVisible(true);
71         }
72
73
74         /**
75          * Create dialog components
76          * @return Panel containing all gui elements in dialog
77          */
78         private Component makeDialogComponents()
79         {
80                 JPanel dialogPanel = new JPanel();
81                 dialogPanel.setLayout(new BorderLayout());
82                 // Main panel
83                 JPanel mainPanel = new JPanel();
84                 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
85                 _serverRadios = new JRadioButton[4];
86                 ButtonGroup serverRadioGroup = new ButtonGroup();
87                 String[] serverKeys = {"dialog.setmapbg.mapnik", "dialog.setmapbg.osma",
88                         "dialog.setmapbg.cyclemap", "dialog.setmapbg.other"};
89                 // action listener for radios
90                 ActionListener changeListener = new ActionListener() {
91                         public void actionPerformed(ActionEvent arg0) {
92                                 enableOK();
93                         }
94                 };
95                 // Create four radio buttons
96                 for (int i=0; i<4; i++)
97                 {
98                         _serverRadios[i] = new JRadioButton(I18nManager.getText(serverKeys[i]));
99                         _serverRadios[i].addActionListener(changeListener);
100                         serverRadioGroup.add(_serverRadios[i]);
101                         mainPanel.add(_serverRadios[i]);
102                 }
103                 // entry field for other server urls
104                 mainPanel.add(new JLabel(I18nManager.getText("dialog.setmapbg.server")));
105                 _serverUrl = new JTextField("", 12);
106                 _serverUrl.addKeyListener(new KeyAdapter() {
107                         public void keyReleased(KeyEvent e) {
108                                 super.keyReleased(e);
109                                 enableOK();
110                         }
111                 });
112                 mainPanel.add(_serverUrl);
113                 dialogPanel.add(mainPanel, BorderLayout.NORTH);
114
115                 // button panel at bottom
116                 JPanel buttonPanel = new JPanel();
117                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
118                 _okButton = new JButton(I18nManager.getText("button.ok"));
119                 ActionListener okListener = new ActionListener() {
120                         public void actionPerformed(ActionEvent e)
121                         {
122                                 finish();
123                         }
124                 };
125                 _okButton.addActionListener(okListener);
126                 buttonPanel.add(_okButton);
127                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
128                 cancelButton.addActionListener(new ActionListener() {
129                         public void actionPerformed(ActionEvent e)
130                         {
131                                 _dialog.dispose();
132                         }
133                 });
134                 buttonPanel.add(cancelButton);
135                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
136                 return dialogPanel;
137         }
138
139
140         /**
141          * Get the initial values from the Config and set gui values accordingly
142          */
143         private void initValues()
144         {
145                 // Get values from config
146                 try {
147                         _serverRadios[Config.getMapServerIndex()].setSelected(true);
148                 }
149                 catch (ArrayIndexOutOfBoundsException e) {} // ignore
150                 String url = Config.getMapServerUrl();
151                 if (url != null) {_serverUrl.setText(url);}
152                 // Choose default if none selected
153                 if (getSelectedServer() < 0) {
154                         _serverRadios[0].setSelected(true);
155                 }
156         }
157
158         /**
159          * @return index of selected radio button, or -1 if none
160          */
161         private int getSelectedServer()
162         {
163                 // Loop over all four radios
164                 for (int i=0; i<4; i++) {
165                         if (_serverRadios[i].isSelected()) {return i;}
166                 }
167                 // None selected
168                 return -1;
169         }
170
171         /**
172          * Enable or disable the OK button according to the selection
173          */
174         private void enableOK()
175         {
176                 int serverNum = getSelectedServer();
177                 _okButton.setEnabled(inputOK());
178                 _serverUrl.setEnabled(serverNum == OTHER_SERVER_NUM);
179         }
180
181         /**
182          * @return true if inputs are ok
183          */
184         private boolean inputOK()
185         {
186                 int serverNum = getSelectedServer();
187                 return serverNum >= 0 && (serverNum != OTHER_SERVER_NUM || _serverUrl.getText().length() > 4);
188         }
189
190         /**
191          * Finish the dialog when OK pressed
192          */
193         private void finish()
194         {
195                 int serverNum = getSelectedServer();
196                 if (!inputOK()) {serverNum = 0;}
197                 Config.setMapServerIndex(serverNum);
198                 Config.setMapServerUrl(_serverUrl.getText());
199                 UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);
200                 _dialog.dispose();
201         }
202 }