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