Skip to content

Commit

Permalink
Fix customExport dialog
Browse files Browse the repository at this point in the history
Use BrowseAction for ExternalFileType, too
TODO: Think about passsing default dir
  • Loading branch information
Siedlerchr committed Aug 1, 2016
1 parent 3065e81 commit be286a4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -248,7 +250,7 @@ public boolean okPressed() {
}


static class BrowseListener implements ActionListener {
/* static class BrowseListener implements ActionListener {
private final JTextField comp;
Expand All @@ -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);
Expand All @@ -274,5 +276,5 @@ public void actionPerformed(ActionEvent e) {
comp.requestFocus();
}
}
}
}*/
}
98 changes: 44 additions & 54 deletions src/main/java/net/sf/jabref/gui/FileListEntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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:
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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, "")));
}
}

Expand Down Expand Up @@ -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<ExternalFileType> list = ExternalFileTypes.getInstance().getExternalFileTypeSelection();

types.setModel(new DefaultComboBoxModel<>(list.toArray(new ExternalFileType[list.size()])));
Expand Down Expand Up @@ -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> 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> 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<String> 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<String> fileDirs = this.databaseContext.getFileDirectory();
newFile = FileUtil.shortenFileName(newFile, fileDirs);

link.setText(newFile.getPath());
link.requestFocus();
}
};
}
56 changes: 18 additions & 38 deletions src/main/java/net/sf/jabref/gui/exporter/CustomExportDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,7 +48,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
* Dialog for creating or modifying custom exports.
*/
Expand All @@ -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()
Expand All @@ -89,49 +91,28 @@ 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
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();
Expand All @@ -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();
Expand Down

0 comments on commit be286a4

Please sign in to comment.