Skip to content

Commit

Permalink
Refactor the UserInterfacePanel with MVC in mind (#1779)
Browse files Browse the repository at this point in the history
- refactor the UserInterfacePanel with MVC in mind
- remove the ":" from after "Theme:"
  • Loading branch information
madoar committed Jan 19, 2019
1 parent 2afa359 commit 2e6bb9a
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package org.phoenicis.javafx.components.setting.control;

import javafx.beans.property.*;
import javafx.collections.ObservableList;
import org.phoenicis.javafx.components.common.control.ControlBase;
import org.phoenicis.javafx.components.setting.skin.UserInterfacePanelSkin;
import org.phoenicis.javafx.views.common.themes.Theme;

/**
* A panel containing the user interface settings
*/
public class UserInterfacePanel extends ControlBase<UserInterfacePanel, UserInterfacePanelSkin> {
/**
* A list containing all available themes
*/
private final ObservableList<Theme> themes;

/**
* The currently selected theme. This value should never be null
*/
private final ObjectProperty<Theme> selectedTheme;

/**
* True if the source repository of a script should be shown
*/
private final BooleanProperty showScriptSource;

/**
* The user interface scaling
*/
private final DoubleProperty scaling;

/**
* Callback for the restore settings button
*/
private final ObjectProperty<Runnable> onRestoreSettings;

/**
* Constructor
*
* @param themes A list containing all available themes
* @param selectedTheme The currently selected theme. This value should never be null
* @param showScriptSource True if the source repository of a script should be shown
* @param scaling The user interface scaling
* @param onRestoreSettings Callback for the restore settings button
*/
public UserInterfacePanel(ObservableList<Theme> themes, ObjectProperty<Theme> selectedTheme,
BooleanProperty showScriptSource, DoubleProperty scaling, ObjectProperty<Runnable> onRestoreSettings) {
super();

this.themes = themes;
this.selectedTheme = selectedTheme;
this.showScriptSource = showScriptSource;
this.scaling = scaling;
this.onRestoreSettings = onRestoreSettings;
}

/**
* Constructor
*
* @param themes A list containing all available themes
*/
public UserInterfacePanel(ObservableList<Theme> themes) {
this(themes, new SimpleObjectProperty<>(), new SimpleBooleanProperty(), new SimpleDoubleProperty(),
new SimpleObjectProperty<>());
}

/**
* {@inheritDoc}
*/
@Override
public UserInterfacePanelSkin createSkin() {
return new UserInterfacePanelSkin(this);
}

public ObservableList<Theme> getThemes() {
return this.themes;
}

public Theme getSelectedTheme() {
return this.selectedTheme.get();
}

public ObjectProperty<Theme> selectedThemeProperty() {
return this.selectedTheme;
}

public void setSelectedTheme(Theme selectedTheme) {
this.selectedTheme.set(selectedTheme);
}

public boolean isShowScriptSource() {
return this.showScriptSource.get();
}

public BooleanProperty showScriptSourceProperty() {
return this.showScriptSource;
}

public void setShowScriptSource(boolean showScriptSource) {
this.showScriptSource.set(showScriptSource);
}

public double getScaling() {
return this.scaling.get();
}

public DoubleProperty scalingProperty() {
return this.scaling;
}

public void setScaling(double scaling) {
this.scaling.set(scaling);
}

public Runnable getOnRestoreSettings() {
return this.onRestoreSettings.get();
}

public ObjectProperty<Runnable> onRestoreSettingsProperty() {
return this.onRestoreSettings;
}

public void setOnRestoreSettings(Runnable onRestoreSettings) {
this.onRestoreSettings.set(onRestoreSettings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.phoenicis.javafx.components.setting.skin;

import javafx.beans.binding.Bindings;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import org.phoenicis.javafx.components.common.skin.SkinBase;
import org.phoenicis.javafx.components.setting.control.UserInterfacePanel;
import org.phoenicis.javafx.views.common.themes.Theme;

import java.util.Optional;

import static org.phoenicis.configuration.localisation.Localisation.tr;

/**
* The skin for the {@link UserInterfacePanel} component
*/
public class UserInterfacePanelSkin extends SkinBase<UserInterfacePanel, UserInterfacePanelSkin> {
/**
* Constructor
*
* @param control The control belonging to the skin
*/
public UserInterfacePanelSkin(UserInterfacePanel control) {
super(control);
}

/**
* {@inheritDoc}
*/
@Override
public void initialise() {
final Text title = new Text(tr("User Interface Settings"));
title.getStyleClass().add("title");

final GridPane themeGrid = new GridPane();
themeGrid.getStyleClass().add("grid");
themeGrid.setHgap(20);
themeGrid.setVgap(10);

// change theme
final Text themeLabel = new Text(tr("Theme"));
themeLabel.getStyleClass().add("captionTitle");

final ComboBox<Theme> themeSelection = new ComboBox<>();
themeSelection.valueProperty().bindBidirectional(getControl().selectedThemeProperty());

Bindings.bindContent(themeSelection.getItems(), getControl().getThemes());

// view script sources
final Label showScriptSourceLabel = new Label(tr("View the scripts’ source repository"));
showScriptSourceLabel.getStyleClass().add("captionTitle");

final CheckBox showScriptSourceSelection = new CheckBox();
showScriptSourceSelection.selectedProperty().bindBidirectional(getControl().showScriptSourceProperty());

// scale UI
final Label scalingLabel = new Label(tr("Scale the user interface"));
scalingLabel.getStyleClass().add("captionTitle");

final Slider scalingSelection = new Slider();
scalingSelection.setMin(8);
scalingSelection.setMax(16);
scalingSelection.valueProperty().bindBidirectional(getControl().scalingProperty());

// restore default
final Button restoreDefaultButton = new Button(tr("Restore defaults (requires restart)"));
restoreDefaultButton.setOnAction(
event -> Optional.ofNullable(getControl().getOnRestoreSettings()).ifPresent(Runnable::run));

themeGrid.addRow(0, themeLabel, themeSelection);
themeGrid.addRow(1, showScriptSourceLabel, showScriptSourceSelection);
themeGrid.addRow(2, scalingLabel, scalingSelection);
themeGrid.add(restoreDefaultButton, 1, 3);

final VBox container = new VBox(title, themeGrid);

container.getStyleClass().add("containerConfigurationPane");

getChildren().setAll(container);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@

package org.phoenicis.javafx.views.mainwindow.settings;

import javafx.animation.PauseTransition;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.util.Duration;
import org.phoenicis.javafx.components.setting.control.AboutPanel;
import org.phoenicis.javafx.components.setting.control.FileAssociationsPanel;
import org.phoenicis.javafx.components.setting.control.NetworkPanel;
import org.phoenicis.javafx.components.setting.control.SettingsSidebar;
import org.phoenicis.javafx.components.setting.control.UserInterfacePanel;
import org.phoenicis.javafx.components.setting.utils.ApplicationBuildInformation;
import org.phoenicis.javafx.components.setting.utils.SettingsSidebarItem;
import org.phoenicis.javafx.settings.JavaFxSettingsManager;
import org.phoenicis.javafx.views.common.ThemeManager;
import org.phoenicis.javafx.views.common.themes.Theme;
import org.phoenicis.javafx.views.common.themes.Themes;
import org.phoenicis.javafx.views.mainwindow.ui.MainWindowView;
import org.phoenicis.repository.RepositoryManager;
import org.phoenicis.settings.SettingsManager;
Expand All @@ -37,6 +42,8 @@
import static org.phoenicis.configuration.localisation.Localisation.tr;

public class SettingsView extends MainWindowView<SettingsSidebar> {
private final PauseTransition pause = new PauseTransition(Duration.seconds(0.5));

private final String applicationName;
private final String applicationVersion;
private final String applicationGitRevision;
Expand Down Expand Up @@ -86,8 +93,7 @@ private void initializeSettingsItems() {
this.applicationBuildTimestamp);

this.settingsItems = FXCollections.observableArrayList(
new SettingsSidebarItem(
new UserInterfacePanel(this.javaFxSettingsManager, this.themeManager),
new SettingsSidebarItem(createUserInterfacePanel(),
"userInterfaceButton", tr("User Interface")),
new SettingsSidebarItem(
new RepositoriesPanel(this.settingsManager, this.repositoryManager),
Expand All @@ -98,4 +104,49 @@ private void initializeSettingsItems() {
new SettingsSidebarItem(new AboutPanel(this.opener, buildInformation), "aboutButton",
tr("About")));
}

private UserInterfacePanel createUserInterfacePanel() {
final UserInterfacePanel userInterfacePanel = new UserInterfacePanel(
FXCollections.observableArrayList(Themes.all()));

// set the initial values
userInterfacePanel.setScaling(javaFxSettingsManager.getScale());
userInterfacePanel.setSelectedTheme(
Themes.fromShortName(javaFxSettingsManager.getTheme()).orElse(Themes.STANDARD));
userInterfacePanel.setShowScriptSource(javaFxSettingsManager.isViewScriptSource());

userInterfacePanel.setOnRestoreSettings(javaFxSettingsManager::restoreDefault);

// react on changes
userInterfacePanel.scalingProperty().addListener((Observable invalidation) -> {
final double scaling = userInterfacePanel.getScaling();

this.pause.setOnFinished(event -> {
getTabPane().getScene().getRoot().setStyle(String.format("-fx-font-size: %.2fpt;", scaling));

javaFxSettingsManager.setScale(userInterfacePanel.getScaling());
javaFxSettingsManager.save();
});

this.pause.playFromStart();
});

userInterfacePanel.selectedThemeProperty().addListener((Observable invalidation) -> {
final Theme selectedTheme = userInterfacePanel.getSelectedTheme();

themeManager.setCurrentTheme(selectedTheme);

javaFxSettingsManager.setTheme(selectedTheme.getShortName());
javaFxSettingsManager.save();
});

userInterfacePanel.showScriptSourceProperty().addListener((Observable invalidation) -> {
final boolean showScriptSource = userInterfacePanel.isShowScriptSource();

javaFxSettingsManager.setViewScriptSource(showScriptSource);
javaFxSettingsManager.save();
});

return userInterfacePanel;
}
}
Loading

0 comments on commit 2e6bb9a

Please sign in to comment.