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