1 package net.sf.jabref.external;
3 import net.sf.jabref.Globals;
4 import net.sf.jabref.Util;
5 import net.sf.jabref.GUIGlobals;
8 import javax.swing.event.DocumentListener;
9 import javax.swing.event.DocumentEvent;
11 import com.jgoodies.forms.builder.DefaultFormBuilder;
12 import com.jgoodies.forms.builder.ButtonBarBuilder;
13 import com.jgoodies.forms.layout.FormLayout;
15 import java.awt.event.ActionListener;
16 import java.awt.event.ActionEvent;
17 import java.awt.event.FocusListener;
18 import java.awt.event.FocusEvent;
23 * This class produces a dialog box for editing an external file type.
25 public class ExternalFileTypeEntryEditor {
27 JFrame fParent = null;
28 JDialog dParent = null;
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;
42 private ExternalFileType entry;
43 private boolean okPressed = false;
45 public ExternalFileTypeEntryEditor(JFrame parent, ExternalFileType entry) {
50 public ExternalFileTypeEntryEditor(JDialog parent, ExternalFileType entry) {
55 private void init(ExternalFileType entry) {
59 ButtonGroup bg = new ButtonGroup();
63 DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout
64 ("left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref", ""));
65 builder.append(Globals.lang("Icon"));
68 builder.append(Globals.lang("Name"));
71 builder.append(Globals.lang("Extension"));
72 builder.append(extension);
73 builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
75 builder.append(Globals.lang("Application"));
76 JButton browseBut = new JButton(Globals.lang("Browse"));
78 builder.append(useDefault);
80 JPanel p1 = new JPanel();
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();
89 //b2.append(application);
91 p2.add(other, BorderLayout.WEST);
92 p2.add(application, BorderLayout.CENTER);
94 //builder.append(b2.getPanel());
95 builder.append(browseBut);
97 builder.append(application);
98 builder.append(browseBut);
100 ButtonBarBuilder bb = new ButtonBarBuilder();
103 bb.addGridded(cancel);
106 ok.addActionListener(new ActionListener() {
107 public void actionPerformed(ActionEvent e) {
109 storeSettings(ExternalFileTypeEntryEditor.this.entry);
113 cancel.addActionListener(new ActionListener() {
114 public void actionPerformed(ActionEvent e) {
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);
126 if (ic.isOkPressed()) {
127 selectedIcon = ic.getSelectedIconKey();
128 icon.setIcon(GUIGlobals.getImage(selectedIcon));
130 //JOptionPane.showMessageDialog(null, "Sorry, the icon can unfortunately not be changed in this version of JabRef");
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);
140 other.setSelected(true);
143 public void insertUpdate(DocumentEvent e) {
147 public void removeUpdate(DocumentEvent documentEvent) {
148 handle(documentEvent);
151 public void changedUpdate(DocumentEvent documentEvent) {
152 handle(documentEvent);
158 diag = new JDialog(dParent, Globals.lang("Edit file type"), true);
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);
165 BrowseListener browse = new BrowseListener(diag, application);
166 browseBut.addActionListener(browse);
169 diag.setLocationRelativeTo(dParent);
171 diag.setLocationRelativeTo(fParent);
172 //Util.placeDialog(diag, parent);
177 public void setEntry(ExternalFileType entry) {
182 public void setVisible(boolean visible) {
185 diag.setVisible(visible);
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);
196 other.setSelected(true);
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());
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("");
213 entry.setOpenWith(application.getText().trim());
217 public boolean okPressed() {
221 class BrowseListener implements ActionListener {
222 private JDialog parent;
223 private JTextField comp;
225 public BrowseListener(JDialog parent, JTextField comp) {
226 this.parent = parent;
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"));
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());