001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.preferences.map; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.GridBagLayout; 007import java.awt.event.ActionListener; 008 009import javax.swing.BorderFactory; 010import javax.swing.Box; 011import javax.swing.JCheckBox; 012import javax.swing.JLabel; 013import javax.swing.JPanel; 014import javax.swing.JScrollPane; 015import javax.swing.JSeparator; 016 017import org.openstreetmap.josm.data.AutosaveTask; 018import org.openstreetmap.josm.data.preferences.BooleanProperty; 019import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory; 020import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane; 021import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting; 022import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting; 023import org.openstreetmap.josm.gui.util.GuiHelper; 024import org.openstreetmap.josm.gui.widgets.HtmlPanel; 025import org.openstreetmap.josm.gui.widgets.JosmTextField; 026import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel; 027import org.openstreetmap.josm.tools.GBC; 028 029/** 030 * Preference settings for data layer autosave. 031 */ 032public class BackupPreference implements SubPreferenceSetting { 033 034 /** 035 * Factory used to create a new {@code BackupPreference}. 036 */ 037 public static class Factory implements PreferenceSettingFactory { 038 @Override 039 public BackupPreference createPreferenceSetting() { 040 return new BackupPreference(); 041 } 042 } 043 044 private static final BooleanProperty PROP_KEEP_BACKUP = new BooleanProperty("save.keepbackup", false); 045 private JCheckBox notification; 046 private JCheckBox keepBackup; 047 private JCheckBox autosave; 048 private final JosmTextField autosaveInterval = new JosmTextField(8); 049 private final JosmTextField backupPerLayer = new JosmTextField(8); 050 051 @Override 052 public void addGui(PreferenceTabbedPane gui) { 053 JPanel panel = new VerticallyScrollablePanel(new GridBagLayout()); 054 panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 055 056 autosave = new JCheckBox(tr("Auto save enabled")); 057 autosave.setSelected(AutosaveTask.PROP_AUTOSAVE_ENABLED.get()); 058 panel.add(autosave, GBC.eol()); 059 060 final JLabel autosaveIntervalLabel = new JLabel(tr("Auto save interval (seconds)")); 061 autosaveIntervalLabel.setLabelFor(autosaveInterval); 062 panel.add(autosaveIntervalLabel, GBC.std().insets(60, 0, 0, 0)); 063 autosaveInterval.setText(Integer.toString(AutosaveTask.PROP_INTERVAL.get())); 064 autosaveInterval.setToolTipText(tr("Default value: {0}", AutosaveTask.PROP_INTERVAL.getDefaultValue())); 065 autosaveInterval.setMinimumSize(autosaveInterval.getPreferredSize()); 066 panel.add(autosaveInterval, GBC.eol().insets(5, 0, 0, 5)); 067 068 final JLabel backupPerLayerLabel = new JLabel(tr("Auto saved files per layer")); 069 backupPerLayerLabel.setLabelFor(backupPerLayer); 070 panel.add(backupPerLayerLabel, GBC.std().insets(60, 0, 0, 0)); 071 backupPerLayer.setText(Integer.toString(AutosaveTask.PROP_FILES_PER_LAYER.get())); 072 backupPerLayer.setToolTipText(tr("Default value: {0}", AutosaveTask.PROP_FILES_PER_LAYER.getDefaultValue())); 073 backupPerLayer.setMinimumSize(backupPerLayer.getPreferredSize()); 074 panel.add(backupPerLayer, GBC.eol().insets(5, 0, 0, 10)); 075 076 panel.add(new HtmlPanel( 077 tr("<i>(Autosave stores the changed data layers in periodic intervals. " + 078 "The backups are saved in JOSM''s preference folder. " + 079 "In case of a crash, JOSM tries to recover the unsaved changes " + 080 "on next start.)</i>")), 081 GBC.eop().fill(GBC.HORIZONTAL).insets(5, 0, 0, 10)); 082 083 panel.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL)); 084 085 keepBackup = new JCheckBox(tr("Keep backup files when saving data layers")); 086 keepBackup.setSelected(PROP_KEEP_BACKUP.get()); 087 keepBackup.setToolTipText(tr("When saving, keep backup files ending with a ~")); 088 panel.add(keepBackup, GBC.eop()); 089 090 panel.add(new HtmlPanel( 091 tr("<i>(JOSM can keep a backup file when saving data layers. "+ 092 "It appends ''~'' to the file name and saves it in the same folder.)</i>")), 093 GBC.eop().fill(GBC.HORIZONTAL).insets(5, 0, 0, 0)); 094 095 panel.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL)); 096 097 notification = new JCheckBox(tr("Notification at each save")); 098 notification.setSelected(AutosaveTask.PROP_NOTIFICATION.get()); 099 notification.setToolTipText(tr("When saving, display a small notification")); 100 panel.add(notification, GBC.eop()); 101 102 ActionListener autosaveEnabled = e -> { 103 boolean enabled = autosave.isSelected(); 104 autosaveIntervalLabel.setEnabled(enabled); 105 autosaveInterval.setEnabled(enabled); 106 backupPerLayerLabel.setEnabled(enabled); 107 backupPerLayer.setEnabled(enabled); 108 }; 109 autosave.addActionListener(autosaveEnabled); 110 autosaveEnabled.actionPerformed(null); 111 112 panel.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH)); 113 JScrollPane sp = GuiHelper.embedInVerticalScrollPane(panel); 114 115 gui.getMapPreference().addSubTab(this, tr("File backup"), sp, tr("Configure whether to create backup files")); 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 TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) { 137 return gui.getMapPreference(); 138 } 139}