| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.preferences.map;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.GridBagLayout;
|
|---|
| 7 | import java.awt.event.ActionListener;
|
|---|
| 8 |
|
|---|
| 9 | import javax.swing.BorderFactory;
|
|---|
| 10 | import javax.swing.Box;
|
|---|
| 11 | import javax.swing.JCheckBox;
|
|---|
| 12 | import javax.swing.JLabel;
|
|---|
| 13 | import javax.swing.JPanel;
|
|---|
| 14 | import javax.swing.JSeparator;
|
|---|
| 15 |
|
|---|
| 16 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
|---|
| 17 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
|---|
| 18 | import org.openstreetmap.josm.gui.layer.AutosaveTask;
|
|---|
| 19 | import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting;
|
|---|
| 20 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
|---|
| 21 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
|---|
| 22 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
|---|
| 23 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
|---|
| 24 | import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
|
|---|
| 25 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Preference settings for data layer autosave.
|
|---|
| 29 | */
|
|---|
| 30 | public class BackupPreference extends DefaultTabPreferenceSetting {
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Factory used to create a new {@code BackupPreference}.
|
|---|
| 34 | */
|
|---|
| 35 | public static class Factory implements PreferenceSettingFactory {
|
|---|
| 36 | @Override
|
|---|
| 37 | public BackupPreference createPreferenceSetting() {
|
|---|
| 38 | return new BackupPreference();
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | private static final BooleanProperty PROP_KEEP_BACKUP = new BooleanProperty("save.keepbackup", false);
|
|---|
| 43 | private JCheckBox notification;
|
|---|
| 44 | private JCheckBox keepBackup;
|
|---|
| 45 | private JCheckBox autosave;
|
|---|
| 46 | private final JosmTextField autosaveInterval = new JosmTextField(8);
|
|---|
| 47 | private final JosmTextField backupPerLayer = new JosmTextField(8);
|
|---|
| 48 |
|
|---|
| 49 | BackupPreference() {
|
|---|
| 50 | super(/* ICON(preferences/) */ "backup", tr("File backup"), tr("Configure whether to create backup files"));
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | @Override
|
|---|
| 54 | public void addGui(PreferenceTabbedPane gui) {
|
|---|
| 55 | JPanel panel = new VerticallyScrollablePanel(new GridBagLayout());
|
|---|
| 56 | panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
|---|
| 57 |
|
|---|
| 58 | autosave = new JCheckBox(tr("Auto save enabled"));
|
|---|
| 59 | autosave.setSelected(AutosaveTask.PROP_AUTOSAVE_ENABLED.get());
|
|---|
| 60 | panel.add(autosave, GBC.eol());
|
|---|
| 61 |
|
|---|
| 62 | final JLabel autosaveIntervalLabel = new JLabel(tr("Auto save interval (seconds)"));
|
|---|
| 63 | autosaveIntervalLabel.setLabelFor(autosaveInterval);
|
|---|
| 64 | panel.add(autosaveIntervalLabel, GBC.std().insets(60, 0, 0, 0));
|
|---|
| 65 | autosaveInterval.setText(Integer.toString(AutosaveTask.PROP_INTERVAL.get()));
|
|---|
| 66 | autosaveInterval.setToolTipText(tr("Default value: {0}", AutosaveTask.PROP_INTERVAL.getDefaultValue()));
|
|---|
| 67 | autosaveInterval.setMinimumSize(autosaveInterval.getPreferredSize());
|
|---|
| 68 | panel.add(autosaveInterval, GBC.eol().insets(5, 0, 0, 5));
|
|---|
| 69 |
|
|---|
| 70 | final JLabel backupPerLayerLabel = new JLabel(tr("Auto saved files per layer"));
|
|---|
| 71 | backupPerLayerLabel.setLabelFor(backupPerLayer);
|
|---|
| 72 | panel.add(backupPerLayerLabel, GBC.std().insets(60, 0, 0, 0));
|
|---|
| 73 | backupPerLayer.setText(Integer.toString(AutosaveTask.PROP_FILES_PER_LAYER.get()));
|
|---|
| 74 | backupPerLayer.setToolTipText(tr("Default value: {0}", AutosaveTask.PROP_FILES_PER_LAYER.getDefaultValue()));
|
|---|
| 75 | backupPerLayer.setMinimumSize(backupPerLayer.getPreferredSize());
|
|---|
| 76 | panel.add(backupPerLayer, GBC.eol().insets(5, 0, 0, 10));
|
|---|
| 77 |
|
|---|
| 78 | panel.add(new HtmlPanel(
|
|---|
| 79 | tr("<i>(Autosave stores the changed data layers in periodic intervals. " +
|
|---|
| 80 | "The backups are saved in JOSM''s preference folder. " +
|
|---|
| 81 | "In case of a crash, JOSM tries to recover the unsaved changes " +
|
|---|
| 82 | "on next start.)</i>")),
|
|---|
| 83 | GBC.eop().fill(GBC.HORIZONTAL).insets(5, 0, 0, 10));
|
|---|
| 84 |
|
|---|
| 85 | panel.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL));
|
|---|
| 86 |
|
|---|
| 87 | keepBackup = new JCheckBox(tr("Keep backup files when saving data layers"));
|
|---|
| 88 | keepBackup.setSelected(PROP_KEEP_BACKUP.get());
|
|---|
| 89 | keepBackup.setToolTipText(tr("When saving, keep backup files ending with a ~"));
|
|---|
| 90 | panel.add(keepBackup, GBC.eop());
|
|---|
| 91 |
|
|---|
| 92 | panel.add(new HtmlPanel(
|
|---|
| 93 | tr("<i>(JOSM can keep a backup file when saving data layers. "+
|
|---|
| 94 | "It appends ''~'' to the file name and saves it in the same folder.)</i>")),
|
|---|
| 95 | GBC.eop().fill(GBC.HORIZONTAL).insets(5, 0, 0, 0));
|
|---|
| 96 |
|
|---|
| 97 | panel.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL));
|
|---|
| 98 |
|
|---|
| 99 | notification = new JCheckBox(tr("Notification at each save"));
|
|---|
| 100 | notification.setSelected(AutosaveTask.PROP_NOTIFICATION.get());
|
|---|
| 101 | notification.setToolTipText(tr("When saving, display a small notification"));
|
|---|
| 102 | panel.add(notification, GBC.eop());
|
|---|
| 103 |
|
|---|
| 104 | ActionListener autosaveEnabled = e -> {
|
|---|
| 105 | boolean enabled = autosave.isSelected();
|
|---|
| 106 | autosaveIntervalLabel.setEnabled(enabled);
|
|---|
| 107 | autosaveInterval.setEnabled(enabled);
|
|---|
| 108 | backupPerLayerLabel.setEnabled(enabled);
|
|---|
| 109 | backupPerLayer.setEnabled(enabled);
|
|---|
| 110 | };
|
|---|
| 111 | autosave.addActionListener(autosaveEnabled);
|
|---|
| 112 | autosaveEnabled.actionPerformed(null);
|
|---|
| 113 |
|
|---|
| 114 | panel.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH));
|
|---|
| 115 | gui.createPreferenceTab(this).add(panel, GBC.eol().fill(GBC.BOTH));
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | @Override
|
|---|
| 119 | public boolean ok() {
|
|---|
| 120 | boolean restartRequired = false;
|
|---|
| 121 | PROP_KEEP_BACKUP.put(keepBackup.isSelected());
|
|---|
| 122 |
|
|---|
| 123 | restartRequired |= AutosaveTask.PROP_AUTOSAVE_ENABLED.put(autosave.isSelected());
|
|---|
| 124 | restartRequired |= AutosaveTask.PROP_INTERVAL.parseAndPut(autosaveInterval.getText());
|
|---|
| 125 | AutosaveTask.PROP_FILES_PER_LAYER.parseAndPut(backupPerLayer.getText());
|
|---|
| 126 | AutosaveTask.PROP_NOTIFICATION.put(notification.isSelected());
|
|---|
| 127 | return restartRequired;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | @Override
|
|---|
| 131 | public boolean isExpert() {
|
|---|
| 132 | return false;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | @Override
|
|---|
| 136 | public String getHelpContext() {
|
|---|
| 137 | return HelpUtil.ht("/Preferences/BackupPreference");
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|