| 1 | package org.openstreetmap.josm.gui.preferences.advanced;
|
|---|
| 2 |
|
|---|
| 3 | import java.awt.Color;
|
|---|
| 4 | import java.awt.Component;
|
|---|
| 5 | import java.awt.Font;
|
|---|
| 6 | import java.awt.GridBagLayout;
|
|---|
| 7 | import java.awt.event.MouseAdapter;
|
|---|
| 8 | import java.awt.event.MouseEvent;
|
|---|
| 9 | import java.util.ArrayList;
|
|---|
| 10 | import java.util.Collection;
|
|---|
| 11 | import java.util.List;
|
|---|
| 12 | import java.util.Map;
|
|---|
| 13 | import javax.swing.ButtonGroup;
|
|---|
| 14 | import javax.swing.DefaultCellEditor;
|
|---|
| 15 | import javax.swing.JComponent;
|
|---|
| 16 | import javax.swing.JLabel;
|
|---|
| 17 | import javax.swing.JOptionPane;
|
|---|
| 18 | import javax.swing.JPanel;
|
|---|
| 19 | import javax.swing.JRadioButton;
|
|---|
| 20 | import javax.swing.JTable;
|
|---|
| 21 | import javax.swing.table.DefaultTableCellRenderer;
|
|---|
| 22 | import javax.swing.table.DefaultTableModel;
|
|---|
| 23 | import org.openstreetmap.josm.Main;
|
|---|
| 24 | import org.openstreetmap.josm.data.Preferences;
|
|---|
| 25 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 26 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
|---|
| 27 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 28 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
|---|
| 29 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 30 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Component for editing list of preferences as a table
|
|---|
| 34 | * @since 6021 : extracted from AdvancedPreference class
|
|---|
| 35 | */
|
|---|
| 36 | public class PreferencesTable extends JTable {
|
|---|
| 37 | private AllSettingsTableModel model;
|
|---|
| 38 | private final List<PrefEntry> displayData;
|
|---|
| 39 |
|
|---|
| 40 | public PreferencesTable(List<PrefEntry> displayData) {
|
|---|
| 41 | this.displayData = displayData;
|
|---|
| 42 | model = new AllSettingsTableModel();
|
|---|
| 43 | setModel(model);
|
|---|
| 44 | putClientProperty("terminateEditOnFocusLost", true);
|
|---|
| 45 | getColumnModel().getColumn(1).setCellRenderer(new SettingCellRenderer());
|
|---|
| 46 | getColumnModel().getColumn(1).setCellEditor(new SettingCellEditor());
|
|---|
| 47 |
|
|---|
| 48 | addMouseListener(new MouseAdapter(){
|
|---|
| 49 | @Override public void mouseClicked(MouseEvent e) {
|
|---|
| 50 | if (e.getClickCount() == 2) {
|
|---|
| 51 | editPreference(PreferencesTable.this);
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 | });
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * This method should be called when displayed data was changed form external code
|
|---|
| 59 | */
|
|---|
| 60 | public void fireDataChanged() {
|
|---|
| 61 | model.fireTableDataChanged();
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * The list of currently selected rows
|
|---|
| 67 | * @return newly created list of PrefEntry
|
|---|
| 68 | */
|
|---|
| 69 | public List<PrefEntry> getSelectedItems() {
|
|---|
| 70 | List<PrefEntry> entries = new ArrayList<PrefEntry>();
|
|---|
| 71 | for (int row : getSelectedRows()) {
|
|---|
| 72 | PrefEntry p = (PrefEntry) model.getValueAt(row, -1);
|
|---|
| 73 | entries.add(p);
|
|---|
| 74 | }
|
|---|
| 75 | return entries;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * Call this to edit selected row in preferences table
|
|---|
| 80 | * @param gui - parent component for messagebox
|
|---|
| 81 | * @return true if editing was actually performed during this call
|
|---|
| 82 | */
|
|---|
| 83 | public boolean editPreference(final JComponent gui) {
|
|---|
| 84 | if (getSelectedRowCount() != 1) {
|
|---|
| 85 | JOptionPane.showMessageDialog(
|
|---|
| 86 | gui,
|
|---|
| 87 | tr("Please select the row to edit."),
|
|---|
| 88 | tr("Warning"),
|
|---|
| 89 | JOptionPane.WARNING_MESSAGE
|
|---|
| 90 | );
|
|---|
| 91 | return false;
|
|---|
| 92 | }
|
|---|
| 93 | final PrefEntry e = (PrefEntry) model.getValueAt(getSelectedRow(), 1);
|
|---|
| 94 | Preferences.Setting stg = e.getValue();
|
|---|
| 95 | if (stg instanceof Preferences.StringSetting) {
|
|---|
| 96 | editCellAt(getSelectedRow(), 1);
|
|---|
| 97 | Component editor = getEditorComponent();
|
|---|
| 98 | if (editor != null) {
|
|---|
| 99 | editor.requestFocus();
|
|---|
| 100 | }
|
|---|
| 101 | } else if (stg instanceof Preferences.ListSetting) {
|
|---|
| 102 | Preferences.ListSetting lSetting = (Preferences.ListSetting) stg;
|
|---|
| 103 | ListEditor lEditor = new ListEditor(gui, e, lSetting);
|
|---|
| 104 | lEditor.showDialog();
|
|---|
| 105 | if (lEditor.getValue() == 1) {
|
|---|
| 106 | List<String> data = lEditor.getData();
|
|---|
| 107 | if (!Preferences.equalCollection(lSetting.getValue(), data)) {
|
|---|
| 108 | e.setValue(new Preferences.ListSetting(data));
|
|---|
| 109 | return true;
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 | } else if (stg instanceof Preferences.ListListSetting) {
|
|---|
| 113 | ListListEditor llEditor = new ListListEditor(gui, e, (Preferences.ListListSetting) stg);
|
|---|
| 114 | llEditor.showDialog();
|
|---|
| 115 | if (llEditor.getValue() == 1) {
|
|---|
| 116 | List<List<String>> data = llEditor.getData();
|
|---|
| 117 | @SuppressWarnings("unchecked")
|
|---|
| 118 | Collection<Collection<String>> stgValue = (Collection) stg.getValue();
|
|---|
| 119 | if (!Preferences.equalArray(stgValue, data)) {
|
|---|
| 120 | e.setValue(new Preferences.ListListSetting(data));
|
|---|
| 121 | return true;
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | } else if (stg instanceof Preferences.MapListSetting) {
|
|---|
| 125 | Preferences.MapListSetting mlSetting = (Preferences.MapListSetting) stg;
|
|---|
| 126 | MapListEditor mlEditor = new MapListEditor(gui, e, mlSetting);
|
|---|
| 127 | mlEditor.showDialog();
|
|---|
| 128 | if (mlEditor.getValue() == 1) {
|
|---|
| 129 | List<Map<String, String>> data = mlEditor.getData();
|
|---|
| 130 | if (!Preferences.equalListOfStructs(mlSetting.getValue(), data)) {
|
|---|
| 131 | e.setValue(new Preferences.MapListSetting(data));
|
|---|
| 132 | return true;
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | return false;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * Add new preference to the table
|
|---|
| 141 | * @param gui - parent component for asking dialogs
|
|---|
| 142 | * @return newly created entry or null if adding was cancelled
|
|---|
| 143 | */
|
|---|
| 144 | public PrefEntry addPreference(final JComponent gui) {
|
|---|
| 145 | JPanel p = new JPanel(new GridBagLayout());
|
|---|
| 146 | p.add(new JLabel(tr("Key")), GBC.std().insets(0,0,5,0));
|
|---|
| 147 | JosmTextField tkey = new JosmTextField("", 50);
|
|---|
| 148 | p.add(tkey, GBC.eop().insets(5,0,0,0).fill(GBC.HORIZONTAL));
|
|---|
| 149 |
|
|---|
| 150 | p.add(new JLabel(tr("Select Setting Type:")), GBC.eol().insets(5,15,5,0));
|
|---|
| 151 |
|
|---|
| 152 | JRadioButton rbString = new JRadioButton(tr("Simple"));
|
|---|
| 153 | JRadioButton rbList = new JRadioButton(tr("List"));
|
|---|
| 154 | JRadioButton rbListList = new JRadioButton(tr("List of lists"));
|
|---|
| 155 | JRadioButton rbMapList = new JRadioButton(tr("List of maps"));
|
|---|
| 156 |
|
|---|
| 157 | ButtonGroup group = new ButtonGroup();
|
|---|
| 158 | group.add(rbString);
|
|---|
| 159 | group.add(rbList);
|
|---|
| 160 | group.add(rbListList);
|
|---|
| 161 | group.add(rbMapList);
|
|---|
| 162 |
|
|---|
| 163 | p.add(rbString, GBC.eol());
|
|---|
| 164 | p.add(rbList, GBC.eol());
|
|---|
| 165 | p.add(rbListList, GBC.eol());
|
|---|
| 166 | p.add(rbMapList, GBC.eol());
|
|---|
| 167 |
|
|---|
| 168 | rbString.setSelected(true);
|
|---|
| 169 |
|
|---|
| 170 | ExtendedDialog dlg = new ExtendedDialog(gui, tr("Add setting"), new String[] {tr("OK"), tr("Cancel")});
|
|---|
| 171 | dlg.setButtonIcons(new String[] {"ok.png", "cancel.png"});
|
|---|
| 172 | dlg.setContent(p);
|
|---|
| 173 | dlg.showDialog();
|
|---|
| 174 |
|
|---|
| 175 | PrefEntry pe = null;
|
|---|
| 176 | boolean ok = false;
|
|---|
| 177 | if (dlg.getValue() == 1) {
|
|---|
| 178 | if (rbString.isSelected()) {
|
|---|
| 179 | Preferences.StringSetting sSetting = new Preferences.StringSetting(null);
|
|---|
| 180 | pe = new PrefEntry(tkey.getText(), sSetting, sSetting, false);
|
|---|
| 181 | StringEditor sEditor = new StringEditor(gui, pe, sSetting);
|
|---|
| 182 | sEditor.showDialog();
|
|---|
| 183 | if (sEditor.getValue() == 1) {
|
|---|
| 184 | String data = sEditor.getData();
|
|---|
| 185 | if (!Utils.equal(sSetting.getValue(), data)) {
|
|---|
| 186 | pe.setValue(new Preferences.StringSetting(data));
|
|---|
| 187 | ok = true;
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 | } else if (rbList.isSelected()) {
|
|---|
| 191 | Preferences.ListSetting lSetting = new Preferences.ListSetting(null);
|
|---|
| 192 | pe = new PrefEntry(tkey.getText(), lSetting, lSetting, false);
|
|---|
| 193 | ListEditor lEditor = new ListEditor(gui, pe, lSetting);
|
|---|
| 194 | lEditor.showDialog();
|
|---|
| 195 | if (lEditor.getValue() == 1) {
|
|---|
| 196 | List<String> data = lEditor.getData();
|
|---|
| 197 | if (!Preferences.equalCollection(lSetting.getValue(), data)) {
|
|---|
| 198 | pe.setValue(new Preferences.ListSetting(data));
|
|---|
| 199 | ok = true;
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 | } else if (rbListList.isSelected()) {
|
|---|
| 203 | Preferences.ListListSetting llSetting = new Preferences.ListListSetting(null);
|
|---|
| 204 | pe = new PrefEntry(tkey.getText(), llSetting, llSetting, false);
|
|---|
| 205 | ListListEditor llEditor = new ListListEditor(gui, pe, llSetting);
|
|---|
| 206 | llEditor.showDialog();
|
|---|
| 207 | if (llEditor.getValue() == 1) {
|
|---|
| 208 | List<List<String>> data = llEditor.getData();
|
|---|
| 209 | @SuppressWarnings("unchecked")
|
|---|
| 210 | Collection<Collection<String>> llSettingValue = (Collection) llSetting.getValue();
|
|---|
| 211 | if (!Preferences.equalArray(llSettingValue, data)) {
|
|---|
| 212 | pe.setValue(new Preferences.ListListSetting(data));
|
|---|
| 213 | ok = true;
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|
| 216 | } else if (rbMapList.isSelected()) {
|
|---|
| 217 | Preferences.MapListSetting mlSetting = new Preferences.MapListSetting(null);
|
|---|
| 218 | pe = new PrefEntry(tkey.getText(), mlSetting, mlSetting, false);
|
|---|
| 219 | MapListEditor mlEditor = new MapListEditor(gui, pe, mlSetting);
|
|---|
| 220 | mlEditor.showDialog();
|
|---|
| 221 | if (mlEditor.getValue() == 1) {
|
|---|
| 222 | List<Map<String, String>> data = mlEditor.getData();
|
|---|
| 223 | if (!Preferences.equalListOfStructs(mlSetting.getValue(), data)) {
|
|---|
| 224 | pe.setValue(new Preferences.MapListSetting(data));
|
|---|
| 225 | ok = true;
|
|---|
| 226 | }
|
|---|
| 227 | }
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 | if (ok)
|
|---|
| 231 | return pe;
|
|---|
| 232 | else
|
|---|
| 233 | return null;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /**
|
|---|
| 237 | * Reset selected preferences to their default values
|
|---|
| 238 | * @param gui - parent component to display warning messages
|
|---|
| 239 | */
|
|---|
| 240 | public void resetPreferences(final JComponent gui) {
|
|---|
| 241 | if (getSelectedRowCount() == 0) {
|
|---|
| 242 | JOptionPane.showMessageDialog(
|
|---|
| 243 | gui,
|
|---|
| 244 | tr("Please select the row to delete."),
|
|---|
| 245 | tr("Warning"),
|
|---|
| 246 | JOptionPane.WARNING_MESSAGE
|
|---|
| 247 | );
|
|---|
| 248 | return;
|
|---|
| 249 | }
|
|---|
| 250 | for (int row : getSelectedRows()) {
|
|---|
| 251 | PrefEntry e = displayData.get(row);
|
|---|
| 252 | e.reset();
|
|---|
| 253 | }
|
|---|
| 254 | fireDataChanged();
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | private class AllSettingsTableModel extends DefaultTableModel {
|
|---|
| 258 |
|
|---|
| 259 | public AllSettingsTableModel() {
|
|---|
| 260 | setColumnIdentifiers(new String[]{tr("Key"), tr("Value")});
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | @Override
|
|---|
| 264 | public boolean isCellEditable(int row, int column) {
|
|---|
| 265 | return column == 1 && (displayData.get(row).getValue() instanceof Preferences.StringSetting);
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | @Override
|
|---|
| 269 | public int getRowCount() {
|
|---|
| 270 | return displayData.size();
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | @Override
|
|---|
| 274 | public Object getValueAt(int row, int column) {
|
|---|
| 275 | if (column == 0)
|
|---|
| 276 | return displayData.get(row).getKey();
|
|---|
| 277 | else
|
|---|
| 278 | return displayData.get(row);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | @Override
|
|---|
| 282 | public void setValueAt(Object o, int row, int column) {
|
|---|
| 283 | PrefEntry pe = displayData.get(row);
|
|---|
| 284 | String s = (String) o;
|
|---|
| 285 | if (!s.equals(pe.getValue().getValue())) {
|
|---|
| 286 | pe.setValue(new Preferences.StringSetting(s));
|
|---|
| 287 | fireTableCellUpdated(row, column);
|
|---|
| 288 | }
|
|---|
| 289 | }
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | private static class SettingCellRenderer extends DefaultTableCellRenderer {
|
|---|
| 293 | private Color backgroundColor = Main.pref.getUIColor("Table.background");
|
|---|
| 294 | private Color changedColor = Main.pref.getColor(
|
|---|
| 295 | marktr("Advanced Background: Changed"),
|
|---|
| 296 | new Color(200,255,200));
|
|---|
| 297 | private Color foregroundColor = Main.pref.getUIColor("Table.foreground");
|
|---|
| 298 | private Color nonDefaultColor = Main.pref.getColor(
|
|---|
| 299 | marktr("Advanced Background: NonDefalut"),
|
|---|
| 300 | new Color(255,255,200));
|
|---|
| 301 |
|
|---|
| 302 | @Override
|
|---|
| 303 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
|---|
| 304 | if (value == null)
|
|---|
| 305 | return this;
|
|---|
| 306 | PrefEntry pe = (PrefEntry) value;
|
|---|
| 307 | Preferences.Setting setting = pe.getValue();
|
|---|
| 308 | Object val = setting.getValue();
|
|---|
| 309 | String display = val != null ? val.toString() : "<html><i><"+tr("unset")+"></i></html>";
|
|---|
| 310 |
|
|---|
| 311 | JLabel label = (JLabel)super.getTableCellRendererComponent(table,
|
|---|
| 312 | display, isSelected, hasFocus, row, column);
|
|---|
| 313 |
|
|---|
| 314 | label.setBackground(backgroundColor);
|
|---|
| 315 | if (isSelected) {
|
|---|
| 316 | label.setForeground(foregroundColor);
|
|---|
| 317 | }
|
|---|
| 318 | if(pe.isChanged()) {
|
|---|
| 319 | label.setBackground(changedColor);
|
|---|
| 320 | } else if(!pe.isDefault()) {
|
|---|
| 321 | label.setBackground(nonDefaultColor);
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | if (!pe.isDefault()) {
|
|---|
| 325 | label.setFont(label.getFont().deriveFont(Font.BOLD));
|
|---|
| 326 | }
|
|---|
| 327 | val = pe.getDefaultValue().getValue();
|
|---|
| 328 | if(val != null)
|
|---|
| 329 | {
|
|---|
| 330 | if(pe.isDefault()) {
|
|---|
| 331 | label.setToolTipText(tr("Current value is default."));
|
|---|
| 332 | } else {
|
|---|
| 333 | label.setToolTipText(tr("Default value is ''{0}''.", val));
|
|---|
| 334 | }
|
|---|
| 335 | } else {
|
|---|
| 336 | label.setToolTipText(tr("Default value currently unknown (setting has not been used yet)."));
|
|---|
| 337 | }
|
|---|
| 338 | return label;
|
|---|
| 339 | }
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | private static class SettingCellEditor extends DefaultCellEditor {
|
|---|
| 343 | public SettingCellEditor() {
|
|---|
| 344 | super(new JosmTextField());
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | @Override
|
|---|
| 348 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
|---|
| 349 | PrefEntry pe = (PrefEntry) value;
|
|---|
| 350 | Preferences.StringSetting stg = (Preferences.StringSetting) pe.getValue();
|
|---|
| 351 | String s = stg.getValue() == null ? "" : stg.getValue();
|
|---|
| 352 | return super.getTableCellEditorComponent(table, s, isSelected, row, column);
|
|---|
| 353 | }
|
|---|
| 354 | }
|
|---|
| 355 | }
|
|---|