1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.map;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 |
|
---|
10 | import javax.swing.BorderFactory;
|
---|
11 | import javax.swing.Box;
|
---|
12 | import javax.swing.JCheckBox;
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 | import javax.swing.JScrollPane;
|
---|
16 | import javax.swing.JSeparator;
|
---|
17 | import javax.swing.JTextField;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.data.AutosaveTask;
|
---|
20 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
21 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
22 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
23 | import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
|
---|
25 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
26 | import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
|
---|
27 | import org.openstreetmap.josm.tools.GBC;
|
---|
28 |
|
---|
29 | public class BackupPreference implements SubPreferenceSetting {
|
---|
30 |
|
---|
31 | public static class Factory implements PreferenceSettingFactory {
|
---|
32 | @Override
|
---|
33 | public BackupPreference createPreferenceSetting() {
|
---|
34 | return new BackupPreference();
|
---|
35 | }
|
---|
36 | }
|
---|
37 | private static final BooleanProperty PROP_KEEP_BACKUP = new BooleanProperty("save.keepbackup", false);
|
---|
38 | private JCheckBox keepBackup;
|
---|
39 | private JCheckBox autosave;
|
---|
40 | private final JTextField autosaveInterval = new JTextField(8);
|
---|
41 | private final JTextField backupPerLayer = new JTextField(8);
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void addGui(PreferenceTabbedPane gui) {
|
---|
45 | JPanel panel = new VerticallyScrollablePanel();
|
---|
46 | panel.setLayout(new GridBagLayout());
|
---|
47 | panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
48 |
|
---|
49 | autosave = new JCheckBox(tr("Auto save enabled"));
|
---|
50 | autosave.setSelected(AutosaveTask.PROP_AUTOSAVE_ENABLED.get());
|
---|
51 | panel.add(autosave, GBC.eol());
|
---|
52 |
|
---|
53 | final JLabel autosaveIntervalLabel = new JLabel(tr("Auto save interval (seconds)"));
|
---|
54 | panel.add(autosaveIntervalLabel, GBC.std().insets(60,0,0,0));
|
---|
55 | autosaveInterval.setText(Integer.toString(AutosaveTask.PROP_INTERVAL.get()));
|
---|
56 | autosaveInterval.setToolTipText(tr("Default value: {0}", AutosaveTask.PROP_INTERVAL.getDefaultValue()));
|
---|
57 | autosaveInterval.setMinimumSize(autosaveInterval.getPreferredSize());
|
---|
58 | panel.add(autosaveInterval, GBC.eol().insets(5,0,0,5));
|
---|
59 |
|
---|
60 | final JLabel backupPerLayerLabel = new JLabel(tr("Auto saved files per layer"));
|
---|
61 | panel.add(backupPerLayerLabel, GBC.std().insets(60,0,0,0));
|
---|
62 | backupPerLayer.setText(Integer.toString(AutosaveTask.PROP_FILES_PER_LAYER.get()));
|
---|
63 | backupPerLayer.setToolTipText(tr("Default value: {0}", AutosaveTask.PROP_FILES_PER_LAYER.getDefaultValue()));
|
---|
64 | backupPerLayer.setMinimumSize(backupPerLayer.getPreferredSize());
|
---|
65 | panel.add(backupPerLayer, GBC.eol().insets(5,0,0,10));
|
---|
66 |
|
---|
67 | panel.add(new HtmlPanel(
|
---|
68 | tr("<i>(Autosave stores the changed data layers in periodic intervals. " +
|
---|
69 | "The backups are saved in JOSM''s preference folder. " +
|
---|
70 | "In case of a crash, JOSM tries to recover the unsaved changes " +
|
---|
71 | "on next start.)</i>")),
|
---|
72 | GBC.eop().fill(GBC.HORIZONTAL).insets(5,0,0,10));
|
---|
73 |
|
---|
74 | panel.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL));
|
---|
75 |
|
---|
76 | keepBackup = new JCheckBox(tr("Keep backup files when saving data layers"));
|
---|
77 | keepBackup.setSelected(PROP_KEEP_BACKUP.get());
|
---|
78 | keepBackup.setToolTipText(tr("When saving, keep backup files ending with a ~"));
|
---|
79 | panel.add(keepBackup, GBC.eop());
|
---|
80 |
|
---|
81 | panel.add(new HtmlPanel(
|
---|
82 | tr("<i>(JOSM can keep a backup file when saving data layers. "+
|
---|
83 | "It appends ''~'' to the file name and saves it in the same folder.)</i>")),
|
---|
84 | GBC.eop().fill(GBC.HORIZONTAL).insets(5,0,0,0));
|
---|
85 |
|
---|
86 | ActionListener autosaveEnabled = new ActionListener(){
|
---|
87 | public void actionPerformed(ActionEvent e) {
|
---|
88 | boolean enabled = autosave.isSelected();
|
---|
89 | autosaveIntervalLabel.setEnabled(enabled);
|
---|
90 | autosaveInterval.setEnabled(enabled);
|
---|
91 | backupPerLayerLabel.setEnabled(enabled);
|
---|
92 | backupPerLayer.setEnabled(enabled);
|
---|
93 | }
|
---|
94 | };
|
---|
95 | autosave.addActionListener(autosaveEnabled);
|
---|
96 | autosaveEnabled.actionPerformed(null);
|
---|
97 |
|
---|
98 | panel.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH));
|
---|
99 | JScrollPane sp = new JScrollPane(panel);
|
---|
100 | sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
|
---|
101 | sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
|
---|
102 |
|
---|
103 | gui.getMapPreference().addSubTab(this, tr("File backup"), sp, tr("Configure whether to create backup files"));
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public boolean ok() {
|
---|
108 | boolean restartRequired = false;
|
---|
109 | PROP_KEEP_BACKUP.put(keepBackup.isSelected());
|
---|
110 |
|
---|
111 | restartRequired |= AutosaveTask.PROP_AUTOSAVE_ENABLED.put(autosave.isSelected());
|
---|
112 | restartRequired |= AutosaveTask.PROP_INTERVAL.parseAndPut(autosaveInterval.getText());
|
---|
113 | AutosaveTask.PROP_FILES_PER_LAYER.parseAndPut(backupPerLayer.getText());
|
---|
114 | return restartRequired;
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public boolean isExpert() {
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | public TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) {
|
---|
124 | return gui.getMapPreference();
|
---|
125 | }
|
---|
126 | }
|
---|