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(new GridBagLayout());
055        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
056
057        autosave = new JCheckBox(tr("Auto save enabled"));
058        autosave.setSelected(AutosaveTask.PROP_AUTOSAVE_ENABLED.get());
059        panel.add(autosave, GBC.eol());
060
061        final JLabel autosaveIntervalLabel = new JLabel(tr("Auto save interval (seconds)"));
062        autosaveIntervalLabel.setLabelFor(autosaveInterval);
063        panel.add(autosaveIntervalLabel, GBC.std().insets(60, 0, 0, 0));
064        autosaveInterval.setText(Integer.toString(AutosaveTask.PROP_INTERVAL.get()));
065        autosaveInterval.setToolTipText(tr("Default value: {0}", AutosaveTask.PROP_INTERVAL.getDefaultValue()));
066        autosaveInterval.setMinimumSize(autosaveInterval.getPreferredSize());
067        panel.add(autosaveInterval, GBC.eol().insets(5, 0, 0, 5));
068
069        final JLabel backupPerLayerLabel = new JLabel(tr("Auto saved files per layer"));
070        backupPerLayerLabel.setLabelFor(backupPerLayer);
071        panel.add(backupPerLayerLabel, GBC.std().insets(60, 0, 0, 0));
072        backupPerLayer.setText(Integer.toString(AutosaveTask.PROP_FILES_PER_LAYER.get()));
073        backupPerLayer.setToolTipText(tr("Default value: {0}", AutosaveTask.PROP_FILES_PER_LAYER.getDefaultValue()));
074        backupPerLayer.setMinimumSize(backupPerLayer.getPreferredSize());
075        panel.add(backupPerLayer, GBC.eol().insets(5, 0, 0, 10));
076
077        panel.add(new HtmlPanel(
078            tr("<i>(Autosave stores the changed data layers in periodic intervals. " +
079                "The backups are saved in JOSM''s preference folder. " +
080                "In case of a crash, JOSM tries to recover the unsaved changes " +
081                "on next start.)</i>")),
082            GBC.eop().fill(GBC.HORIZONTAL).insets(5, 0, 0, 10));
083
084        panel.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL));
085
086        keepBackup = new JCheckBox(tr("Keep backup files when saving data layers"));
087        keepBackup.setSelected(PROP_KEEP_BACKUP.get());
088        keepBackup.setToolTipText(tr("When saving, keep backup files ending with a ~"));
089        panel.add(keepBackup, GBC.eop());
090
091        panel.add(new HtmlPanel(
092            tr("<i>(JOSM can keep a backup file when saving data layers. "+
093                "It appends ''~'' to the file name and saves it in the same folder.)</i>")),
094            GBC.eop().fill(GBC.HORIZONTAL).insets(5, 0, 0, 0));
095
096        panel.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL));
097
098        notification = new JCheckBox(tr("Notification at each save"));
099        notification.setSelected(AutosaveTask.PROP_NOTIFICATION.get());
100        notification.setToolTipText(tr("When saving, display a small notification"));
101        panel.add(notification, GBC.eop());
102
103        ActionListener autosaveEnabled = new ActionListener() {
104            @Override
105            public void actionPerformed(ActionEvent e) {
106                boolean enabled = autosave.isSelected();
107                autosaveIntervalLabel.setEnabled(enabled);
108                autosaveInterval.setEnabled(enabled);
109                backupPerLayerLabel.setEnabled(enabled);
110                backupPerLayer.setEnabled(enabled);
111            }
112        };
113        autosave.addActionListener(autosaveEnabled);
114        autosaveEnabled.actionPerformed(null);
115
116        panel.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH));
117        JScrollPane sp = GuiHelper.embedInVerticalScrollPane(panel);
118
119        gui.getMapPreference().addSubTab(this, tr("File backup"), sp, tr("Configure whether to create backup files"));
120    }
121
122    @Override
123    public boolean ok() {
124        boolean restartRequired = false;
125        PROP_KEEP_BACKUP.put(keepBackup.isSelected());
126
127        restartRequired |= AutosaveTask.PROP_AUTOSAVE_ENABLED.put(autosave.isSelected());
128        restartRequired |= AutosaveTask.PROP_INTERVAL.parseAndPut(autosaveInterval.getText());
129        AutosaveTask.PROP_FILES_PER_LAYER.parseAndPut(backupPerLayer.getText());
130        AutosaveTask.PROP_NOTIFICATION.put(notification.isSelected());
131        return restartRequired;
132    }
133
134    @Override
135    public boolean isExpert() {
136        return false;
137    }
138
139    @Override
140    public TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) {
141        return gui.getMapPreference();
142    }
143}