Skip to content

Commit

Permalink
Dialogstojavafx (#3801)
Browse files Browse the repository at this point in the history
* convert dialogs to javafx
start with preferences

* convert open office and other preferences

* Convert entryTypeList and entry customization and shared database

* And convert some more

* Pass dialogService instead of frame
convert some more dialogs
Fix index out of bounds in Entry Table columns Pref Tab

* entry types list

* create choice dialog

* ADd method for returning list in Encodings
Display charsets in  Choices dialog

* Convert some more confirmation dialogs

* convert some more dialogs
Extend choice dialog with cancel button and ok button label

* fix checkstyle and add missing lang keys

* fix dialog conditions

* add better title for share db dialog

* Add dialogservice to push to apps
Remove comment form import insepction dlg

* add new keys

* fix dialog service in push

* checkstyle
and convert some open office dlgs, too

* use dialogservice as local variable
Fix l10n keys

* pass Dialog service to push dialogs as ctor arg

* checkstyle
  • Loading branch information
Siedlerchr authored and tobiasdiez committed Mar 5, 2018
1 parent f6df529 commit 21355ab
Show file tree
Hide file tree
Showing 48 changed files with 799 additions and 683 deletions.
26 changes: 14 additions & 12 deletions src/main/java/org/jabref/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.jabref.gui.AbstractView;
import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.FXDialogService;
import org.jabref.gui.GUIGlobals;
import org.jabref.gui.IconTheme;
Expand Down Expand Up @@ -51,12 +52,14 @@ public class JabRefGUI {
private final boolean isBlank;
private final List<ParserResult> failed = new ArrayList<>();
private final List<ParserResult> toOpenTab = new ArrayList<>();
private final DialogService dialogService;

private final String focusedFile;

public JabRefGUI(Stage mainStage, List<ParserResult> argsDatabases, boolean isBlank) {
this.bibDatabases = argsDatabases;
this.isBlank = isBlank;
this.dialogService = new FXDialogService(mainStage);

// passed file (we take the first one) should be focused
focusedFile = argsDatabases.stream().findFirst().flatMap(ParserResult::getFile).map(File::getAbsolutePath)
Expand Down Expand Up @@ -87,7 +90,7 @@ private void openWindow(Stage mainStage) {

// If the option is enabled, open the last edited libraries, if any.
if (!isBlank && Globals.prefs.getBoolean(JabRefPreferences.OPEN_LAST_EDITED)) {
openLastEditedDatabases(mainStage);
openLastEditedDatabases();
}

GUIGlobals.init();
Expand Down Expand Up @@ -159,12 +162,11 @@ private void openWindow(Stage mainStage) {
});

for (ParserResult pr : failed) {
String message = "<html>" + Localization.lang("Error opening file '%0'.", pr.getFile().get().getName())
+ "<p>"
+ pr.getErrorMessage() + "</html>";
String message = Localization.lang("Error opening file '%0'.", pr.getFile().get().getName()) + "\n"
+ pr.getErrorMessage();

dialogService.showErrorDialogAndWait(Localization.lang("Error opening file"), message);

JOptionPane.showMessageDialog(null, message, Localization.lang("Error opening file"),
JOptionPane.ERROR_MESSAGE);
}

// Display warnings, if any
Expand All @@ -191,7 +193,7 @@ private void openWindow(Stage mainStage) {
LOGGER.debug("Finished adding panels");
}

private void openLastEditedDatabases(Stage mainStage) {
private void openLastEditedDatabases() {
if (Globals.prefs.get(JabRefPreferences.LAST_EDITED) == null) {
return;
}
Expand All @@ -206,7 +208,7 @@ private void openLastEditedDatabases(Stage mainStage) {
}

if (BackupManager.checkForBackupFile(dbFile.toPath())) {
BackupUIManager.showRestoreBackupDialog(new FXDialogService(mainStage), dbFile.toPath());
BackupUIManager.showRestoreBackupDialog(dialogService, dbFile.toPath());
}

ParserResult parsedDatabase = OpenDatabase.loadDatabase(fileName,
Expand Down Expand Up @@ -264,11 +266,11 @@ private void setLookAndFeel() {
// also set system l&f as default
Globals.prefs.put(JabRefPreferences.WIN_LOOK_AND_FEEL, systemLookFeel);
// notify the user
JOptionPane.showMessageDialog(null,
Localization
.lang("Unable to find the requested look and feel and thus the default one is used."),
Localization.lang("Warning"), JOptionPane.WARNING_MESSAGE);

LOGGER.warn("Unable to find requested look and feel", e);
dialogService.showWarningDialogAndWait(Localization.lang("Warning"),
Localization.lang("Unable to find the requested look and feel and thus the default one is used."));

}
}

Expand Down
19 changes: 10 additions & 9 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
import org.jabref.gui.undo.UndoableRemoveEntry;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.gui.util.component.CheckBoxMessage;
import org.jabref.gui.worker.AbstractWorker;
import org.jabref.gui.worker.CallBack;
import org.jabref.gui.worker.CitationStyleToClipboardWorker;
Expand Down Expand Up @@ -420,15 +419,16 @@ public void run() {
// if we're going to override some cite keys warn the user about it
} else if (Globals.prefs.getBoolean(JabRefPreferences.WARN_BEFORE_OVERWRITING_KEY)) {
if (entries.parallelStream().anyMatch(BibEntry::hasCiteKey)) {
CheckBoxMessage cbm = new CheckBoxMessage(

boolean overwriteKeysPressed = dialogService.showConfirmationDialogWithOptOutAndWait(Localization.lang("Overwrite keys"),
Localization.lang("One or more keys will be overwritten. Continue?"),
Localization.lang("Disable this confirmation dialog"), false);
final int answer = JOptionPane.showConfirmDialog(null, cbm,
Localization.lang("Overwrite keys"), JOptionPane.YES_NO_OPTION);
Globals.prefs.putBoolean(JabRefPreferences.WARN_BEFORE_OVERWRITING_KEY, !cbm.isSelected());
Localization.lang("Overwrite keys"),
Localization.lang("Cancel"),
Localization.lang("Disable this confirmation dialog"),
optOut -> Globals.prefs.putBoolean(JabRefPreferences.WARN_BEFORE_OVERWRITING_KEY, !optOut));

// The user doesn't want to overide cite keys
if (answer == JOptionPane.NO_OPTION) {
if (!overwriteKeysPressed) {
canceled = true;
return;
}
Expand Down Expand Up @@ -525,7 +525,7 @@ public void update() {

actions.put(Actions.OPEN_URL, new OpenURLAction());

actions.put(Actions.MERGE_WITH_FETCHED_ENTRY, new MergeWithFetchedEntryAction(this));
actions.put(Actions.MERGE_WITH_FETCHED_ENTRY, new MergeWithFetchedEntryAction(this, frame.getDialogService()));

actions.put(Actions.REPLACE_ALL, (BaseAction) () -> {
final ReplaceStringDialog rsd = new ReplaceStringDialog(frame);
Expand Down Expand Up @@ -616,7 +616,7 @@ public void update() {
actions.put(Actions.REMOVE_FROM_GROUP, new GroupAddRemoveDialog(this, false, false));
actions.put(Actions.MOVE_TO_GROUP, new GroupAddRemoveDialog(this, true, true));

actions.put(Actions.DOWNLOAD_FULL_TEXT, new FindFullTextAction(this));
actions.put(Actions.DOWNLOAD_FULL_TEXT, new FindFullTextAction(frame.getDialogService(), this));
}

/**
Expand Down Expand Up @@ -925,6 +925,7 @@ private boolean saveDatabase(File file, boolean selectedOnly, Charset enc,
new String[] {Localization.lang("Save"), tryDiff, Localization.lang("Cancel")}, tryDiff);

if (answer == JOptionPane.NO_OPTION) {

// The user wants to use another encoding.
Object choice = JOptionPane.showInputDialog(null, Localization.lang("Select encoding"), SAVE_DATABASE,
JOptionPane.QUESTION_MESSAGE, null, Encodings.ENCODINGS_DISPLAYNAMES, enc);
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/org/jabref/gui/DialogService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui;

import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
Expand All @@ -9,7 +10,9 @@
import javafx.print.PrinterJob;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextInputDialog;

import org.jabref.gui.util.DirectoryDialogConfiguration;
import org.jabref.gui.util.FileDialogConfiguration;
Expand All @@ -22,6 +25,14 @@
*/
public interface DialogService {

/**
* This will create and display new {@link ChoiceDialog} of type T with a default choice and a collection of possible choices
*/
<T> Optional<T> showChoiceDialogAndWait(String title, String content, String okButtonLabel, T defaultChoice, Collection<T> choices);

/**
* This will create and display new {@link TextInputDialog} with a text fields to enter data
*/
Optional<String> showInputDialogAndWait(String title, String content);

/**
Expand Down Expand Up @@ -119,7 +130,7 @@ default void showErrorDialogAndWait(Exception exception) {
* @return true if the use clicked "YES" otherwise false
*/
boolean showConfirmationDialogWithOptOutAndWait(String title, String content,
String optOutMessage, Consumer<Boolean> optOutAction);
String optOutMessage, Consumer<Boolean> optOutAction);

/**
* Create and display a new confirmation dialog.
Expand All @@ -131,8 +142,8 @@ boolean showConfirmationDialogWithOptOutAndWait(String title, String content,
* @return true if the use clicked "YES" otherwise false
*/
boolean showConfirmationDialogWithOptOutAndWait(String title, String content,
String okButtonLabel, String cancelButtonLabel,
String optOutMessage, Consumer<Boolean> optOutAction);
String okButtonLabel, String cancelButtonLabel,
String optOutMessage, Consumer<Boolean> optOutAction);

/**
* This will create and display a new dialog of the specified
Expand All @@ -142,7 +153,7 @@ boolean showConfirmationDialogWithOptOutAndWait(String title, String content,
* @return Optional with the pressed Button as ButtonType
*/
Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String title, String content,
ButtonType... buttonTypes);
ButtonType... buttonTypes);

/**
* This will create and display a new dialog showing a custom {@link DialogPane}
Expand Down Expand Up @@ -224,4 +235,5 @@ Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String
* @return false if the user opts to cancel printing
*/
boolean showPrintDialog(PrinterJob job);

}
11 changes: 5 additions & 6 deletions src/main/java/org/jabref/gui/EntryTypeDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
Expand Down Expand Up @@ -331,13 +330,13 @@ protected void done() {

dispose();
} else if (searchID.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, Localization.lang("The given search ID was empty."), Localization.lang("Empty search ID"), JOptionPane.WARNING_MESSAGE);
frame.getDialogService().showWarningDialogAndWait(Localization.lang("Empty search ID"),
Localization.lang("The given search ID was empty."));
} else if (!fetcherException) {
JOptionPane.showMessageDialog(null, Localization.lang("Fetcher '%0' did not find an entry for id '%1'.", fetcher.getName(), searchID) + "\n" + fetcherExceptionMessage, Localization.lang("No files found."), JOptionPane.WARNING_MESSAGE);
frame.getDialogService().showErrorDialogAndWait(Localization.lang("No files found.",
Localization.lang("Fetcher '%0' did not find an entry for id '%1'.", fetcher.getName(), searchID) + "\n" + fetcherExceptionMessage));
} else {
JOptionPane.showMessageDialog(null,
Localization.lang("Error while fetching from %0", fetcher.getName()) + "." + "\n" + fetcherExceptionMessage,
Localization.lang("Error"), JOptionPane.ERROR_MESSAGE);
frame.getDialogService().showErrorDialogAndWait(Localization.lang("Error"), Localization.lang("Error while fetching from %0", fetcher.getName()) + "." + "\n" + fetcherExceptionMessage);
}
fetcherWorker = new FetcherWorker();
SwingUtilities.invokeLater(() -> {
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/org/jabref/gui/FXDialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
Expand All @@ -17,6 +18,7 @@
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.Region;
Expand Down Expand Up @@ -66,7 +68,7 @@ private static FXDialog createDialog(AlertType type, String title, String conten
}

private static FXDialog createDialogWithOptOut(AlertType type, String title, String content,
String optOutMessage, Consumer<Boolean> optOutAction) {
String optOutMessage, Consumer<Boolean> optOutAction) {
FXDialog alert = new FXDialog(type, title, true);
// Need to force the alert to layout in order to grab the graphic as we are replacing the dialog pane with a custom pane
alert.getDialogPane().applyCss();
Expand All @@ -75,6 +77,7 @@ private static FXDialog createDialogWithOptOut(AlertType type, String title, Str
// Create a new dialog pane that has a checkbox instead of the hide/show details button
// Use the supplied callback for the action of the checkbox
alert.setDialogPane(new DialogPane() {

@Override
protected Node createDetailsButton() {
CheckBox optOut = new CheckBox();
Expand All @@ -97,6 +100,17 @@ protected Node createDetailsButton() {
return alert;
}

@Override
public <T> Optional<T> showChoiceDialogAndWait(String title, String content, String okButtonLabel, T defaultChoice, Collection<T> choices) {
ChoiceDialog<T> choiceDialog = new ChoiceDialog<>(defaultChoice, choices);
ButtonType okButtonType = new ButtonType(okButtonLabel, ButtonBar.ButtonData.OK_DONE);
choiceDialog.getDialogPane().getButtonTypes().setAll(ButtonType.CANCEL, okButtonType);
choiceDialog.setHeaderText(title);
choiceDialog.setContentText(content);
return choiceDialog.showAndWait();

}

@Override
public Optional<String> showInputDialogAndWait(String title, String content) {
TextInputDialog inputDialog = new TextInputDialog();
Expand Down Expand Up @@ -160,7 +174,7 @@ public boolean showConfirmationDialogAndWait(String title, String content, Strin

@Override
public boolean showConfirmationDialogAndWait(String title, String content,
String okButtonLabel, String cancelButtonLabel) {
String okButtonLabel, String cancelButtonLabel) {
FXDialog alert = createDialog(AlertType.CONFIRMATION, title, content);
ButtonType okButtonType = new ButtonType(okButtonLabel, ButtonBar.ButtonData.OK_DONE);
ButtonType cancelButtonType = new ButtonType(cancelButtonLabel, ButtonBar.ButtonData.NO);
Expand All @@ -170,16 +184,16 @@ public boolean showConfirmationDialogAndWait(String title, String content,

@Override
public boolean showConfirmationDialogWithOptOutAndWait(String title, String content,
String optOutMessage, Consumer<Boolean> optOutAction) {
String optOutMessage, Consumer<Boolean> optOutAction) {
FXDialog alert = createDialogWithOptOut(AlertType.CONFIRMATION, title, content, optOutMessage, optOutAction);
alert.getButtonTypes().setAll(ButtonType.YES, ButtonType.NO);
return alert.showAndWait().filter(buttonType -> buttonType == ButtonType.YES).isPresent();
}

@Override
public boolean showConfirmationDialogWithOptOutAndWait(String title, String content,
String okButtonLabel, String cancelButtonLabel,
String optOutMessage, Consumer<Boolean> optOutAction) {
String okButtonLabel, String cancelButtonLabel,
String optOutMessage, Consumer<Boolean> optOutAction) {
FXDialog alert = createDialogWithOptOut(AlertType.CONFIRMATION, title, content, optOutMessage, optOutAction);
ButtonType okButtonType = new ButtonType(okButtonLabel, ButtonBar.ButtonData.YES);
ButtonType cancelButtonType = new ButtonType(cancelButtonLabel, ButtonBar.ButtonData.NO);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ public boolean quit() {
private void initLayout() {
setProgressBarVisible(false);

pushApplications = new PushToApplications();
pushApplications = new PushToApplications(this.getDialogService());

BorderPane head = new BorderPane();
head.setTop(createMenu());
Expand Down Expand Up @@ -670,7 +670,7 @@ private Node createToolbar() {
HBox rightSide = new HBox (
factory.createIconButton(StandardActions.NEW_ENTRY, new NewEntryAction(this, BiblatexEntryTypes.ARTICLE)),
factory.createIconButton(StandardActions.DELETE_ENTRY, new EditAction(Actions.DELETE)),

factory.createIconButton(StandardActions.UNDO, new OldDatabaseCommandWrapper(Actions.UNDO, this, Globals.stateManager)),
factory.createIconButton(StandardActions.REDO, new OldDatabaseCommandWrapper(Actions.REDO, this, Globals.stateManager)),
factory.createIconButton(StandardActions.CUT, new EditAction(Actions.CUT)),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/actions/ActionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public class ActionFactory {

private KeyBindingRepository keyBindingRepository;
private final KeyBindingRepository keyBindingRepository;

public ActionFactory(KeyBindingRepository keyBindingRepository) {
this.keyBindingRepository = Objects.requireNonNull(keyBindingRepository);
Expand Down
Loading

0 comments on commit 21355ab

Please sign in to comment.