1 package net.sf.jabref.gui;
3 import com.jgoodies.forms.builder.DefaultFormBuilder;
4 import com.jgoodies.forms.builder.ButtonBarBuilder;
5 import com.jgoodies.forms.layout.FormLayout;
8 import javax.swing.event.DocumentListener;
9 import javax.swing.event.DocumentEvent;
11 import net.sf.jabref.*;
12 import net.sf.jabref.external.ExternalFileType;
13 import net.sf.jabref.external.ConfirmCloseFileListEntryEditor;
16 import java.awt.event.*;
18 import java.io.FileFilter;
19 import java.util.ArrayList;
22 * This class produces a dialog box for editing a single file link from a Bibtex entry.
24 * The information to be edited includes the file description, the link itself and the
25 * file type. The dialog also includes convenience buttons for quick linking.
27 * For use when downloading files, this class also offers a progress bar and a "Downloading..."
28 * label that can be hidden when the download is complete.
30 public class FileListEntryEditor {
33 JTextField link = new JTextField(), description = new JTextField();
34 JButton ok = new JButton(Globals.lang("Ok")),
35 cancel = new JButton(Globals.lang("Cancel"));
37 JProgressBar prog = new JProgressBar(JProgressBar.HORIZONTAL);
38 JLabel downloadLabel = new JLabel(Globals.lang("Downloading..."));
39 ConfirmCloseFileListEntryEditor externalConfirm = null;
41 private FileListEntry entry;
42 private MetaData metaData;
43 private boolean okPressed = false;
45 public FileListEntryEditor(JFrame parent, FileListEntry entry, boolean showProgressBar,
48 this.metaData = metaData;
50 types = new JComboBox(Globals.prefs.getExternalFileTypeSelection());
51 types.addItemListener(new ItemListener() {
52 public void itemStateChanged(ItemEvent itemEvent) {
53 ok.setEnabled(types.getSelectedItem() != null);
56 DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout
57 ("left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref", ""));
58 builder.append(Globals.lang("Link"));
60 BrowseListener browse = new BrowseListener(parent, link);
61 JButton browseBut = new JButton(Globals.lang("Browse"));
62 browseBut.addActionListener(browse);
63 builder.append(browseBut);
65 builder.append(Globals.lang("Description"));
66 builder.append(description);
67 builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
69 builder.append(Globals.lang("File type"));
70 builder.append(types);
71 if (showProgressBar) {
73 builder.append(downloadLabel);
77 ButtonBarBuilder bb = new ButtonBarBuilder();
80 bb.addGridded(cancel);
83 ok.addActionListener(new ActionListener() {
84 public void actionPerformed(ActionEvent e) {
85 // If necessary, ask the external confirm object whether we are ready to close.
86 if (externalConfirm != null) {
87 // Construct an updated FileListEntry:
88 FileListEntry testEntry = new FileListEntry("", "", null);
89 storeSettings(testEntry);
90 if (!externalConfirm.confirmClose(testEntry))
94 storeSettings(FileListEntryEditor.this.entry);
98 cancel.addActionListener(new ActionListener() {
99 public void actionPerformed(ActionEvent e) {
103 link.getDocument().addDocumentListener(new DocumentListener() {
104 public void insertUpdate(DocumentEvent documentEvent) {
107 public void removeUpdate(DocumentEvent documentEvent) {
109 public void changedUpdate(DocumentEvent documentEvent) {
112 private void checkExtension() {
113 if ((types.getSelectedIndex() == -1) &&
114 (link.getText().trim().length() > 0)) {
115 // Try to guess the file type:
116 String theLink = link.getText().trim();
117 int index = theLink.lastIndexOf('.');
118 if ((index >= 0) && (index < theLink.length()-1)) {
120 ExternalFileType type = Globals.prefs.getExternalFileTypeByExt
121 (theLink.substring(index+1));
123 types.setSelectedItem(type);
130 diag = new JDialog(parent, Globals.lang("Edit file link"), true);
131 diag.getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
132 diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH);
134 Util.placeDialog(diag, parent);
139 public void setExternalConfirm(ConfirmCloseFileListEntryEditor eC) {
140 this.externalConfirm = eC;
143 public void setOkEnabled(boolean enabled) {
144 ok.setEnabled(enabled);
147 public JProgressBar getProgressBar() {
151 public JLabel getProgressBarLabel() {
152 return downloadLabel;
155 public void setEntry(FileListEntry entry) {
160 public void setVisible(boolean visible) {
163 diag.setVisible(visible);
166 public void setValues(FileListEntry entry) {
167 description.setText(entry.getDescription());
168 link.setText(entry.getLink());
169 types.setSelectedItem(entry.getType());
172 public void storeSettings(FileListEntry entry) {
173 entry.setDescription(description.getText().trim());
174 entry.setLink(link.getText().trim());
175 entry.setType((ExternalFileType)types.getSelectedItem());
178 public boolean okPressed() {
182 class BrowseListener implements ActionListener {
183 private JFrame parent;
184 private JTextField comp;
186 public BrowseListener(JFrame parent, JTextField comp) {
187 this.parent = parent;
191 public void actionPerformed(ActionEvent e) {
192 File initial = new File(comp.getText().trim());
193 if (comp.getText().trim().length() == 0) {
194 // Nothing in the field. Go to the last file dir used:
195 initial = new File(Globals.prefs.get("fileWorkingDirectory"));
197 String chosen = Globals.getNewFile(parent, initial, Globals.NONE,
198 JFileChooser.OPEN_DIALOG, false);
199 if (chosen != null) {
200 File newFile = new File(chosen);
201 // Store the directory for next time:
202 Globals.prefs.put("fileWorkingDirectory", newFile.getParent());
204 // If the file is below the file directory, make the path relative:
205 ArrayList<File> dirs = new ArrayList<File>();
206 String fileDir = metaData.getFileDirectory(GUIGlobals.FILE_FIELD);
208 dirs.add(new File(fileDir));
209 if (dirs.size() > 0) {
210 newFile = FileListEditor.relativizePath(newFile, dirs);
213 comp.setText(newFile.getPath());