001/* 002 * ============================================================================ 003 * Copyright © 2002-2022 by Thomas Thrien. 004 * All Rights Reserved. 005 * ============================================================================ 006 * Licensed to the public under the agreements of the GNU Lesser General Public 007 * License, version 3.0 (the "License"). You may obtain a copy of the License at 008 * 009 * http://www.gnu.org/licenses/lgpl.html 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 014 * License for the specific language governing permissions and limitations 015 * under the License. 016 */ 017 018package org.tquadrat.foundation.fx.internal; 019 020import static org.apiguardian.api.API.Status.INTERNAL; 021import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument; 022import static org.tquadrat.foundation.lang.Objects.requireNotEmptyArgument; 023 024import java.net.URL; 025import java.util.Map; 026import java.util.Optional; 027import java.util.TreeMap; 028 029import org.apiguardian.api.API; 030import org.tquadrat.foundation.annotation.ClassVersion; 031import org.tquadrat.foundation.fx.beans.SceneUserData; 032import javafx.application.Application; 033import javafx.stage.Stage; 034 035/** 036 * The abstract base class for user data beans that can be used with several 037 * JavaFX entities. 038 * 039 * @param <A> The class of the JavaFX application. 040 * 041 * @version $Id: FXUserDataBean.java 1110 2024-03-04 15:26:06Z tquadrat $ 042 * @extauthor Thomas Thrien - thomas.thrien@tquadrat.org 043 * @UMLGraph.link 044 * @since 0.1.0 045 */ 046@SuppressWarnings( "AbstractClassWithoutAbstractMethods" ) 047@ClassVersion( sourceVersion = "$Id: FXUserDataBean.java 1110 2024-03-04 15:26:06Z tquadrat $" ) 048@API( status = INTERNAL, since = "0.1.0" ) 049public abstract sealed class FXUserDataBean<A extends Application> 050 permits SceneUserData 051{ 052 /*------------*\ 053 ====** Attributes **======================================================= 054 \*------------*/ 055 /** 056 * A reference to the application's main class. 057 */ 058 private final A m_Application; 059 060 /** 061 * The URL for the application's main CSS file. 062 */ 063 @SuppressWarnings( "OptionalUsedAsFieldOrParameterType" ) 064 private Optional<URL> m_ApplicationCSS = Optional.empty(); 065 066 /** 067 * A reference to the application's primary stage. 068 */ 069 private final Stage m_PrimaryStage; 070 071 /** 072 * The additional properties. 073 */ 074 private final Map<String,Object> m_Properties = new TreeMap<>(); 075 076 /*--------------*\ 077 ====** Constructors **===================================================== 078 \*--------------*/ 079 /** 080 * Creates a new {@code FXUserDataBean} instance. 081 * 082 * @param application The reference for the application's main class. 083 * @param primaryStage The reference for the application's primary 084 * stage. 085 */ 086 protected FXUserDataBean( final A application, final Stage primaryStage ) 087 { 088 m_Application = requireNonNullArgument( application, "application" ); 089 m_PrimaryStage = requireNonNullArgument( primaryStage, "primaryStage" ); 090 } // FXUserDataBean() 091 092 /*---------*\ 093 ====** Methods **========================================================== 094 \*---------*/ 095 /** 096 * Returns the reference to the application's main class. 097 * 098 * @return The application class. 099 */ 100 public final A getApplication() { return m_Application; } 101 102 /** 103 * Returns the URL for tha application's main CSS file. 104 * 105 * @return An instance of 106 * {@link Optional} 107 * that holds the CSS URL. 108 */ 109 public final Optional<URL> getApplicationCSS() { return m_ApplicationCSS; } 110 111 /** 112 * Returns the reference to the application's primary stage. 113 * 114 * @return The primary stage 115 */ 116 public final Stage getPrimaryStage() { return m_PrimaryStage; } 117 118 /** 119 * Returns the property with the given name. 120 * 121 * @param name The name of the property. 122 * @return An instance of 123 * {@link Optional} 124 * that holds the property. 125 */ 126 public final Optional<Object> getProperty( final String name ) 127 { 128 final var retValue = Optional.ofNullable( m_Properties.get( requireNotEmptyArgument( name, "name" ) ) ); 129 130 //---* Done *---------------------------------------------------------- 131 return retValue; 132 } // getProperty() 133 134 /** 135 * Checks whether a property with the given name exists. 136 * 137 * @param name The name of the property. 138 * @return {@code true} if there is a property with the given name (that 139 * can still be {@code null}), {@code false} otherwise. 140 */ 141 public final boolean hasProperty( final String name ) { return m_Properties.containsKey( requireNotEmptyArgument( name, "name" ) ); } 142 143 /** 144 * Removes the property with the given name; nothing happens if there is 145 * no property with the given name. 146 * 147 * @param name The name of the property. 148 */ 149 public final void removeProperty( final String name ) { m_Properties.remove( requireNotEmptyArgument( name, "name" ) ); } 150 151 /** 152 * Sets the URL for the application's CSS file. 153 * 154 * @param cssURL The URL for the CSS file. 155 */ 156 public final void setApplicationCSS( final URL cssURL ) { m_ApplicationCSS = Optional.of( requireNonNullArgument( cssURL, "cssURL" ) ); } 157 158 /** 159 * Sets the property with the given name to the given value. 160 * 161 * @param name The name of the property. 162 * @param value The new value of the property; this can be 163 * {@code null}. 164 */ 165 public final void setProperty( final String name, final Object value ) 166 { 167 m_Properties.put( requireNotEmptyArgument( name, "name" ), value ); 168 } // setProperty() 169} 170// class FXUserDataBean 171 172/* 173 * End of File 174 */