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 |
|
---|
18 | import org.openstreetmap.josm.data.AutosaveTask;
|
---|
19 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
20 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
21 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
22 | import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
|
---|
23 | import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
|
---|
24 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
25 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
26 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
27 | import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
|
---|
28 | import org.openstreetmap.josm.tools.GBC;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Preference settings for data layer autosave.
|
---|
32 | */
|
---|
33 | public class BackupPreference implements SubPreferenceSetting {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Factory used to create a new {@code BackupPreference}.
|
---|
37 | */
|
---|
38 | public static class Factory implements PreferenceSettingFactory {
|
---|
39 | @Override
|
---|
40 | public BackupPreference createPreferenceSetting() {
|
---|
41 | return new BackupPreference();
|
---|
42 | }
|
---|
43 | }
|
---|
44 | private static final BooleanProperty PROP_KEEP_BACKUP = new BooleanProperty("save.keepbackup", false);
|
---|
45 | private JCheckBox notification;
|
---|
46 | private JCheckBox keepBackup;
|
---|
47 | private JCheckBox autosave;
|
---|
48 | private final JosmTextField autosaveInterval = new JosmTextField(8);
|
---|
49 | private final JosmTextField backupPerLayer = new JosmTextField(8);
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public void addGui(PreferenceTabbedPane gui) {
|
---|
53 | JPanel panel = new VerticallyScrollablePanel();
|
---|
54 | panel.setLayout(new GridBagLayout());
|
---|
55 | panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
56 |
|
---|
57 | autosave = new JCheckBox(tr("Auto save enabled"));
|
---|
58 | autosave.setSelected(AutosaveTask.PROP_AUTOSAVE_ENABLED.get());
|
---|
59 | panel.add(autosave, GBC.eol());
|
---|
60 |
|
---|
61 | final JLabel autosaveIntervalLabel = new JLabel(tr("Auto save interval (seconds)"));
|
---|
62 | panel.add(autosaveIntervalLabel, GBC.std().insets(60,0,0,0));
|
---|
63 | autosaveInterval.setText(Integer.toString(AutosaveTask.PROP_INTERVAL.get()));
|
---|
64 | autosaveInterval.setToolTipText(tr("Default value: {0}", AutosaveTask.PROP_INTERVAL.getDefaultValue()));
|
---|
65 | autosaveInterval.setMinimumSize(autosaveInterval.getPreferredSize());
|
---|
66 | panel.add(autosaveInterval, GBC.eol().insets(5,0,0,5));
|
---|
67 |
|
---|
68 | final JLabel backupPerLayerLabel = new JLabel(tr("Auto saved files per layer"));
|
---|
69 | panel.add(backupPerLayerLabel, GBC.std().insets(60,0,0,0));
|
---|
70 | backupPerLayer.setText(Integer.toString(AutosaveTask.PROP_FILES_PER_LAYER.get()));
|
---|
71 | backupPerLayer.setToolTipText(tr("Default value: {0}", AutosaveTask.PROP_FILES_PER_LAYER.getDefaultValue()));
|
---|
72 | backupPerLayer.setMinimumSize(backupPerLayer.getPreferredSize());
|
---|
73 | panel.add(backupPerLayer, GBC.eol().insets(5,0,0,10));
|
---|
74 |
|
---|
75 | panel.add(new HtmlPanel(
|
---|
76 | tr("<i>(Autosave stores the changed data layers in periodic intervals. " +
|
---|
77 | "The backups are saved in JOSM''s preference folder. " +
|
---|
78 | "In case of a crash, JOSM tries to recover the unsaved changes " +
|
---|
79 | "on next start.)</i>")),
|
---|
80 | GBC.eop().fill(GBC.HORIZONTAL).insets(5,0,0,10));
|
---|
81 |
|
---|
82 | panel.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL));
|
---|
83 |
|
---|
84 | keepBackup = new JCheckBox(tr("Keep backup files when saving data layers"));
|
---|
85 | keepBackup.setSelected(PROP_KEEP_BACKUP.get());
|
---|
86 | keepBackup.setToolTipText(tr("When saving, keep backup files ending with a ~"));
|
---|
87 | panel.add(keepBackup, GBC.eop());
|
---|
88 |
|
---|
89 | panel.add(new HtmlPanel(
|
---|
90 | tr("<i>(JOSM can keep a backup file when saving data layers. "+
|
---|
91 | "It appends ''~'' to the file name and saves it in the same folder.)</i>")),
|
---|
92 | GBC.eop().fill(GBC.HORIZONTAL).insets(5,0,0,0));
|
---|
93 |
|
---|
94 | panel.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL));
|
---|
95 |
|
---|
96 | notification = new JCheckBox(tr("Notification at each save"));
|
---|
97 | notification.setSelected(AutosaveTask.PROP_NOTIFICATION.get());
|
---|
98 | notification.setToolTipText(tr("When saving, display a small notification"));
|
---|
99 | 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 | }
|
---|