Skip to content

Commit

Permalink
Fix #195 Use only @link annotation for Component and InnerComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
sbordes committed Apr 12, 2016
1 parent 1ddfc4c commit 6e639b4
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 90 deletions.
Expand Up @@ -35,7 +35,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface LinkComponent {
public @interface Link {

/**
* Define default string key part.
Expand Down

This file was deleted.

Expand Up @@ -24,12 +24,12 @@

import org.jrebirth.af.api.annotation.AfterInit;
import org.jrebirth.af.api.annotation.BeforeInit;
import org.jrebirth.af.api.annotation.LinkComponent;
import org.jrebirth.af.api.annotation.LinkInnerComponent;
import org.jrebirth.af.api.annotation.Link;
import org.jrebirth.af.api.annotation.OnRelease;
import org.jrebirth.af.api.annotation.SkipAnnotation;
import org.jrebirth.af.api.command.Command;
import org.jrebirth.af.api.component.basic.Component;
import org.jrebirth.af.api.component.basic.InnerComponent;
import org.jrebirth.af.api.exception.CoreException;
import org.jrebirth.af.api.exception.CoreRuntimeException;
import org.jrebirth.af.api.facade.FacadeReady;
Expand Down Expand Up @@ -78,19 +78,50 @@ public static boolean canProcessAnnotation(final Class<? extends Component<?>> c
}

/**
* Inject component.
* Inject components.
*
* @param component the component
* @param component the parent component
*/
public static void injectComponent(final Component<?> component) {
injectComponent(component, false);
}

/**
* Inject inner components.
*
* @param component the parent component
*/
public static void injectInnerComponent(final Component<?> component) {
injectComponent(component, true);
}

// Retrieve all fields annotated with LinkComponent
for (final Field field : ClassUtility.getAnnotatedFields(component.getClass(), LinkComponent.class)) {
final String keyPart = field.getAnnotation(LinkComponent.class).value();
if (keyPart.isEmpty()) {
inject(component, field);
/**
* Inject component.
*
* @param component the component
* @param inner only manage innercomponent otherwise mange component
*/
private static void injectComponent(final Component<?> component, boolean inner) {

// Retrieve all fields annotated with Link
for (final Field field : ClassUtility.getAnnotatedFields(component.getClass(), Link.class)) {
final String keyPart = field.getAnnotation(Link.class).value();
if (inner) {
if (InnerComponent.class.isAssignableFrom(field.getType())) {
if (keyPart.isEmpty()) {
injectInnerComponent(component, field);
} else {
injectInnerComponent(component, field, keyPart);
}
}
} else {
inject(component, field, keyPart);
if (Component.class.isAssignableFrom(field.getType())) {
if (keyPart.isEmpty()) {
injectComponent(component, field);
} else {
injectComponent(component, field, keyPart);
}
}
}
}

Expand All @@ -104,7 +135,7 @@ public static void injectComponent(final Component<?> component) {
* @param keyParts the key parts
*/
@SuppressWarnings("unchecked")
private static void inject(final FacadeReady<?> component, final Field field, final Object... keyParts) {
private static void injectComponent(final FacadeReady<?> component, final Field field, final Object... keyParts) {

try {
if (Command.class.isAssignableFrom(field.getType())) {
Expand All @@ -124,31 +155,16 @@ private static void inject(final FacadeReady<?> component, final Field field, fi
* Inject Inner component.
*
* @param component the component
*/
public static void injectInnerComponent(final Component<?> component) {

// Retrieve all fields annotated with LinkInnerComponent
for (final Field field : ClassUtility.getAnnotatedFields(component.getClass(), LinkInnerComponent.class)) {
injectInner(component, field, field.getAnnotation(LinkInnerComponent.class).value());
}

}

/**
* Inject a component into the property of an other.
*
* @param component the component
* @param field the field
* @param keyParts the key parts
*/
@SuppressWarnings("unchecked")
private static void injectInner(final FacadeReady<?> component, final Field field, final Object... keyParts) {
private static void injectInnerComponent(final Component<?> component, final Field field, final Object... keyParts) {

final ParameterizedType innerComponentType = (ParameterizedType) field.getGenericType();
final Class<?> componentType = (Class<?>) innerComponentType.getActualTypeArguments()[0];

try {

ClassUtility.setFieldValue(field, component, InnerComponentBase.create((Class<Command>) componentType, keyParts));

} catch (IllegalArgumentException | CoreException e) {
Expand All @@ -157,6 +173,17 @@ private static void injectInner(final FacadeReady<?> component, final Field fiel

}

// /**
// * Inject a component into the property of an other.
// *
// * @param component the component
// * @param field the field
// * @param keyParts the key parts
// */
// private static void injectInner(final FacadeReady<?> component, final Field field, final Object... keyParts) {
//
// }

/**
* Parse all methods to search annotated methods that are attached to a lifecycle phase.
*
Expand Down
Expand Up @@ -20,7 +20,7 @@
import java.util.ArrayList;
import java.util.List;

import org.jrebirth.af.api.annotation.LinkComponent;
import org.jrebirth.af.api.annotation.Link;
import org.jrebirth.af.api.wave.Wave;
import org.jrebirth.af.core.ui.object.DefaultObjectModel;
import org.jrebirth.af.presentation.model.Slide;
Expand All @@ -40,7 +40,7 @@ public final class SlideMenuModel extends DefaultObjectModel<SlideMenuModel, Sli
private static final Logger LOGGER = LoggerFactory.getLogger(SlideMenuModel.class);

/** Store a strong reference to the service to avoid reloading. */
@LinkComponent
@Link
private PresentationService presentationService;

private final List<Slide> slides = new ArrayList<>();
Expand Down
Expand Up @@ -28,7 +28,7 @@
import javafx.scene.Node;
import javafx.scene.layout.StackPane;

import org.jrebirth.af.api.annotation.LinkComponent;
import org.jrebirth.af.api.annotation.Link;
import org.jrebirth.af.api.ui.Model;
import org.jrebirth.af.api.wave.Wave;
import org.jrebirth.af.core.key.Key;
Expand Down Expand Up @@ -65,7 +65,7 @@ public final class SlideStackModel extends DefaultModel<SlideStackModel, SlideSt
private SlideModel<SlideStep> selectedSlideModel;

/** Store a strong reference to the service to avoid reloading. */
@LinkComponent
@Link
private PresentationService presentationService;

private final AtomicBoolean menuShown = new AtomicBoolean(false);
Expand Down
Expand Up @@ -17,7 +17,7 @@
*/
package org.jrebirth.af.showcase.analyzer.ui.workbench;

import org.jrebirth.af.api.annotation.LinkInnerComponent;
import org.jrebirth.af.api.annotation.Link;
import org.jrebirth.af.api.component.basic.InnerComponent;
import org.jrebirth.af.api.wave.Wave;
import org.jrebirth.af.core.component.basic.InnerComponentEnum;
Expand All @@ -33,14 +33,14 @@
*/
public final class WorkbenchModel extends DefaultModel<WorkbenchModel, WorkbenchView> {

@LinkInnerComponent
static InnerComponent<ControlsModel> CONTROLS;// = InnerComponent.create(ControlsModel.class);
@Link
static InnerComponent<ControlsModel> CONTROLS;

@LinkInnerComponent
static InnerComponent<PropertiesModel> PROPERTIES;// = InnerComponent.create(PropertiesModel.class);
@Link
static InnerComponent<PropertiesModel> PROPERTIES;

@LinkInnerComponent
static InnerComponent<EditorModel> EDITOR;// = InnerComponent.create(EditorModel.class);
@Link
static InnerComponent<EditorModel> EDITOR;

// @formatter:off
enum Components implements InnerComponentEnum {
Expand Down
Expand Up @@ -20,7 +20,7 @@
import java.util.ArrayList;
import java.util.List;

import org.jrebirth.af.api.annotation.LinkComponent;
import org.jrebirth.af.api.annotation.Link;
import org.jrebirth.af.api.ui.ModuleModel;
import org.jrebirth.af.component.ui.stack.StackModel;
import org.jrebirth.af.core.ui.DefaultModel;
Expand All @@ -34,7 +34,7 @@ public final class MainModel extends DefaultModel<MainModel, MainView> {

private final List<ModuleModel> modules = new ArrayList<>();

@LinkComponent("DemoStack")
@Link("DemoStack")
private StackModel stackModel;

/**
Expand Down

0 comments on commit 6e639b4

Please sign in to comment.