~ubuntu-branches/ubuntu/natty/jabref/natty

« back to all changes in this revision

Viewing changes to src/java/net/sf/jabref/gui/FileListEntryEditor.java

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2007-11-16 18:38:18 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071116183818-20x934jdsju14618
Tags: 2.3-2
* Add /usr/lib/jvm/java-7-icedtea to wrapper script. Doesn't work on
  Debian yet but helps Ubuntu users (TODO: add dependency on
  icedtea-java7-jre (not yet in Debian)).
* Reformat wrapper script.
* debian/rules: re-arrange targets and their dependencies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package net.sf.jabref.gui;
 
2
 
 
3
import com.jgoodies.forms.builder.DefaultFormBuilder;
 
4
import com.jgoodies.forms.builder.ButtonBarBuilder;
 
5
import com.jgoodies.forms.layout.FormLayout;
 
6
 
 
7
import javax.swing.*;
 
8
import javax.swing.event.DocumentListener;
 
9
import javax.swing.event.DocumentEvent;
 
10
 
 
11
import net.sf.jabref.*;
 
12
import net.sf.jabref.external.ExternalFileType;
 
13
import net.sf.jabref.external.ConfirmCloseFileListEntryEditor;
 
14
import net.sf.jabref.external.ExternalFileMenuItem;
 
15
import net.sf.jabref.external.UnknownExternalFileType;
 
16
 
 
17
import java.awt.*;
 
18
import java.awt.event.*;
 
19
import java.io.File;
 
20
import java.io.FileFilter;
 
21
import java.io.IOException;
 
22
import java.util.ArrayList;
 
23
 
 
24
/**
 
25
 * This class produces a dialog box for editing a single file link from a Bibtex entry.
 
26
 *
 
27
 * The information to be edited includes the file description, the link itself and the
 
28
 * file type. The dialog also includes convenience buttons for quick linking.
 
29
 *
 
30
 * For use when downloading files, this class also offers a progress bar and a "Downloading..."
 
31
 * label that can be hidden when the download is complete.
 
32
 */
 
33
public class FileListEntryEditor {
 
34
 
 
35
    JDialog diag;
 
36
    JTextField link = new JTextField(), description = new JTextField();
 
37
    JButton ok = new JButton(Globals.lang("Ok")),
 
38
            cancel = new JButton(Globals.lang("Cancel")),
 
39
            open = new JButton(Globals.lang("Open"));
 
40
    JComboBox types;
 
41
    JProgressBar prog = new JProgressBar(JProgressBar.HORIZONTAL);
 
42
    JLabel downloadLabel = new JLabel(Globals.lang("Downloading..."));
 
43
    ConfirmCloseFileListEntryEditor externalConfirm = null;
 
44
 
 
45
    private AbstractAction okAction;
 
46
    private JabRefFrame frame;
 
47
    private FileListEntry entry;
 
48
    private MetaData metaData;
 
49
    private boolean okPressed = false, okDisabledExternally = false;
 
50
 
 
51
    public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showProgressBar,
 
52
                               boolean showOpenButton, MetaData metaData) {
 
53
        this.frame = frame;
 
54
        this.entry = entry;
 
55
        this.metaData = metaData;
 
56
 
 
57
        okAction = new AbstractAction() {
 
58
                public void actionPerformed(ActionEvent e) {
 
59
                    // If OK button is disabled, ignore this event:
 
60
                    if (!ok.isEnabled())
 
61
                        return;
 
62
                    // If necessary, ask the external confirm object whether we are ready to close.
 
63
                    if (externalConfirm != null) {
 
64
                        // Construct an updated FileListEntry:
 
65
                        FileListEntry testEntry = new FileListEntry("", "", null);
 
66
                        storeSettings(testEntry);
 
67
                        if (!externalConfirm.confirmClose(testEntry))
 
68
                            return;
 
69
                    }
 
70
                    diag.dispose();
 
71
                    storeSettings(FileListEntryEditor.this.entry);
 
72
                    okPressed = true;
 
73
                }
 
74
            };
 
75
        types = new JComboBox();
 
76
        types.addItemListener(new ItemListener() {
 
77
            public void itemStateChanged(ItemEvent itemEvent) {
 
78
                if (!okDisabledExternally)
 
79
                    ok.setEnabled(types.getSelectedItem() != null);
 
80
            }
 
81
        });
 
82
        
 
83
        DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout
 
84
                ("left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref, 4dlu, fill:pref", ""));
 
