Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
#10 [api] Create new class 'FXMLView'.
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoghuman committed Dec 21, 2018
1 parent 94de8e3 commit 42b0dff
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 1 deletion.
1 change: 1 addition & 0 deletions release/Release_v0.1.0-PRERELEASE_2018-12-dd_HH-mm.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ TODO
#### Feature
#15 [test] Create unittests for the class 'FXMLValidator'.
#14 [internal] Create new class 'FXMLValidator'.
#10 [api] Create new class 'FXMLView'.
#9 [api] Create new class 'FXMLPresenterData'.
#8 [api] Create new interface 'FXMLPresenter'.
#6 [api] Move content from 'afterburner.fx' to this library.
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/github/naoghuman/lib/fxml/core/FXMLModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,16 @@ public void put(final String key, final Object value) {
data.put(key, value);
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("FXMLModel [").append("\n"); // NOI18N

sb.append(" data: ").append(data.toString()).append("\n"); // NOI18N

sb.append("]"); // NOI18N

return sb.toString();
}

}
230 changes: 230 additions & 0 deletions src/main/java/com/github/naoghuman/lib/fxml/core/FXMLView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
* Copyright (C) 2018 Naoghuman's dream
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.github.naoghuman.lib.fxml.core;

import com.github.naoghuman.lib.fxml.internal.DefaultFXMLValidator;
import java.io.IOException;
import java.net.URL;
import java.util.MissingResourceException;
import java.util.Optional;
import java.util.ResourceBundle;
import static java.util.ResourceBundle.getBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;

