Finished implementing ClearableTextField (IMPORTANT! read description)

in order to use SceneBuilder further, you have to import the attached
JAR "CustomComponents.jar" into the SceneBuilder. If you don't do this,
Scenebuilder no longer can load FXML files that depend on a custom
component. If you are implementing another custom component, feel free
to add it to the jar.
Note however that SceneBuilder cannot load any components that rely on
libraries other than the standard Java library or the JavaFX standard.
Meaning that even if you are referencing another Envoy file, the
component will not be importable. Because of this, the
ClearableTextField is also present only in a slimmed down version, as
SceneBuilder additionally has problems when dealing with loading
resources.
This commit is contained in:
delvh 2020-06-28 22:44:34 +02:00
parent 28fe5cdb27
commit 589353a927
4 changed files with 27 additions and 118 deletions

View File

@ -1,16 +1,14 @@
package envoy.client.ui; package envoy.client.ui;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.ObjectProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.geometry.Insets; import javafx.event.ActionEvent;
import javafx.geometry.Pos; import javafx.event.EventHandler;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.layout.Background; import javafx.scene.layout.Background;
import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import envoy.client.data.Settings; import envoy.client.data.Settings;
@ -25,7 +23,6 @@ import envoy.client.data.Settings;
* @author Leon Hofmeister * @author Leon Hofmeister
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
@SuppressWarnings("javadoc")
public class ClearableTextField extends GridPane { public class ClearableTextField extends GridPane {
private final TextField textField; private final TextField textField;
@ -56,6 +53,7 @@ public class ClearableTextField extends GridPane {
Settings.getInstance().getCurrentTheme().equals("dark") ? "/icons/clear_button_white.png" : "/icons/clear_button_black.png", Settings.getInstance().getCurrentTheme().equals("dark") ? "/icons/clear_button_white.png" : "/icons/clear_button_black.png",
size))); size)));
clearButton.setOnAction(e -> textField.clear()); clearButton.setOnAction(e -> textField.clear());
clearButton.setFocusTraversable(false);
clearButton.getStyleClass().clear(); clearButton.getStyleClass().clear();
clearButton.setBackground(Background.EMPTY); clearButton.setBackground(Background.EMPTY);
// Adding the two elements to the GridPane // Adding the two elements to the GridPane
@ -64,17 +62,11 @@ public class ClearableTextField extends GridPane {
// Setting the percent - widths of the two columns. // Setting the percent - widths of the two columns.
// Used to locate the button on the right. // Used to locate the button on the right.
final var columnConstraints = new ColumnConstraints(); final var columnConstraints = new ColumnConstraints();
columnConstraints.setPercentWidth(85); columnConstraints.setPercentWidth(90);
getColumnConstraints().add(columnConstraints); getColumnConstraints().add(columnConstraints);
final var columnConstraints2 = new ColumnConstraints(); final var columnConstraints2 = new ColumnConstraints();
columnConstraints2.setPercentWidth(15); columnConstraints2.setPercentWidth(10);
columnConstraints2.setPrefWidth(size);
getColumnConstraints().add(columnConstraints2); getColumnConstraints().add(columnConstraints2);
setAlignment(Pos.CENTER);
setHgap(10);
setVgap(10);
setPadding(new Insets(5, 5, 5, 5));
setMargin(clearButton, new Insets(5));
} }
/** /**
@ -84,169 +76,84 @@ public class ClearableTextField extends GridPane {
public TextField getTextField() { return textField; } public TextField getTextField() { return textField; }
/** /**
* @return * This method offers the freedom to perform custom actions when the
* @see javafx.scene.control.TextInputControl#fontProperty() * {@code clearButton} has been pressed.
* <p>
* The default is
* <b><code> e -> {clearableTextField.getTextField().clear();}</code></b>
*
* @param onClearButtonAction the action that should be performed
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public final ObjectProperty<Font> fontProperty() { return textField.fontProperty(); } public void setClearButtonListener(EventHandler<ActionEvent> onClearButtonAction) { clearButton.setOnAction(onClearButtonAction); }
/** /**
* @return * @return the current property of the prompt text
* @see javafx.scene.control.TextField#prefColumnCountProperty()
* @since Envoy Client v0.1-beta
*/
public final IntegerProperty prefColumnCountProperty() { return textField.prefColumnCountProperty(); }
/**
* @return
* @see javafx.scene.control.TextField#getPrefColumnCount()
* @since Envoy Client v0.1-beta
*/
public final int getPrefColumnCount() { return textField.getPrefColumnCount(); }
/**
* @param value
* @see javafx.scene.control.TextField#setPrefColumnCount(int)
* @since Envoy Client v0.1-beta
*/
public final void setPrefColumnCount(int value) { textField.setPrefColumnCount(value); }
/**
* @return
* @see javafx.scene.control.Control#skinProperty()
* @since Envoy Client v0.1-beta
*/
public final ObjectProperty<Skin<?>> skinProperty() { return textField.skinProperty(); }
/**
* @param value
* @see javafx.scene.control.TextInputControl#setFont(javafx.scene.text.Font)
* @since Envoy Client v0.1-beta
*/
public final void setFont(Font value) { textField.setFont(value); }
/**
* @return
* @see javafx.scene.control.TextInputControl#getFont()
* @since Envoy Client v0.1-beta
*/
public final Font getFont() { return textField.getFont(); }
/**
* @return
* @see javafx.scene.control.TextInputControl#promptTextProperty() * @see javafx.scene.control.TextInputControl#promptTextProperty()
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public final StringProperty promptTextProperty() { return textField.promptTextProperty(); } public final StringProperty promptTextProperty() { return textField.promptTextProperty(); }
/** /**
* @return * @return the current prompt text
* @see javafx.scene.control.TextInputControl#getPromptText() * @see javafx.scene.control.TextInputControl#getPromptText()
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public final String getPromptText() { return textField.getPromptText(); } public final String getPromptText() { return textField.getPromptText(); }
/** /**
* @param value * @param value the prompt text to display
* @see javafx.scene.control.TextInputControl#setPromptText(java.lang.String) * @see javafx.scene.control.TextInputControl#setPromptText(java.lang.String)
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public final void setPromptText(String value) { textField.setPromptText(value); } public final void setPromptText(String value) { textField.setPromptText(value); }
/** /**
* @return * @return the current property of the tooltip
* @see javafx.scene.control.TextInputControl#getText()
* @since Envoy Client v0.1-beta
*/
public final String getText() { return textField.getText(); }
/**
* @param value
* @see javafx.scene.control.TextInputControl#setText(java.lang.String)
* @since Envoy Client v0.1-beta
*/
public final void setText(String value) { textField.setText(value); }
/**
* @return
* @see javafx.scene.control.TextInputControl#textProperty()
* @since Envoy Client v0.1-beta
*/
public final StringProperty textProperty() { return textField.textProperty(); }
/**
* @param value
* @see javafx.scene.control.TextInputControl#setEditable(boolean)
* @since Envoy Client v0.1-beta
*/
public final void setEditable(boolean value) { textField.setEditable(value); }
/**
* @return
* @see javafx.scene.control.Control#tooltipProperty() * @see javafx.scene.control.Control#tooltipProperty()
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public final ObjectProperty<Tooltip> tooltipProperty() { return textField.tooltipProperty(); } public final ObjectProperty<Tooltip> tooltipProperty() { return textField.tooltipProperty(); }
/** /**
* @param value * @param value the new tooltip
* @see javafx.scene.control.Control#setTooltip(javafx.scene.control.Tooltip) * @see javafx.scene.control.Control#setTooltip(javafx.scene.control.Tooltip)
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public final void setTooltip(Tooltip value) { textField.setTooltip(value); } public final void setTooltip(Tooltip value) { textField.setTooltip(value); }
/** /**
* @return * @return the current tooltip
* @see javafx.scene.control.Control#getTooltip() * @see javafx.scene.control.Control#getTooltip()
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public final Tooltip getTooltip() { return textField.getTooltip(); } public final Tooltip getTooltip() { return textField.getTooltip(); }
/** /**
* @return * @return the current property of the context menu
* @see javafx.scene.control.Control#contextMenuProperty() * @see javafx.scene.control.Control#contextMenuProperty()
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public final ObjectProperty<ContextMenu> contextMenuProperty() { return textField.contextMenuProperty(); } public final ObjectProperty<ContextMenu> contextMenuProperty() { return textField.contextMenuProperty(); }
/** /**
* @param value * @param value the new context menu
* @see javafx.scene.control.Control#setContextMenu(javafx.scene.control.ContextMenu) * @see javafx.scene.control.Control#setContextMenu(javafx.scene.control.ContextMenu)
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public final void setContextMenu(ContextMenu value) { textField.setContextMenu(value); } public final void setContextMenu(ContextMenu value) { textField.setContextMenu(value); }
/** /**
* @return * @return the current context menu
* @see javafx.scene.control.Control#getContextMenu() * @see javafx.scene.control.Control#getContextMenu()
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public final ContextMenu getContextMenu() { return textField.getContextMenu(); } public final ContextMenu getContextMenu() { return textField.getContextMenu(); }
/** /**
* @param minWidth * @param value whether this ClearableTextField should be editable
* @param minHeight * @see javafx.scene.control.TextInputControl#setEditable(boolean)
* @see javafx.scene.layout.Region#setMinSize(double, double)
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
@Override public final void setEditable(boolean value) { textField.setEditable(value); }
public void setMinSize(double minWidth, double minHeight) { textField.setMinSize(minWidth, minHeight); }
/**
* @param prefWidth
* @param prefHeight
* @see javafx.scene.layout.Region#setPrefSize(double, double)
* @since Envoy Client v0.1-beta
*/
@Override
public void setPrefSize(double prefWidth, double prefHeight) { textField.setPrefSize(prefWidth, prefHeight); }
/**
* @param maxWidth
* @param maxHeight
* @see javafx.scene.layout.Region#setMaxSize(double, double)
* @since Envoy Client v0.1-beta
*/
@Override
public void setMaxSize(double maxWidth, double maxHeight) { textField.setMaxSize(maxWidth, maxHeight); }
} }

View File

@ -59,6 +59,7 @@ public class ContactSearchScene {
@FXML @FXML
private void initialize() { private void initialize() {
contactList.setCellFactory(e -> new ContactListCell()); contactList.setCellFactory(e -> new ContactListCell());
searchBar.setClearButtonListener(e -> { searchBar.getTextField().clear(); contactList.getItems().clear(); });
eventBus.register(ContactSearchResult.class, eventBus.register(ContactSearchResult.class,
response -> Platform.runLater(() -> { contactList.getItems().clear(); contactList.getItems().addAll(response.get()); })); response -> Platform.runLater(() -> { contactList.getItems().clear(); contactList.getItems().addAll(response.get()); }));
} }

View File

@ -44,6 +44,7 @@ public class GroupCreationScene {
private void initialize() { private void initialize() {
contactList.setCellFactory(e -> new ContactListCell()); contactList.setCellFactory(e -> new ContactListCell());
contactList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); contactList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
groupNameField.setClearButtonListener(e -> { groupNameField.getTextField().clear(); createButton.setDisable(true); });
} }
/** /**

Binary file not shown.