This commit is contained in:
Bane Stojkovic
2025-03-18 22:12:52 +01:00
parent f6863fb628
commit a5a4a3632f
2 changed files with 57 additions and 49 deletions
@@ -26,12 +26,8 @@ import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.lang.reflect.Modifier;
import java.util.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.apache.log4j.Logger;
@@ -47,6 +43,11 @@ import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.annotations.KeywordPolicy;
import org.diylc.lang.LangUtil;
import org.reflections.Reflections;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import static org.reflections.scanners.Scanners.TypesAnnotated;
/**
* Utility class with component processing methods.
@@ -63,6 +64,7 @@ public class ComponentProcessor {
private Map<String, IPropertyValidator> propertyValidatorCache;
private Map<String, ComponentType> componentTypeMap;
private Map<String, IComponentTransformer> componentTransformerMap;
private Map<String, List<ComponentType>> componentTypes;
public static ComponentProcessor getInstance() {
if (instance == null) {
@@ -218,6 +220,50 @@ public class ComponentProcessor {
return result;
}
public Map<String, List<ComponentType>> getComponentTypes() {
if (componentTypes == null) {
LOG.info("Loading component types.");
componentTypes = new HashMap<String, List<ComponentType>>();
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.forPackage("org.diylc")
.filterInputsBy(new FilterBuilder().includePackage("org.diylc.components"))
.setScanners(TypesAnnotated));
Set<Class<?>> componentTypeClasses = null;
try {
componentTypeClasses = reflections.getTypesAnnotatedWith(ComponentDescriptor.class, false);
for (Class<?> clazz : componentTypeClasses) {
if (!Modifier.isAbstract(clazz.getModifiers()) && IDIYComponent.class.isAssignableFrom(clazz)) {
ComponentType componentType =
ComponentProcessor.getInstance().extractComponentTypeFrom((Class<? extends IDIYComponent<?>>) clazz);
if (componentType == null)
continue;
// just to store in the cache
ComponentProcessor.getInstance().extractProperties(clazz);
List<ComponentType> nestedList;
if (componentTypes.containsKey(componentType.getCategory())) {
nestedList = componentTypes.get(componentType.getCategory());
} else {
nestedList = new ArrayList<ComponentType>();
componentTypes.put(componentType.getCategory(), nestedList);
}
nestedList.add(componentType);
}
}
for (Map.Entry<String, List<ComponentType>> e : componentTypes.entrySet()) {
LOG.debug(e.getKey() + ": " + e.getValue());
}
} catch (Exception e) {
LOG.error("Error loading component types", e);
}
}
return componentTypes;
}
/**
* Returns properties that have the same value for all the selected components.
*
@@ -136,6 +136,9 @@ public class Presenter implements IPlugInPort, IConfigListener {
private static final String APPLY_ERROR = LangUtil.translate("Could not apply changes. Check the log for details.");
private static final Logger LOG = Logger.getLogger(Presenter.class);
public static final Double[] ZOOM_LEVELS =
{0.25d, 0.3333d, 0.5d, 0.6667d, 0.75d, 1d, 1.25d, 1.5d, 2d, 2.5d, 3d};
public static VersionNumber CURRENT_VERSION = null;
private static List<Version> RECENT_VERSIONS = null;
@@ -149,7 +152,6 @@ public class Presenter implements IPlugInPort, IConfigListener {
private static final int MAX_RECENT_FILES = 20;
private Project currentProject;
private Map<String, List<ComponentType>> componentTypes;
// Maps component class names to ComponentType objects.
private List<IPlugIn> plugIns;
@@ -242,7 +244,7 @@ public class Presenter implements IPlugInPort, IConfigListener {
@Override
public java.lang.Double[] getAvailableZoomLevels() {
return new java.lang.Double[] {0.25d, 0.3333d, 0.5d, 0.6667d, 0.75d, 1d, 1.25d, 1.5d, 2d, 2.5d, 3d};
return ZOOM_LEVELS;
}
@Override
@@ -424,47 +426,7 @@ public class Presenter implements IPlugInPort, IConfigListener {
@SuppressWarnings("unchecked")
@Override
public Map<String, List<ComponentType>> getComponentTypes() {
if (componentTypes == null) {
LOG.info("Loading component types.");
componentTypes = new HashMap<String, List<ComponentType>>();
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.forPackage("org.diylc")
.filterInputsBy(new FilterBuilder().includePackage("org.diylc.components"))
.setScanners(TypesAnnotated));
Set<Class<?>> componentTypeClasses = null;
try {
componentTypeClasses = reflections.getTypesAnnotatedWith(ComponentDescriptor.class, false);
for (Class<?> clazz : componentTypeClasses) {
if (!Modifier.isAbstract(clazz.getModifiers()) && IDIYComponent.class.isAssignableFrom(clazz)) {
ComponentType componentType =
ComponentProcessor.getInstance().extractComponentTypeFrom((Class<? extends IDIYComponent<?>>) clazz);
if (componentType == null)
continue;
// just to store in the cache
ComponentProcessor.getInstance().extractProperties(clazz);
List<ComponentType> nestedList;
if (componentTypes.containsKey(componentType.getCategory())) {
nestedList = componentTypes.get(componentType.getCategory());
} else {
nestedList = new ArrayList<ComponentType>();
componentTypes.put(componentType.getCategory(), nestedList);
}
nestedList.add(componentType);
}
}
for (Map.Entry<String, List<ComponentType>> e : componentTypes.entrySet()) {
LOG.debug(e.getKey() + ": " + e.getValue());
}
} catch (Exception e) {
LOG.error("Error loading component types", e);
}
}
return componentTypes;
return ComponentProcessor.getInstance().getComponentTypes();
}
@SuppressWarnings({"unchecked"})