From be286a4f64abe76ac4a639833906b695f29c2853 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Mon, 1 Aug 2016 18:55:15 +0200 Subject: [PATCH] Fix customExport dialog Use BrowseAction for ExternalFileType, too TODO: Think about passsing default dir --- .../external/ExternalFileTypeEntryEditor.java | 24 ++--- .../sf/jabref/gui/FileListEntryEditor.java | 98 +++++++++---------- .../gui/exporter/CustomExportDialog.java | 56 ++++------- 3 files changed, 75 insertions(+), 103 deletions(-) diff --git a/src/main/java/net/sf/jabref/external/ExternalFileTypeEntryEditor.java b/src/main/java/net/sf/jabref/external/ExternalFileTypeEntryEditor.java index b584e79ea441..929cbcb88469 100644 --- a/src/main/java/net/sf/jabref/external/ExternalFileTypeEntryEditor.java +++ b/src/main/java/net/sf/jabref/external/ExternalFileTypeEntryEditor.java @@ -17,16 +17,13 @@ import java.awt.BorderLayout; import java.awt.Dimension; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.io.File; -import java.util.Collections; +import java.nio.file.Path; +import java.nio.file.Paths; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JDialog; -import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; @@ -36,8 +33,8 @@ import javax.swing.event.DocumentListener; import net.sf.jabref.Globals; -import net.sf.jabref.gui.FileDialogs; import net.sf.jabref.gui.IconTheme; +import net.sf.jabref.gui.actions.BrowseAction; import net.sf.jabref.logic.l10n.Localization; import net.sf.jabref.logic.util.OS; import net.sf.jabref.preferences.JabRefPreferences; @@ -128,6 +125,11 @@ private void init(ExternalFileType inEntry) { ok.addActionListener(e -> { okPressed = true; + Path applicationDir = Paths.get(application.getText()).getParent(); + if (applicationDir != null) { + Globals.prefs.put(JabRefPreferences.EXPORT_WORKING_DIRECTORY, applicationDir.toString()); + System.out.println("External app dir "+ applicationDir); + } storeSettings(ExternalFileTypeEntryEditor.this.entry); diag.dispose(); @@ -177,8 +179,8 @@ public void changedUpdate(DocumentEvent documentEvent) { diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH); diag.pack(); - BrowseListener browse = new BrowseListener(application); - browseBut.addActionListener(browse); + + browseBut.addActionListener(BrowseAction.buildForFile(application)); if (dParent == null) { diag.setLocationRelativeTo(fParent); @@ -248,7 +250,7 @@ public boolean okPressed() { } - static class BrowseListener implements ActionListener { + /* static class BrowseListener implements ActionListener { private final JTextField comp; @@ -262,7 +264,7 @@ public void actionPerformed(ActionEvent e) { File initial = new File(comp.getText().trim()); if (comp.getText().trim().isEmpty()) { // Nothing in the field. Go to the last file dir used: - initial = new File(Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY)); + initial = new File()); } String chosen = FileDialogs.getNewFile(null, initial, Collections.emptyList(), JFileChooser.OPEN_DIALOG, false); @@ -274,5 +276,5 @@ public void actionPerformed(ActionEvent e) { comp.requestFocus(); } } - } + }*/ } diff --git a/src/main/java/net/sf/jabref/gui/FileListEntryEditor.java b/src/main/java/net/sf/jabref/gui/FileListEntryEditor.java index 0d1ec15efd2f..892248141cd1 100644 --- a/src/main/java/net/sf/jabref/gui/FileListEntryEditor.java +++ b/src/main/java/net/sf/jabref/gui/FileListEntryEditor.java @@ -36,12 +36,10 @@ import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JDialog; -import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JProgressBar; import javax.swing.JTextField; import javax.swing.SwingConstants; -import javax.swing.SwingUtilities; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; @@ -87,19 +85,24 @@ public class FileListEntryEditor { private ConfirmCloseFileListEntryEditor externalConfirm; private FileListEntry entry; - private final BibDatabaseContext databaseContext; + //Do not make this variable final, as then the lambda action listener will fail on compiƶe + private BibDatabaseContext databaseContext; private boolean okPressed; private boolean okDisabledExternally; private boolean openBrowseWhenShown; private boolean dontOpenBrowseUntilDisposed; + //Do not make this variable final, as then the lambda action listener will fail on compile + private JabRefFrame frame; + private static final Pattern REMOTE_LINK_PATTERN = Pattern.compile("[a-z]+://.*"); - public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showProgressBar, - boolean showOpenButton, BibDatabaseContext databaseContext) { + public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showProgressBar, boolean showOpenButton, + BibDatabaseContext databaseContext) { this.entry = entry; this.databaseContext = databaseContext; + this.frame = frame; ActionListener okAction = e -> { // If OK button is disabled, ignore this event: @@ -125,27 +128,28 @@ public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showP } }); - FormBuilder builder = FormBuilder.create().layout(new FormLayout - ("left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref, 4dlu, fill:pref", "p, 2dlu, p, 2dlu, p")); + FormBuilder builder = FormBuilder.create().layout(new FormLayout( + "left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref, 4dlu, fill:pref", "p, 2dlu, p, 2dlu, p")); builder.add(Localization.lang("Link")).xy(1, 1); builder.add(link).xy(3, 1); - final BrowseListener browse = new BrowseListener(frame, link); //TODO: Maybe use browse action + //final BrowseListener browse = new BrowseListener(frame, link); //TODO: Maybe use browse action + final JButton browseBut = new JButton(Localization.lang("Browse")); - browseBut.addActionListener(browse); + browseBut.addActionListener(browsePressed); builder.add(browseBut).xy(5, 1); JButton open = new JButton(Localization.lang("Open")); if (showOpenButton) { builder.add(open).xy(7, 1); } builder.add(Localization.lang("Description")).xy(1, 3); - builder.add(description).xyw(3,3,3); + builder.add(description).xyw(3, 3, 3); builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); builder.add(Localization.lang("File type")).xy(1, 5); builder.add(types).xyw(3, 5, 3); if (showProgressBar) { builder.appendRows("2dlu, p"); - builder.add(downloadLabel).xy(1,7); - builder.add(prog).xyw(3,7,3); + builder.add(downloadLabel).xy(1, 7); + builder.add(prog).xyw(3, 7, 3); } ButtonBarBuilder bb = new ButtonBarBuilder(); @@ -165,6 +169,7 @@ public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showP open.addActionListener(e -> openFile()); AbstractAction cancelAction = new AbstractAction() { + @Override public void actionPerformed(ActionEvent e) { diag.dispose(); @@ -197,6 +202,8 @@ public void changedUpdate(DocumentEvent documentEvent) { }); + System.out.println("LInk field " + link.getText()); + diag = new JDialog(frame, Localization.lang("Save file"), true); diag.getContentPane().add(builder.getPanel(), BorderLayout.CENTER); diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH); @@ -208,7 +215,8 @@ public void changedUpdate(DocumentEvent documentEvent) { public void windowActivated(WindowEvent event) { if (openBrowseWhenShown && !dontOpenBrowseUntilDisposed) { dontOpenBrowseUntilDisposed = true; - SwingUtilities.invokeLater(() -> browse.actionPerformed(new ActionEvent(browseBut, 0, ""))); + System.out.println("Window activated "); + // SwingUtilities.invokeLater(() -> browse.actionPerformed(new ActionEvent(browseBut, 0, ""))); } } @@ -286,8 +294,7 @@ public boolean isVisible() { private void setValues(FileListEntry entry) { description.setText(entry.description); link.setText(entry.link); - //if (link.getText().length() > 0) - // checkExtension(); + Collection list = ExternalFileTypes.getInstance().getExternalFileTypeSelection(); types.setModel(new DefaultComboBoxModel<>(list.toArray(new ExternalFileType[list.size()]))); @@ -343,46 +350,29 @@ public boolean okPressed() { } - class BrowseListener implements ActionListener { - private final JFrame parent; - private final JTextField comp; - - public BrowseListener(JFrame parent, JTextField comp) { - this.parent = parent; - this.comp = comp; + ActionListener browsePressed = e -> { + String filePath = link.getText().trim(); + Optional file = FileUtil.expandFilename(this.databaseContext, filePath); + String workingDir; + // no file set yet or found + if (file.isPresent()) { + workingDir = file.get().getPath(); + } else { + workingDir = Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY); } + String selection = new NewFileDialogs(this.frame, workingDir).getSelectedFile().toString(); - @Override - public void actionPerformed(ActionEvent e) { - String filePath = comp.getText().trim(); - Optional file = FileUtil.expandFilename(databaseContext, filePath); - String workingDir; - // no file set yet or found - if (file.isPresent()) { - workingDir = file.get().getPath(); - } else { - workingDir = Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY); - } - System.out.println("FILE SELECTED ENTRY WDIR " + workingDir); - - String selection = new NewFileDialogs(parent, workingDir).getSelectedFile().toString(); - //TODO: Prevent dialog opening on first start - System.out.println("FILE SELECTED ENTRY " + selection); - //String selection = FileDialogs.getNewFile(parent, workingDir, Collections.emptyList(), - // JFileChooser.OPEN_DIALOG, false); - if (selection != null) { - File newFile = new File(selection); - // Store the directory for next time: - Globals.prefs.put(JabRefPreferences.FILE_WORKING_DIRECTORY, newFile.getPath()); - - // If the file is below the file directory, make the path relative: - List fileDirs = databaseContext.getFileDirectory(); - newFile = FileUtil.shortenFileName(newFile, fileDirs); - - comp.setText(newFile.getPath()); - comp.requestFocus(); - } - } - } + if (selection != null) { + File newFile = new File(selection); + // Store the directory for next time: + Globals.prefs.put(JabRefPreferences.FILE_WORKING_DIRECTORY, newFile.getPath()); + + // If the file is below the file directory, make the path relative: + List fileDirs = this.databaseContext.getFileDirectory(); + newFile = FileUtil.shortenFileName(newFile, fileDirs); + link.setText(newFile.getPath()); + link.requestFocus(); + } + }; } diff --git a/src/main/java/net/sf/jabref/gui/exporter/CustomExportDialog.java b/src/main/java/net/sf/jabref/gui/exporter/CustomExportDialog.java index e4b35f90c633..9af2c407fb6a 100644 --- a/src/main/java/net/sf/jabref/gui/exporter/CustomExportDialog.java +++ b/src/main/java/net/sf/jabref/gui/exporter/CustomExportDialog.java @@ -21,8 +21,10 @@ import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.EnumSet; + import javax.swing.AbstractAction; import javax.swing.ActionMap; import javax.swing.BorderFactory; @@ -46,7 +48,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - /** * Dialog for creating or modifying custom exports. */ @@ -71,10 +72,11 @@ public CustomExportDialog(final JabRefFrame parent, final String exporterName, f public CustomExportDialog(final JabRefFrame parent) { super(parent, Localization.lang("Edit custom export"), true); ActionListener okListener = e -> { - String p = Paths.get(layoutFile.getText()).getParent().toString(); - System.out.println("P layout new dir " + p); - extension.setText(FileExtensions.LAYOUT.getFirstExtensionWithDot()); - Globals.prefs.put(JabRefPreferences.EXPORT_WORKING_DIRECTORY, p); + Path layoutFileDir = Paths.get(layoutFile.getText()).getParent(); + if (layoutFileDir != null) { + Globals.prefs.put(JabRefPreferences.EXPORT_WORKING_DIRECTORY, layoutFileDir.toString()); + + } // Check that there are no empty strings. if (layoutFile.getText().isEmpty() || name.getText().isEmpty() || extension.getText().isEmpty() @@ -89,12 +91,21 @@ public CustomExportDialog(final JabRefFrame parent) { okPressed = true; dispose(); }; + + layoutFile.setText(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)); + JButton ok = new JButton(Localization.lang("OK")); ok.addActionListener(okListener); name.addActionListener(okListener); layoutFile.addActionListener(okListener); extension.addActionListener(okListener); + JButton cancel = new JButton(Localization.lang("Cancel")); + cancel.addActionListener(e -> dispose()); + + JButton browse = new JButton(Localization.lang("Browse")); + browse.addActionListener(BrowseAction.buildForFile(layoutFile, EnumSet.of(FileExtensions.LAYOUT))); + AbstractAction cancelAction = new AbstractAction() { @Override @@ -102,36 +113,6 @@ public void actionPerformed(ActionEvent e) { dispose(); } }; - - JButton cancel = new JButton(Localization.lang("Cancel")); - cancel.addActionListener(cancelAction); - - JButton browse = new JButton(Localization.lang("Browse")); - browse.addActionListener(BrowseAction.buildForFile(layoutFile, EnumSet.of(FileExtensions.LAYOUT))); - - - - - - - /* - * - */ - /* browse.addActionListener(e -> { - File directory = new File(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)); - String chosenStr = FileDialogs.getNewFile(parent, directory, Collections.singletonList(".layout"), - JFileChooser.OPEN_DIALOG, false); - if (chosenStr == null) { - return; - } - File chosen = new File(chosenStr); - - // Update working directory for layout files. - - layoutFile.setText(chosen.getPath()); - }); - */ - // Key bindings: JPanel main = new JPanel(); ActionMap am = main.getActionMap(); @@ -142,9 +123,8 @@ public void actionPerformed(ActionEvent e) { // Layout starts here. GridBagLayout gbl = new GridBagLayout(); main.setLayout(gbl); - main.setBorder(BorderFactory.createTitledBorder - (BorderFactory.createEtchedBorder(), - Localization.lang("Export properties"))); + main.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), + Localization.lang("Export properties"))); // Main panel: GridBagConstraints con = new GridBagConstraints();