85
        builder.append(Globals.lang("Link"));
 
86
        builder.append(link);
 
87
        BrowseListener browse = new BrowseListener(frame, link);
 
88
        JButton browseBut = new JButton(Globals.lang("Browse"));
 
89
        browseBut.addActionListener(browse);
 
90
        builder.append(browseBut);
 
91
        if (showOpenButton)
 
92
            builder.append(open);
 
93
        builder.nextLine();
 
94
        builder.append(Globals.lang("Description"));
 
95
        builder.append(description, 3);
 
96
        builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
 
97
        builder.nextLine();
 
98
        builder.append(Globals.lang("File type"));
 
99
        builder.append(types, 3);
 
100
        if (showProgressBar) {
 
101
            builder.nextLine();
 
102
            builder.append(downloadLabel);
 
103
            builder.append(prog, 3);
 
104
        }
 
105
        
 
106
        ButtonBarBuilder bb = new ButtonBarBuilder();
 
107
        bb.addGlue();
 
108
        //bb.addGridded(open);
 
109
        //bb.addRelatedGap();
 
110
        bb.addGridded(ok);
 
111
        bb.addGridded(cancel);
 
112
        bb.addGlue();
 
113
 
 
114
 
 
115
        ok.addActionListener(okAction);
 
116
        // Add OK action to the two text fields to simplify entering:
 
117
        link.addActionListener(okAction);
 
118
        description.addActionListener(okAction);
 
119
 
 
120
        open.addActionListener(new ActionListener() {
 
121
            public void actionPerformed(ActionEvent actionEvent) {
 
122
                openFile();
 
123
            }
 
124
        });
 
125
 
 
126
        AbstractAction cancelAction = new AbstractAction() {
 
127
                public void actionPerformed(ActionEvent e) {
 
128
                    diag.dispose();
 
129
                }
 
130
            };
 
131
        cancel.addActionListener(cancelAction);
 
132
 
 
133
        // Key bindings:
 
134
        ActionMap am = builder.getPanel().getActionMap();
 
135
        InputMap im = builder.getPanel().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
 
136
        im.put(Globals.prefs.getKey("Close dialog"), "close");
 
137
        am.put("close", cancelAction);
 
138
 
 
139
        link.getDocument().addDocumentListener(new DocumentListener() {
 
140
            public void insertUpdate(DocumentEvent documentEvent) {
 
141
                checkExtension();
 
142
            }
 
143
            public void removeUpdate(DocumentEvent documentEvent) {
 
144
            }
 
145
            public void changedUpdate(DocumentEvent documentEvent) {
 
146
                checkExtension();
 
147
            }
 
148
 
 
149
        });
 
150
 
 
151
 
 
152
        diag = new JDialog(frame, Globals.lang("Edit file link"), true);
 
153
        diag.getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
 
154
        diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH);
 
155
        diag.pack();
 
156
        Util.placeDialog(diag, frame);
 
157
 
 
158
        setValues(entry);
 
159
    }
 
160
 
 
161
    private void checkExtension() {
 
162
        if ((types.getSelectedIndex() == -1) &&
 
163
                (link.getText().trim().length() > 0)) {
 
164
            // Try to guess the file type:
 
165
            String theLink = link.getText().trim();
 
166
            int index = theLink.lastIndexOf('.');
 
167
            if ((index >= 0) && (index < theLink.length()-1)) {
 
168
 
 
169
                ExternalFileType type = Globals.prefs.getExternalFileTypeByExt
 
170
                        (theLink.substring(index+1));
 
171
                if (type != null)
 
172
                    types.setSelectedItem(type);
 
173
            }
 
174
        }
 
175
    }
 
176
 
 
177
    public void openFile() {
 
178
        ExternalFileType type = (ExternalFileType)types.getSelectedItem();
 
179
        if (type != null)
 
180
            try {
 
181
                Util.openExternalFileAnyFormat(metaData, link.getText(), type);
 
182
            } catch (IOException e) {
 
183
                e.printStackTrace();
 
184
            }
 
185
    }
 
186
 
 
187
    public void setExternalConfirm(ConfirmCloseFileListEntryEditor eC) {
 
188
        this.externalConfirm = eC;
 
189
    }
 
190
 
 
191
    public void setOkEnabled(boolean enabled) {
 
192
        okDisabledExternally = !enabled;
 
193
        ok.setEnabled(enabled);
 
194
    }
 
195
 
 
196
    public JProgressBar getProgressBar() {
 
197
        return prog;
 
198
    }
 
