1 package net.sf.jabref.external;
3 import net.sf.jabref.JabRefFrame;
4 import net.sf.jabref.Globals;
5 import net.sf.jabref.GUIGlobals;
6 import net.sf.jabref.MnemonicAwareAction;
9 import javax.swing.table.AbstractTableModel;
10 import javax.swing.table.TableCellRenderer;
11 import java.util.ArrayList;
12 import java.util.Collections;
14 import java.awt.event.ActionListener;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.MouseAdapter;
17 import java.awt.event.MouseEvent;
19 import com.jgoodies.forms.builder.ButtonBarBuilder;
20 import com.jgoodies.forms.builder.ButtonStackBuilder;
23 * Editor for external file types.
25 public class ExternalFileTypeEditor extends JDialog {
27 private JFrame frame = null;
28 private JDialog dialog = null;
29 private ArrayList<ExternalFileType> fileTypes;
31 private ExternalFileTypeEntryEditor entryEditor = null;
32 private FileTypeTableModel tableModel;
33 private JButton ok = new JButton(Globals.lang("Ok")),
34 cancel = new JButton(Globals.lang("Cancel"));
35 private JButton add = new JButton(GUIGlobals.getImage("add")),
36 remove = new JButton(GUIGlobals.getImage("remove")),
37 edit = new JButton(GUIGlobals.getImage("edit")),
38 toDefaults = new JButton(Globals.lang("Default"));
39 private EditListener editListener = new EditListener();
41 public ExternalFileTypeEditor(JFrame frame) {
42 super(frame, Globals.lang("Manage external file types"), true);
47 public ExternalFileTypeEditor(JDialog dialog) {
48 super(dialog, Globals.lang("Manage external file types"), true);
54 * Update the editor to show the current settings in Preferences.
56 public void setValues() {
58 ExternalFileType[] types = Globals.prefs.getExternalFileTypeSelection();
59 for (int i = 0; i < types.length; i++) {
61 fileTypes.add(types[i].copy());
63 Collections.sort(fileTypes);
67 * Store the list of external entry types to Preferences.
69 public void storeSettings() {
70 Globals.prefs.setExternalFileTypes(fileTypes);
75 ok.addActionListener(new ActionListener() {
76 public void actionPerformed(ActionEvent e) {
81 AbstractAction cancelAction = new AbstractAction() {
82 public void actionPerformed(ActionEvent e) {
86 cancel.addActionListener(cancelAction);
87 // The toDefaults resets the entire list to its default values.
88 toDefaults.addActionListener(new ActionListener() {
89 public void actionPerformed(ActionEvent e) {
90 /*int reply = JOptionPane.showConfirmDialog(ExternalFileTypeEditor.this,
91 Globals.lang("All custom file types will be lost. Proceed?"),
92 Globals.lang("Reset file type definitons"), JOptionPane.YES_NO_OPTION,
93 JOptionPane.QUESTION_MESSAGE);*/
94 //if (reply == JOptionPane.YES_OPTION) {
95 java.util.List<ExternalFileType> list = Globals.prefs.getDefaultExternalFileTypes();
97 fileTypes.addAll(list);
98 Collections.sort(fileTypes);
99 //Globals.prefs.resetExternalFileTypesToDefault();
101 tableModel.fireTableDataChanged();
107 add.addActionListener(new AddListener());
108 remove.addActionListener(new RemoveListener());
109 edit.addActionListener(editListener);
110 fileTypes = new ArrayList<ExternalFileType>();
114 tableModel = new FileTypeTableModel();
115 table = new JTable(tableModel);
116 table.setDefaultRenderer(ImageIcon.class, new IconRenderer());
117 table.addMouseListener(new TableClickListener());
119 table.getColumnModel().getColumn(0).setMaxWidth(24);
120 table.getColumnModel().getColumn(0).setMinWidth(24);
121 table.getColumnModel().getColumn(1).setMinWidth(170);
122 table.getColumnModel().getColumn(2).setMinWidth(60);
123 table.getColumnModel().getColumn(3).setMinWidth(100);
124 table.getColumnModel().getColumn(0).setResizable(false);
126 JScrollPane sp = new JScrollPane(table);
128 JPanel upper = new JPanel();
129 upper.setLayout(new BorderLayout());
130 upper.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
131 upper.add(sp, BorderLayout.CENTER);
132 getContentPane().add(upper, BorderLayout.CENTER);
134 ButtonStackBuilder bs = new ButtonStackBuilder();
136 bs.addGridded(remove);
139 bs.addGridded(toDefaults);
140 upper.add(bs.getPanel(), BorderLayout.EAST);
142 ButtonBarBuilder bb = new ButtonBarBuilder();
145 bb.addGridded(cancel);
147 bb.getPanel().setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
148 getContentPane().add(bb.getPanel(), BorderLayout.SOUTH);
152 ActionMap am = upper.getActionMap();
153 InputMap im = upper.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
154 im.put(Globals.prefs.getKey("Close dialog"), "close");
155 am.put("close", cancelAction);
156 am = bb.getPanel().getActionMap();
157 im = bb.getPanel().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
158 im.put(Globals.prefs.getKey("Close dialog"), "close");
159 am.put("close", cancelAction);
162 setLocationRelativeTo(frame);
164 setLocationRelativeTo(dialog);
167 private ExternalFileTypeEntryEditor getEditor(ExternalFileType type) {
168 if (entryEditor == null)
169 entryEditor = new ExternalFileTypeEntryEditor(ExternalFileTypeEditor.this, type);
171 entryEditor.setEntry(type);
176 * Get an AbstractAction for opening the external file types editor.
177 * @param frame The JFrame used as parent window for the dialog.
178 * @return An Action for opening the editor.
180 public static AbstractAction getAction(JabRefFrame frame) {
181 return new EditExternalFileTypesAction(frame);
185 * Get an AbstractAction for opening the external file types editor.
186 * @param dialog The JDialog used as parent window for the dialog.
187 * @return An Action for opening the editor.
189 public static AbstractAction getAction(JDialog dialog) {
190 return new EditExternalFileTypesAction(dialog);
193 class AddListener implements ActionListener {
194 public void actionPerformed(ActionEvent e) {
195 // Generate a new file type:
196 ExternalFileType type = new ExternalFileType("", "", "", "new");
197 // Show the file type editor:
198 getEditor(type).setVisible(true);
199 if (entryEditor.okPressed()) {
200 // Ok was pressed. Add the new file type and update the table:
202 tableModel.fireTableDataChanged();
207 class RemoveListener implements ActionListener {
209 public void actionPerformed(ActionEvent e) {
210 int[] rows = table.getSelectedRows();
211 if (rows.length == 0)
213 for (int i=rows.length-1; i>=0; i--) {
214 fileTypes.remove(rows[i]);
216 tableModel.fireTableDataChanged();
217 if (fileTypes.size() > 0) {
218 int row = Math.min(rows[0], fileTypes.size()-1);
219 table.setRowSelectionInterval(row, row);
224 class EditListener implements ActionListener {
226 public void actionPerformed(ActionEvent e) {
227 int[] rows = table.getSelectedRows();
228 if (rows.length != 1)
230 getEditor(fileTypes.get(rows[0])).setVisible(true);
231 if (entryEditor.okPressed())
232 tableModel.fireTableDataChanged();
236 class IconRenderer implements TableCellRenderer {
237 JLabel lab = new JLabel();
239 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
241 lab.setIcon((ImageIcon)value);
246 class FileTypeTableModel extends AbstractTableModel {
248 public int getColumnCount() {
252 public int getRowCount() {
253 return fileTypes.size();
256 public String getColumnName(int column) {
261 return Globals.lang("Name");
263 return Globals.lang("Extension");
265 return Globals.lang("Application");
271 public Class<?> getColumnClass(int columnIndex) {
272 if (columnIndex == 0)
273 return ImageIcon.class;
274 else return String.class;
277 public Object getValueAt(int rowIndex, int columnIndex) {
278 ExternalFileType type = fileTypes.get(rowIndex);
279 switch (columnIndex) {
281 return type.getIcon();
283 return type.getName();
285 return type.getExtension();
287 return type.getOpenWith();
294 class TableClickListener extends MouseAdapter {
296 private void handleClick(MouseEvent e) {
297 if (e.getClickCount() == 2) {
298 editListener.actionPerformed(null);
302 public void mouseClicked(MouseEvent e) {
306 public void mousePressed(MouseEvent e) {
310 public void mouseReleased(MouseEvent e) {
315 public static class EditExternalFileTypesAction extends MnemonicAwareAction {
316 private JFrame frame = null;
317 private JDialog dialog = null;
318 ExternalFileTypeEditor editor = null;
320 public EditExternalFileTypesAction(JFrame frame) {
322 putValue(NAME, "Manage external file types");
326 public EditExternalFileTypesAction(JDialog dialog) {
328 putValue(NAME, "Manage external file types");
329 this.dialog = dialog;
332 public void actionPerformed(ActionEvent e) {
333 if (editor == null) {
335 editor = new ExternalFileTypeEditor(frame);
337 editor = new ExternalFileTypeEditor(dialog);
340 editor.setVisible(true);