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