1b6c9adf88b2999d87ae16a5cbde51e0ca891001
[debian/jabref.git] / src / java / net / sf / jabref / GeneralTab.java
1 package net.sf.jabref;
2
3 import javax.swing.*;
4 import javax.swing.event.ChangeListener;
5 import javax.swing.event.ChangeEvent;
6 import java.awt.*;
7 import java.util.Iterator;
8 import java.text.SimpleDateFormat;
9
10 import com.jgoodies.forms.layout.*;
11 import com.jgoodies.forms.builder.*;
12
13 public class GeneralTab extends JPanel implements PrefsTab {
14
15     private JCheckBox autoOpenForm, backup, openLast, showSource,
16     defSource, editSource, defSort, ctrlClick, disableOnMultiple,
17     useOwner, keyDuplicateWarningDialog, keyEmptyWarningDialog, autoDoubleBraces,
18     confirmDelete, allowEditing, /*preserveFormatting, */useImportInspector,
19     useImportInspectorForSingle, inspectionWarnDupli, useTimeStamp;
20     private JRadioButton
21         saveOriginalOrder, saveAuthorOrder, saveTableOrder;
22     private JTextField defOwnerField, timeStampFormat, timeStampField,
23             bracesAroundCapitalsFields, nonWrappableFields;
24     JabRefPreferences _prefs;
25     JabRefFrame _frame;
26     private JComboBox language = new JComboBox(GUIGlobals.LANGUAGES.keySet().toArray()),
27     encodings = new JComboBox(Globals.ENCODINGS);
28     private HelpAction ownerHelp, timeStampHelp;
29
30     public GeneralTab(JabRefFrame frame, JabRefPreferences prefs) {
31         _prefs = prefs;
32         _frame = frame;
33         setLayout(new BorderLayout());
34
35         autoOpenForm = new JCheckBox(Globals.lang("Open editor when a new entry is created"));
36         openLast = new JCheckBox(Globals.lang("Open last edited databases at startup"));
37         allowEditing = new JCheckBox(Globals.lang("Allow editing in table cells"));
38         backup = new JCheckBox(Globals.lang("Backup old file when saving"));
39         defSource = new JCheckBox(Globals.lang("Show BibTeX source by default"));
40         showSource = new JCheckBox(Globals.lang("Show BibTeX source panel"));
41         editSource = new JCheckBox(Globals.lang("Enable source editing"));
42         defSort = new JCheckBox(Globals.lang("Sort Automatically"));
43         ctrlClick = new JCheckBox(Globals.lang("Open right-click menu with Ctrl+left button"));
44         disableOnMultiple = new JCheckBox(Globals.lang("Disable entry editor when multiple entries are selected"));
45         useOwner = new JCheckBox(Globals.lang("Mark new entries with owner name") + ":");
46         useTimeStamp = new JCheckBox(Globals.lang("Mark new entries with addition date") + ". "
47             +Globals.lang("Date format")+ ":");
48         keyDuplicateWarningDialog = new JCheckBox(Globals.lang("Show warning dialog when a duplicate BibTeX key is entered"));
49         keyEmptyWarningDialog = new JCheckBox(Globals.lang("Show warning dialog when an empty BibTeX key is entered")); // JZTODO lyrics
50         confirmDelete = new JCheckBox(Globals.lang("Show confirmation dialog when deleting entries"));
51         saveAuthorOrder = new JRadioButton(Globals.lang("Save ordered by author/editor/year"));
52         saveOriginalOrder = new JRadioButton(Globals.lang("Save entries in their original order"));
53         saveTableOrder = new JRadioButton(Globals.lang("Save in default table sort order"));
54         ButtonGroup bg = new ButtonGroup();
55         bg.add(saveAuthorOrder);
56         bg.add(saveOriginalOrder);
57         bg.add(saveTableOrder);
58         autoDoubleBraces = new JCheckBox(
59                 //+ Globals.lang("Store fields with double braces, and remove extra braces when loading.<BR>"
60                 //+ "Double braces signal that BibTeX should preserve character case.") + "</HTML>");
61                 Globals.lang("Remove double braces around BibTeX fields when loading."));
62         useImportInspector = new JCheckBox(Globals.lang("Display imported entries in an inspection window before they are added."));
63         useImportInspectorForSingle = new JCheckBox(Globals.lang("Use inspection window also when a single entry is imported.")); 
64         JPanel general = new JPanel();
65         defOwnerField = new JTextField();
66         timeStampFormat = new JTextField();
67         timeStampField = new JTextField();
68         ownerHelp = new HelpAction(frame.helpDiag, GUIGlobals.ownerHelp,
69                 "Help", GUIGlobals.helpSmallIconFile);
70         timeStampHelp = new HelpAction(frame.helpDiag, GUIGlobals.timeStampHelp, "Help",
71                 GUIGlobals.helpSmallIconFile);
72         inspectionWarnDupli = new JCheckBox(Globals.lang("Warn about unresolved duplicates when closing inspection window"));
73
74         Insets marg = new Insets(0,12,3,0);
75         useImportInspectorForSingle.setMargin(marg);
76         editSource.setMargin(marg);
77         defSource.setMargin(marg);
78         inspectionWarnDupli.setMargin(marg);
79         bracesAroundCapitalsFields = new JTextField(25);
80         nonWrappableFields = new JTextField(25);
81         // We need a listener on showSource to enable and disable the source panel-related choices:
82         showSource.addChangeListener(new ChangeListener() {
83             public void stateChanged(ChangeEvent event) {
84                 defSource.setEnabled(showSource.isSelected());
85                 editSource.setEnabled(showSource.isSelected());
86             }
87         }
88         );
89         // We need a listener on useImportInspector to enable and disable the
90         // import inspector related choices;
91         useImportInspector.addChangeListener(new ChangeListener() {
92             public void stateChanged(ChangeEvent event) {
93                 useImportInspectorForSingle.setEnabled(useImportInspector.isSelected());
94                 inspectionWarnDupli.setEnabled(useImportInspector.isSelected());
95             }
96         }
97         );
98
99         FormLayout layout = new FormLayout
100                 ("8dlu, left:pref, 8dlu, fill:pref, 4dlu, fill:pref", // 4dlu, left:pref, 4dlu",
101                         "pref, 6dlu, pref, 6dlu, pref, 6dlu, pref, 6dlu, pref, 6dlu, "
102                         +"pref, 6dlu, pref, 6dlu, pref, 6dlu, pref, 6dlu, pref, 6dlu, "
103                         +"pref, 6dlu, pref, 6dlu, pref, 6dlu, pref, 6dlu, pref, 6dlu, "
104                                     +"pref, 6dlu, pref, 6dlu, pref");
105         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
106         CellConstraints cc = new CellConstraints();
107         builder.addSeparator(Globals.lang("File"), cc.xyw(1,1, 5));
108         builder.add(openLast, cc.xy(2,3));
109         builder.add(backup, cc.xy(2,5));
110         builder.add(autoDoubleBraces, cc.xy(2, 7));
111         builder.add(saveAuthorOrder, cc.xy(4, 3));
112         builder.add(saveTableOrder, cc.xy(4, 5));
113         builder.add(saveOriginalOrder, cc.xy(4, 7));
114         JLabel label = new JLabel(Globals.lang("Store the following fields with braces around capital letters")+":");
115         DefaultFormBuilder builder3 = new DefaultFormBuilder
116                 (new FormLayout("left:pref, 4dlu, fill:pref",""));
117         builder3.append(label);
118         builder3.append(bracesAroundCapitalsFields);
119         label = new JLabel(Globals.lang("Do not wrap the following fields when saving")+":");
120         builder3.append(label);
121         builder3.append(nonWrappableFields);
122         builder.add(builder3.getPanel(), cc.xyw(2, 9, 3));
123
124         builder.addSeparator(Globals.lang("Entry editor"), cc.xyw(1, 11, 5));
125         builder.add(autoOpenForm, cc.xy(2, 13));
126         builder.add(disableOnMultiple, cc.xy(2, 15));
127         builder.add(showSource, cc.xy(2, 17));
128         builder.add(defSource, cc.xy(2, 19));
129         builder.addSeparator(Globals.lang("Miscellaneous"), cc.xyw(1, 21, 5));
130         builder.add(useImportInspector, cc.xy(2, 23));
131         builder.add(useImportInspectorForSingle, cc.xy(4, 23));
132         builder.add(inspectionWarnDupli, cc.xy(4, 25));
133         builder.add(ctrlClick, cc.xy(2, 27));
134         builder.add(confirmDelete, cc.xy(2, 29));
135         builder.add(keyDuplicateWarningDialog, cc.xy(2, 31));
136         builder.add(keyEmptyWarningDialog, cc.xy(2, 33));
137         // Create a new panel with its own FormLayout for the last items:
138         FormLayout layout2 = new FormLayout
139                 ("left:pref, 8dlu, fill:60dlu, 4dlu, left:pref, 4dlu, fill:60dlu, 4dlu, fill:pref", "");
140         DefaultFormBuilder builder2 = new DefaultFormBuilder(layout2);
141         builder2.append(useOwner);
142         builder2.append(defOwnerField);
143         JButton hlp = new JButton(ownerHelp);
144         hlp.setText(null);
145         hlp.setPreferredSize(new Dimension(24, 24));
146         builder2.append(hlp);
147         builder2.nextLine();
148         builder2.append(useTimeStamp);
149         builder2.append(timeStampFormat);
150         builder2.append(Globals.lang("Field name")+":");
151         builder2.append(timeStampField);
152         hlp = new JButton(timeStampHelp);
153         hlp.setText(null);
154         hlp.setPreferredSize(new Dimension(24, 24));
155         builder2.append(hlp);
156         builder2.nextLine();
157         JLabel lab;
158         lab = new JLabel(Globals.lang("Language") + ":");
159         builder2.append(lab);
160         builder2.append(language);
161         builder2.nextLine();
162         lab = new JLabel(Globals.lang("Default encoding") + ":");
163         builder2.append(lab);
164         builder2.append(encodings);
165
166         builder.add(builder2.getPanel(), cc.xyw(2, 35, 3));
167
168
169         JPanel pan = builder.getPanel();
170         pan.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
171         add(pan, BorderLayout.CENTER);
172
173     }
174
175     public void setValues() {
176         autoOpenForm.setSelected(_prefs.getBoolean("autoOpenForm"));
177         openLast.setSelected(_prefs.getBoolean("openLastEdited"));
178         allowEditing.setSelected(_prefs.getBoolean("allowTableEditing"));
179         backup.setSelected(_prefs.getBoolean("backup"));
180         defSource.setSelected(_prefs.getBoolean("defaultShowSource"));
181         showSource.setSelected(_prefs.getBoolean("showSource"));
182         editSource.setSelected(_prefs.getBoolean("enableSourceEditing"));
183         defSort.setSelected(_prefs.getBoolean("defaultAutoSort"));
184         ctrlClick.setSelected(_prefs.getBoolean("ctrlClick"));
185         disableOnMultiple.setSelected(_prefs.getBoolean("disableOnMultipleSelection"));
186         useOwner.setSelected(_prefs.getBoolean("useOwner"));
187         useTimeStamp.setSelected(_prefs.getBoolean("useTimeStamp"));
188         keyDuplicateWarningDialog.setSelected(_prefs.getBoolean("dialogWarningForDuplicateKey"));
189         keyEmptyWarningDialog.setSelected(_prefs.getBoolean("dialogWarningForEmptyKey"));
190         confirmDelete.setSelected(_prefs.getBoolean("confirmDelete"));
191         if (_prefs.getBoolean("saveInStandardOrder"))
192             saveAuthorOrder.setSelected(true);
193         else if (_prefs.getBoolean("saveInOriginalOrder"))
194             saveOriginalOrder.setSelected(true);
195         else
196             saveTableOrder.setSelected(true);
197         //preserveFormatting.setSelected(_prefs.getBoolean("preserveFieldFormatting"));
198         autoDoubleBraces.setSelected(_prefs.getBoolean("autoDoubleBraces"));
199         defOwnerField.setText(_prefs.get("defaultOwner"));
200         timeStampFormat.setText(_prefs.get("timeStampFormat"));
201         timeStampField.setText(_prefs.get("timeStampField"));
202         useImportInspector.setSelected(_prefs.getBoolean("useImportInspectionDialog"));
203         useImportInspectorForSingle.setSelected(_prefs.getBoolean("useImportInspectionDialogForSingle"));
204         inspectionWarnDupli.setSelected(_prefs.getBoolean("warnAboutDuplicatesInInspection"));
205         // Two choices only make sense when the source panel is visible:
206         defSource.setEnabled(showSource.isSelected());
207         editSource.setEnabled(showSource.isSelected());
208         useImportInspectorForSingle.setEnabled(useImportInspector.isSelected());
209         inspectionWarnDupli.setEnabled(useImportInspector.isSelected());
210         bracesAroundCapitalsFields.setText(_prefs.get("putBracesAroundCapitals"));
211         nonWrappableFields.setText(_prefs.get("nonWrappableFields"));
212
213         String enc = _prefs.get("defaultEncoding");
214         outer: for (int i = 0; i < Globals.ENCODINGS.length; i++) {
215             if (Globals.ENCODINGS[i].equalsIgnoreCase(enc)) {
216                 encodings.setSelectedIndex(i);
217                 break outer;
218             }
219         }
220         String oldLan = _prefs.get("language");
221
222         // Language choice
223         int ilk = 0;
224         for (Iterator i = GUIGlobals.LANGUAGES.keySet().iterator(); i.hasNext();) {
225             if (GUIGlobals.LANGUAGES.get(i.next()).equals(oldLan)) {
226                 language.setSelectedIndex(ilk);
227             }
228             ilk++;
229         }
230
231     }
232
233     public void storeSettings() {
234         _prefs.putBoolean("autoOpenForm", autoOpenForm.isSelected());
235         _prefs.putBoolean("backup", backup.isSelected());
236         _prefs.putBoolean("openLastEdited", openLast.isSelected());
237         _prefs.putBoolean("defaultShowSource", defSource.isSelected());
238         _prefs.putBoolean("enableSourceEditing", editSource.isSelected());
239         _prefs.putBoolean("disableOnMultipleSelection", disableOnMultiple.isSelected());
240         _prefs.putBoolean("useOwner", useOwner.isSelected());
241         _prefs.putBoolean("useTimeStamp", useTimeStamp.isSelected());
242         _prefs.putBoolean("dialogWarningForDuplicateKey", keyDuplicateWarningDialog.isSelected());
243         _prefs.putBoolean("dialogWarningForEmptyKey", keyEmptyWarningDialog.isSelected());
244         _prefs.putBoolean("confirmDelete", confirmDelete.isSelected());
245         _prefs.putBoolean("saveInStandardOrder", saveAuthorOrder.isSelected());
246         _prefs.putBoolean("saveInOriginalOrder", saveOriginalOrder.isSelected());
247         _prefs.putBoolean("allowTableEditing", allowEditing.isSelected());
248         _prefs.putBoolean("ctrlClick", ctrlClick.isSelected());
249         //_prefs.putBoolean("preserveFieldFormatting", preserveFormatting.isSelected());
250         _prefs.putBoolean("autoDoubleBraces", autoDoubleBraces.isSelected());
251         _prefs.putBoolean("useImportInspectionDialog", useImportInspector.isSelected());
252         _prefs.putBoolean("useImportInspectionDialogForSingle", useImportInspectorForSingle.isSelected());
253         _prefs.putBoolean("warnAboutDuplicatesInInspection", inspectionWarnDupli.isSelected());
254         //_prefs.putBoolean("defaultAutoSort", defSorrrt.isSelected());
255         String owner = defOwnerField.getText().trim();
256         _prefs.put("defaultOwner", owner);
257         _prefs.WRAPPED_USERNAME = "["+owner+"]";
258         _prefs.put("timeStampFormat", timeStampFormat.getText().trim());
259         _prefs.put("timeStampField", timeStampField.getText().trim());
260         _prefs.put("defaultEncoding", (String) encodings.getSelectedItem());
261         boolean updateSpecialFields = false;
262         if (!bracesAroundCapitalsFields.getText().trim().equals(_prefs.get("putBracesAroundCapitals"))) {
263             _prefs.put("putBracesAroundCapitals", bracesAroundCapitalsFields.getText());
264             updateSpecialFields = true;
265         }
266         if (!nonWrappableFields.getText().trim().equals(_prefs.get("nonWrappableFields"))) {
267             _prefs.put("nonWrappableFields", nonWrappableFields.getText());
268             updateSpecialFields = true;
269         }
270         // If either of the two last entries were changed, run the update for special field handling:
271         if (updateSpecialFields)
272                 _prefs.updateSpecialFieldHandling();
273
274         // We want to know if the following setting has been modified:
275         boolean oldShowSource = _prefs.getBoolean("showSource");
276         _prefs.putBoolean("showSource", showSource.isSelected());
277         if (oldShowSource != showSource.isSelected()) {
278             // The source panel has been enabled or disabled, so we need to
279             // remove all entry editors from cache:
280             for (int j=0; j<_frame.getTabbedPane().getTabCount(); j++) {
281                     BasePanel bp = (BasePanel)_frame.getTabbedPane().getComponentAt(j);
282                     bp.entryEditors.clear();
283             }
284         }
285
286
287         if (!GUIGlobals.LANGUAGES.get(language.getSelectedItem()).equals(_prefs.get("language"))) {
288             _prefs.put("language", GUIGlobals.LANGUAGES.get(language.getSelectedItem()).toString());
289             Globals.setLanguage(GUIGlobals.LANGUAGES.get(language.getSelectedItem()).toString(), "");
290             JOptionPane.showMessageDialog(null, Globals.lang("You have changed the language setting. "
291                     + "You must restart JabRef for this to come into effect."), Globals.lang("Changed language settings"),
292                     JOptionPane.WARNING_MESSAGE);
293         }
294
295
296
297     }
298
299     public boolean readyToClose() {
300         try {
301             // Test if date format is legal:
302             SimpleDateFormat sdf = new SimpleDateFormat(timeStampFormat.getText());
303
304         } catch (IllegalArgumentException ex2) {
305             JOptionPane.showMessageDialog
306                     (null, Globals.lang("The chosen date format for new entries is not valid"),
307                             Globals.lang("Invalid date format"),
308                             JOptionPane.ERROR_MESSAGE);
309             return false;
310         }
311         return true;
312     }
313
314 }