2a2bb5d7618ffc308a6ed4fba47dc5bff52770d6
[debian/jabref.git] / src / java / net / sf / jabref / TabLabelPattern.java
1 /*
2  * Created on 09-Dec-2003
3  */
4 package net.sf.jabref;
5
6 import java.util.Iterator;
7 import java.util.HashMap;
8 import java.awt.GridBagConstraints;
9 import java.awt.GridBagLayout;
10 import java.awt.Insets;
11 import java.awt.Font;
12 import java.awt.Container;
13 import java.awt.BorderLayout;
14 import java.awt.event.ActionEvent;
15 import java.awt.event.ActionListener;
16
17 import javax.swing.*;
18 import javax.swing.event.ChangeListener;
19 import javax.swing.event.ChangeEvent;
20
21 import net.sf.jabref.labelPattern.LabelPattern;
22 import net.sf.jabref.labelPattern.LabelPatternUtil;
23 import com.jgoodies.forms.layout.FormLayout;
24 import com.jgoodies.forms.builder.DefaultFormBuilder;
25
26 /**
27  * The Preferences panel for key generation.
28  */
29 public class TabLabelPattern extends JPanel implements PrefsTab{
30         
31     private String def = Globals.lang("Default");
32     private GridBagLayout gbl = new GridBagLayout();
33     private GridBagConstraints con = new GridBagConstraints();
34     private HashMap textFields = new HashMap();
35
36         private JabRefPreferences _prefs;
37         private LabelPattern _keypatterns = null;
38         
39     private JCheckBox dontOverwrite = new JCheckBox(Globals.lang("Do not overwrite existing keys")),
40         warnBeforeOverwriting = new JCheckBox(Globals.lang("Warn before overwriting existing keys")),
41         generateOnSave = new JCheckBox(Globals.lang("Generate keys before saving (for entries without a key)"));
42
43
44     private JLabel lblEntryType, lblKeyPattern;
45
46     private JTextField defaultPat = new JTextField();
47
48     //private JTextField basenamePatternRegex = new JTextField(20);
49     //private JTextField basenamePatternReplacement = new JTextField(20);
50     private JTextField KeyPatternRegex = new JTextField(20);
51     private JTextField KeyPatternReplacement = new JTextField(20);
52
53         private JButton btnDefaultAll, btnDefault;
54
55
56     private HelpAction help;
57         
58         /**
59          * The constructor
60          */
61         public TabLabelPattern(JabRefPreferences prefs, HelpDialog helpDiag) {
62                 _prefs = prefs;
63                 //_keypatterns = _prefs.getKeyPattern();
64                 help = new HelpAction(helpDiag, GUIGlobals.labelPatternHelp,
65                                       "Help on key patterns");
66                 buildGUI();
67         //fillTextfields();
68         }
69
70         /**
71          * Store changes to table preferences. This method is called when
72          * the user clicks Ok.
73          *
74          */
75         public void storeSettings() {
76
77          // Set the default value:
78          Globals.prefs.put("defaultLabelPattern", defaultPat.getText());
79
80          Globals.prefs.putBoolean("warnBeforeOverwritingKey", warnBeforeOverwriting.isSelected());
81          Globals.prefs.putBoolean("avoidOverwritingKey", dontOverwrite.isSelected());
82
83          //Globals.prefs.put("basenamePatternRegex", basenamePatternRegex.getText());
84          //Globals.prefs.put("basenamePatternReplacement", basenamePatternReplacement.getText());
85          Globals.prefs.put("KeyPatternRegex", KeyPatternRegex.getText());
86          Globals.prefs.put("KeyPatternReplacement", KeyPatternReplacement.getText());
87          Globals.prefs.putBoolean("generateKeysBeforeSaving", generateOnSave.isSelected());
88          LabelPatternUtil.updateDefaultPattern();
89
90
91             LabelPattern defKeyPattern = _keypatterns.getParent();
92             _keypatterns = new LabelPattern(defKeyPattern);
93             
94             // then we rebuild... 
95             Iterator i=textFields.keySet().iterator();
96             //String defa = (String)LabelPatternUtil.DEFAULT_LABELPATTERN.get(0);
97             while (i.hasNext()) {
98                 String s = (String)i.next(),
99                     text = ((JTextField)textFields.get(s)).getText();
100                 if (!"".equals(text.trim())) //(!defa.equals(text))
101                     _keypatterns.addLabelPattern(s, text);
102             }
103
104             _prefs.putKeyPattern(_keypatterns);
105
106         }
107         
108     private  JTextField addEntryType(Container c, String name, int y) { 
109
110         JLabel lab = new JLabel(Util.nCase(name));
111         name = name.toLowerCase();
112         con.gridx = 0;
113         con.gridy = y;
114         con.fill = GridBagConstraints.BOTH;
115         con.weightx = 0;
116         con.weighty = 0;
117         con.anchor = GridBagConstraints.WEST;
118         con.insets = new Insets( 0,5,0,5 );
119         gbl.setConstraints( lab, con );
120         c.add( lab );
121         
122         JTextField tf = new JTextField();//_keypatterns.getValue(name).get(0).toString());
123         tf.setColumns( 15 );
124         con.gridx = 1;
125         con.fill = GridBagConstraints.HORIZONTAL;
126         con.weightx = 1;
127         con.weighty = 0;
128         con.anchor = GridBagConstraints.CENTER;
129         con.insets = new Insets( 0,5,0,5 );
130         gbl.setConstraints( tf, con );
131         c.add( tf );    
132         
133         JButton but = new JButton( def );
134         con.gridx = 2;
135         con.fill = GridBagConstraints.BOTH;
136         con.weightx = 0;
137         con.weighty = 0;
138         con.anchor = GridBagConstraints.CENTER;
139         con.insets = new Insets( 0,5,0,5 );
140         gbl.setConstraints( but, con );
141         but.setActionCommand(name);
142         but.addActionListener(new buttonHandler());
143         c.add( but );           
144
145         return tf;
146     }
147
148     private void setValue(JTextField tf, String fieldName) {
149         if (_keypatterns.isDefaultValue(fieldName))
150             tf.setText("");
151         else {
152             //System.out.println(":: "+_keypatterns.getValue(fieldName).get(0).toString());
153             tf.setText(_keypatterns.getValue(fieldName).get(0).toString());
154         }
155     }
156
157         /**
158          * Method to build GUI
159          *
160          */
161         private void buildGUI(){
162
163             JPanel pan = new JPanel();
164             JScrollPane sp = new JScrollPane(pan);      
165             sp.setBorder(BorderFactory.createEmptyBorder());
166             pan.setLayout(gbl);
167             setLayout(gbl);         
168             // The header - can be removed
169             lblEntryType = new JLabel(Globals.lang("Entry type"));
170             Font f = new Font("plain", Font.BOLD, 12);
171             lblEntryType.setFont(f);
172             con.gridx = 0;
173             con.gridy = 0;
174             con.gridwidth = 1;
175             con.gridheight = 1;
176             con.fill = GridBagConstraints.VERTICAL;
177             con.anchor = GridBagConstraints.WEST;
178             con.insets = new Insets( 5,5,10,0 );
179             gbl.setConstraints( lblEntryType, con );
180             pan. add( lblEntryType );
181             
182             lblKeyPattern = new JLabel(Globals.lang("Key pattern"));
183             lblKeyPattern.setFont(f);
184             con.gridx = 1;
185             con.gridy = 0;
186             //con.gridwidth = 2;
187             con.gridheight = 1;
188             con.fill = GridBagConstraints.HORIZONTAL;
189             con.anchor = GridBagConstraints.WEST;
190             con.insets = new Insets( 5,5,10,5 );
191             gbl.setConstraints( lblKeyPattern, con );
192             pan.add( lblKeyPattern );
193
194
195             con.gridy = 1;
196             con.gridx = 0;
197             JLabel lab = new JLabel(Globals.lang("Default pattern"));
198             gbl.setConstraints(lab, con);
199             pan.add(lab);
200             con.gridx = 1;
201             gbl.setConstraints(defaultPat, con);
202             pan.add(defaultPat);
203         con.insets = new Insets( 5,5,10,5 );
204         btnDefault = new JButton(Globals.lang("Default"));
205         btnDefault.addActionListener(new ActionListener() {
206             public void actionPerformed(ActionEvent event) {
207                 defaultPat.setText((String)Globals.prefs.defaults.get("defaultLabelPattern"));
208             }
209         });
210         con.gridx = 2;
211             int y = 2;
212         gbl.setConstraints(btnDefault, con);
213         pan.add(btnDefault);
214
215             Iterator i=BibtexEntryType.ALL_TYPES.keySet().iterator();
216             while (i.hasNext()) {
217                 String s = (String)i.next();
218                 textFields.put(s, addEntryType(pan, s, y));
219                 y++;
220             }
221
222             con.fill = GridBagConstraints.BOTH;
223             con.gridx = 0;
224             con.gridy = 1;
225             con.gridwidth = 3;
226             con.weightx = 1;
227             con.weighty = 1;
228             gbl.setConstraints(sp, con );
229             add(sp);
230
231             // A help button
232             con.gridwidth = 1;
233             con.gridx = 1;
234             con.gridy = 2;
235             con.fill = GridBagConstraints.HORIZONTAL;
236             //
237             con.weightx = 0;
238             con.weighty = 0;
239             con.anchor = GridBagConstraints.SOUTHEAST;
240             con.insets = new Insets( 0,5,0,5 );
241             JButton hlb = new JButton(GUIGlobals.getImage("helpSmall"));
242             hlb.setToolTipText(Globals.lang("Help on key patterns"));
243             gbl.setConstraints( hlb, con );
244             add(hlb);
245             hlb.addActionListener(help);
246             
247             // And finally a button to reset everything
248             btnDefaultAll = new JButton(Globals.lang("Reset all"));
249             con.gridx = 2;
250             con.gridy = 2;
251
252             //con.fill = GridBagConstraints.BOTH;
253             con.weightx = 1;
254             con.weighty = 0;
255             con.anchor = GridBagConstraints.SOUTHEAST;
256             con.insets = new Insets( 20,5,0,5 );
257             gbl.setConstraints( btnDefaultAll, con );
258             btnDefaultAll.addActionListener(new buttonHandler());
259             add( btnDefaultAll );
260
261
262         // Build a panel for checkbox settings:
263         FormLayout layout = new FormLayout
264                 ("1dlu, 8dlu, left:pref, 8dlu, left:pref", "");//, 8dlu, 20dlu, 8dlu, fill:pref", "");
265         pan = new JPanel();
266             DefaultFormBuilder builder = new DefaultFormBuilder(layout);
267         builder.appendSeparator(Globals.lang("Key generator settings"));
268
269         builder.nextLine();
270         builder.append(pan);
271         builder.append(warnBeforeOverwriting);
272         builder.append(dontOverwrite);
273         builder.nextLine();
274         builder.append(pan);
275         builder.append(generateOnSave);        
276         builder.nextLine();
277         builder.append(pan);
278         builder.append(Globals.lang("Replace (regular expression)")+":");
279         builder.append(Globals.lang("by")+":");
280
281         builder.nextLine();
282         builder.append(pan);
283         builder.append(KeyPatternRegex);
284         builder.append(KeyPatternReplacement);
285
286         builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
287         con.gridx = 1;
288             con.gridy = 3;
289         con.gridwidth = GridBagConstraints.REMAINDER;
290         con.weightx = 1;
291         con.fill = GridBagConstraints.BOTH;
292         gbl.setConstraints(builder.getPanel(), con);
293         add(builder.getPanel());
294
295         dontOverwrite.addChangeListener(new ChangeListener() {
296             public void stateChanged(ChangeEvent event) {
297                 // Warning before overwriting is only relevant if overwriting can happen:
298                 warnBeforeOverwriting.setEnabled(!dontOverwrite.isSelected());
299             }
300         });
301
302       /*
303        Simon Fischer's patch for replacing a regexp in keys before converting to filename:
304
305         layout = new FormLayout
306                 ("left:pref, 8dlu, left:pref, left:pref", "");
307         builder = new DefaultFormBuilder(layout);
308         builder.appendSeparator(Globals.lang("Bibkey to filename conversion"));
309         builder.nextLine();
310         builder.append(Globals.lang("Replace"), basenamePatternRegex);
311         builder.nextLine();
312         builder.append(Globals.lang("by"), basenamePatternReplacement);
313         builder.nextLine();
314
315         builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
316         con.gridx = 2;
317         con.gridy = 3;
318         con.gridwidth = GridBagConstraints.REMAINDER;
319         con.weightx = 1;
320         con.fill = GridBagConstraints.BOTH;
321         gbl.setConstraints(builder.getPanel(), con);
322         add(builder.getPanel());
323         */
324         }
325         
326         /**
327          * Method for filling the text fields with user defined key patterns or default.
328          * The method used (<code>getValue(key)</code>) to get the ArrayList 
329          * corrosponding to an entry type throws a <code>NullPointerException</code>
330          * and <code>?</code> if an entry cannot be found. It really shouln't be
331          * nessesary to catch those exceptions here... 
332          */
333     /*  private void fillTextfields(){
334                 txtArticle.setText(_keypatterns.getValue("article").get(0).toString());
335                 txtBook.setText(_keypatterns.getValue("book").get(0).toString());
336                 txtBooklet.setText(_keypatterns.getValue("booklet").get(0).toString());
337                 txtConference.setText(_keypatterns.getValue("conference").get(0).toString());
338                 txtInbook.setText(_keypatterns.getValue("inbook").get(0).toString());
339                 txtIncollection.setText(_keypatterns.getValue("incollection").get(0).toString());
340                 txtInproceedings.setText(_keypatterns.getValue("inproceedings").get(0).toString());
341                 txtManual.setText(_keypatterns.getValue("manual").get(0).toString());
342                 txtMastersthesis.setText(_keypatterns.getValue("mastersthesis").get(0).toString());
343                 txtMisc.setText(_keypatterns.getValue("misc").get(0).toString());
344                 txtPhdthesis.setText(_keypatterns.getValue("phdthesis").get(0).toString());
345                 txtProceedings.setText(_keypatterns.getValue("proceedings").get(0).toString());
346                 txtTechreport.setText(_keypatterns.getValue("techreport").get(0).toString());
347                 txtUnpublished.setText(_keypatterns.getValue("unpublished").get(0).toString());
348         }
349
350     */
351         /**
352          * An inner class to handle button actions
353          * @author Ulrik Stervbo (ulriks AT ruc.dk)
354          */
355         class buttonHandler implements ActionListener{
356                 public void actionPerformed(ActionEvent evt){
357
358                     if (evt.getSource() == btnDefaultAll) {
359                         // All to default
360                         Iterator i=textFields.keySet().iterator();
361                         while (i.hasNext()) {
362                             String s = (String)i.next();
363                             //_keypatterns.removeLabelPattern(s);
364                             JTextField tf = (JTextField)textFields.get(s);
365                             tf.setText("");
366                             /*tf.setText(_keypatterns.getParent()
367                                        .getValue(s).get(0).toString());*/
368                         }
369
370                         return;
371                     }
372
373                     //_keypatterns.removeLabelPattern(evt.getActionCommand());
374                     JTextField tf = (JTextField)textFields.get(evt.getActionCommand());
375                     tf.setText("");
376                     /*tf.setText(_keypatterns.getParent()
377                                .getValue(evt.getActionCommand()).get(0).toString());*/
378                 }
379             
380         }
381
382     public boolean readyToClose() {
383         return true;
384     }
385
386     public void setValues() {
387         _keypatterns = _prefs.getKeyPattern();
388         defaultPat.setText(Globals.prefs.get("defaultLabelPattern"));
389         dontOverwrite.setSelected(Globals.prefs.getBoolean("avoidOverwritingKey"));
390         generateOnSave.setSelected(Globals.prefs.getBoolean("generateKeysBeforeSaving"));
391         warnBeforeOverwriting.setSelected(Globals.prefs.getBoolean("warnBeforeOverwritingKey"));
392         // Warning before overwriting is only relevant if overwriting can happen:
393         warnBeforeOverwriting.setEnabled(!dontOverwrite.isSelected());
394             for (Iterator i=textFields.keySet().iterator(); i.hasNext();) {
395             String name = (String)i.next();
396             JTextField tf = (JTextField)textFields.get(name);
397             setValue(tf, name);
398         }
399
400         KeyPatternRegex.setText(Globals.prefs.get("KeyPatternRegex"));
401         KeyPatternReplacement.setText(Globals.prefs.get("KeyPatternReplacement"));
402
403             //basenamePatternRegex.setText(Globals.prefs.get("basenamePatternRegex"));
404             //basenamePatternReplacement.setText(Globals.prefs.get("basenamePatternReplacement"));
405     }
406 }