Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/************************************************************************
*
* Copyright (C) 2010 - 2014
*
* [FX2Util.java]
* JACPFX Project (https://github.com/JacpFX/JacpFX/)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
*
************************************************************************/
package org.jacpfx.rcp.util;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import org.jacpfx.api.annotations.Resource;
import org.jacpfx.api.component.Component;
import org.jacpfx.api.component.Injectable;
import org.jacpfx.api.component.SubComponent;
import org.jacpfx.api.context.JacpContext;
import org.jacpfx.rcp.registry.ComponentRegistry;
import org.jacpfx.rcp.registry.PerspectiveRegistry;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Util class with helper methods
*
* @author Andy Moncsek
*/
public class FXUtil {
public static final String AFXCOMPONENT_ROOT = "root";
public static final String ACOMPONENT_ACTIVE = "active";
public static final String ACOMPONENT_ID = "id";
public static final String ACOMPONENT_NAME = "name";
public static final String ACOMPONENT_EXTARGET = "executionTarget";
public static final String ACOMPONENT_BLOCKED = "blocked";
public static final String ACOMPONENT_STARTED = "started";
public static final String APERSPECTIVE_MQUEUE = "messageQueue";
public static final String IDECLARATIVECOMPONENT_VIEW_LOCATION = "viewLocation";
public static final String IDECLARATIVECOMPONENT_TYPE = "type";
public static final String IDECLARATIVECOMPONENT_DOCUMENT_URL = "documentURL";
public static final String IDECLARATIVECOMPONENT_LOCALE = "localeID";
public static final String IDECLARATIVECOMPONENT_BUNDLE_LOCATION = "resourceBundleLocation";
public static final String AFXPERSPECTIVE_PERSPECTIVE_LAYOUT = "perspectiveLayout";
private final static String PATTERN_LOCALE ="_";
private final static String PATTERN_SPLIT="\\.";
public final static String PATTERN_GLOBAL=".";
/**
* contains constant values
*
* @author Andy Moncsek
*/
public static class MessageUtil {
public static final String INIT = "init";
}
/**
* returns children of current node
*
* @param node , the node where you want to get the child list
* @return all children of that node
*/
@SuppressWarnings("unchecked")
public static ObservableList getChildren(final Node node) {
if (node instanceof Parent) {
final Parent tmp = (Parent) node;
Method protectedChildrenMethod;
ObservableList returnValue = null;
try {
protectedChildrenMethod = Parent.class
.getDeclaredMethod("getChildren");
protectedChildrenMethod.setAccessible(true);
returnValue = (ObservableList) protectedChildrenMethod
.invoke(tmp);
} catch (final NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(FXUtil.class.getName()).log(Level.SEVERE,
null, ex);
}
return returnValue;
}
return null;
}
/**
* Set a value to a private member on specified object
* @param superClass , the class
* @param object , the Object with the private member to be set
* @param member , the name of the member
* @param value , the vakue of the member
*/
public static void setPrivateMemberValue(final Class superClass,
final Object object, final String member, final Object value) {
try {
final Field privateStringField = superClass
.getDeclaredField(member);
privateStringField.setAccessible(true);
privateStringField.set(object, value);
} catch (final SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
Logger.getLogger(FXUtil.class.getName()).log(Level.SEVERE, null, e);
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
/**
* find annotated method in component and pass value
* @param annotation , the annotation to find
* @param component , the component with the annotated method
* @param value , the values to pass to the annotated method
*/
public static void invokeHandleMethodsByAnnotation(
final Class annotation, final Object component,
final Object... value) {
final Class componentClass = component.getClass();
final Method[] methods = componentClass.getMethods();
for (final Method m : methods) {
if (m.isAnnotationPresent(annotation)) {
try {
final Class[] types = m.getParameterTypes();
if (types.length == value.length) {
m.invoke(component, value);
return;
}
if (types.length > 0) {
m.invoke(component, getValidParameterList(types, value));
return;
}
m.invoke(component);
return;
} catch (final IllegalArgumentException e) {
throw new UnsupportedOperationException(
"use @PostConstruct and @PreDestroy either with paramter extending BaseLayout layout (like FXComponentLayout) or with no arguments ",
e.getCause());
} catch (final IllegalAccessException | InvocationTargetException e) {
Logger.getLogger(FXUtil.class.getName()).log(Level.SEVERE,
null, e);
}
break;
}
}
}
/**
* Injects all Resource memberc like Context
* @param handler , the component where injection should be performed
* @param context , the context object
*/
public static void performResourceInjection(final Injectable handler,JacpContext, Object> context) {
final Field[] fields = handler.getClass().getDeclaredFields();
final List fieldList = Arrays.asList(fields);
fieldList.stream().filter(f -> f.isAnnotationPresent(Resource.class)).forEach(f -> {
// context injection
if (f.getType().isAssignableFrom(context.getClass())) {
injectContext(handler, f, context);
} else if (context.getResourceBundle() != null && f.getType().isAssignableFrom(context.getResourceBundle().getClass())) {
injectResourceBundle(handler, f, context.getResourceBundle());
}
});
}
/**
*
* @param handler the component where injection should be performed
* @param f the field which should be injected
* @param context, the context object
*/
private static void injectContext(final Injectable handler,final Field f, final JacpContext context) {
f.setAccessible(true);
try {
f.set(handler, context);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* Injects the resource bundle to component
* @param handler the component where injection should be performed
* @param f the field which should be injected
* @param bundle the bundle that sould be injected
*/
private static void injectResourceBundle(final Injectable handler,final Field f, final ResourceBundle bundle) {
f.setAccessible(true);
try {
f.set(handler, bundle);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private static Object[] getValidParameterList(final Class[] types,
Object... value) {
final List