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