199
 
 
200
    public JLabel getProgressBarLabel() {
 
201
        return downloadLabel;
 
202
    }
 
203
 
 
204
    public void setEntry(FileListEntry entry) {
 
205
        this.entry = entry;
 
206
        setValues(entry);
 
207
    }
 
208
 
 
209
    public void setVisible(boolean visible) {
 
210
        if (visible)
 
211
            okPressed = false;
 
212
        diag.setVisible(visible);
 
213
    }
 
214
 
 
215
    public void setValues(FileListEntry entry) {
 
216
        description.setText(entry.getDescription());
 
217
        link.setText(entry.getLink());
 
218
        //if (link.getText().length() > 0)
 
219
        //    checkExtension();
 
220
        types.setModel(new DefaultComboBoxModel(Globals.prefs.getExternalFileTypeSelection()));
 
221
        types.setSelectedIndex(-1);
 
222
        // See what is a reasonable selection for the type combobox:
 
223
        if ((entry.getType() != null) && !(entry.getType() instanceof UnknownExternalFileType))
 
224
            types.setSelectedItem(entry.getType());
 
225
        // If the entry has a link but not a file type, try to deduce the file type:
 
226
        else if ((entry.getLink() != null) && (entry.getLink().length() > 0)) {
 
227
            checkExtension();    
 
228
        }
 
229
 
 
230
    }
 
231
 
 
232
    public void storeSettings(FileListEntry entry) {
 
233
        entry.setDescription(description.getText().trim());
 
234
        // See if we should trim the file link to be relative to the file directory:
 
235
        try {
 
236
        String fileDir = metaData.getFileDirectory(GUIGlobals.FILE_FIELD);
 
237
        if ((fileDir == null) ||(fileDir.equals(""))) {
 
238
            entry.setLink(link.getText().trim());
 
239
        } else {
 
240
            String canPath = (new File(fileDir)).getCanonicalPath();
 
241
            File fl = new File(link.getText().trim());
 
242
            String flPath = fl.getCanonicalPath();
 
243
            if ((flPath.length() > canPath.length()) && (flPath.startsWith(canPath))) {
 
244
                String relFileName = fl.getCanonicalPath().substring(canPath.length()+1);
 
245
                entry.setLink(relFileName);
 
246
            } else
 
247
                entry.setLink(link.getText().trim());
 
248
        }
 
249
    } catch (java.io.IOException ex)
 
250
        { 
 
251
                ex.printStackTrace();
 
252
                // Don't think this should happen, but set the file link directly as a fallback:
 
253
                entry.setLink(link.getText().trim());
 
254
        }
 
255
        
 
256
        entry.setType((ExternalFileType)types.getSelectedItem());
 
257
    }
 
258
 
 
259
    public boolean okPressed() {
 
260
        return okPressed;
 
261
    }
 
262
 
 
263
    class BrowseListener implements ActionListener {
 
264
        private JFrame parent;
 
265
        private JTextField comp;
 
266
 
 
267
        public BrowseListener(JFrame parent, JTextField comp) {
 
268
            this.parent = parent;
 
269
            this.comp = comp;
 
270
        }
 
271
 
 
272
        public void actionPerformed(ActionEvent e) {
 
273
            File initial = new File(comp.getText().trim());
 
274
            if (comp.getText().trim().length() == 0) {
 
275
                // Nothing in the field. Go to the last file dir used:
 
276
                initial = new File(Globals.prefs.get("fileWorkingDirectory"));
 
277
            }
 
278
            String chosen = Globals.getNewFile(parent, initial, Globals.NONE,
 
279
                JFileChooser.OPEN_DIALOG, false);
 
280
            if (chosen != null) {
 
281
                File newFile = new File(chosen);
 
282
                // Store the directory for next time:
 
283
                Globals.prefs.put("fileWorkingDirectory", newFile.getParent());
 
284
 
 
285
                // If the file is below the file directory, make the path relative:
 
286
                ArrayList<File> dirs = new ArrayList<File>();
 
287
                String fileDir = metaData.getFileDirectory(GUIGlobals.FILE_FIELD);
 
288
                if (fileDir != null)
 
289
                    dirs.add(new File(fileDir));
 
290
                if (dirs.size() > 0) {
 
291
                    newFile = FileListEditor.relativizePath(newFile, dirs);
 
292
                }
 
293
 
 
294
                comp.setText(newFile.getPath());
 
295
                comp.requestFocus();
 
296
            }
 
297
        }
 
298
    }
 
299
}