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