1 package net.sf.jabref.external;
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.MouseAdapter;
8 import java.awt.event.MouseEvent;
9 import java.util.ArrayList;
10 import java.util.Collections;
13 import javax.swing.table.AbstractTableModel;
14 import javax.swing.table.TableCellRenderer;
16 import net.sf.jabref.GUIGlobals;
17 import net.sf.jabref.Globals;
18 import net.sf.jabref.JabRefFrame;
19 import net.sf.jabref.MnemonicAwareAction;
21 import com.jgoodies.forms.builder.ButtonBarBuilder;
22 import com.jgoodies.forms.builder.ButtonStackBuilder;
25 * Editor for external file types.
27 public class ExternalFileTypeEditor extends JDialog {
29 private JFrame frame = null;
30 private JDialog dialog = null;
31 private ArrayList<ExternalFileType> fileTypes;
33 private ExternalFileTypeEntryEditor entryEditor = null;
34 private FileTypeTableModel tableModel;
35 private JButton ok = new JButton(Globals.lang("Ok")),
36 cancel = new JButton(Globals.lang("Cancel"));
37 private JButton add = new JButton(GUIGlobals.getImage("add")),
38 remove = new JButton(GUIGlobals.getImage("remove")),
39 edit = new JButton(GUIGlobals.getImage("edit")),
40 toDefaults = new JButton(Globals.lang("Default"));
41 private EditListener editListener = new EditListener();
43 public ExternalFileTypeEditor(JFrame frame) {
44 super(frame, Globals.lang("Manage external file types"), true);
49 public ExternalFileTypeEditor(JDialog dialog) {
50 super(dialog, Globals.lang("Manage external file types"), true);
56 * Update the editor to show the current settings in Preferences.
58 public void setValues() {
60 ExternalFileType[] types = Globals.prefs.getExternalFileTypeSelection();
61 for (int i = 0; i < types.length; i++) {
63 fileTypes.add(types[i].copy());
65 Collections.sort(fileTypes);
69 * Store the list of external entry types to Preferences.
71 public void storeSettings() {
72 Globals.prefs.setExternalFileTypes(fileTypes);
77 ok.addActionListener(new ActionListener() {
78 public void actionPerformed(ActionEvent e) {
83 AbstractAction cancelAction = new AbstractAction() {
84 public void actionPerformed(ActionEvent e) {
88 cancel.addActionListener(cancelAction);
89 // The toDefaults resets the entire list to its default values.
90 toDefaults.addActionListener(new ActionListener() {
91 public void actionPerformed(ActionEvent e) {
92 /*int reply = JOptionPane.showConfirmDialog(ExternalFileTypeEditor.this,
93 Globals.lang("All custom file types will be lost. Proceed?"),
94 Globals.lang("Reset file type definitons"), JOptionPane.YES_NO_OPTION,
95 JOptionPane.QUESTION_MESSAGE);*/
96 //if (reply == JOptionPane.YES_OPTION) {
97 java.util.List<ExternalFileType> list = Globals.prefs.getDefaultExternalFileTypes();
99 fileTypes.addAll(list);
100 Collections.sort(fileTypes);
101 //Globals.prefs.resetExternalFileTypesToDefault();
103 tableModel.fireTableDataChanged();
109 add.addActionListener(new AddListener());
110 remove.addActionListener(new RemoveListener());
111 edit.addActionListener(editListener);
112 fileTypes = new ArrayList<ExternalFileType>();
116 tableModel = new FileTypeTableModel();
117 table = new JTable(tableModel);
118 table.setDefaultRenderer(ImageIcon.class, new IconRenderer());
119 table.addMouseListener(new TableClickListener());
121 table.getColumnModel().getColumn(0).setMaxWidth(24);
122 table.getColumnModel().getColumn(0).setMinWidth(24);
123 table.getColumnModel().getColumn(1).setMinWidth(170);
124 table.getColumnModel().getColumn(2).setMinWidth(60);
125 table.getColumnModel().getColumn(3).setMinWidth(100);
126 table.getColumnModel().getColumn(0).setResizable(false);
128 JScrollPane sp = new JScrollPane(table);
130 JPanel upper = new JPanel();
131 upper.setLayout(new BorderLayout());
132 upper.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
133 upper.add(sp, BorderLayout.CENTER);
134 getContentPane().add(upper, BorderLayout.CENTER);
136 ButtonStackBuilder bs = new ButtonStackBuilder();
138 bs.addGridded(remove);
141 bs.addGridded(toDefaults);
142 upper.add(bs.getPanel(), BorderLayout.EAST);
144 ButtonBarBuilder bb = new ButtonBarBuilder();
147 bb.addGridded(cancel);
149 bb.getPanel().setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
150 getContentPane().add(bb.getPanel(), BorderLayout.SOUTH);
154 ActionMap am = upper.getActionMap();
155 InputMap im = upper.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
156 im.put(Globals.prefs.getKey("Close dialog"), "close");
157 am.put("close", cancelAction);
158 am = bb.getPanel().getActionMap();
159 im = bb.getPanel().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
160 im.put(Globals.prefs.getKey("Close dialog"), "close");
161 am.put("close", cancelAction);
164 setLocationRelativeTo(frame);
166 setLocationRelativeTo(dialog);
169 private ExternalFileTypeEntryEditor getEditor(ExternalFileType type) {
170 if (entryEditor == null)
171 entryEditor = new ExternalFileTypeEntryEditor(ExternalFileTypeEditor.this, type);
173 entryEditor.setEntry(type);
178 * Get an AbstractAction for opening the external file types editor.
179 * @param frame The JFrame used as parent window for the dialog.
180 * @return An Action for opening the editor.
182 public static AbstractAction getAction(JabRefFrame frame) {
183 return new EditExternalFileTypesAction(frame);
187 * Get an AbstractAction for opening the external file types editor.
188 * @param dialog The JDialog used as parent window for the dialog.
189 * @return An Action for opening the editor.
191 public static AbstractAction getAction(JDialog dialog) {
192 return new EditExternalFileTypesAction(dialog);
195 class AddListener implements ActionListener {
196 public void actionPerformed(ActionEvent e) {
197 // Generate a new file type:
198 ExternalFileType type = new ExternalFileType("", "", "", "new");
199 // Show the file type editor:
200 getEditor(type).setVisible(true);
201 if (entryEditor.okPressed()) {
202 // Ok was pressed. Add the new file type and update the table:
204 tableModel.fireTableDataChanged();
209 class RemoveListener implements ActionListener {
211 public void actionPerformed(ActionEvent e) {
212 int[] rows = table.getSelectedRows();
213 if (rows.length == 0)
215 for (int i=rows.length-1; i>=0; i--) {
216 fileTypes.remove(rows[i]);
218 tableModel.fireTableDataChanged();
219 if (fileTypes.size() > 0) {
220 int row = Math.min(rows[0], fileTypes.size()-1);
221 table.setRowSelectionInterval(row, row);
226 class EditListener implements ActionListener {
228 public void actionPerformed(ActionEvent e) {
229 int[] rows = table.getSelectedRows();
230 if (rows.length != 1)
232 getEditor(fileTypes.get(rows[0])).setVisible(true);
233 if (entryEditor.okPressed())
234 tableModel.fireTableDataChanged();
238 class IconRenderer implements TableCellRenderer {
239 JLabel lab = new JLabel();
241 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
243 lab.setIcon((ImageIcon)value);
248 class FileTypeTableModel extends AbstractTableModel {
250 public int getColumnCount() {
254 public int getRowCount() {
255 return fileTypes.size();
258 public String getColumnName(int column) {
263 return Globals.lang("Name");
265 return Globals.lang("Extension");
267 return Globals.lang("Application");
273 public Class<?> getColumnClass(int columnIndex) {
274 if (columnIndex == 0)
275 return ImageIcon.class;
276 else return String.class;
279 public Object getValueAt(int rowIndex, int columnIndex) {
280 ExternalFileType type = fileTypes.get(rowIndex);
281 switch (columnIndex) {
283 return type.getIcon();
285 return type.getName();
287 return type.getExtension();
289 return type.getOpenWith();
296 class TableClickListener extends MouseAdapter {
298 private void handleClick(MouseEvent e) {
299 if (e.getClickCount() == 2) {
300 editListener.actionPerformed(null);
304 public void mouseClicked(MouseEvent e) {
308 public void mousePressed(MouseEvent e) {
312 public void mouseReleased(MouseEvent e) {
317 public static class EditExternalFileTypesAction extends MnemonicAwareAction {
318 private JabRefFrame frame = null;
319 private JDialog dialog = null;
320 ExternalFileTypeEditor editor = null;
322 public EditExternalFileTypesAction(JabRefFrame frame) {
324 putValue(NAME, "Manage external file types");
328 public EditExternalFileTypesAction(JDialog dialog) {
330 putValue(NAME, "Manage external file types");
331 this.dialog = dialog;
334 public void actionPerformed(ActionEvent e) {
335 if (editor == null) {
337 editor = new ExternalFileTypeEditor(frame);
339 editor = new ExternalFileTypeEditor(dialog);
342 editor.setVisible(true);
344 if (frame.basePanel() != null)
345 frame.basePanel().mainTable.repaint();