Skip to content

Commit

Permalink
Ported the KeyBindings dialog to JavaFX (JabRef#1390)
Browse files Browse the repository at this point in the history
* Initial port of the keybindings dialog to javafx

* Test javafx import order
  • Loading branch information
boceckts authored and Daniel Brühl committed Aug 29, 2016
1 parent 0aeb62d commit b431a6e
Show file tree
Hide file tree
Showing 34 changed files with 1,022 additions and 249 deletions.
12 changes: 12 additions & 0 deletions src/main/java/net/sf/jabref/gui/FXAlert.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
import java.awt.event.WindowEvent;

import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Alert;
import javafx.scene.image.Image;
import javafx.stage.Stage;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRefGUI;
import net.sf.jabref.gui.keyboard.KeyBinding;
import net.sf.jabref.gui.keyboard.KeyBindingPreferences;

/**
* This class shall provide a super class for future dialogs implemented in java fx.
Expand Down Expand Up @@ -67,6 +71,7 @@ public void windowGainedFocus(WindowEvent e) {
}
};


public FXAlert(AlertType type, String title, Image image) {
this(type, title);
setDialogIcon(image);
Expand All @@ -89,6 +94,13 @@ public FXAlert(AlertType type) {
fxDialogWindow.setOnHiding(evt -> setSwingWindowsEnabledAndFocusable(true));

fxDialogWindow.setOnCloseRequest(evt -> this.close());

fxDialogWindow.getScene().setOnKeyPressed(evt -> {
KeyBindingPreferences keyPreferences = Globals.getKeyPrefs();
if (keyPreferences.checkKeyCombinationEquality(KeyBinding.CLOSE_DIALOG, evt)) {
fxDialogWindow.close();
}
});
}

public void setDialogStyle(String pathToStyleSheet) {
Expand Down
135 changes: 135 additions & 0 deletions src/main/java/net/sf/jabref/gui/FXDialogs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/* Copyright (C) 2016 JabRef contributors.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package net.sf.jabref.gui;

import java.util.Optional;

import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;

/**
* This class provides static methods to create default
* JavaFX dialogs which will also work on top of swing
* windows. The created dialogs are instances of the
* {@link FXAlert} class. The available dialogs in this class
* are useful for displaying small information graphic dialogs
* rather than complex windows. For more complex dialogs it is
* advised to rather create a new sub class of {@link FXAlert}.
*
*/
public abstract class FXDialogs {

/**
* This will create and display a new information dialog.
* It will include a blue information icon on the left and
* a single OK Button. To create a information dialog with custom
* buttons see also {@link #showCustomButtonDialogAndWait(AlertType, String, String, ButtonType...)}
*
* @param title as String
* @param content as String
*/
public static void showInformationDialogAndWait(String title, String content) {
FXAlert alert = createDialog(AlertType.INFORMATION, title, content);
alert.showAndWait();
}

/**
* This will create and display a new information dialog.
* It will include a yellow warning icon on the left and
* a single OK Button. To create a warning dialog with custom
* buttons see also {@link #showCustomButtonDialogAndWait(AlertType, String, String, ButtonType...)}
*
* @param title as String
* @param content as String
*/
public static void showWarningDialogAndWait(String title, String content) {
FXAlert alert = createDialog(AlertType.WARNING, title, content);
alert.showAndWait();
}

/**
* This will create and display a new error dialog.
* It will include a red error icon on the left and
* a single OK Button. To create a error dialog with custom
* buttons see also {@link #showCustomButtonDialogAndWait(AlertType, String, String, ButtonType...)}
*
* @param title as String
* @param content as String
*/
public static void showErrorDialogAndWait(String title, String content) {
FXAlert alert = createDialog(AlertType.ERROR, title, content);
alert.showAndWait();
}

/**
* This will create and display a new confirmation dialog.
* It will include a blue question icon on the left and
* a OK and Cancel Button. To create a confirmation dialog with custom
* buttons see also {@link #showCustomButtonDialogAndWait(AlertType, String, String, ButtonType...)}
*
* @param title as String
* @param content as String
* @return Optional with the pressed Button as ButtonType
*/
public static Optional<ButtonType> showConfirmationDialogAndWait(String title, String content) {
FXAlert alert = createDialog(AlertType.CONFIRMATION, title, content);
return alert.showAndWait();
}

/**
* This will create and display a new dialog of the specified
* {@link AlertType} but with user defined buttons as optional
* {@link ButtonType}s.
*
* @param type as {@link AlertType}
* @param title as String
* @param content as String
* @param buttonTypes
* @return Optional with the pressed Button as ButtonType
*/
public static Optional<ButtonType> showCustomButtonDialogAndWait(AlertType type, String title, String content,
ButtonType... buttonTypes) {
FXAlert alert = createDialog(type, title, content);
alert.getButtonTypes().setAll(buttonTypes);
return alert.showAndWait();
}

/**
* This will create and display a new dialog showing a custom {@link DialogPane}
* and using custom {@link ButtonType}s.
*
* @param title as String
* @param contentPane as DialogPane
* @param buttonTypes as ButtonType
* @return Optional with the pressed Button as ButtonType
*/
public static Optional<ButtonType> showCustomDialogAndWait(String title, DialogPane contentPane,
ButtonType... buttonTypes) {
FXAlert alert = new FXAlert(AlertType.NONE, title);
alert.setDialogPane(contentPane);
alert.getButtonTypes().setAll(buttonTypes);
return alert.showAndWait();
}

private static FXAlert createDialog(AlertType type, String title, String content) {
FXAlert alert = new FXAlert(type, title);
alert.setHeaderText(null);
alert.setContentText(content);
return alert;
}

}
4 changes: 4 additions & 0 deletions src/main/java/net/sf/jabref/gui/IconTheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class IconTheme {

public static Font FONT;
public static Font FONT_16;
public static javafx.scene.text.Font FX_FONT;

/* Colors */

Expand All @@ -54,6 +55,9 @@ public class IconTheme {
try (InputStream stream = FontBasedIcon.class.getResourceAsStream("/fonts/materialdesignicons-webfont.ttf")) {
FONT = Font.createFont(Font.TRUETYPE_FONT, stream);
FONT_16 = FONT.deriveFont(Font.PLAIN, 16f);
try (InputStream stream2 = FontBasedIcon.class.getResourceAsStream("/fonts/materialdesignicons-webfont.ttf")) {
FX_FONT = javafx.scene.text.Font.loadFont(stream2, DEFAULT_SIZE);
}
} catch (FontFormatException | IOException e) {
LOGGER.warn("Error loading font", e);
}
Expand Down
23 changes: 3 additions & 20 deletions src/main/java/net/sf/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@
import net.sf.jabref.gui.importer.fetcher.GeneralFetcher;
import net.sf.jabref.gui.journals.ManageJournalsAction;
import net.sf.jabref.gui.keyboard.KeyBinding;
import net.sf.jabref.gui.keyboard.KeyBindingRepository;
import net.sf.jabref.gui.keyboard.KeyBindingsDialog;
import net.sf.jabref.gui.keyboard.KeyBindingAction;
import net.sf.jabref.gui.menus.ChangeEntryTypeMenu;
import net.sf.jabref.gui.menus.FileHistoryMenu;
import net.sf.jabref.gui.menus.RightClickMenu;
Expand Down Expand Up @@ -206,7 +205,7 @@ public class JabRefFrame extends JFrame implements OutputPrinter {
private final OpenDatabaseAction open = new OpenDatabaseAction(this, true);
private final EditModeAction editModeAction = new EditModeAction();
private final AbstractAction quit = new CloseAction();
private final AbstractAction selectKeys = new SelectKeysAction();
private final AbstractAction keyBindingAction = new KeyBindingAction();
private final AbstractAction newBibtexDatabaseAction = new NewDatabaseAction(this, BibDatabaseMode.BIBTEX);
private final AbstractAction newBiblatexDatabaseAction = new NewDatabaseAction(this, BibDatabaseMode.BIBLATEX);
private final AbstractAction openSharedDatabaseAction = new OpenSharedDatabaseAction(this);
Expand Down Expand Up @@ -1360,6 +1359,7 @@ private void fillMenu() {
options.add(customFileTypesAction);
options.add(manageJournals);
options.add(manageSelectors);
options.add(keyBindingAction);
options.add(protectTerms);
options.add(selectKeys);
mb.add(options);
Expand Down Expand Up @@ -1712,23 +1712,6 @@ public void createDisabledIconsForMenuEntries(MenuElement menuElement) {
}
}

private class SelectKeysAction extends AbstractAction {

public SelectKeysAction() {
super(Localization.lang("Customize key bindings"));
this.putValue(Action.SMALL_ICON, IconTheme.JabRefIcon.KEY_BINDINGS.getSmallIcon());
}

@Override
public void actionPerformed(ActionEvent e) {
KeyBindingsDialog d = new KeyBindingsDialog(new KeyBindingRepository(Globals.getKeyPrefs().getKeyBindings()));
d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
d.pack(); //setSize(300,500);
d.setLocationRelativeTo(JabRefFrame.this);
d.setVisible(true);
}
}

/**
* The action concerned with closing the window.
*/
Expand Down
Loading

0 comments on commit b431a6e

Please sign in to comment.