1 package net.sf.jabref.external;
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
10 import javax.swing.event.DocumentEvent;
11 import javax.swing.event.DocumentListener;
13 import net.sf.jabref.GUIGlobals;
14 import net.sf.jabref.Globals;
16 import com.jgoodies.forms.builder.ButtonBarBuilder;
17 import com.jgoodies.forms.builder.DefaultFormBuilder;
18 import com.jgoodies.forms.layout.FormLayout;
21 * This class produces a dialog box for editing an external file type.
23 public class ExternalFileTypeEntryEditor {
25 JFrame fParent = null;
26 JDialog dParent = null;
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;
40 private ExternalFileType entry;
41 private boolean okPressed = false;
43 public ExternalFileTypeEntryEditor(JFrame parent, ExternalFileType entry) {
48 public ExternalFileTypeEntryEditor(JDialog parent, ExternalFileType entry) {
53 private void init(ExternalFileType entry) {
57 ButtonGroup bg = new ButtonGroup();
61 DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout
62 ("left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref", ""));
63 builder.append(Globals.lang("Icon"));
66 builder.append(Globals.lang("Name"));
69 builder.append(Globals.lang("Extension"));
70 builder.append(extension);
71 builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
73 builder.append(Globals.lang("Application"));
74 JButton browseBut = new JButton(Globals.lang("Browse"));
76 builder.append(useDefault);
78 JPanel p1 = new JPanel();
80 JPanel p2 = new JPanel();
81 application.setPreferredSize(new Dimension(300, application.getPreferredSize().height));
82 BorderLayout bl = new BorderLayout();
85 p2.add(other, BorderLayout.WEST);
86 p2.add(application, BorderLayout.CENTER);
88 builder.append(browseBut);
90 builder.append(application);
91 builder.append(browseBut);
93 ButtonBarBuilder bb = new ButtonBarBuilder();
96 bb.addGridded(cancel);
99 ok.addActionListener(new ActionListener() {
100 public void actionPerformed(ActionEvent e) {
102 storeSettings(ExternalFileTypeEntryEditor.this.entry);
107 cancel.addActionListener(new ActionListener() {
108 public void actionPerformed(ActionEvent e) {
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);
120 if (ic.isOkPressed()) {
121 selectedIcon = ic.getSelectedIconKey();
122 icon.setIcon(GUIGlobals.getImage(selectedIcon));
124 //JOptionPane.showMessageDialog(null, "Sorry, the icon can unfortunately not be changed in this version of JabRef");
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);
134 other.setSelected(true);
137 public void insertUpdate(DocumentEvent e) {
141 public void removeUpdate(DocumentEvent documentEvent) {
142 handle(documentEvent);
145 public void changedUpdate(DocumentEvent documentEvent) {
146 handle(documentEvent);
152 diag = new JDialog(dParent, Globals.lang("Edit file type"), true);
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);
159 BrowseListener browse = new BrowseListener(diag, application);
160 browseBut.addActionListener(browse);
163 diag.setLocationRelativeTo(dParent);
165 diag.setLocationRelativeTo(fParent);
166 //Util.placeDialog(diag, parent);
171 public void setEntry(ExternalFileType entry) {
176 public void setVisible(boolean visible) {
179 diag.setVisible(visible);
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);
190 other.setSelected(true);
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());
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("");
207 entry.setOpenWith(application.getText().trim());
211 public boolean okPressed() {
215 class BrowseListener implements ActionListener {
216 private JTextField comp;
218 public BrowseListener(JDialog parent, JTextField comp) {
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"));
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());