moved logic to component processor

This commit is contained in:
bancika
2011-01-08 18:37:11 +00:00
parent 43d931a1e6
commit fa2709efc0
2 changed files with 58 additions and 49 deletions
@@ -1,14 +1,23 @@
package org.diylc.presenter; package org.diylc.presenter;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.diylc.common.ComponentType;
import org.diylc.common.PropertyWrapper; import org.diylc.common.PropertyWrapper;
import org.diylc.core.ComponentLayer;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import com.rits.cloning.Cloner; import com.rits.cloning.Cloner;
@@ -41,6 +50,52 @@ public class ComponentProcessor {
this.propertyCache = new HashMap<Class<?>, List<PropertyWrapper>>(); this.propertyCache = new HashMap<Class<?>, List<PropertyWrapper>>();
} }
public ComponentType createComponentTypeFrom(Class<? extends IDIYComponent<?>> clazz) {
String name;
String description;
String category;
String namePrefix;
String author;
Icon icon;
ComponentLayer layer;
boolean stretchable;
if (clazz.isAnnotationPresent(ComponentDescriptor.class)) {
ComponentDescriptor annotation = clazz.getAnnotation(ComponentDescriptor.class);
name = annotation.name();
description = annotation.desciption();
category = annotation.category();
namePrefix = annotation.instanceNamePrefix();
author = annotation.author();
layer = annotation.componentLayer();
stretchable = annotation.stretchable();
} else {
name = clazz.getSimpleName();
description = "";
category = "Uncategorized";
namePrefix = "Unknown";
author = "Unknown";
layer = ComponentLayer.COMPONENT;
stretchable = true;
}
icon = null;
// Draw component icon.
try {
IDIYComponent<?> componentInstance = (IDIYComponent<?>) clazz.newInstance();
Image image = new BufferedImage(Presenter.ICON_SIZE, Presenter.ICON_SIZE,
java.awt.image.BufferedImage.TYPE_INT_ARGB);
componentInstance.drawIcon((Graphics2D) image.getGraphics(), Presenter.ICON_SIZE,
Presenter.ICON_SIZE);
icon = new ImageIcon(image);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
ComponentType componentType = new ComponentType(name, description, category, namePrefix,
author, icon, clazz, layer, stretchable);
return componentType;
}
/** /**
* Extracts all editable properties from the component class. * Extracts all editable properties from the component class.
* *
@@ -5,14 +5,12 @@ import java.awt.Color;
import java.awt.Cursor; import java.awt.Cursor;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point; import java.awt.Point;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.RenderingHints; import java.awt.RenderingHints;
import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
import java.awt.geom.Area; import java.awt.geom.Area;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@@ -24,8 +22,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.swing.Action; import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent; import javax.swing.JComponent;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@@ -41,7 +37,6 @@ import org.diylc.core.ComponentLayer;
import org.diylc.core.ComponentState; import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.measures.SizeUnit; import org.diylc.core.measures.SizeUnit;
import org.diylc.gui.IView; import org.diylc.gui.IView;
import org.diylc.utils.Constants; import org.diylc.utils.Constants;
@@ -202,50 +197,9 @@ public class Presenter implements IPlugInPort {
IDIYComponent.class); IDIYComponent.class);
for (Class<?> clazz : componentTypeClasses) { for (Class<?> clazz : componentTypeClasses) {
if (!Modifier.isAbstract(clazz.getModifiers())) { if (!Modifier.isAbstract(clazz.getModifiers())) {
ComponentDescriptor annotation = clazz.getAnnotation(ComponentDescriptor.class); ComponentType componentType = ComponentProcessor.getInstance()
String name; .createComponentTypeFrom((Class<? extends IDIYComponent<?>>) clazz);
String description; componentTypeMap.put(componentType.getInstanceClass(), componentType);
String category;
String namePrefix;
String author;
Icon icon;
Class<? extends IDIYComponent> instanceClass = (Class<? extends IDIYComponent>) clazz;
ComponentLayer layer;
boolean stretchable;
if (annotation == null) {
name = clazz.getSimpleName();
description = "";
category = "Uncategorized";
namePrefix = "Unknown";
author = "Unknown";
layer = ComponentLayer.COMPONENT;
stretchable = true;
} else {
name = annotation.name();
description = annotation.desciption();
category = annotation.category();
namePrefix = annotation.instanceNamePrefix();
author = annotation.author();
layer = annotation.componentLayer();
stretchable = annotation.stretchable();
}
icon = null;
// Draw component icon.
try {
IDIYComponent componentInstance = (IDIYComponent) clazz.newInstance();
Image image = new BufferedImage(ICON_SIZE, ICON_SIZE,
java.awt.image.BufferedImage.TYPE_INT_ARGB);
componentInstance.drawIcon((Graphics2D) image.getGraphics(), ICON_SIZE,
ICON_SIZE);
icon = new ImageIcon(image);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
ComponentType componentType = new ComponentType(name, description, category,
namePrefix, author, icon, instanceClass, layer, stretchable);
componentTypeMap.put(instanceClass, componentType);
List<ComponentType> nestedList; List<ComponentType> nestedList;
if (componentTypes.containsKey(componentType.getCategory())) { if (componentTypes.containsKey(componentType.getCategory())) {
nestedList = componentTypes.get(componentType.getCategory()); nestedList = componentTypes.get(componentType.getCategory());