b83da0df0fc365dc01a693c39af08dfd89fce666
[debian/jabref.git] / src / java / net / sf / jabref / XmpPrefsTab.java
1 package net.sf.jabref;
2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.event.ActionEvent;
6 import java.util.Vector;
7
8 import javax.swing.AbstractAction;
9 import javax.swing.BorderFactory;
10 import javax.swing.JCheckBox;
11 import javax.swing.JPanel;
12 import javax.swing.JScrollPane;
13 import javax.swing.JTable;
14 import javax.swing.JToolBar;
15 import javax.swing.SwingConstants;
16 import javax.swing.table.AbstractTableModel;
17 import javax.swing.table.TableColumnModel;
18 import javax.swing.table.TableModel;
19
20 import com.jgoodies.forms.builder.DefaultFormBuilder;
21 import com.jgoodies.forms.layout.FormLayout;
22
23 /**
24  * Preference Tab for XMP.
25  * 
26  * Allows the user to enable and configure the XMP privacy filter.
27  * 
28  * @author $Author: mortenalver $
29  * @version $Revision: 1922 $ ($Date: 2007-01-31 17:52:09 +0100 (Wed, 31 Jan 2007) $)
30  * 
31  */
32 class XmpPrefsTab extends JPanel implements PrefsTab {
33
34         boolean tableChanged = false;
35
36         int rowCount;
37
38         JTable table;
39
40         JCheckBox privacyFilterCheckBox = new JCheckBox(Globals
41                 .lang("Do not write the following fields to XMP Metadata:"));
42
43         Vector tableRows = new Vector(10);
44
45         /**
46          * Customization of external program paths.
47          */
48         public XmpPrefsTab() {
49                 setLayout(new BorderLayout());
50
51                 TableModel tm = new AbstractTableModel() {
52                         public int getRowCount() {
53                                 return rowCount;
54                         }
55
56                         public int getColumnCount() {
57                                 return 1;
58                         }
59
60                         public Object getValueAt(int row, int column) {
61                                 if (row >= tableRows.size())
62                                         return "";
63                                 Object rowContent = tableRows.elementAt(row);
64                                 if (rowContent == null)
65                                         return "";
66                                 return rowContent;
67                         }
68
69                         public String getColumnName(int col) {
70                                 return Globals.lang("Field to filter");
71                         }
72
73                         public Class getColumnClass(int column) {
74                                 return String.class;
75                         }
76
77                         public boolean isCellEditable(int row, int col) {
78                                 return true;
79                         }
80
81                         public void setValueAt(Object value, int row, int col) {
82                                 tableChanged = true;
83
84                                 if (tableRows.size() <= row) {
85                                         tableRows.setSize(row + 1);
86                                 }
87
88                                 tableRows.setElementAt(value, row);
89                         }
90
91                 };
92
93                 table = new JTable(tm);
94                 TableColumnModel cm = table.getColumnModel();
95                 cm.getColumn(0).setPreferredWidth(140);
96
97                 FormLayout layout = new FormLayout("1dlu, 8dlu, left:pref, 4dlu, fill:pref", "");
98                 DefaultFormBuilder builder = new DefaultFormBuilder(layout);
99                 JPanel pan = new JPanel();
100
101                 JPanel tablePanel = new JPanel();
102                 tablePanel.setLayout(new BorderLayout());
103                 JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
104                         JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
105                 table.setPreferredScrollableViewportSize(new Dimension(250, 200));
106                 scrollPane.setMinimumSize(new Dimension(250, 300));
107                 tablePanel.add(scrollPane, BorderLayout.CENTER);
108
109                 JToolBar toolbar = new JToolBar(SwingConstants.VERTICAL);
110                 toolbar.setFloatable(false);
111                 toolbar.setBorder(null);
112                 toolbar.add(new AddRowAction());
113                 toolbar.add(new DeleteRowAction());
114
115                 tablePanel.add(toolbar, BorderLayout.EAST);
116
117                 // Build Prefs Tabs
118                 builder.appendSeparator(Globals.lang("XMP Export Privacy Settings"));
119                 builder.nextLine();
120
121                 builder.append(pan);
122                 builder.append(privacyFilterCheckBox);
123                 builder.nextLine();
124
125                 builder.append(pan);
126                 builder.append(tablePanel);
127                 builder.nextLine();
128
129                 pan = builder.getPanel();
130                 pan.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
131                 add(pan, BorderLayout.CENTER);
132         }
133
134         class DeleteRowAction extends AbstractAction {
135                 public DeleteRowAction() {
136                         super("Delete row", GUIGlobals.getImage("remove"));
137                         putValue(SHORT_DESCRIPTION, Globals.lang("Delete rows"));
138                 }
139
140                 public void actionPerformed(ActionEvent e) {
141                         int[] rows = table.getSelectedRows();
142                         if (rows.length == 0)
143                                 return;
144
145                         for (int i = rows.length - 1; i >= 0; i--) {
146                                 if (rows[i] < tableRows.size()) {
147                                         tableRows.remove(rows[i]);
148                                 }
149                         }
150                         rowCount -= rows.length;
151                         if (rows.length > 1)
152                                 table.clearSelection();
153                         table.revalidate();
154                         table.repaint();
155                         tableChanged = true;
156                 }
157         }
158
159         class AddRowAction extends AbstractAction {
160                 public AddRowAction() {
161                         super("Add row", GUIGlobals.getImage("add"));
162                         putValue(SHORT_DESCRIPTION, Globals.lang("Insert rows"));
163                 }
164
165                 public void actionPerformed(ActionEvent e) {
166                         int[] rows = table.getSelectedRows();
167                         if (rows.length == 0) {
168                                 // No rows selected, so we just add one at the end.
169                                 rowCount++;
170                                 table.revalidate();
171                                 table.repaint();
172                                 return;
173                         }
174                         for (int i = 0; i < rows.length; i++) {
175                                 if (rows[i] + i < tableRows.size())
176                                         tableRows.add(rows[i] + i, "");
177                         }
178                         rowCount += rows.length;
179                         if (rows.length > 1)
180                                 table.clearSelection();
181                         table.revalidate();
182                         table.repaint();
183                         tableChanged = true;
184                 }
185         }
186
187         /**
188          * Load settings from the preferences and initialize the table.
189          */
190         public void setValues() {
191                 tableRows.clear();
192                 String[] names = JabRefPreferences.getInstance().getStringArray("xmpPrivacyFilters");
193                 for (int i = 0; i < names.length; i++) {
194                         tableRows.add(names[i]);
195                 }
196                 rowCount = tableRows.size() + 5;
197
198                 privacyFilterCheckBox.setSelected(JabRefPreferences.getInstance().getBoolean(
199                         "useXmpPrivacyFilter"));
200         }
201
202         /**
203          * Store changes to table preferences. This method is called when the user
204          * clicks Ok.
205          * 
206          */
207         public void storeSettings() {
208
209                 if (table.isEditing()) {
210                         int col = table.getEditingColumn();
211                         int row = table.getEditingRow();
212                         table.getCellEditor(row, col).stopCellEditing();
213                 }
214
215                 // Now we need to make sense of the contents the user has made to the
216                 // table setup table.
217                 if (tableChanged) {
218
219                         // First we remove all rows with empty names.
220                         for (int i = tableRows.size() - 1; i >= 0; i--) {
221                                 if (tableRows.elementAt(i).equals(""))
222                                         tableRows.removeElementAt(i);
223                         }
224
225                         // Finally, we store the new preferences.
226                         JabRefPreferences.getInstance().putStringArray("xmpPrivacyFilters",
227                                 (String[]) tableRows.toArray(new String[tableRows.size()]));
228                 }
229
230                 JabRefPreferences.getInstance().putBoolean("useXmpPrivacyFilter", privacyFilterCheckBox.isSelected());
231         }
232
233         public boolean readyToClose() {
234                 return true;
235         }
236
237         public String getTabName() {
238                 return Globals.lang("XMP metadata");
239         }
240 }