Skip to content

Commit

Permalink
Customizable log font
Browse files Browse the repository at this point in the history
  • Loading branch information
joffrey-bion committed Feb 17, 2016
1 parent 00a5230 commit 74de2c7
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 77 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def mainClassName = "org.hildan.fxlog.FXLog"
repositories { mavenCentral() }

dependencies {
compile 'org.controlsfx:controlsfx:8.40.10'
compile 'commons-io:commons-io:2.4'
compile 'com.google.code.gson:gson:2.5'
compile 'org.jetbrains:annotations:13.0'
Expand Down
68 changes: 0 additions & 68 deletions src/main/java/org/hildan/fxlog/coloring/ColorizedRowFactory.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/org/hildan/fxlog/coloring/Colorizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ObservableList<StyleRule> getRules() {
* @param log
* the log on which to test the rules
*/
void applyTo(@NotNull Node node, @NotNull LogEntry log) {
public void applyTo(@NotNull Node node, @NotNull LogEntry log) {
node.setStyle(null);
for (StyleRule rule : styleRules) {
if (rule.applyTo(node, log)) {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/hildan/fxlog/coloring/StyleRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.paint.Color;

import org.hildan.fxlog.core.LogEntry;
Expand Down Expand Up @@ -68,8 +69,12 @@ boolean applyTo(@NotNull Node node, @NotNull LogEntry log) {
if (filter.getValue().test(log)) {
String style = "";
if (foreground.getValue() != null) {
// surprisingly, this is the property affecting the text's *foreground* color in a TableRow
style += "-fx-text-background-color: " + toString(foreground.getValue()) + "; ";
if (node instanceof TableRow) {
// surprisingly, this is the property affecting the text's *foreground* color in a TableRow
style += "-fx-text-background-color: " + toString(foreground.getValue()) + "; ";
} else {
style += "-fx-fill: " + toString(foreground.getValue()) + "; ";
}
}
if (background.getValue() != null) {
style += "-fx-background-color: " + toString(background.getValue()) + "; ";
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/hildan/fxlog/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.text.Font;

import org.hildan.fxlog.coloring.Colorizer;
import org.hildan.fxlog.columns.Columnizer;
Expand Down Expand Up @@ -47,6 +48,8 @@ public static Config getInstance() {

private final Property<Theme> currentTheme;

private final Property<Font> logsFont;

private final ObservableList<String> recentFiles;

private final ObservableList<Columnizer> columnizers;
Expand All @@ -60,6 +63,7 @@ public static Config getInstance() {
this.selectedColumnizerIndex = new SimpleIntegerProperty(0);
this.selectedColorizerIndex = new SimpleIntegerProperty(0);
this.currentTheme = new SimpleObjectProperty<>(Theme.LIGHT);
this.logsFont = new SimpleObjectProperty<>(Font.getDefault());
this.recentFiles = FXCollections.observableArrayList();
this.columnizers = FXCollections.observableArrayList();
this.colorizers = FXCollections.observableArrayList();
Expand All @@ -82,6 +86,18 @@ public void setCurrentTheme(@NotNull Theme currentTheme) {
this.currentTheme.setValue(currentTheme);
}

public Font getLogsFont() {
return logsFont.getValue();
}

public Property<Font> logsFontProperty() {
return logsFont;
}

public void setLogsFont(Font logsFont) {
this.logsFont.setValue(logsFont);
}

/**
* Returns the list of recent files' absolute paths.
*
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/hildan/fxlog/config/FxGson.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
Expand Down Expand Up @@ -51,6 +53,7 @@ static GsonBuilder builder() {
.registerTypeAdapter(IntegerProperty.class, new IntegerPropertySerializer())
.registerTypeAdapter(Property.class, new PropertySerializer())
.registerTypeAdapter(Color.class, new ColorSerializer())
.registerTypeAdapter(Font.class, new FontSerializer())
.registerTypeAdapter(Pattern.class, new PatternSerializer());
}

Expand Down Expand Up @@ -183,6 +186,26 @@ public Color deserialize(JsonElement json, Type typeOfT, JsonDeserializationCont
}
}

private static class FontSerializer implements JsonSerializer<Font>, JsonDeserializer<Font> {
@Override
public JsonElement serialize(Font font, Type type, JsonSerializationContext context) {
String family = font.getFamily();
String weight = font.getStyle();
double size = font.getSize();
return new JsonPrimitive(family + "," + weight + "," + size);
}

@Override
public Font deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws
JsonParseException {
String[] font = json.getAsString().split(",");
String family = font[0];
FontWeight weight = FontWeight.findByName(font[1]);
double size = Double.parseDouble(font[2]);
return Font.font(family, weight, size);
}
}

private static class PatternSerializer implements JsonSerializer<Pattern>, JsonDeserializer<Pattern> {
@Override
public JsonElement serialize(Pattern pattern, Type type, JsonSerializationContext context) {
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/org/hildan/fxlog/controllers/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import com.sun.javafx.scene.control.skin.VirtualFlow;
import org.apache.commons.io.input.Tailer;
import org.hildan.fxlog.FXLog;
import org.hildan.fxlog.coloring.ColorizedRowFactory;
import org.hildan.fxlog.coloring.Colorizer;
import org.hildan.fxlog.columns.ColumnDefinition;
import org.hildan.fxlog.columns.Columnizer;
Expand All @@ -56,6 +55,7 @@
import org.hildan.fxlog.filtering.Filter;
import org.hildan.fxlog.themes.Css;
import org.hildan.fxlog.themes.Theme;
import org.hildan.fxlog.view.StyledTableCell;
import org.jetbrains.annotations.NotNull;

public class MainController implements Initializable {
Expand Down Expand Up @@ -221,16 +221,23 @@ private void configureFiltering() {
private void configureLogsTable() {
logsTable.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
if (columnizer.getValue() != null) {
logsTable.getColumns().addAll(columnizer.getValue().getColumns());
logsTable.getColumns().addAll(getConfiguredColumns(columnizer.getValue()));
}
columnizer.addListener((observable, oldValue, newValue) -> {
logsTable.getColumns().clear();
logsTable.getColumns().addAll(newValue.getColumns());
logsTable.getColumns().addAll(getConfiguredColumns(newValue));
});
logsTable.setItems(filteredLogs);
ColorizedRowFactory colorizedRowFactory = new ColorizedRowFactory();
colorizedRowFactory.colorizerProperty().bind(colorizer);
logsTable.setRowFactory(colorizedRowFactory);
}

private List<TableColumn<LogEntry, String>> getConfiguredColumns(Columnizer columnizer) {
List<TableColumn<LogEntry, String>> columns = columnizer.getColumns();
columns.forEach(col -> col.setCellFactory(column -> {
StyledTableCell cell = new StyledTableCell(column);
cell.colorizerProperty().bind(colorizer);
return cell;
}));
return columns;
}

/**
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/org/hildan/fxlog/view/StyledTableCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.hildan.fxlog.view;

import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.*;
import javafx.scene.text.Text;

import org.hildan.fxlog.coloring.Colorizer;
import org.hildan.fxlog.config.Config;
import org.hildan.fxlog.core.LogEntry;

/**
* A table cell that can be styled.
*/
public class StyledTableCell extends TableCell<LogEntry, String> {

private final Text text = new Text();

private final Property<Colorizer> colorizer = new SimpleObjectProperty<>();

public StyledTableCell(TableColumn<LogEntry, String> column) {
text.wrappingWidthProperty().bind(column.widthProperty());
text.fontProperty().bind(Config.getInstance().logsFontProperty());
setGraphic(text);
setText(null);
}

@Override
public void updateItem(String item, boolean empty) {
if (item == getItem()) {
return;
}
super.updateItem(item, empty);
if (item == null) {
setGraphic(null);
text.setText(null);
return;
}
setGraphic(text);
text.setText(item);
if (colorizer.getValue() != null) {
TableRow row = getTableRow();
if (row != null && row.getItem() != null) {
LogEntry log = (LogEntry) row.getItem();
colorizer.getValue().applyTo(text, log);
}
}
}

public Colorizer getColorizer() {
return colorizer.getValue();
}

public Property<Colorizer> colorizerProperty() {
return colorizer;
}

public void setColorizer(Colorizer colorizer) {
this.colorizer.setValue(colorizer);
}
}

0 comments on commit 74de2c7

Please sign in to comment.