From fa2709efc03666a2b86f0d229b0d3150ff31e64a Mon Sep 17 00:00:00 2001 From: bancika Date: Sat, 8 Jan 2011 18:37:11 +0000 Subject: [PATCH] moved logic to component processor --- .../diylc/presenter/ComponentProcessor.java | 55 +++++++++++++++++++ .../src/org/diylc/presenter/Presenter.java | 52 +----------------- 2 files changed, 58 insertions(+), 49 deletions(-) diff --git a/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/presenter/ComponentProcessor.java b/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/presenter/ComponentProcessor.java index e18f8752..a116186f 100644 --- a/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/presenter/ComponentProcessor.java +++ b/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/presenter/ComponentProcessor.java @@ -1,14 +1,23 @@ package org.diylc.presenter; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.image.BufferedImage; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.swing.Icon; +import javax.swing.ImageIcon; + import org.apache.log4j.Logger; +import org.diylc.common.ComponentType; import org.diylc.common.PropertyWrapper; +import org.diylc.core.ComponentLayer; import org.diylc.core.IDIYComponent; +import org.diylc.core.annotations.ComponentDescriptor; import org.diylc.core.annotations.EditableProperty; import com.rits.cloning.Cloner; @@ -41,6 +50,52 @@ public class ComponentProcessor { this.propertyCache = new HashMap, List>(); } + public ComponentType createComponentTypeFrom(Class> 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. * diff --git a/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/presenter/Presenter.java b/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/presenter/Presenter.java index e9796114..600f1950 100644 --- a/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/presenter/Presenter.java +++ b/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/presenter/Presenter.java @@ -5,14 +5,12 @@ import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Graphics2D; -import java.awt.Image; import java.awt.Point; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.geom.Area; import java.awt.geom.Point2D; -import java.awt.image.BufferedImage; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collections; @@ -24,8 +22,6 @@ import java.util.Map; import java.util.Set; import javax.swing.Action; -import javax.swing.Icon; -import javax.swing.ImageIcon; import javax.swing.JComponent; import org.apache.log4j.Logger; @@ -41,7 +37,6 @@ import org.diylc.core.ComponentLayer; import org.diylc.core.ComponentState; import org.diylc.core.IDIYComponent; import org.diylc.core.Project; -import org.diylc.core.annotations.ComponentDescriptor; import org.diylc.core.measures.SizeUnit; import org.diylc.gui.IView; import org.diylc.utils.Constants; @@ -202,50 +197,9 @@ public class Presenter implements IPlugInPort { IDIYComponent.class); for (Class clazz : componentTypeClasses) { if (!Modifier.isAbstract(clazz.getModifiers())) { - ComponentDescriptor annotation = clazz.getAnnotation(ComponentDescriptor.class); - String name; - String description; - String category; - String namePrefix; - String author; - Icon icon; - Class instanceClass = (Class) 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); + ComponentType componentType = ComponentProcessor.getInstance() + .createComponentTypeFrom((Class>) clazz); + componentTypeMap.put(componentType.getInstanceClass(), componentType); List nestedList; if (componentTypes.containsKey(componentType.getCategory())) { nestedList = componentTypes.get(componentType.getCategory());