Skip to content

Commit

Permalink
Add export current theme JabRef#5790
Browse files Browse the repository at this point in the history
* Add method for saving theme to file

* Add modal for selection witch theme to export as CSS
  • Loading branch information
nilsstre committed Feb 27, 2020
1 parent 198be47 commit 00eac4e
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/main/java/org/jabref/gui/preferences/ExportThemeDialog.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<?import javafx.geometry.Insets?>
<DialogPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="org.jabref.gui.preferences.ExportThemeDialog"
prefHeight="300.0" prefWidth="500.0">

<content>
<BorderPane>
<center>
<TableView fx:id="table">
<columns>
<TableColumn fx:id="columnName" text="%Theme name"/>
<TableColumn fx:id="columnPath" text="%Path to theme"/>
</columns>
<padding>
<Insets bottom="30.0"/>
</padding>
</TableView>
</center>
</BorderPane>
</content>
<ButtonType fx:constant="CLOSE"/>
</DialogPane>
121 changes: 121 additions & 0 deletions src/main/java/org/jabref/gui/preferences/ExportThemeDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package org.jabref.gui.preferences;

import com.airhacks.afterburner.views.ViewLoader;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import org.jabref.JabRefException;
import org.jabref.gui.DialogService;
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.gui.util.ThemeLoader;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.StandardFileType;
import org.jabref.preferences.JabRefPreferences;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExportThemeDialog extends BaseDialog<Void> {

private static final Logger LOGGER = LoggerFactory.getLogger(ExportThemeDialog.class);

@FXML private TableView<Theme> table;
@FXML private TableColumn<Theme, String> columnName;
@FXML private TableColumn<Theme, String> columnPath;

private JabRefPreferences preferences;
private DialogService dialogService;

public ExportThemeDialog(DialogService dialogService, JabRefPreferences preferences) {
this.dialogService = dialogService;
this.preferences = preferences;

ViewLoader
.view(this)
.load()
.setAsDialogPane(this);

this.setTitle(Localization.lang("Export Theme"));
}

@FXML
private void initialize() {
columnName.setCellValueFactory(new PropertyValueFactory<>("name"));
columnPath.setCellValueFactory(new PropertyValueFactory<>("path"));

ObservableList<Theme> data =
FXCollections.observableArrayList(new Theme("Light theme", ThemeLoader.MAIN_CSS), new Theme("Dark theme", ThemeLoader.DARK_CSS));

if (!(ThemeLoader.CUSTOM_CSS.isBlank() || ThemeLoader.CUSTOM_CSS.isEmpty())) {
data.add(new Theme("Custom theme", ThemeLoader.CUSTOM_CSS));
}

table.setItems(data);

table.setOnKeyPressed(event -> {
TablePosition tablePosition;
if (event.getCode().equals(KeyCode.ENTER)) {
tablePosition = table.getFocusModel().getFocusedCell();
final int row = tablePosition.getRow();
ObservableList<Theme> list = table.getItems();
Theme theme = list.get(row);
exportCSSFile(theme.getPath());
}
});

table.setRowFactory(tv -> {
TableRow<Theme> row = new TableRow<>();
row.setOnMouseClicked(event -> handleSelectedRowEvent(row));
return row;
});
}

private void handleSelectedRowEvent(TableRow<Theme> row) {
if (!row.isEmpty()) {
exportCSSFile(row.getItem().getPath());
}
}

private void exportCSSFile(String theme) {
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilter(StandardFileType.CSS)
.withDefaultExtension(StandardFileType.CSS)
.withInitialDirectory(preferences.setLastPreferencesExportPath())
.build();

dialogService.showFileSaveDialog(fileDialogConfiguration)
.ifPresent(exportFile -> {
try {
preferences.exportTheme(exportFile.getFileName(), theme);
} catch (JabRefException ex) {
LOGGER.warn(ex.getMessage(), ex);
dialogService.showErrorDialogAndWait(Localization.lang("Export theme"), ex);
}
});
}

public static class Theme {
private SimpleStringProperty name;
private SimpleStringProperty path;

public Theme(String name, String path) {
this.name = new SimpleStringProperty(name);
this.path = new SimpleStringProperty(path);
}

public String getName() {
return name.get();
}

public String getPath() {
return path.get();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@
</Button>
<Button maxWidth="Infinity" onAction="#showAllPreferences" text="%Show preferences"/>
<Button maxWidth="Infinity" onAction="#resetPreferences" text="%Reset preferences"/>
<Button maxWidth="Infinity" onAction="#importStyle" text="%Import CSS file">
<Button maxWidth="Infinity" onAction="#importTheme" text="%Import theme">
<tooltip>
<Tooltip text="%Import custom CSS file" />
</tooltip>
</Button>
<Button maxWidth="Infinity" onAction="#exportTheme" text="%Export theme">
<tooltip>
<Tooltip text="%Export theme as CSS" />
</tooltip>
</Button>
</VBox>
</VBox>
<ScrollPane fx:id="preferencesContainer" maxHeight="Infinity" maxWidth="Infinity"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,8 @@ private void savePreferencesAndCloseDialog() {
void resetPreferences() { viewModel.resetPreferences(); }

@FXML
void importStyle() {viewModel.importCSSFile();}
void importTheme() {viewModel.importCSSFile();}

@FXML
void exportTheme() {viewModel.openExportThemeDialog();}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,8 @@ public void importCSSFile() {
Localization.lang("You must restart JabRef for this to come into effect."));
});
}

public void openExportThemeDialog() {
new ExportThemeDialog(dialogService, preferences).showAndWait();
}
}
22 changes: 22 additions & 0 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.Reader;
import java.io.StringReader;
import java.net.InetAddress;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -37,6 +38,7 @@
import org.jabref.Globals;
import org.jabref.JabRefException;
import org.jabref.JabRefMain;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.SidePaneType;
import org.jabref.gui.autocompleter.AutoCompleteFirstNameMode;
import org.jabref.gui.autocompleter.AutoCompletePreferences;
Expand Down Expand Up @@ -69,6 +71,7 @@
import org.jabref.logic.exporter.TemplateExporter;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.fetcher.DoiFetcher;
import org.jabref.logic.importer.fileformat.endnote.Url;
import org.jabref.logic.journals.JournalAbbreviationLoader;
import org.jabref.logic.journals.JournalAbbreviationPreferences;
import org.jabref.logic.l10n.Language;
Expand Down Expand Up @@ -1391,6 +1394,25 @@ public void importPreferences(Path file) throws JabRefException {
}
}

public void exportTheme(Path targetFile, String theme) throws JabRefException {
try (OutputStream os = Files.newOutputStream(targetFile)) {
if (theme.equals(ThemeLoader.MAIN_CSS) || theme.equals(ThemeLoader.DARK_CSS)) {
Path path = new File(JabRefFrame.class.getResource(theme).toURI()).toPath();
writeThemeToFile(path, os);
} else {
Path path = new File(theme).toPath();
writeThemeToFile(path, os);
}
} catch (IOException | URISyntaxException ex) {
throw new JabRefException("Could not export theme", Localization.lang("Could not export theme"),
ex);
}
}

private void writeThemeToFile(Path path, OutputStream os) throws IOException {
Files.copy(path, os);
}

/**
* ONLY FOR TESTING!
*
Expand Down

0 comments on commit 00eac4e

Please sign in to comment.