001package conexp.fx.gui.util; 002 003/* 004 * #%L 005 * Concept Explorer FX 006 * %% 007 * Copyright (C) 2010 - 2023 Francesco Kriegel 008 * %% 009 * This program is free software: you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as 011 * published by the Free Software Foundation, either version 3 of the 012 * License, or (at your option) any later version. 013 * 014 * This program is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public 020 * License along with this program. If not, see 021 * <http://www.gnu.org/licenses/gpl-3.0.html>. 022 * #L% 023 */ 024 025 026import conexp.fx.gui.ConExpFX; 027import javafx.beans.value.ChangeListener; 028import javafx.beans.value.ObservableValue; 029import javafx.event.ActionEvent; 030import javafx.event.EventHandler; 031import javafx.scene.control.Button; 032import javafx.scene.control.Control; 033import javafx.scene.control.TextField; 034import javafx.scene.input.KeyEvent; 035import javafx.scene.input.MouseEvent; 036import javafx.scene.layout.Region; 037 038public class SearchBox 039 extends Region 040{ 041 public final TextField textBox; 042 public final Button clearButton; 043 044 public SearchBox() 045 { 046 super(); 047 this.getStylesheets().add(ConExpFX.class.getResource("style/SearchBox.css").toExternalForm()); 048 this.setId("SearchBox"); 049 this.getStyleClass().add("search-box"); 050 this.setMinHeight(24); 051 this.setPrefSize(200, 24); 052 this.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE); 053 this.clearButton = new Button(); 054 clearButton.setVisible(false); 055 clearButton.setOnAction(new EventHandler<ActionEvent>() 056 { 057 public final void handle(final ActionEvent actionEvent) 058 { 059 textBox.setText(""); 060 textBox.requestFocus(); 061 } 062 }); 063 this.textBox = new TextField(); 064 textBox.setPromptText("Search"); 065 textBox.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() 066 { 067 public final void handle(final MouseEvent event) 068 { 069 textBox.requestFocus(); 070 } 071 }); 072 textBox.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() 073 { 074 @SuppressWarnings("incomplete-switch") 075 public final void handle(final KeyEvent event) 076 { 077 switch (event.getCode()) { 078 case ESCAPE: 079 textBox.setText(""); 080 textBox.requestFocus(); 081 break; 082 } 083 } 084 }); 085 textBox.textProperty().addListener(new ChangeListener<String>() 086 { 087 public void changed( 088 final ObservableValue<? extends String> observable, 089 final String oldValue, 090 final String newValue) 091 { 092 clearButton.setVisible(textBox.getText().length() != 0); 093 } 094 }); 095 this.getChildren().addAll(textBox, clearButton); 096 } 097 098 protected void layoutChildren() 099 { 100 textBox.resize(getWidth(), getHeight()); 101 clearButton.resizeRelocate(getWidth() - 18, 6, 12, 13); 102 } 103}