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