a9e8db03eecf915c9e61d99958293c1a95aa2a7a
[debian/jabref.git] / src / java / net / sf / jabref / TablePrefsTab.java
1 package net.sf.jabref;
2
3 import java.util.Vector;
4 import javax.swing.*;
5 import javax.swing.event.ChangeListener;
6 import javax.swing.event.ChangeEvent;
7 import javax.swing.table.*;
8 import java.awt.*;
9 import java.awt.event.*;
10 import com.jgoodies.forms.layout.*;
11 import com.jgoodies.forms.factories.*;
12 import com.jgoodies.forms.builder.*;
13 import net.sf.jabref.gui.ColorSetupPanel;
14
15 class TablePrefsTab extends JPanel implements PrefsTab {
16
17     JabRefPreferences _prefs;
18     private JCheckBox autoResizeMode, priDesc, secDesc, terDesc,
19     pdfColumn, urlColumn, citeseerColumn;
20     private JRadioButton namesAsIs, namesFf, namesFl, namesNatbib, abbrNames, noAbbrNames, lastNamesOnly;
21     private JComboBox
22     priSort = new JComboBox(BibtexFields.getAllFieldNames()),
23     secSort = new JComboBox(BibtexFields.getAllFieldNames()),
24     terSort = new JComboBox(BibtexFields.getAllFieldNames());
25     private JTextField priField, secField, terField;
26     private JabRefFrame frame;
27
28
29     /**
30      * Customization of external program paths.
31      *
32      * @param prefs a <code>JabRefPreferences</code> value
33      */
34     public TablePrefsTab(JabRefPreferences prefs, JabRefFrame frame) {
35         _prefs = prefs;
36         this.frame = frame;
37         setLayout(new BorderLayout());
38
39
40
41         autoResizeMode = new JCheckBox(Globals.lang
42                        ("Fit table horizontally on screen"));
43
44         namesAsIs = new JRadioButton(Globals.lang("Show names unchanged"));
45         namesFf = new JRadioButton(Globals.lang("Show 'Firstname Lastname'"));
46         namesFl = new JRadioButton(Globals.lang("Show 'Lastname, Firstname'"));
47         namesNatbib = new JRadioButton(Globals.lang("Natbib style"));
48         noAbbrNames = new JRadioButton(Globals.lang("Do not abbreviate names"));
49         abbrNames = new JRadioButton(Globals.lang("Abbreviate names"));
50         lastNamesOnly = new JRadioButton("Show last names only");
51         pdfColumn = new JCheckBox(Globals.lang("Show PDF/PS column"));
52         urlColumn = new JCheckBox(Globals.lang("Show URL/DOI column"));
53         citeseerColumn = new JCheckBox(Globals.lang("Show CiteSeer column"));
54
55         priField = new JTextField(10);
56         secField = new JTextField(10);
57         terField = new JTextField(10);
58
59         priSort.insertItemAt(Globals.lang("<select>"), 0);
60         secSort.insertItemAt(Globals.lang("<select>"), 0);
61         terSort.insertItemAt(Globals.lang("<select>"), 0);
62
63         priSort.addActionListener(new ActionListener() {
64           public void actionPerformed(ActionEvent e) {
65             if (priSort.getSelectedIndex() > 0) {
66               priField.setText(BibtexFields.getFieldName( priSort.getSelectedIndex() - 1));
67               priSort.setSelectedIndex(0);
68             }
69           }
70         });
71         secSort.addActionListener(new ActionListener() {
72           public void actionPerformed(ActionEvent e) {
73             if (secSort.getSelectedIndex() > 0) {
74               secField.setText(BibtexFields.getFieldName( secSort.getSelectedIndex() - 1));
75               secSort.setSelectedIndex(0);
76             }
77           }
78         });
79         terSort.addActionListener(new ActionListener() {
80           public void actionPerformed(ActionEvent e) {
81             if (terSort.getSelectedIndex() > 0) {
82               terField.setText(BibtexFields.getFieldName( terSort.getSelectedIndex() - 1));
83               terSort.setSelectedIndex(0);
84             }
85           }
86         });
87
88     ButtonGroup bg = new ButtonGroup();
89     bg.add(namesAsIs);
90     bg.add(namesNatbib);
91     bg.add(namesFf);
92     bg.add(namesFl);
93     ButtonGroup bg2 = new ButtonGroup();
94     bg2.add(lastNamesOnly);
95     bg2.add(abbrNames);
96     bg2.add(noAbbrNames);
97     priDesc = new JCheckBox(Globals.lang("Descending"));
98     secDesc = new JCheckBox(Globals.lang("Descending"));
99     terDesc = new JCheckBox(Globals.lang("Descending"));
100
101     FormLayout layout = new FormLayout
102         ("1dlu, 8dlu, left:pref, 4dlu, fill:pref, 4dlu, fill:60dlu, 4dlu, fill:pref",
103          "");
104     DefaultFormBuilder builder = new DefaultFormBuilder(layout);
105     JLabel lab;
106     JPanel pan = new JPanel();
107     builder.appendSeparator(Globals.lang("Special table columns"));
108     builder.nextLine();
109     builder.append(pan); builder.append(pdfColumn); builder.nextLine();
110     builder.append(pan); builder.append(urlColumn); builder.nextLine();
111     builder.append(pan); builder.append(citeseerColumn); builder.nextLine();
112     builder.appendSeparator(Globals.lang("Format of author and editor names"));
113     DefaultFormBuilder nameBuilder = new DefaultFormBuilder(
114             new FormLayout("left:pref, 8dlu, left:pref",""));
115
116     nameBuilder.append(namesAsIs); nameBuilder.append(noAbbrNames); nameBuilder.nextLine();
117     nameBuilder.append(namesFf); nameBuilder.append(abbrNames); nameBuilder.nextLine();
118     nameBuilder.append(namesFl); nameBuilder.append(lastNamesOnly); nameBuilder.nextLine();
119     nameBuilder.append(namesNatbib);
120         builder.append(pan); builder.append(nameBuilder.getPanel()); builder.nextLine();
121     //builder.append(pan); builder.append(noAbbrNames); builder.nextLine();
122     //builder.append(pan); builder.append(abbrNames); builder.nextLine();
123     //builder.append(pan); builder.append(lastNamesOnly); builder.nextLine();
124
125     builder.appendSeparator(Globals.lang("Default sort criteria"));
126     // Create a new panel with its own FormLayout for these items:
127     FormLayout layout2 = new FormLayout
128         ("left:pref, 8dlu, fill:pref, 4dlu, fill:60dlu, 4dlu, left:pref", "");
129     DefaultFormBuilder builder2 = new DefaultFormBuilder(layout2);
130     lab = new JLabel(Globals.lang("Primary sort criterion"));
131     builder2.append(lab);
132     builder2.append(priSort);
133     builder2.append(priField);
134     builder2.append(priDesc);
135     builder2.nextLine();
136     lab = new JLabel(Globals.lang("Secondary sort criterion"));
137     builder2.append(lab);
138     builder2.append(secSort);
139     builder2.append(secField);
140     builder2.append(secDesc);
141     builder2.nextLine();
142     lab = new JLabel(Globals.lang("Tertiary sort criterion"));
143     builder2.append(lab);
144     builder2.append(terSort);
145     builder2.append(terField);
146     builder2.append(terDesc);
147     builder.nextLine();
148     builder.append(pan);
149     builder.append(builder2.getPanel());
150     builder.nextLine();
151     builder.appendSeparator(Globals.lang("General"));
152     builder.append(pan); builder.append(autoResizeMode); builder.nextLine();
153
154
155     pan = builder.getPanel();
156     pan.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
157     add(pan, BorderLayout.CENTER);
158
159         namesNatbib.addChangeListener(new ChangeListener() {
160             public void stateChanged(ChangeEvent changeEvent) {
161                 abbrNames.setEnabled(!namesNatbib.isSelected());
162                 lastNamesOnly.setEnabled(!namesNatbib.isSelected());
163                 noAbbrNames.setEnabled(!namesNatbib.isSelected());
164             }
165         });
166     }
167
168     public void setValues() {
169     autoResizeMode.setSelected((_prefs.getInt("autoResizeMode")==JTable.AUTO_RESIZE_ALL_COLUMNS));
170     pdfColumn.setSelected(_prefs.getBoolean("pdfColumn"));
171     urlColumn.setSelected(_prefs.getBoolean("urlColumn"));
172     citeseerColumn.setSelected(_prefs.getBoolean("citeseerColumn"));
173
174     priField.setText(_prefs.get("priSort"));
175     secField.setText(_prefs.get("secSort"));
176     terField.setText(_prefs.get("terSort"));
177         priSort.setSelectedIndex(0);
178         secSort.setSelectedIndex(0);
179         terSort.setSelectedIndex(0);
180
181     if (_prefs.getBoolean("namesAsIs"))
182         namesAsIs.setSelected(true);
183     else if (_prefs.getBoolean("namesFf"))
184         namesFf.setSelected(true);
185     else if (_prefs.getBoolean("namesNatbib"))
186         namesNatbib.setSelected(true);
187     else
188         namesFl.setSelected(true);
189     if (_prefs.getBoolean("abbrAuthorNames"))
190         abbrNames.setSelected(true);
191     else if (_prefs.getBoolean("namesLastOnly"))
192         lastNamesOnly.setSelected(true);
193     else
194         noAbbrNames.setSelected(true);
195     priDesc.setSelected(_prefs.getBoolean("priDescending"));
196     secDesc.setSelected(_prefs.getBoolean("secDescending"));
197     terDesc.setSelected(_prefs.getBoolean("terDescending"));
198
199         abbrNames.setEnabled(!namesNatbib.isSelected());
200         lastNamesOnly.setEnabled(!namesNatbib.isSelected());
201         noAbbrNames.setEnabled(!namesNatbib.isSelected());
202     }
203
204     /**
205      * Store changes to table preferences. This method is called when
206      * the user clicks Ok.
207      *
208      */
209     public void storeSettings() {
210
211     _prefs.putBoolean("namesAsIs", namesAsIs.isSelected());
212     _prefs.putBoolean("namesFf", namesFf.isSelected());
213     _prefs.putBoolean("namesNatbib", namesNatbib.isSelected());
214     _prefs.putBoolean("namesLastOnly", lastNamesOnly.isSelected());
215     _prefs.putBoolean("abbrAuthorNames", abbrNames.isSelected());
216
217         _prefs.putBoolean("pdfColumn", pdfColumn.isSelected());
218         _prefs.putBoolean("urlColumn", urlColumn.isSelected());
219         _prefs.putBoolean("citeseerColumn", citeseerColumn.isSelected());
220     _prefs.putInt("autoResizeMode",
221               autoResizeMode.isSelected() ?
222               JTable.AUTO_RESIZE_ALL_COLUMNS :
223               JTable.AUTO_RESIZE_OFF);
224     _prefs.putBoolean("priDescending", priDesc.isSelected());
225     _prefs.putBoolean("secDescending", secDesc.isSelected());
226     _prefs.putBoolean("terDescending", terDesc.isSelected());
227     //_prefs.put("secSort", GUIGlobals.ALL_FIELDS[secSort.getSelectedIndex()]);
228     //_prefs.put("terSort", GUIGlobals.ALL_FIELDS[terSort.getSelectedIndex()]);
229         _prefs.put("priSort", priField.getText().toLowerCase().trim());
230         _prefs.put("secSort", secField.getText().toLowerCase().trim());
231         _prefs.put("terSort", terField.getText().toLowerCase().trim());
232     // updatefont
233     }
234
235     public boolean readyToClose() {
236     return true;
237     }
238
239 }
240
241         /*
242         Boolean[] sel = new Boolean[GUIGlobals.ALL_FIELDS.length];
243         boolean found;
244         _choices = GUIGlobals.ALL_FIELDS;
245         _sel = sel;
246         String[] columns = prefs.getStringArray("columnNames");
247         for (int i=0; i<_choices.length; i++) {
248             found = false;
249             for (int j=0; j<columns.length; j++)
250                 if (columns[j].equals(_choices[i]))
251                     found = true;
252             if (found)
253                 sel[i] = new Boolean(true);
254             else
255                 sel[i] = new Boolean(false);
256         }
257
258         TableModel tm = new AbstractTableModel() {
259                 public int getRowCount() { return (_choices.length-1)/2; }
260                 public int getColumnCount() { return 4; }
261                 public Object getValueAt(int row, int column) {
262                     switch (column) {
263                     case 0:
264                         return _choices[row];
265                     case 1:
266                         return _sel[row];
267                     case 2:
268                         return _choices[getRowCount()+row];
269                     case 3:
270                         return _sel[getRowCount()+row];
271                     }
272                     return null; // Unreachable.
273                 }
274                 public Class getColumnClass(int column) {
275                     if ((column == 0) || (column == 2)) return String.class;
276                     else return Boolean.class;
277                 }
278                 public boolean isCellEditable(int row, int col) {
279                     if ((col == 1) || (col == 3)) return true;
280                     else return false;
281                 }
282                 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
283                     if (columnIndex == 1)
284                         _sel[rowIndex] = (Boolean)aValue;
285                     if (columnIndex == 3)
286                         _sel[getRowCount()+rowIndex] = (Boolean)aValue;
287                 }
288
289             };
290
291         JTable table = new JTable(tm);
292         table.setRowSelectionAllowed(false);
293         table.setColumnSelectionAllowed(false);
294         //table.getInputMap().put(GUIGlobals.exitDialog, "close");
295         //table.getActionMap().put("close", new CancelAction());
296         JPanel
297             tablePanel = new JPanel(),
298             innerTablePanel = new JPanel();
299
300         table.setShowVerticalLines(false);
301         innerTablePanel.setBorder(BorderFactory.createEtchedBorder());
302         //innerTablePanel.setBorder(BorderFactory.createLoweredBevelBorder());
303         innerTablePanel.add(table);
304         tablePanel.add(innerTablePanel);
305
306
307         TableColumnModel cm = table.getColumnModel();
308         cm.getColumn(0).setPreferredWidth(90);
309         cm.getColumn(1).setPreferredWidth(25);
310         cm.getColumn(2).setPreferredWidth(90);
311         cm.getColumn(3).setPreferredWidth(25);
312         */