ed30023cf7f821dfa835ff7fd152d17cc44a6f11
[debian/jabref.git] / src / java / net / sf / jabref / external / ExternalFileTypeEntryEditor.java
1 package net.sf.jabref.external;
2
3 import net.sf.jabref.Globals;
4 import net.sf.jabref.Util;
5 import net.sf.jabref.GUIGlobals;
6
7 import javax.swing.*;
8 import javax.swing.event.DocumentListener;
9 import javax.swing.event.DocumentEvent;
10
11 import com.jgoodies.forms.builder.DefaultFormBuilder;
12 import com.jgoodies.forms.builder.ButtonBarBuilder;
13 import com.jgoodies.forms.layout.FormLayout;
14
15 import java.awt.event.ActionListener;
16 import java.awt.event.ActionEvent;
17 import java.awt.event.FocusListener;
18 import java.awt.event.FocusEvent;
19 import java.awt.*;
20 import java.io.File;
21
22 /**
23  * This class produces a dialog box for editing an external file type.
24  */
25 public class ExternalFileTypeEntryEditor {
26
27     JFrame fParent = null;
28     JDialog dParent = null;
29     JDialog diag;
30     JTextField extension = new JTextField(),
31         name = new JTextField(),
32         application = new JTextField();
33     String selectedIcon = null;
34     JButton icon = new JButton(GUIGlobals.getImage("picture"));
35     JButton ok = new JButton(Globals.lang("Ok")),
36             cancel = new JButton(Globals.lang("Cancel"));
37     JRadioButton useDefault = new JRadioButton(Globals.lang("Default")),
38         other = new JRadioButton("");
39     final String emptyMessage = "<"+Globals.lang("Use default viewer")+">";
40     boolean applicationFieldEmpty = false;
41
42     private ExternalFileType entry;
43     private boolean okPressed = false;
44
45     public ExternalFileTypeEntryEditor(JFrame parent, ExternalFileType entry) {
46         fParent = parent;
47         init(entry);
48     }
49
50     public ExternalFileTypeEntryEditor(JDialog parent, ExternalFileType entry) {
51         dParent = parent;
52         init(entry);
53     }
54
55     private void init(ExternalFileType entry) {
56         this.entry = entry;
57         icon.setText(null);
58
59         ButtonGroup bg = new ButtonGroup();
60         bg.add(useDefault);
61         bg.add(other);
62
63         DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout
64                 ("left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref", ""));
65         builder.append(Globals.lang("Icon"));
66         builder.append(icon);
67         builder.nextLine();
68         builder.append(Globals.lang("Name"));
69         builder.append(name);
70         builder.nextLine();
71         builder.append(Globals.lang("Extension"));
72         builder.append(extension);
73         builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
74         builder.nextLine();
75         builder.append(Globals.lang("Application"));
76         JButton browseBut = new JButton(Globals.lang("Browse"));
77         if (Globals.ON_WIN) {
78             builder.append(useDefault);
79             builder.nextLine();
80             JPanel p1 = new JPanel();
81             builder.append(p1);
82             JPanel p2 = new JPanel();
83             DefaultFormBuilder b2 = new DefaultFormBuilder(
84                     new FormLayout("left:pref, 4dlu, fill:pref", ""));
85             application.setPreferredSize(new Dimension(300, application.getPreferredSize().height));
86             BorderLayout bl = new BorderLayout();
87             bl.setHgap(4);
88             //b2.append(other);
89             //b2.append(application);
90             p2.setLayout(bl);
91             p2.add(other, BorderLayout.WEST);
92             p2.add(application, BorderLayout.CENTER);
93             builder.append(p2);
94             //builder.append(b2.getPanel());
95             builder.append(browseBut);
96         } else {
97             builder.append(application);
98             builder.append(browseBut);
99         }
100         ButtonBarBuilder bb = new ButtonBarBuilder();
101         bb.addGlue();
102         bb.addGridded(ok);
103         bb.addGridded(cancel);
104         bb.addGlue();
105
106         ok.addActionListener(new ActionListener() {
107             public void actionPerformed(ActionEvent e) {
108                 diag.dispose();
109                 storeSettings(ExternalFileTypeEntryEditor.this.entry);
110                 okPressed = true;
111             }
112         });
113         cancel.addActionListener(new ActionListener() {
114             public void actionPerformed(ActionEvent e) {
115                 diag.dispose();
116             }
117         });
118
119         icon.addActionListener(new ActionListener() {
120             public void actionPerformed(ActionEvent actionEvent) {
121                 String initSel = ExternalFileTypeEntryEditor.this.entry.getIconName();
122                 if (selectedIcon != null)
123                     initSel = selectedIcon;
124                 IconSelection ic = new IconSelection(diag, initSel);
125                 ic.setVisible(true);
126                 if (ic.isOkPressed()) {
127                     selectedIcon = ic.getSelectedIconKey();
128                     icon.setIcon(GUIGlobals.getImage(selectedIcon));
129                 }
130                 //JOptionPane.showMessageDialog(null, "Sorry, the icon can unfortunately not be changed in this version of JabRef");
131             }
132         });
133
134         if (Globals.ON_WIN) {
135             application.getDocument().addDocumentListener(new DocumentListener() {
136                 private void handle(DocumentEvent e) {
137                     if (application.getText().length() == 0) {
138                         useDefault.setSelected(true);
139                     } else {
140                         other.setSelected(true);
141                     }
142                 }
143                 public void insertUpdate(DocumentEvent e) {
144                     handle(e);
145                 }
146
147                 public void removeUpdate(DocumentEvent documentEvent) {
148                     handle(documentEvent);
149                 }
150
151                 public void changedUpdate(DocumentEvent documentEvent) {
152                     handle(documentEvent);
153                 }
154             });
155         }
156
157         if (dParent != null)
158             diag = new JDialog(dParent, Globals.lang("Edit file type"), true);
159         else
160             diag = new JDialog(fParent, Globals.lang("Edit file type"), true);
161         diag.getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
162         diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH);
163         diag.pack();
164
165         BrowseListener browse = new BrowseListener(diag, application);
166         browseBut.addActionListener(browse);
167
168         if (dParent != null)
169             diag.setLocationRelativeTo(dParent);
170         else
171             diag.setLocationRelativeTo(fParent);
172         //Util.placeDialog(diag, parent);
173
174         setValues(entry);
175     }
176
177     public void setEntry(ExternalFileType entry) {
178         this.entry = entry;
179         setValues(entry);
180     }
181
182     public void setVisible(boolean visible) {
183         if (visible)
184             okPressed = false;
185         diag.setVisible(visible);
186     }
187
188     public void setValues(ExternalFileType entry) {
189         name.setText(entry.getName());
190         extension.setText(entry.getExtension());
191         application.setText(entry.getOpenWith());
192         icon.setIcon(entry.getIcon());
193         if (true && (application.getText().length() == 0))
194             useDefault.setSelected(true);
195         else
196             other.setSelected(true);
197         selectedIcon = null;
198     }
199
200     public void storeSettings(ExternalFileType entry) {
201         entry.setName(name.getText().trim());
202         entry.setExtension(extension.getText().trim());
203         if (selectedIcon != null)
204             entry.setIconName(selectedIcon);
205         if (!Globals.ON_WIN) {
206             entry.setOpenWith(application.getText().trim());
207         } else {
208             // On Windows, store application as empty if the "Default" option is selected,
209             // or if the application name is empty:
210             if (useDefault.isSelected() || (application.getText().trim().length() == 0))
211                 entry.setOpenWith("");
212             else
213                 entry.setOpenWith(application.getText().trim());
214         }
215     }
216
217     public boolean okPressed() {
218         return okPressed;
219     }
220
221     class BrowseListener implements ActionListener {
222         private JDialog parent;
223         private JTextField comp;
224
225         public BrowseListener(JDialog parent, JTextField comp) {
226             this.parent = parent;
227             this.comp = comp;
228         }
229
230         public void actionPerformed(ActionEvent e) {
231             File initial = new File(comp.getText().trim());
232             if (comp.getText().trim().length() == 0) {
233                 // Nothing in the field. Go to the last file dir used:
234                 initial = new File(Globals.prefs.get("fileWorkingDirectory"));
235             }
236             String chosen = Globals.getNewFile(/*parent*/null, initial, Globals.NONE,
237                 JFileChooser.OPEN_DIALOG, false);
238             if (chosen != null) {
239                 File newFile = new File(chosen);
240                 // Store the directory for next time:
241                 Globals.prefs.put("fileWorkingDirectory", newFile.getParent());
242                 comp.setText(newFile.getPath());
243                 comp.requestFocus();
244             }
245         }
246     }
247 }