Skip to content

Commit

Permalink
Convert PushToApp Settings to javafx (#4891)
Browse files Browse the repository at this point in the history
* Convert PushToApp Settings to javafx

Replace Runnable with BackgroundThread in Push

* Remove old swing code
Rename variable
Only overwrite one method
  • Loading branch information
Siedlerchr committed Apr 18, 2019
1 parent e230a95 commit 3414ac6
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 192 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/preferences/ExternalTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private void addSettingsButton(final PushToApplication application, GridPane pan
PushToApplicationSettings settings = PushToApplications.getSettings(application);
Button button = new Button(Localization.lang("Settings for %0", application.getApplicationName()));
button.setPrefSize(150, 20);
button.setOnAction(e -> PushToApplicationSettingsDialog.showSettingsDialog(null, settings, index));
button.setOnAction(e -> PushToApplicationSettingsDialog.showSettingsDialog(dialogService, settings, index));
if ((index % 2) == 0) {
panel.add(button, 1, (index / 2) + 1);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jabref/gui/push/PushToApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface PushToApplication {

JabRefIcon getIcon();


/**
* The actual operation. This method will not be called on the event dispatch thread, so it should not do GUI
* operations without utilizing invokeLater().
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/org/jabref/gui/push/PushToApplicationAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import java.util.List;
import java.util.Optional;

import javax.swing.SwingUtilities;

import org.jabref.Globals;
import org.jabref.JabRefExecutorService;
import org.jabref.gui.BasePanel;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.Action;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.icon.JabRefIcon;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.JabRefPreferences;
Expand All @@ -23,10 +21,10 @@
/**
* An Action class representing the process of invoking a PushToApplication operation.
*/
public class PushToApplicationAction extends SimpleCommand implements Runnable {
public class PushToApplicationAction extends SimpleCommand {

private PushToApplication operation;
private JabRefFrame frame;
private final PushToApplication operation;
private final JabRefFrame frame;
private BasePanel panel;
private List<BibEntry> entries;

Expand All @@ -50,6 +48,7 @@ private PushToApplication getLastUsedApplication(List<PushToApplication> pushAct

public Action getActionInformation() {
return new Action() {

@Override
public Optional<JabRefIcon> getIcon() {
return Optional.of(operation.getIcon());
Expand Down Expand Up @@ -85,7 +84,7 @@ public void execute() {
entries = panel.getSelectedEntries();
if (entries.isEmpty()) {
frame.getDialogService().showErrorDialogAndWait(operation.getApplicationName(),
Localization.lang("This operation requires one or more entries to be selected."));
Localization.lang("This operation requires one or more entries to be selected."));

return;
}
Expand All @@ -95,24 +94,23 @@ public void execute() {
for (BibEntry entry : entries) {
if (!(entry.getCiteKeyOptional().isPresent()) || entry.getCiteKeyOptional().get().trim().isEmpty()) {
frame.getDialogService().showErrorDialogAndWait(operation.getApplicationName(),
Localization.lang("This operation requires all selected entries to have BibTeX keys defined."));
Localization.lang("This operation requires all selected entries to have BibTeX keys defined."));

return;
}
}
}

// All set, call the operation in a new thread:
JabRefExecutorService.INSTANCE.execute(this);

BackgroundTask.wrap(this::pushentries)
.onSuccess(s -> operation.operationCompleted(panel))
.executeWith(Globals.TASK_EXECUTOR);

}

@Override
public void run() {
// Do the operation:
private void pushentries() {
operation.pushEntries(panel.getDatabase(), entries, getKeyString(entries), panel.getBibDatabaseContext().getMetaData());

// Call the operationCompleted() method on the event dispatch thread:
SwingUtilities.invokeLater(() -> operation.operationCompleted(panel));
}

private static String getKeyString(List<BibEntry> bibentries) {
Expand Down
114 changes: 34 additions & 80 deletions src/main/java/org/jabref/gui/push/PushToApplicationSettings.java
Original file line number Diff line number Diff line change
@@ -1,102 +1,56 @@
package org.jabref.gui.push;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

import org.jabref.Globals;
import org.jabref.gui.DialogService;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.JabRefPreferences;

import com.jgoodies.forms.builder.FormBuilder;
import com.jgoodies.forms.layout.FormLayout;

public class PushToApplicationSettings {
protected final JTextField path = new JTextField(30);
protected final TextField path1 = new TextField();
protected JPanel settings;

protected final TextField path = new TextField();
protected Label commandLabel = new Label();
protected GridPane jfxSettings;
protected FormBuilder builder;
protected AbstractPushToApplication application;
private DialogService dialogService;

/**
* This method asks the implementing class to return a JPanel populated with the imlementation's options panel, if
* necessary. If the JPanel is shown to the user, and the user indicates that settings should be stored, the
* implementation's storeSettings() method will be called. This method must make sure all widgets in the panel are
* in the correct selection states.
*
* @return a JPanel containing options, or null if options are not needed.
*/
public JPanel getSettingsPanel(int n) {
public GridPane getJFXSettingPane(int n) {
switch (n) {
case 0: application = new PushToEmacs(dialogService);
break;
case 1: application = new PushToLyx(dialogService);
break;
case 2: application = new PushToTexmaker(dialogService);
break;
case 3: application = new PushToTeXstudio(dialogService);
break;
case 4: application = new PushToVim(dialogService);
break;
case 5: application = new PushToWinEdt(dialogService);
break;
default: application = null;
break;
case 0:
application = new PushToEmacs(dialogService);
break;
case 1:
application = new PushToLyx(dialogService);
break;
case 2:
application = new PushToTexmaker(dialogService);
break;
case 3:
application = new PushToTeXstudio(dialogService);
break;
case 4:
application = new PushToVim(dialogService);
break;
case 5:
application = new PushToWinEdt(dialogService);
break;
default:
application = null;
break;
}
application.initParameters();
String commandPath = Globals.prefs.get(application.commandPathPreferenceKey);
if (settings == null) {
initSettingsPanel();
}
path.setText(commandPath);
return settings;
}

public GridPane getJFXSettingPane() {
application.initParameters();
String commandPath = Globals.prefs.get(application.commandPathPreferenceKey);
if (jfxSettings == null) {
initJFXSettingsPanel();
}
path1.setText(commandPath);
return jfxSettings;
}

/**
* Create a FormBuilder, fill it with a textbox for the path and store the JPanel in settings
*/
protected void initSettingsPanel() {
builder = FormBuilder.create();
builder.layout(new FormLayout("left:pref, 4dlu, fill:pref:grow, 4dlu, fill:pref", "p"));
StringBuilder label = new StringBuilder(Localization.lang("Path to %0", application.commandPathPreferenceKey));
// In case the application name and the actual command is not the same, add the command in brackets
if (application.getCommandName() == null) {
label.append(':');
} else {
label.append(" (").append(application.getCommandName()).append("):");
}
builder.add(label.toString()).xy(1, 1);
builder.add(path).xy(3, 1);
JButton browse = new JButton(Localization.lang("Browse"));

FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build();
path.setText(commandPath);

browse.addActionListener(
e -> DefaultTaskExecutor.runInJavaFXThread(() -> dialogService.showFileOpenDialog(fileDialogConfiguration))
.ifPresent(f -> path.setText(f.toAbsolutePath().toString())));
builder.add(browse).xy(5, 1);
settings = builder.build();
return jfxSettings;
}

protected void initJFXSettingsPanel() {
Expand All @@ -108,17 +62,17 @@ protected void initJFXSettingsPanel() {
} else {
label.append(" (").append(application.getCommandName()).append("):");
}
jfxSettings.add(new Label(label.toString()),1,1);
jfxSettings.add(path1,2, 1);
commandLabel = new Label(label.toString());
jfxSettings.add(commandLabel, 0, 0);
jfxSettings.add(path, 1, 0);
Button browse = new Button(Localization.lang("Browse"));

FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build();
.withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build();

browse.setOnAction(
e -> DefaultTaskExecutor.runInJavaFXThread(() -> dialogService.showFileOpenDialog(fileDialogConfiguration))
.ifPresent(f -> path.setText(f.toAbsolutePath().toString())));
jfxSettings.add(browse,3, 1);
browse.setOnAction(e -> dialogService.showFileOpenDialog(fileDialogConfiguration)
.ifPresent(f -> path.setText(f.toAbsolutePath().toString())));
jfxSettings.add(browse, 2, 0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,22 @@
package org.jabref.gui.push;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jabref.Globals;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.gui.DialogService;
import org.jabref.logic.l10n.Localization;

import com.jgoodies.forms.builder.ButtonBarBuilder;

public class PushToApplicationSettingsDialog {
public static void showSettingsDialog(JFrame parent, PushToApplicationSettings toApp, int n) {
final JDialog diag = new JDialog(parent, Localization.lang("Settings"), true);
JPanel options = toApp.getSettingsPanel(n);
options.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
diag.getContentPane().add(options, BorderLayout.CENTER);
ButtonBarBuilder bb = new ButtonBarBuilder();
JButton ok = new JButton(Localization.lang("OK"));
JButton cancel = new JButton(Localization.lang("Cancel"));
bb.addGlue();
bb.addButton(ok);
bb.addButton(cancel);
bb.addGlue();
bb.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH);
ok.addActionListener(e -> {
// If the user pressed Ok, ask the PushToApplication implementation to store its settings:
toApp.storeSettings();
diag.dispose();
});
cancel.addActionListener(e -> diag.dispose());

// Key bindings:
ActionMap am = bb.getPanel().getActionMap();
InputMap im = bb.getPanel().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE), "close");
am.put("close", new AbstractAction() {
public static void showSettingsDialog(DialogService dialogService, PushToApplicationSettings toApp, int n) {

@Override
public void actionPerformed(ActionEvent e) {
diag.dispose();
DialogPane dialogPane = new DialogPane();
dialogPane.setContent(toApp.getJFXSettingPane(n));

dialogService.showCustomDialogAndWait(Localization.lang("App settings"), dialogPane, ButtonType.OK, ButtonType.CANCEL).ifPresent(btn -> {
if (btn == ButtonType.OK) {
toApp.storeSettings();
}
});
diag.pack();
diag.setLocationRelativeTo(parent);

// Show the dialog:
diag.setVisible(true);
}
}
31 changes: 8 additions & 23 deletions src/main/java/org/jabref/gui/push/PushToEmacsSettings.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
package org.jabref.gui.push;

import javax.swing.JPanel;
import javax.swing.JTextField;

import javafx.scene.layout.GridPane;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

import org.jabref.Globals;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.JabRefPreferences;

public class PushToEmacsSettings extends PushToApplicationSettings {

private final JTextField additionalParams = new JTextField(30);

@Override
public JPanel getSettingsPanel(int n) {
additionalParams.setText(Globals.prefs.get(JabRefPreferences.EMACS_ADDITIONAL_PARAMETERS));
return super.getSettingsPanel(n);
}

@Override
public GridPane getJFXSettingPane() {
additionalParams.setText(Globals.prefs.get(JabRefPreferences.EMACS_ADDITIONAL_PARAMETERS));
return super.getJFXSettingPane();
}
private final TextField additionalParams = new TextField();

@Override
public void storeSettings() {
Expand All @@ -32,11 +18,10 @@ public void storeSettings() {
}

@Override
protected void initSettingsPanel() {
super.initSettingsPanel();
builder.appendRows("2dlu, p, 2dlu, p");
builder.add(Localization.lang("Additional parameters") + ":").xy(1, 3);
builder.add(additionalParams).xy(3, 3);
settings = builder.build();
protected void initJFXSettingsPanel() {
super.initJFXSettingsPanel();
jfxSettings.add(new Label(Localization.lang("Additional parameters") + ":"), 0, 1);
jfxSettings.add(additionalParams, 1, 1);
additionalParams.setText(Globals.prefs.get(JabRefPreferences.EMACS_ADDITIONAL_PARAMETERS));
}
}
1 change: 0 additions & 1 deletion src/main/java/org/jabref/gui/push/PushToLyx.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public void operationCompleted(BasePanel panel) {

@Override
public void pushEntries(BibDatabase database, final List<BibEntry> entries, final String keyString, MetaData metaData) {

couldNotConnect = false;
couldNotCall = false;
notDefined = false;
Expand Down
Loading

0 comments on commit 3414ac6

Please sign in to comment.