| | private final List<MenuAction> mSubActions = new ArrayList<>(); |
| | |
| | + /** |
| | + * Provides a fluent interface around constructing actions so that duplication |
| | + * can be avoided. |
| | + */ |
| | + public static class Builder { |
| | + private String mText; |
| | + private String mAccelerator; |
| | + private String mIcon; |
| | + private EventHandler<ActionEvent> mHandler; |
| | + |
| | + /** |
| | + * Sets the text, icon, and accelerator for a given action identifier. |
| | + * See the messages properties file for details. |
| | + * |
| | + * @param id The identifier to look up in the properties file. |
| | + * @return An instance of {@link Builder} that can be built into an |
| | + * instance of {@link Action}. |
| | + */ |
| | + public Builder setId( final String id ) { |
| | + final var prefix = ACTION_PREFIX + id + "."; |
| | + final var text = prefix + "text"; |
| | + final var icon = prefix + "icon"; |
| | + final var accelerator = prefix + "accelerator"; |
| | + final var builder = setText( text ).setIcon( icon ); |
| | + |
| | + return Messages.containsKey( accelerator ) |
| | + ? builder.setAccelerator( Messages.get( accelerator ) ) |
| | + : builder; |
| | + } |
| | + |
| | + /** |
| | + * Sets the action text based on a resource bundle key. |
| | + * |
| | + * @param key The key to look up in the {@link Messages}. |
| | + * @return The corresponding value, or the key name if none found. |
| | + */ |
| | + private Builder setText( final String key ) { |
| | + mText = Messages.get( key, key ); |
| | + return this; |
| | + } |
| | + |
| | + private Builder setAccelerator( final String accelerator ) { |
| | + mAccelerator = accelerator; |
| | + return this; |
| | + } |
| | + |
| | + private Builder setIcon( final String iconKey ) { |
| | + assert iconKey != null; |
| | + |
| | + // If there's no icon associated with the icon key name, don't attempt |
| | + // to create a graphic for the icon, because it won't exist. |
| | + final var iconName = Messages.get( iconKey ); |
| | + mIcon = iconKey.equals( iconName ) ? "" : iconName; |
| | + |
| | + return this; |
| | + } |
| | + |
| | + public Builder setHandler( final EventHandler<ActionEvent> handler ) { |
| | + mHandler = handler; |
| | + return this; |
| | + } |
| | + |
| | + public Action build() { |
| | + return new Action( mText, mAccelerator, mIcon, mHandler ); |
| | + } |
| | + } |
| | + |
| | + /** |
| | + * TODO: Reuse the {@link GenericBuilder}. |
| | + * |
| | + * @return The {@link Builder} for an instance of {@link Action}. |
| | + */ |
| | + public static Builder builder() { |
| | + return new Builder(); |
| | + } |
| | + |
| | + private static Button createIconButton( final String icon ) { |
| | + return new Button( null, createGraphic( icon ) ); |
| | + } |
| | + |
| | public Action( |
| | final String text, |
 |
| | @Override |
| | public Button createToolBarNode() { |
| | - final var button = createIconButton(); |
| | + final var button = createIconButton( mIcon ); |
| | var tooltip = mText; |
| | |
 |
| | |
| | return button; |
| | - } |
| | - |
| | - private Button createIconButton() { |
| | - return new Button( null, createGraphic( mIcon ) ); |
| | } |
| | |
 |
| | mSubActions.addAll( List.of( action ) ); |
| | return this; |
| | - } |
| | - |
| | - /** |
| | - * TODO: Reuse the {@link GenericBuilder}. |
| | - * |
| | - * @return The {@link Builder} for an instance of {@link Action}. |
| | - */ |
| | - public static Builder builder() { |
| | - return new Builder(); |
| | - } |
| | - |
| | - /** |
| | - * Provides a fluent interface around constructing actions so that duplication |
| | - * can be avoided. |
| | - */ |
| | - public static class Builder { |
| | - private String mText; |
| | - private String mAccelerator; |
| | - private String mIcon; |
| | - private EventHandler<ActionEvent> mHandler; |
| | - |
| | - /** |
| | - * Sets the text, icon, and accelerator for a given action identifier. |
| | - * See the messages properties file for details. |
| | - * |
| | - * @param id The identifier to look up in the properties file. |
| | - * @return An instance of {@link Builder} that can be built into an |
| | - * instance of {@link Action}. |
| | - */ |
| | - public Builder setId( final String id ) { |
| | - final var prefix = ACTION_PREFIX + id + "."; |
| | - final var text = prefix + "text"; |
| | - final var icon = prefix + "icon"; |
| | - final var accelerator = prefix + "accelerator"; |
| | - final var builder = setText( text ).setIcon( icon ); |
| | - |
| | - return Messages.containsKey( accelerator ) |
| | - ? builder.setAccelerator( Messages.get( accelerator ) ) |
| | - : builder; |
| | - } |
| | - |
| | - /** |
| | - * Sets the action text based on a resource bundle key. |
| | - * |
| | - * @param key The key to look up in the {@link Messages}. |
| | - * @return The corresponding value, or the key name if none found. |
| | - */ |
| | - private Builder setText( final String key ) { |
| | - mText = Messages.get( key, key ); |
| | - return this; |
| | - } |
| | - |
| | - private Builder setAccelerator( final String accelerator ) { |
| | - mAccelerator = accelerator; |
| | - return this; |
| | - } |
| | - |
| | - private Builder setIcon( final String iconKey ) { |
| | - assert iconKey != null; |
| | - |
| | - // If there's no icon associated with the icon key name, don't attempt |
| | - // to create a graphic for the icon, because it won't exist. |
| | - final var iconName = Messages.get( iconKey ); |
| | - mIcon = iconKey.equals( iconName ) ? "" : iconName; |
| | - |
| | - return this; |
| | - } |
| | - |
| | - public Builder setHandler( final EventHandler<ActionEvent> handler ) { |
| | - mHandler = handler; |
| | - return this; |
| | - } |
| | - |
| | - public Action build() { |
| | - return new Action( mText, mAccelerator, mIcon, mHandler ); |
| | - } |
| | } |
| | } |