1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
package tim.prune.function;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
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.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
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.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 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;
/**
* Constructor
* @param inApp app object
*/
public SetMapBgFunction(App inApp)
{
super(inApp);
}
/** Get the name key */
public String getNameKey() {
return "function.setmapbg";
}
/**
* Begin the function
*/
public void begin()
{
// Make dialog window
if (_dialog == null)
{
_dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
_dialog.setLocationRelativeTo(_parentFrame);
_dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
_dialog.getContentPane().add(makeDialogComponents());
_dialog.pack();
}
initValues();
enableButtons();
_dialog.setVisible(true);
}
/**
* Create dialog components
* @return Panel containing all gui elements in dialog
*/
private Component makeDialogComponents()
{
JPanel dialogPanel = new JPanel();
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();
}
});
_list.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
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();
}
});
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();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
_okButton = new JButton(I18nManager.getText("button.ok"));
ActionListener okListener = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
finish();
}
};
_okButton.addActionListener(okListener);
buttonPanel.add(_okButton);
_cancelButton = new JButton(I18nManager.getText("button.cancel"));
_cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
_dialog.dispose();
}
});
buttonPanel.add(_cancelButton);
dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
return dialogPanel;
}
/**
* Get the initial values from the Config and set gui values accordingly
*/
private void initValues()
{
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 server, or -1 if none
*/
private int getSelectedServer()
{
return _list.getSelectedIndex();
}
/**
* Enable or disable the buttons according to the selection
*/
private void enableButtons()
{
int serverNum = getSelectedServer();
_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);
}
/**
* 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 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();
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();
}
/**
* Finish the dialog when OK pressed
*/
private void finish()
{
int serverNum = getSelectedServer();
if (serverNum < 0) {serverNum = 0;}
Config.setConfigInt(Config.KEY_MAPSOURCE_INDEX, serverNum);
UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);
_dialog.dispose();
}
}
|