org.controlsfx.dialog.FontSelectorDialog Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of controlsfx Show documentation
Show all versions of controlsfx Show documentation
High quality UI controls and other tools to complement the core JavaFX distribution
/**
* Copyright (c) 2014 ControlsFX
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of ControlsFX, any associated website, nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL CONTROLSFX BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.controlsfx.dialog;
import static impl.org.controlsfx.i18n.Localization.asKey;
import static impl.org.controlsfx.i18n.Localization.localize;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import javafx.application.Platform;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.transformation.FilteredList;
import javafx.geometry.Pos;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.util.Callback;
public class FontSelectorDialog extends Dialog {
private FontPanel fontPanel;
public FontSelectorDialog(Font defaultFont) {
fontPanel = new FontPanel();
fontPanel.setFont(defaultFont);
setResultConverter(dialogButton -> dialogButton == ButtonType.OK ? fontPanel.getFont() : null);
final DialogPane dialogPane = getDialogPane();
setTitle(localize(asKey("font.dlg.title"))); //$NON-NLS-1$
dialogPane.setHeaderText(localize(asKey("font.dlg.header"))); //$NON-NLS-1$
dialogPane.getStyleClass().add("font-selector-dialog"); //$NON-NLS-1$
dialogPane.getStylesheets().add(FontSelectorDialog.class.getResource("dialogs.css").toExternalForm()); //$NON-NLS-1$
dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
dialogPane.setContent(fontPanel);
}
/**************************************************************************
*
* Support classes
*
**************************************************************************/
/**
* Font style as combination of font weight and font posture.
* Weight does not have to be there (represented by null)
* Posture is required, null posture is converted to REGULAR
*/
private static class FontStyle implements Comparable {
private FontPosture posture;
private FontWeight weight;
public FontStyle( FontWeight weight, FontPosture posture ) {
this.posture = posture == null? FontPosture.REGULAR: posture;
this.weight = weight;
}
public FontStyle() {
this( null, null);
}
public FontStyle(String styles) {
this();
String[] fontStyles = (styles == null? "": styles.trim().toUpperCase()).split(" "); //$NON-NLS-1$ //$NON-NLS-2$
for ( String style: fontStyles) {
FontWeight w = FontWeight.findByName(style);
if ( w != null ) {
weight = w;
} else {
FontPosture p = FontPosture.findByName(style);
if ( p != null ) posture = p;
}
}
}
public FontStyle(Font font) {
this( font.getStyle());
}
public FontPosture getPosture() {
return posture;
}
public FontWeight getWeight() {
return weight;
}
@Override public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((posture == null) ? 0 : posture.hashCode());
result = prime * result + ((weight == null) ? 0 : weight.hashCode());
return result;
}
@Override public boolean equals(Object that) {
if (this == that)
return true;
if (that == null)
return false;
if (getClass() != that.getClass())
return false;
FontStyle other = (FontStyle) that;
if (posture != other.posture)
return false;
if (weight != other.weight)
return false;
return true;
}
private static String makePretty(Object o) {
String s = o == null? "": o.toString(); //$NON-NLS-1$
if ( !s.isEmpty()) {
s = s.replace("_", " "); //$NON-NLS-1$ //$NON-NLS-2$
s = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
}
return s;
}
@Override public String toString() {
return String.format("%s %s", makePretty(weight), makePretty(posture) ).trim(); //$NON-NLS-1$
}
private > int compareEnums( T e1, T e2) {
if ( e1 == e2 ) return 0;
if ( e1 == null ) return -1;
if ( e2 == null ) return 1;
return e1.compareTo(e2);
}
@Override public int compareTo(FontStyle fs) {
int result = compareEnums(weight,fs.weight);
return ( result != 0 )? result: compareEnums(posture,fs.posture);
}
}
private static class FontPanel extends GridPane {
private static final double HGAP = 10;
private static final double VGAP = 5;
private static final Predicate