1c6d119b7337f1b8e99b1679a17983caaed22493
[debian/jabref.git] / src / java / net / sf / jabref / FileTab.java
1 /*  Copyright (C) 2003-2011 JabRef contributors.
2     This program is free software; you can redistribute it and/or modify
3     it under the terms of the GNU General Public License as published by
4     the Free Software Foundation; either version 2 of the License, or
5     (at your option) any later version.
6
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10     GNU General Public License for more details.
11
12     You should have received a copy of the GNU General Public License along
13     with this program; if not, write to the Free Software Foundation, Inc.,
14     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 */
16 package net.sf.jabref;
17
18 import java.awt.*;
19
20 import javax.swing.*;
21 import javax.swing.event.ChangeListener;
22 import javax.swing.event.ChangeEvent;
23
24 import net.sf.jabref.help.HelpAction;
25
26 import com.jgoodies.forms.builder.DefaultFormBuilder;
27 import com.jgoodies.forms.layout.FormLayout;
28
29 /**
30  * Preferences tab for file options. These options were moved out from GeneralTab to
31  * resolve the space issue.
32  */
33 public class FileTab extends JPanel implements PrefsTab {
34
35     JabRefPreferences _prefs;
36     JabRefFrame _frame;
37
38     private JCheckBox backup, openLast, autoDoubleBraces, autoSave,
39             promptBeforeUsingAutoSave, includeEmptyFields, camelCase, sameColumn;
40     private JComboBox valueDelimiter;
41     private JRadioButton
42         saveOriginalOrder, saveAuthorOrder, saveTableOrder, saveTitleOrder,
43         exportOriginalOrder, exportAuthorOrder, exportTableOrder, exportTitleOrder,
44         resolveStringsStandard, resolveStringsAll;
45     private JTextField bracesAroundCapitalsFields, nonWrappableFields,
46             doNotResolveStringsFor;
47     private JSpinner autoSaveInterval;
48     private boolean origAutoSaveSetting = false;
49     private HelpAction autosaveHelp;
50
51     public FileTab(JabRefFrame frame, JabRefPreferences prefs) {
52         _prefs = prefs;
53         _frame = frame;
54
55         autosaveHelp = new HelpAction(frame.helpDiag, GUIGlobals.autosaveHelp, "Help",
56                 GUIGlobals.getIconUrl("helpSmall"));
57         openLast = new JCheckBox(Globals.lang("Open last edited databases at startup"));
58         backup = new JCheckBox(Globals.lang("Backup old file when saving"));
59         saveAuthorOrder = new JRadioButton(Globals.lang("Save ordered by author/editor/year"));
60         exportAuthorOrder = new JRadioButton(Globals.lang("Export ordered by author/editor/year"));
61         saveOriginalOrder = new JRadioButton(Globals.lang("Save entries in their original order"));
62         exportOriginalOrder = new JRadioButton(Globals.lang("Export entries in their original order"));
63         saveTableOrder = new JRadioButton(Globals.lang("Save in current table sort order"));
64         exportTableOrder = new JRadioButton(Globals.lang("Export in current table sort order"));
65         saveTitleOrder = new JRadioButton(Globals.lang("Save entries ordered by title"));
66         exportTitleOrder = new JRadioButton(Globals.lang("Export entries ordered by title"));
67         autoSave = new JCheckBox(Globals.lang("Autosave"));
68         promptBeforeUsingAutoSave = new JCheckBox(Globals.lang("Prompt before recovering a database from an autosave file"));
69         autoSaveInterval = new JSpinner(new SpinnerNumberModel(1, 1, 60, 1));
70         valueDelimiter = new JComboBox(new String[]{
71                 Globals.lang("Quotes") + ": \", \"",
72                 Globals.lang("Curly Brackets") + ": {, }" });
73         includeEmptyFields = new JCheckBox(Globals.lang("Include empty fields"));
74         camelCase = new JCheckBox(Globals.lang("Start field contents in same column"));
75         sameColumn = new JCheckBox(Globals.lang("Use camel case for field names (e.g., \"HowPublished\" instead of \"howpublished\")"));
76         ButtonGroup bg = new ButtonGroup();
77         bg.add(saveAuthorOrder);
78         bg.add(saveOriginalOrder);
79         bg.add(saveTableOrder);
80         bg.add(saveTitleOrder);
81         bg = new ButtonGroup();
82         bg.add(exportAuthorOrder);
83         bg.add(exportOriginalOrder);
84         bg.add(exportTableOrder);
85         bg.add(exportTitleOrder);
86         resolveStringsAll = new JRadioButton(Globals.lang("Resolve strings for all fields except")+":");
87         resolveStringsStandard = new JRadioButton(Globals.lang("Resolve strings for standard BibTeX fields only"));
88         bg = new ButtonGroup();
89         bg.add(resolveStringsAll);
90         bg.add(resolveStringsStandard);
91
92         bracesAroundCapitalsFields = new JTextField(25);
93         nonWrappableFields = new JTextField(25);
94         doNotResolveStringsFor = new JTextField(30);
95         autoDoubleBraces = new JCheckBox(
96                 //+ Globals.lang("Store fields with double braces, and remove extra braces when loading.<BR>"
97                 //+ "Double braces signal that BibTeX should preserve character case.") + "</HTML>");
98                 Globals.lang("Remove double braces around BibTeX fields when loading."));
99
100         autoSave.addChangeListener(new ChangeListener() {
101             public void stateChanged(ChangeEvent changeEvent) {
102                 autoSaveInterval.setEnabled(autoSave.isSelected());
103                 promptBeforeUsingAutoSave.setEnabled(autoSave.isSelected());
104             }
105         });
106
107         FormLayout layout = new FormLayout("left:pref, 4dlu, fill:pref", "");
108         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
109
110         builder.appendSeparator(Globals.lang("General"));
111         builder.nextLine();
112         builder.append(openLast, 3);
113         builder.nextLine();
114         builder.append(backup, 3);
115         builder.nextLine();
116         builder.append(autoDoubleBraces, 3);
117         builder.nextLine();
118
119         JLabel label = new JLabel(Globals.lang("Store the following fields with braces around capital letters")+":");
120         builder.append(label);
121         builder.append(bracesAroundCapitalsFields);
122         builder.nextLine();
123         label = new JLabel(Globals.lang("Do not wrap the following fields when saving")+":");
124         builder.append(label);
125         builder.append(nonWrappableFields);
126         builder.nextLine();
127         builder.append(resolveStringsStandard, 3);
128         builder.nextLine();
129         builder.append(resolveStringsAll);
130         builder.append(doNotResolveStringsFor);
131         builder.nextLine();
132         builder.appendSeparator(Globals.lang("Autosave"));
133         builder.append(autoSave, 1);
134         JButton hlp = new JButton(autosaveHelp);
135         hlp.setText(null);
136         hlp.setPreferredSize(new Dimension(24, 24));
137         JPanel hPan = new JPanel();
138         hPan.setLayout(new BorderLayout());
139         hPan.add(hlp, BorderLayout.EAST);
140         builder.append(hPan);
141         builder.nextLine();
142         builder.append(Globals.lang("Autosave interval (minutes)")+":");
143         builder.append(autoSaveInterval);
144         builder.nextLine();
145         builder.append(promptBeforeUsingAutoSave);
146         builder.nextLine();
147         builder.appendSeparator(Globals.lang("Sort order"));
148         builder.append(saveAuthorOrder, 1);
149         builder.append(exportAuthorOrder, 1);
150         builder.nextLine();
151         builder.append(saveTableOrder, 1);
152         builder.append(exportTableOrder, 1);
153         builder.nextLine();
154         builder.append(saveOriginalOrder, 1);
155         builder.append(exportOriginalOrder, 1);
156         builder.nextLine();
157         builder.append(saveTitleOrder, 1);
158         builder.append(exportTitleOrder, 1);
159         builder.nextLine();
160         builder.appendSeparator(Globals.lang("Field saving options"));
161         builder.nextLine();
162         builder.append(camelCase);
163         builder.nextLine();
164         builder.append(sameColumn);
165         /*FormLayout layout2 = new FormLayout(
166                 "left:pref, 8dlu, fill:pref", "");
167         DefaultFormBuilder builder2 = new DefaultFormBuilder(layout2);
168         builder2.append(new JLabel(Globals.lang("Field value delimiter. E.g., \"author={x}\" or \"author='x'\"") + ":"));
169         builder2.append(valueDelimiter);
170         builder.nextLine();
171         builder.append(builder2.getPanel());*/
172         builder.append(new JPanel());
173         builder.nextLine();
174         builder.append(includeEmptyFields);
175         builder.append(new JPanel());
176         builder.nextLine();
177
178         JPanel pan = builder.getPanel();
179         pan.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
180         setLayout(new BorderLayout());
181         add(pan, BorderLayout.CENTER);
182     }
183
184     public void setValues() {
185         openLast.setSelected(_prefs.getBoolean("openLastEdited"));
186         backup.setSelected(_prefs.getBoolean("backup"));
187         if (_prefs.getBoolean("saveInStandardOrder"))
188             saveAuthorOrder.setSelected(true);
189         else if (_prefs.getBoolean("saveInOriginalOrder"))
190             saveOriginalOrder.setSelected(true);
191         else if (_prefs.getBoolean("saveInTitleOrder"))
192             saveTitleOrder.setSelected(true);
193         else
194             saveTableOrder.setSelected(true);
195         if (_prefs.getBoolean("exportInStandardOrder"))
196             exportAuthorOrder.setSelected(true);
197         else if (_prefs.getBoolean("exportInOriginalOrder"))
198             exportOriginalOrder.setSelected(true);
199         else if (_prefs.getBoolean("exportInTitleOrder"))
200             exportTitleOrder.setSelected(true);
201         else
202             exportTableOrder.setSelected(true);
203
204         //preserveFormatting.setSelected(_prefs.getBoolean("preserveFieldFormatting"));
205         autoDoubleBraces.setSelected(_prefs.getBoolean("autoDoubleBraces"));
206         resolveStringsAll.setSelected(_prefs.getBoolean("resolveStringsAllFields"));
207         resolveStringsStandard.setSelected(!resolveStringsAll.isSelected());
208         doNotResolveStringsFor.setText(_prefs.get("doNotResolveStringsFor"));
209         bracesAroundCapitalsFields.setText(_prefs.get("putBracesAroundCapitals"));
210         nonWrappableFields.setText(_prefs.get("nonWrappableFields"));
211
212         autoSave.setSelected(_prefs.getBoolean("autoSave"));
213         promptBeforeUsingAutoSave.setSelected(_prefs.getBoolean("promptBeforeUsingAutosave"));
214         autoSaveInterval.setValue(_prefs.getInt("autoSaveInterval"));
215         origAutoSaveSetting = autoSave.isSelected();
216         valueDelimiter.setSelectedIndex(_prefs.getInt("valueDelimiters"));
217         includeEmptyFields.setSelected(_prefs.getBoolean("includeEmptyFields"));
218         camelCase.setSelected(_prefs.getBoolean(JabRefPreferences.WRITEFIELD_CAMELCASENAME));
219         sameColumn.setSelected(_prefs.getBoolean(JabRefPreferences.WRITEFIELD_ADDSPACES));
220     }
221
222     public void storeSettings() {
223         _prefs.putBoolean("backup", backup.isSelected());
224         _prefs.putBoolean("openLastEdited", openLast.isSelected());
225         _prefs.putBoolean("saveInStandardOrder", saveAuthorOrder.isSelected());
226         _prefs.putBoolean("saveInOriginalOrder", saveOriginalOrder.isSelected());
227         _prefs.putBoolean("saveInTitleOrder", saveTitleOrder.isSelected());
228         _prefs.putBoolean("exportInStandardOrder", exportAuthorOrder.isSelected());
229         _prefs.putBoolean("exportInOriginalOrder", exportOriginalOrder.isSelected());
230         _prefs.putBoolean("exportInTitleOrder", exportTitleOrder.isSelected());
231         _prefs.putBoolean("autoDoubleBraces", autoDoubleBraces.isSelected());
232         _prefs.putBoolean("resolveStringsAllFields", resolveStringsAll.isSelected());
233         _prefs.put("doNotResolveStringsFor", doNotResolveStringsFor.getText().trim());
234         _prefs.putBoolean("autoSave", autoSave.isSelected());
235         _prefs.putBoolean("promptBeforeUsingAutosave", promptBeforeUsingAutoSave.isSelected());
236         _prefs.putInt("autoSaveInterval", (Integer)autoSaveInterval.getValue());
237         _prefs.putInt("valueDelimiters", valueDelimiter.getSelectedIndex());
238         _prefs.putBoolean("includeEmptyFields", includeEmptyFields.isSelected());
239         _prefs.putBoolean(JabRefPreferences.WRITEFIELD_CAMELCASENAME, camelCase.isSelected());
240         _prefs.putBoolean(JabRefPreferences.WRITEFIELD_ADDSPACES, sameColumn.isSelected());
241         doNotResolveStringsFor.setText(_prefs.get("doNotResolveStringsFor"));
242         boolean updateSpecialFields = false;
243         if (!bracesAroundCapitalsFields.getText().trim().equals(_prefs.get("putBracesAroundCapitals"))) {
244             _prefs.put("putBracesAroundCapitals", bracesAroundCapitalsFields.getText());
245             updateSpecialFields = true;
246         }
247         if (!nonWrappableFields.getText().trim().equals(_prefs.get("nonWrappableFields"))) {
248             _prefs.put("nonWrappableFields", nonWrappableFields.getText());
249             updateSpecialFields = true;
250         }
251         // If either of the two last entries were changed, run the update for special field handling:
252         if (updateSpecialFields)
253                 _prefs.updateSpecialFieldHandling();
254
255         // See if we should start or stop the auto save manager:
256         if (!origAutoSaveSetting && autoSave.isSelected()) {
257             Globals.startAutoSaveManager(_frame);
258         }
259         else if (origAutoSaveSetting && !autoSave.isSelected()) {
260             Globals.stopAutoSaveManager();
261         }
262
263     }
264
265     public boolean readyToClose() {
266         return true;
267     }
268
269         public String getTabName() {
270                 return Globals.lang("File");
271         }
272 }