/**
*
* @since 0.1.0-PRERELEASE
* @version 0.1.0-PRERELEASE
* @author Naoghuman
*/
public final class FXMLView {

private final static String DEFAULT_SUFFIX_CSS = ".css"; // NOI18N
private final static String DEFAULT_SUFFIX_FXML = ".fxml"; // NOI18N
private final static String DEFAULT_SUFFIX_PRESENTER = "Presenter"; // NOI18N

/**
*
* @param presenter
* @return
* @since 0.1.0-PRERELEASE
* @version 0.1.0-PRERELEASE
* @author Naoghuman
*/
public static FXMLView create(final String presenter) {
return new FXMLView(presenter);
}

private FXMLLoader fxmlLoader;
private Object instance;
private String baseBundleName;
private String conventionalName;

private Optional<ResourceBundle> resourceBundle = Optional.empty();
private Optional<URL> urlForCSS = Optional.empty();
private Optional<URL> urlForFXML = Optional.empty();

private FXMLView(final String presenter) {
DefaultFXMLValidator.requireEndsWith(presenter, DEFAULT_SUFFIX_PRESENTER);

this.initializeResourceBundle(presenter);

this.initializePresenter(presenter);

this.initializeConventionalName();
this.initializeURLforCSS();
this.initializeURLforFXML();
this.initializeFXMLLoader();
}

private void initializeFXMLLoader() {
try {
fxmlLoader = new FXMLLoader();

fxmlLoader.setController(instance);
fxmlLoader.setLocation( this.getURLforFXML().get());
fxmlLoader.setResources(this.getResourceBundle().get());

fxmlLoader.load();
} catch (IOException ex) {
Logger.getLogger(FXMLView.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void initializePresenter(final String presenter) {
try {
instance = Class.forName(presenter).newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
Logger.getLogger(FXMLView.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void initializeConventionalName() {
conventionalName = this.getInstance().getClass().getSimpleName();
conventionalName = conventionalName.substring(0, conventionalName.lastIndexOf(DEFAULT_SUFFIX_PRESENTER));
conventionalName = conventionalName.toLowerCase();
}

private void initializeResourceBundle(final String presenter) {
baseBundleName = presenter.substring(0, presenter.lastIndexOf(DEFAULT_SUFFIX_PRESENTER));
baseBundleName = baseBundleName.toLowerCase();

try {
resourceBundle = Optional.ofNullable(getBundle(baseBundleName));
} catch (MissingResourceException ex) {
}
}

private void initializeURLforCSS() {
urlForCSS = Optional.ofNullable(this.getInstance().getClass().getResource(this.getConventionalName() + DEFAULT_SUFFIX_CSS));
}

private void initializeURLforFXML() {
urlForFXML = Optional.ofNullable(this.getInstance().getClass().getResource(this.getConventionalName() + DEFAULT_SUFFIX_FXML));
}

/**
*
* @return
* @since 0.1.0-PRERELEASE
* @version 0.1.0-PRERELEASE
* @author Naoghuman
*/
public String getBaseBundleName() {
return baseBundleName;
}

/**
*
* @return
* @since 0.1.0-PRERELEASE
* @version 0.1.0-PRERELEASE
* @author Naoghuman
*/
public String getConventionalName() {
return conventionalName;
}

private Object getInstance() {
return instance;
}

/**
*
* @return
* @since 0.1.0-PRERELEASE
* @version 0.1.0-PRERELEASE
* @author Naoghuman
*/
public FXMLPresenter getPresenter() {
return (FXMLPresenter) fxmlLoader.getController();
}

/**
*
* @return
* @since 0.1.0-PRERELEASE
* @version 0.1.0-PRERELEASE
* @author Naoghuman
*/
public Optional<ResourceBundle> getResourceBundle() {
return resourceBundle;
}

/**
*
* @return
* @since 0.1.0-PRERELEASE
* @version 0.1.0-PRERELEASE
* @author Naoghuman
*/
public Optional<Parent> getView() {
Optional<Parent> parent = Optional.empty();
if (fxmlLoader != null) {
parent = Optional.ofNullable(fxmlLoader.getRoot());
}

if (parent.isPresent() && urlForCSS.isPresent()) {
parent.get().getStylesheets().add(urlForCSS.get().toExternalForm());
}

return parent;
}

/**
*
* @return
* @since 0.1.0-PRERELEASE
* @version 0.1.0-PRERELEASE
* @author Naoghuman
*/
public Optional<URL> getURLforCSS() {
return urlForCSS;
}

/**
*
* @return
* @since 0.1.0-PRERELEASE
* @version 0.1.0-PRERELEASE
* @author Naoghuman
*/
public Optional<URL> getURLforFXML() {
return urlForFXML;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("FXMLVIEW [").append("\n"); // NOI18N

sb.append(" presenter : ").append(this.getInstance().getClass().getName()).append("\n"); // NOI18N
sb.append(" conventionalName: ").append(this.getConventionalName()).append("\n"); // NOI18N
sb.append(" baseBundleName : ").append(this.getBaseBundleName()).append("\n"); // NOI18N
sb.append(" urlForCSS : ").append(this.getURLforCSS().isPresent() ? this.getURLforCSS().get().toString() : "<NOT-DEFINED>").append("\n"); // NOI18N
sb.append(" urlForFXML : ").append(this.getURLforFXML().isPresent() ? this.getURLforFXML().get().toString() : "<NOT-DEFINED>").append("\n"); // NOI18N
sb.append(" parent : ").append(this.getView().isPresent() ? this.getView().get().toString() : "<NOT-DEFINED>").append("\n"); // NOI18N

sb.append("]"); // NOI18N

return sb.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public static <T> void requireNonNull(final T value) throws NullPointerException
* </ul>
*
* @param value the attribute which should be validated.
* @throws NullPointerException if {@code (value == NULL)}.
* @throws IllegalArgumentException if {@code (value.trim() == EMPTY)}.
* @throws NullPointerException if {@code (value == NULL)}.
* @since 0.1.0-PRERELEASE
* @version 0.1.0-PRERELEASE
* @author Naoghuman
Expand All @@ -105,4 +105,30 @@ public static void requireNonNullAndNotEmpty(final String value) throws NullPoin
}
}

/**
* Checks if the {@code value} ends with the given {@code suffix}.
* <p>
* An additional error message will be added to the error stack:
* <ul>
* <li>"The attribute [value] must ends with the suffix: %s"</li>
* </ul>
*
* @param value the String which should be checked.
* @param suffix the suffix which must append at the end from the {@code value}.
* @throws IllegalArgumentException if {@code ((value.trim() || suffxi.trim() == EMPTY) || (!value.endsWith(suffix)))}.
* @throws NullPointerException if {@code (value || suffix == NULL)}.
* @since 0.1.0-PRERELEASE
* @version 0.1.0-PRERELEASE
* @author Naoghuman
*/
public static void requireEndsWith(final String value, final String suffix) {
DefaultFXMLValidator.requireNonNullAndNotEmpty(value);
DefaultFXMLValidator.requireNonNullAndNotEmpty(suffix);

if (!value.endsWith(suffix)) {
throw new IllegalArgumentException(String.format(
"The attribute [value] must ends with the suffix: %s", suffix)); // NOI18N
}
}

}

0 comments on commit 42b0dff

Please sign in to comment.