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