refactored component framework

This commit is contained in:
bancika
2011-01-04 20:55:35 +00:00
parent 73da0f4a9b
commit a88f0d0a45
31 changed files with 309 additions and 317 deletions
@@ -2,7 +2,7 @@ package org.diylc.components;
import java.awt.Point; import java.awt.Point;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
import org.diylc.core.VisibilityPolicy; import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.BomName; import org.diylc.core.annotations.BomName;
import org.diylc.core.annotations.ControlPoint; import org.diylc.core.annotations.ControlPoint;
@@ -17,7 +17,7 @@ import org.diylc.utils.Constants;
* *
* @author Branislav Stojkovic * @author Branislav Stojkovic
*/ */
public abstract class AbstractLeadedComponentInstance implements IComponentInstance { public abstract class AbstractLeadedDIYComponent implements IDIYComponent {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -28,7 +28,7 @@ public abstract class AbstractLeadedComponentInstance implements IComponentInsta
protected Point labelPoint = new Point((int) (Constants.GRID * 5), -(int) Constants.GRID); protected Point labelPoint = new Point((int) (Constants.GRID * 5), -(int) Constants.GRID);
protected String name = "New Component"; protected String name = "New Component";
protected AbstractLeadedComponentInstance() { protected AbstractLeadedDIYComponent() {
super(); super();
this.width = getDefaultWidth(); this.width = getDefaultWidth();
this.height = getDefaultHeight(); this.height = getDefaultHeight();
@@ -119,7 +119,7 @@ public abstract class AbstractLeadedComponentInstance implements IComponentInsta
return false; return false;
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
AbstractLeadedComponentInstance other = (AbstractLeadedComponentInstance) obj; AbstractLeadedDIYComponent other = (AbstractLeadedDIYComponent) obj;
if (name == null) { if (name == null) {
if (other.name != null) if (other.name != null)
return false; return false;
@@ -1,48 +0,0 @@
package org.diylc.components;
import javax.swing.Icon;
import org.diylc.core.ComponentLayer;
import org.diylc.core.IComponentInstance;
import org.diylc.core.IComponentType;
import org.diylc.images.IconLoader;
public class MockComponentType implements IComponentType {
@Override
public Class<? extends IComponentInstance> getComponentInstanceClass() {
return MockComponentInstance.class;
}
@Override
public String getInstanceNamePrefix() {
return "ABC";
}
@Override
public ComponentLayer getComponentLayer() {
return ComponentLayer.BOARD;
}
@Override
public String getDescription() {
return "Some description";
}
@Override
public Icon getIcon() {
return IconLoader.Gears.getIcon();
}
@Override
public String getName() {
return "Mock Component";
}
@Override
public String getCategory() {
return "Samples";
}
}
@@ -5,10 +5,11 @@ import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import org.diylc.core.ComponentState; import org.diylc.core.ComponentState;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
import org.diylc.core.VisibilityPolicy; import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.BomName; import org.diylc.core.annotations.BomName;
import org.diylc.core.annotations.BomValue; import org.diylc.core.annotations.BomValue;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.ControlPoint; import org.diylc.core.annotations.ControlPoint;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Capacitance; import org.diylc.core.measures.Capacitance;
@@ -18,8 +19,8 @@ import org.diylc.core.measures.ResistanceUnit;
import org.diylc.core.measures.Size; import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit; import org.diylc.core.measures.SizeUnit;
@ComponentDescriptor(name = "Mock", author = "bancika", category = "Sample", instanceNamePrefix = "M", desciption = "test")
public class MockComponentInstance implements IComponentInstance { public class MockDIYComponent implements IDIYComponent {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -31,7 +32,7 @@ public class MockComponentInstance implements IComponentInstance {
private Point leftTopCorner = new Point(0, 0); private Point leftTopCorner = new Point(0, 0);
private String name = "something"; private String name = "something";
public MockComponentInstance(String testField, Capacitance c, Resistance r, Size s, public MockDIYComponent(String testField, Capacitance c, Resistance r, Size s,
Color color, Point leftTopCorner) { Color color, Point leftTopCorner) {
super(); super();
this.testField = testField; this.testField = testField;
@@ -42,7 +43,7 @@ public class MockComponentInstance implements IComponentInstance {
this.leftTopCorner = leftTopCorner; this.leftTopCorner = leftTopCorner;
} }
public MockComponentInstance() { public MockDIYComponent() {
super(); super();
} }
@@ -119,9 +120,7 @@ public class MockComponentInstance implements IComponentInstance {
} }
@Override @Override
public MockComponentInstance clone() throws CloneNotSupportedException { public void drawIcon(Graphics2D g2d, int width, int height) {
MockComponentInstance newInstance = new MockComponentInstance(testField, c.clone(), r g2d.drawString("X", 10, 10);
.clone(), s.clone(), color, (Point) leftTopCorner.clone());
return newInstance;
} }
} }
@@ -5,21 +5,22 @@ import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import org.diylc.core.ComponentState; import org.diylc.core.ComponentState;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Resistance; import org.diylc.core.measures.Resistance;
import org.diylc.core.measures.ResistanceUnit; import org.diylc.core.measures.ResistanceUnit;
import org.diylc.core.measures.Size; import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit; import org.diylc.core.measures.SizeUnit;
@ComponentDescriptor(name = "Resistor", author = "bancika", category = "Passive", instanceNamePrefix = "R", desciption = "test")
public class ResistorInstance extends AbstractLeadedComponentInstance { public class Resistor extends AbstractLeadedDIYComponent {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private Resistance r = new Resistance(123d, ResistanceUnit.K); private Resistance r = new Resistance(123d, ResistanceUnit.K);
private Color color = Color.green; private Color color = Color.green;
public ResistorInstance() { public Resistor() {
super(); super();
} }
@@ -40,6 +41,11 @@ public class ResistorInstance extends AbstractLeadedComponentInstance {
g2d.fillRect((point1.x + point2.x) / 2 - 10, (point1.y + point2.y) / 2 - 10, 20, 20); g2d.fillRect((point1.x + point2.x) / 2 - 10, (point1.y + point2.y) / 2 - 10, 20, 20);
} }
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
g2d.drawString("R", 10, 10);
}
@Override @Override
protected Size getDefaultHeight() { protected Size getDefaultHeight() {
return new Size(3d, SizeUnit.mm); return new Size(3d, SizeUnit.mm);
@@ -1,48 +0,0 @@
package org.diylc.components;
import javax.swing.Icon;
import org.diylc.core.ComponentLayer;
import org.diylc.core.IComponentInstance;
import org.diylc.core.IComponentType;
import org.diylc.images.IconLoader;
public class ResistorType implements IComponentType {
@Override
public Class<? extends IComponentInstance> getComponentInstanceClass() {
return ResistorInstance.class;
}
@Override
public String getInstanceNamePrefix() {
return "R";
}
@Override
public ComponentLayer getComponentLayer() {
return ComponentLayer.COMPONENT;
}
@Override
public String getDescription() {
return "Some description";
}
@Override
public Icon getIcon() {
return IconLoader.Gears.getIcon();
}
@Override
public String getName() {
return "Resistor";
}
@Override
public String getCategory() {
return "Samples";
}
}
@@ -57,7 +57,7 @@ public class DIYLCStarter {
// p.installPlugin(new CanvasPlugin()); // p.installPlugin(new CanvasPlugin());
// //
// Project project = new Project(); // Project project = new Project();
// project.getComponents().add(new MockComponentInstance()); // project.getComponents().add(new MockDIYComponent());
// p.loadProject(project); // p.loadProject(project);
// panel.setPreferredSize(new Dimension(2000, 1500)); // panel.setPreferredSize(new Dimension(2000, 1500));
// RulerScrollPane scrollPane = new RulerScrollPane(panel); // RulerScrollPane scrollPane = new RulerScrollPane(panel);
@@ -8,17 +8,17 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
/** /**
* Represents component selection as a {@link List} of * Represents component selection as a {@link List} of
* {@link IComponentInstance} objects. Implements {@link Transferable}, so it is * {@link IDIYComponent} objects. Implements {@link Transferable}, so it is
* suitable for clipboard usage. * suitable for clipboard usage.
* *
* @author Branislav Stojkovic * @author Branislav Stojkovic
*/ */
public class ComponentSelection extends ArrayList<IComponentInstance> implements Transferable { public class ComponentSelection extends ArrayList<IDIYComponent> implements Transferable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -28,7 +28,7 @@ public class ComponentSelection extends ArrayList<IComponentInstance> implements
super(); super();
} }
public ComponentSelection(Collection<IComponentInstance> selectedComponents) { public ComponentSelection(Collection<IDIYComponent> selectedComponents) {
super(selectedComponents); super(selectedComponents);
} }
@@ -4,7 +4,7 @@ import java.awt.Point;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
import org.diylc.core.VisibilityPolicy; import org.diylc.core.VisibilityPolicy;
@@ -44,7 +44,7 @@ public class ControlPointWrapper {
* @throws IllegalAccessException * @throws IllegalAccessException
* @throws InvocationTargetException * @throws InvocationTargetException
*/ */
public void readFrom(IComponentInstance component) throws IllegalArgumentException, public void readFrom(IDIYComponent component) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException { IllegalAccessException, InvocationTargetException {
value = (Point) getter.invoke(component); value = (Point) getter.invoke(component);
} }
@@ -58,7 +58,7 @@ public class ControlPointWrapper {
* @throws IllegalAccessException * @throws IllegalAccessException
* @throws InvocationTargetException * @throws InvocationTargetException
*/ */
public void writeTo(IComponentInstance component) throws IllegalArgumentException, public void writeTo(IDIYComponent component) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException { IllegalAccessException, InvocationTargetException {
setter.invoke(component, value); setter.invoke(component, value);
} }
@@ -2,8 +2,8 @@ package org.diylc.common;
import java.awt.Rectangle; import java.awt.Rectangle;
import org.diylc.core.IComponentType;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.presenter.ComponentType;
/** /**
@@ -44,7 +44,7 @@ public enum EventType {
REPAINT, REPAINT,
/** /**
* Called when new component slot has been changed. The only parameter is * Called when new component slot has been changed. The only parameter is
* {@link IComponentType} and may be null. * {@link ComponentType} and may be null.
*/ */
SLOT_CHANGED, SLOT_CHANGED,
/** /**
@@ -15,9 +15,9 @@ import javax.swing.SwingConstants;
import javax.swing.JPopupMenu.Separator; import javax.swing.JPopupMenu.Separator;
import org.diylc.core.ComponentLayer; import org.diylc.core.ComponentLayer;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
import org.diylc.core.IComponentType;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.presenter.ComponentType;
import com.diyfever.gui.simplemq.IMessageListener; import com.diyfever.gui.simplemq.IMessageListener;
import com.diyfever.gui.simplemq.MessageDispatcher; import com.diyfever.gui.simplemq.MessageDispatcher;
@@ -76,13 +76,13 @@ public interface IPlugInPort {
void loadProject(Project project, boolean freshStart); void loadProject(Project project, boolean freshStart);
/** /**
* Returns all available {@link IComponentType}s classified by category. * Returns all available {@link ComponentType}s classified by category.
* Result is a {@link Map} between category name to a {@link List} of all * Result is a {@link Map} between category name to a {@link List} of all
* {@link IComponentType}s that share that category name. * {@link ComponentType}s that share that category name.
* *
* @return * @return
*/ */
Map<String, List<IComponentType>> getComponentTypes(); Map<String, List<ComponentType>> getComponentTypes();
/** /**
* Draws project on the provided {@link Graphics2D}. * Draws project on the provided {@link Graphics2D}.
@@ -175,7 +175,7 @@ public interface IPlugInPort {
* *
* @return * @return
*/ */
Area getComponentArea(IComponentInstance component); Area getComponentArea(IDIYComponent component);
/** /**
* Notification that drag has been started from the specified point. * Notification that drag has been started from the specified point.
@@ -218,7 +218,7 @@ public interface IPlugInPort {
* @param components * @param components
* @param preferredPoint * @param preferredPoint
*/ */
void addComponents(List<IComponentInstance> components, Point preferredPoint); void addComponents(List<IDIYComponent> components, Point preferredPoint);
/** /**
* Checks if a certain layer is locked for editing. * Checks if a certain layer is locked for editing.
@@ -288,5 +288,5 @@ public interface IPlugInPort {
* Sets the new component slot. Specified component type will be used to * Sets the new component slot. Specified component type will be used to
* instantiate new component. * instantiate new component.
*/ */
void setNewComponentSlot(IComponentType componentType); void setNewComponentSlot(ComponentType componentType);
} }
@@ -3,7 +3,7 @@ package org.diylc.common;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
/** /**
@@ -31,12 +31,12 @@ public class PropertyWrapper implements Cloneable {
this.defaultable = defaultable; this.defaultable = defaultable;
} }
public void readFrom(IComponentInstance component) throws IllegalArgumentException, public void readFrom(IDIYComponent component) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException { IllegalAccessException, InvocationTargetException {
this.value = getter.invoke(component); this.value = getter.invoke(component);
} }
// public void readUniqueFrom(IComponentInstance component) // public void readUniqueFrom(IDIYComponent component)
// throws IllegalArgumentException, IllegalAccessException, // throws IllegalArgumentException, IllegalAccessException,
// InvocationTargetException { // InvocationTargetException {
// Object newValue = getter.invoke(component); // Object newValue = getter.invoke(component);
@@ -45,7 +45,7 @@ public class PropertyWrapper implements Cloneable {
// } // }
// } // }
public void writeTo(IComponentInstance component) throws IllegalArgumentException, public void writeTo(IDIYComponent component) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException { IllegalAccessException, InvocationTargetException {
setter.invoke(component, value); setter.invoke(component, value);
} }
@@ -1,65 +0,0 @@
package org.diylc.core;
import javax.swing.Icon;
/**
* Interface used to describe a component type.
*
* @author Branislav Stojkovic
*
* @see IComponentInstance
*/
public interface IComponentType {
/**
* Returns component type name.
*
* @return
*/
String getName();
/**
* Returns component instance name prefix, e.g. "R" for resistors, "C" for
* capacitors, etc.
*
* @return
*/
String getInstanceNamePrefix();
/**
* Returns component category.
*
* @return
*/
String getCategory();
/**
* Returns component type description that may contain HTML tags.
*
* @return
*/
String getDescription();
/**
* Returns an {@link Icon} used to represent component type.
*
* @return
*/
Icon getIcon();
/**
* Returns a {@link ComponentLayer} where this component belongs.
*
* @return
*/
ComponentLayer getComponentLayer();
/**
* Returns class that represents component instances of this component type.
* Application will instantiate objects of this class when components are
* created.
*
* @return
*/
Class<? extends IComponentInstance> getComponentInstanceClass();
}
@@ -3,6 +3,8 @@ package org.diylc.core;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.io.Serializable; import java.io.Serializable;
import org.diylc.presenter.ComponentType;
/** /**
* Interface for component instance. Implementation classes of this interface * Interface for component instance. Implementation classes of this interface
* will be instantiated by the application when component is added to the * will be instantiated by the application when component is added to the
@@ -12,9 +14,9 @@ import java.io.Serializable;
* *
* @author Branislav Stojkovic * @author Branislav Stojkovic
* *
* @see IComponentType * @see ComponentType
*/ */
public interface IComponentInstance extends Serializable { public interface IDIYComponent extends Serializable {
/** /**
* Draws the component onto the {@link Graphics2D}. * Draws the component onto the {@link Graphics2D}.
@@ -23,4 +25,13 @@ public interface IComponentInstance extends Serializable {
* @param componentState * @param componentState
*/ */
void draw(Graphics2D g2d, ComponentState componentState); void draw(Graphics2D g2d, ComponentState componentState);
/**
* Draws icon representation of the component.
*
* @param g2d
* @param width
* @param height
*/
void drawIcon(Graphics2D g2d, int width, int height);
} }
@@ -29,10 +29,10 @@ public class Project implements Serializable {
private String description; private String description;
private Size width; private Size width;
private Size height; private Size height;
private List<IComponentInstance> components; private List<IDIYComponent> components;
public Project() { public Project() {
components = new ArrayList<IComponentInstance>(); components = new ArrayList<IDIYComponent>();
title = DEFAULT_TITLE; title = DEFAULT_TITLE;
author = System.getProperty("user.name"); author = System.getProperty("user.name");
width = DEFAULT_WIDTH; width = DEFAULT_WIDTH;
@@ -40,7 +40,7 @@ public class Project implements Serializable {
} }
public Project(String title, String author, String description, Size width, Size height, public Project(String title, String author, String description, Size width, Size height,
List<IComponentInstance> components) { List<IDIYComponent> components) {
super(); super();
this.title = title; this.title = title;
this.author = author; this.author = author;
@@ -95,7 +95,7 @@ public class Project implements Serializable {
this.height = height; this.height = height;
} }
public List<IComponentInstance> getComponents() { public List<IDIYComponent> getComponents() {
return components; return components;
} }
@@ -156,9 +156,9 @@ public class Project implements Serializable {
// @Override // @Override
// public Project clone() throws CloneNotSupportedException { // public Project clone() throws CloneNotSupportedException {
// List<IComponentInstance> clonedComponents = new // List<IDIYComponent> clonedComponents = new
// ArrayList<IComponentInstance>(); // ArrayList<IDIYComponent>();
// for (IComponentInstance component : components) { // for (IDIYComponent component : components) {
// clonedComponents.add(component.clone()); // clonedComponents.add(component.clone());
// } // }
// Project project = new Project(title, author, description, // Project project = new Project(title, author, description,
@@ -0,0 +1,32 @@
package org.diylc.core.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.diylc.core.ComponentLayer;
import org.diylc.core.IDIYComponent;
import org.diylc.core.VisibilityPolicy;
/**
* Annotation for {@link IDIYComponent} implementation. Describes component
* properties.
*
* @author Branislav Stojkovic
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface ComponentDescriptor {
String name();
String desciption();
String category();
String author();
String instanceNamePrefix();
ComponentLayer componentLayer() default ComponentLayer.COMPONENT;
boolean stretchable() default true;
}
@@ -1,9 +0,0 @@
package org.diylc.core.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface ComponentName {
}
@@ -73,7 +73,7 @@ public class MainFrame extends JFrame implements IView {
presenter.installPlugin(canvasPlugin); presenter.installPlugin(canvasPlugin);
Project project = new Project(); Project project = new Project();
// project.getComponents().add(new MockComponentInstance()); // project.getComponents().add(new MockDIYComponent());
presenter.loadProject(project, true); presenter.loadProject(project, true);
addWindowListener(new WindowAdapter() { addWindowListener(new WindowAdapter() {
@@ -53,7 +53,7 @@ class CanvasTargetListener implements DropTargetListener {
// (t.isDataFlavorSupported(SelectionTransferable.selectionFlavor)) // (t.isDataFlavorSupported(SelectionTransferable.selectionFlavor))
// { // {
// if (currentPoint != null) { // if (currentPoint != null) {
// List<IComponentInstance> selection = (List<IComponentInstance>) t // List<IDIYComponent> selection = (List<IDIYComponent>) t
// .getTransferData(SelectionTransferable.selectionFlavor); // .getTransferData(SelectionTransferable.selectionFlavor);
// Point startPoint = (Point) t // Point startPoint = (Point) t
// .getTransferData(PointTransferable.pointFlavor); // .getTransferData(PointTransferable.pointFlavor);
@@ -7,7 +7,7 @@ import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
public class SelectionTransferable implements Transferable { public class SelectionTransferable implements Transferable {
@@ -16,9 +16,9 @@ public class SelectionTransferable implements Transferable {
"Component Selection"); "Component Selection");
private Point startPoint; private Point startPoint;
private List<IComponentInstance> selection; private List<IDIYComponent> selection;
public SelectionTransferable(List<IComponentInstance> selection, Point startPoint) { public SelectionTransferable(List<IDIYComponent> selection, Point startPoint) {
super(); super();
this.selection = selection; this.selection = selection;
this.startPoint = startPoint; this.startPoint = startPoint;
@@ -19,7 +19,7 @@ import org.diylc.common.ComponentSelection;
import org.diylc.common.EventType; import org.diylc.common.EventType;
import org.diylc.common.IPlugIn; import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.images.IconLoader; import org.diylc.images.IconLoader;
@@ -163,7 +163,7 @@ public class ClipboardManager implements IPlugIn, ClipboardOwner {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
LOG.info("Paste triggered"); LOG.info("Paste triggered");
try { try {
List<IComponentInstance> components = (List<IComponentInstance>) clipboard List<IDIYComponent> components = (List<IDIYComponent>) clipboard
.getData(ComponentSelection.listFlavor); .getData(ComponentSelection.listFlavor);
plugInPort.addComponents(cloner.deepClone(components), null); plugInPort.addComponents(cloner.deepClone(components), null);
} catch (Exception ex) { } catch (Exception ex) {
@@ -7,7 +7,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
import org.diylc.presenter.ComponentProcessor; import org.diylc.presenter.ComponentProcessor;
@@ -25,9 +25,9 @@ public class BomMaker {
private BomMaker() { private BomMaker() {
} }
public List<BomEntry> createBom(List<IComponentInstance> components) { public List<BomEntry> createBom(List<IDIYComponent> components) {
Map<String, BomEntry> entryMap = new HashMap<String, BomEntry>(); Map<String, BomEntry> entryMap = new HashMap<String, BomEntry>();
for (IComponentInstance component : components) { for (IDIYComponent component : components) {
String name = ComponentProcessor.getInstance().extractBomName(component); String name = ComponentProcessor.getInstance().extractBomName(component);
String value = ComponentProcessor.getInstance().extractBomValue(component); String value = ComponentProcessor.getInstance().extractBomValue(component);
if ((name != null) && (value != null)) { if ((name != null) && (value != null)) {
@@ -21,9 +21,9 @@ import org.diylc.common.ControlPointWrapper;
import org.diylc.common.EventType; import org.diylc.common.EventType;
import org.diylc.common.IPlugIn; import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
import org.diylc.core.IComponentType;
import org.diylc.presenter.ComponentProcessor; import org.diylc.presenter.ComponentProcessor;
import org.diylc.presenter.ComponentType;
import com.diyfever.gui.MemoryBar; import com.diyfever.gui.MemoryBar;
import com.diyfever.gui.miscutils.PercentageListCellRenderer; import com.diyfever.gui.miscutils.PercentageListCellRenderer;
@@ -41,8 +41,8 @@ public class StatusBar extends JPanel implements IPlugIn {
private IPlugInPort plugInPort; private IPlugInPort plugInPort;
// State variables // State variables
private IComponentType componentSlot; private ComponentType componentSlot;
private List<IComponentInstance> componentsUnderCursor; private List<IDIYComponent> componentsUnderCursor;
public StatusBar() { public StatusBar() {
super(); super();
@@ -159,12 +159,12 @@ public class StatusBar extends JPanel implements IPlugIn {
} }
break; break;
case SLOT_CHANGED: case SLOT_CHANGED:
componentSlot = (IComponentType) params[0]; componentSlot = (ComponentType) params[0];
refreshStatusText(); refreshStatusText();
break; break;
case AVAILABLE_CTRL_POINTS_CHANGED: case AVAILABLE_CTRL_POINTS_CHANGED:
componentsUnderCursor = new ArrayList<IComponentInstance>( componentsUnderCursor = new ArrayList<IDIYComponent>(
((Map<IComponentInstance, ControlPointWrapper>) params[0]).keySet()); ((Map<IDIYComponent, ControlPointWrapper>) params[0]).keySet());
refreshStatusText(); refreshStatusText();
break; break;
} }
@@ -177,7 +177,7 @@ public class StatusBar extends JPanel implements IPlugIn {
} else { } else {
// String formattedNames = ""; // String formattedNames = "";
// int n = 1; // int n = 1;
// for (IComponentInstance component : componentsUnderCursor) { // for (IDIYComponent component : componentsUnderCursor) {
// if (n > 1) { // if (n > 1) {
// formattedNames += ", "; // formattedNames += ", ";
// } // }
@@ -8,8 +8,7 @@ import java.awt.event.ActionListener;
import javax.swing.JButton; import javax.swing.JButton;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.core.IComponentType; import org.diylc.presenter.ComponentType;
/** /**
* {@link JButton} that displays component type icon and instantiates the * {@link JButton} that displays component type icon and instantiates the
@@ -22,9 +21,9 @@ class ComponentButton extends JButton {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private IPlugInPort plugInPort; private IPlugInPort plugInPort;
private IComponentType componentType; private ComponentType componentType;
public ComponentButton(final IPlugInPort plugInPort, final IComponentType componentType) { public ComponentButton(final IPlugInPort plugInPort, final ComponentType componentType) {
super(componentType.getIcon()); super(componentType.getIcon());
this.plugInPort = plugInPort; this.plugInPort = plugInPort;
this.componentType = componentType; this.componentType = componentType;
@@ -59,10 +58,8 @@ class ComponentButton extends JButton {
private void initializeDnD() { private void initializeDnD() {
// Initialize drag source recognizer. // Initialize drag source recognizer.
DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer( DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer(this,
this,
DnDConstants.ACTION_MOVE, DnDConstants.ACTION_MOVE,
new ToolboxGestureListener(plugInPort, componentType.getComponentInstanceClass() new ToolboxGestureListener(plugInPort, componentType.getInstanceClass().getName()));
.getName()));
} }
} }
@@ -9,7 +9,7 @@ import javax.swing.JButton;
import javax.swing.JTabbedPane; import javax.swing.JTabbedPane;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.core.IComponentType; import org.diylc.presenter.ComponentType;
class ComponentTabbedPane extends JTabbedPane { class ComponentTabbedPane extends JTabbedPane {
@@ -20,16 +20,16 @@ class ComponentTabbedPane extends JTabbedPane {
public ComponentTabbedPane(IPlugInPort plugInPort) { public ComponentTabbedPane(IPlugInPort plugInPort) {
super(); super();
this.plugInPort = plugInPort; this.plugInPort = plugInPort;
Map<String, List<IComponentType>> componentTypes = plugInPort.getComponentTypes(); Map<String, List<ComponentType>> componentTypes = plugInPort.getComponentTypes();
for (Map.Entry<String, List<IComponentType>> entry : componentTypes.entrySet()) { for (Map.Entry<String, List<ComponentType>> entry : componentTypes.entrySet()) {
addTab(entry.getKey(), createComponentPanel(entry.getValue())); addTab(entry.getKey(), createComponentPanel(entry.getValue()));
} }
} }
private Container createComponentPanel(List<IComponentType> componentTypes) { private Container createComponentPanel(List<ComponentType> componentTypes) {
Container componentPanel = new Container(); Container componentPanel = new Container();
componentPanel.setLayout(new BoxLayout(componentPanel, BoxLayout.X_AXIS)); componentPanel.setLayout(new BoxLayout(componentPanel, BoxLayout.X_AXIS));
for (IComponentType componentType : componentTypes) { for (ComponentType componentType : componentTypes) {
JButton button = new ComponentButton(plugInPort, componentType); JButton button = new ComponentButton(plugInPort, componentType);
componentPanel.add(button); componentPanel.add(button);
} }
@@ -10,7 +10,7 @@ import java.util.Map;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.diylc.common.ControlPointWrapper; import org.diylc.common.ControlPointWrapper;
import org.diylc.common.PropertyWrapper; import org.diylc.common.PropertyWrapper;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
import org.diylc.core.annotations.BomName; import org.diylc.core.annotations.BomName;
import org.diylc.core.annotations.BomValue; import org.diylc.core.annotations.BomValue;
import org.diylc.core.annotations.ControlPoint; import org.diylc.core.annotations.ControlPoint;
@@ -54,7 +54,7 @@ public class ComponentProcessor {
* @param clazz * @param clazz
* @return * @return
*/ */
public List<PropertyWrapper> extractProperties(Class<? extends IComponentInstance> clazz) { public List<PropertyWrapper> extractProperties(Class<? extends IDIYComponent> clazz) {
if (propertyCache.containsKey(clazz)) { if (propertyCache.containsKey(clazz)) {
return cloner.deepClone(propertyCache.get(clazz)); return cloner.deepClone(propertyCache.get(clazz));
} }
@@ -91,13 +91,13 @@ public class ComponentProcessor {
* Reads all control points from the specified component class. Note than * Reads all control points from the specified component class. Note than
* control points are cached, so it may happen that control points returned * control points are cached, so it may happen that control points returned
* already have their values populated. Always use * already have their values populated. Always use
* {@link ControlPointWrapper#readFrom(IComponentInstance)} to update their * {@link ControlPointWrapper#readFrom(IDIYComponent)} to update their
* state from an actual component. * state from an actual component.
* *
* @param clazz * @param clazz
* @return * @return
*/ */
public List<ControlPointWrapper> extractControlPoints(Class<? extends IComponentInstance> clazz) { public List<ControlPointWrapper> extractControlPoints(Class<? extends IDIYComponent> clazz) {
if (controlPointCache.containsKey(clazz)) { if (controlPointCache.containsKey(clazz)) {
return cloner.deepClone(controlPointCache.get(clazz)); return cloner.deepClone(controlPointCache.get(clazz));
} }
@@ -136,12 +136,12 @@ public class ComponentProcessor {
* @return * @return
*/ */
public List<PropertyWrapper> getMutualSelectionProperties( public List<PropertyWrapper> getMutualSelectionProperties(
List<IComponentInstance> selectedComponents) { List<IDIYComponent> selectedComponents) {
if (selectedComponents.isEmpty()) { if (selectedComponents.isEmpty()) {
return null; return null;
} }
List<PropertyWrapper> properties = new ArrayList<PropertyWrapper>(); List<PropertyWrapper> properties = new ArrayList<PropertyWrapper>();
IComponentInstance firstComponent = selectedComponents.get(0); IDIYComponent firstComponent = selectedComponents.get(0);
properties.addAll(extractProperties(firstComponent.getClass())); properties.addAll(extractProperties(firstComponent.getClass()));
// Initialize values // Initialize values
for (PropertyWrapper property : properties) { for (PropertyWrapper property : properties) {
@@ -153,7 +153,7 @@ public class ComponentProcessor {
} }
} }
for (int i = 1; i < selectedComponents.size(); i++) { for (int i = 1; i < selectedComponents.size(); i++) {
IComponentInstance component = selectedComponents.get(i); IDIYComponent component = selectedComponents.get(i);
List<PropertyWrapper> newProperties = extractProperties(component.getClass()); List<PropertyWrapper> newProperties = extractProperties(component.getClass());
for (PropertyWrapper property : newProperties) { for (PropertyWrapper property : newProperties) {
try { try {
@@ -183,7 +183,7 @@ public class ComponentProcessor {
* @param component * @param component
* @return * @return
*/ */
public String extractBomName(IComponentInstance component) { public String extractBomName(IDIYComponent component) {
String name = null; String name = null;
for (Method method : component.getClass().getMethods()) { for (Method method : component.getClass().getMethods()) {
if (method.isAnnotationPresent(BomName.class)) { if (method.isAnnotationPresent(BomName.class)) {
@@ -204,7 +204,7 @@ public class ComponentProcessor {
* @param component * @param component
* @param newBomName * @param newBomName
*/ */
public void writeBomName(IComponentInstance component, String newBomName) { public void writeBomName(IDIYComponent component, String newBomName) {
for (Method method : component.getClass().getMethods()) { for (Method method : component.getClass().getMethods()) {
if (method.isAnnotationPresent(BomName.class)) { if (method.isAnnotationPresent(BomName.class)) {
try { try {
@@ -224,7 +224,7 @@ public class ComponentProcessor {
* @param component * @param component
* @return * @return
*/ */
public String extractBomValue(IComponentInstance component) { public String extractBomValue(IDIYComponent component) {
String value = null; String value = null;
for (Method method : component.getClass().getMethods()) { for (Method method : component.getClass().getMethods()) {
if (method.isAnnotationPresent(BomValue.class)) { if (method.isAnnotationPresent(BomValue.class)) {
@@ -0,0 +1,76 @@
package org.diylc.presenter;
import javax.swing.Icon;
import org.diylc.core.ComponentLayer;
import org.diylc.core.IDIYComponent;
/**
* Entity class used to describe a component type.
*
* @author Branislav Stojkovic
*
* @see IDIYComponent
*/
public class ComponentType {
private String name;
private String description;
private String category;
private String namePrefix;
private String author;
private Icon icon;
private Class<? extends IDIYComponent> instanceClass;
private ComponentLayer layer;
public ComponentType(String name, String description, String category, String namePrefix,
String author, Icon icon, Class<? extends IDIYComponent> instanceClass,
ComponentLayer layer) {
super();
this.name = name;
this.description = description;
this.category = category;
this.namePrefix = namePrefix;
this.author = author;
this.icon = icon;
this.instanceClass = instanceClass;
this.layer = layer;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getCategory() {
return category;
}
public String getNamePrefix() {
return namePrefix;
}
public String getAuthor() {
return author;
}
public Icon getIcon() {
return icon;
}
public Class<? extends IDIYComponent> getInstanceClass() {
return instanceClass;
}
public ComponentLayer getLayer() {
return layer;
}
@Override
public String toString() {
return name;
}
}
@@ -7,12 +7,15 @@ import java.awt.Composite;
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.util.ArrayList; import java.util.ArrayList;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
@@ -22,6 +25,8 @@ import java.util.Set;
import java.util.Map.Entry; import java.util.Map.Entry;
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;
@@ -35,10 +40,10 @@ import org.diylc.common.IPlugInPort;
import org.diylc.common.PropertyWrapper; import org.diylc.common.PropertyWrapper;
import org.diylc.core.ComponentLayer; import org.diylc.core.ComponentLayer;
import org.diylc.core.ComponentState; import org.diylc.core.ComponentState;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
import org.diylc.core.IComponentType;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy; import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.gui.IView; import org.diylc.gui.IView;
import org.diylc.utils.Constants; import org.diylc.utils.Constants;
@@ -61,17 +66,18 @@ public class Presenter implements IPlugInPort {
private static final VersionNumber CURRENT_VERSION = new VersionNumber(0, 0, 0); private static final VersionNumber CURRENT_VERSION = new VersionNumber(0, 0, 0);
private static final String DEFAULTS_KEY = "defaults"; private static final String DEFAULTS_KEY = "defaults";
private static final int CONTROL_POINT_SENSITIVITY = 4; private static final int CONTROL_POINT_SENSITIVITY = 4;
private static final int ICON_SIZE = 32;
private double zoomLevel = 1; private double zoomLevel = 1;
private Map<IComponentInstance, Area> componentAreaMap; private Map<IDIYComponent, Area> componentAreaMap;
private Project currentProject; private Project currentProject;
private Map<String, List<IComponentType>> componentTypes; private Map<String, List<ComponentType>> componentTypes;
private List<IPlugIn> plugIns; private List<IPlugIn> plugIns;
private ComponentSelection selectedComponents; private ComponentSelection selectedComponents;
// List of components that have at least one of their control points // List of components that have at least one of their control points
// under the last recorded mouse position. // under the last recorded mouse position.
private Map<IComponentInstance, ControlPointWrapper> componentControlPointMap; private Map<IDIYComponent, ControlPointWrapper> componentControlPointMap;
private Cloner cloner; private Cloner cloner;
@@ -91,12 +97,12 @@ public class Presenter implements IPlugInPort {
private Point previousDragPoint = null; private Point previousDragPoint = null;
private Project preDragProject = null; private Project preDragProject = null;
private IComponentType componentSlot; private ComponentType componentSlot;
public Presenter(IView view) { public Presenter(IView view) {
super(); super();
this.view = view; this.view = view;
componentAreaMap = new HashMap<IComponentInstance, Area>(); componentAreaMap = new HashMap<IDIYComponent, Area>();
plugIns = new ArrayList<IPlugIn>(); plugIns = new ArrayList<IPlugIn>();
messageDispatcher = new MessageDispatcher<EventType>(); messageDispatcher = new MessageDispatcher<EventType>();
selectedComponents = new ComponentSelection(); selectedComponents = new ComponentSelection();
@@ -141,7 +147,7 @@ public class Presenter implements IPlugInPort {
if (componentSlot == null) { if (componentSlot == null) {
// Scale point to remove zoom factor. // Scale point to remove zoom factor.
Point2D scaledPoint = scalePoint(point); Point2D scaledPoint = scalePoint(point);
for (Map.Entry<IComponentInstance, Area> entry : componentAreaMap.entrySet()) { for (Map.Entry<IDIYComponent, Area> entry : componentAreaMap.entrySet()) {
if (entry.getValue().contains(scaledPoint)) { if (entry.getValue().contains(scaledPoint)) {
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
} }
@@ -178,28 +184,64 @@ public class Presenter implements IPlugInPort {
messageDispatcher.dispatchMessage(EventType.REPAINT); messageDispatcher.dispatchMessage(EventType.REPAINT);
} }
@SuppressWarnings("unchecked")
@Override @Override
public Map<String, List<IComponentType>> getComponentTypes() { public Map<String, List<ComponentType>> getComponentTypes() {
if (componentTypes == null) { if (componentTypes == null) {
LOG.info("Loading component types."); LOG.info("Loading component types.");
componentTypes = new HashMap<String, List<IComponentType>>(); componentTypes = new HashMap<String, List<ComponentType>>();
List<Class<?>> componentTypeClasses = JarScanner.getInstance().scanFolder("library/", List<Class<?>> componentTypeClasses = JarScanner.getInstance().scanFolder("library/",
IComponentType.class); IDIYComponent.class);
for (Class<?> clazz : componentTypeClasses) { for (Class<?> clazz : componentTypeClasses) {
try { if (!Modifier.isAbstract(clazz.getModifiers())) {
IComponentType componentType = (IComponentType) clazz.newInstance(); ComponentDescriptor annotation = clazz.getAnnotation(ComponentDescriptor.class);
List<IComponentType> nestedList; String name;
String description;
String category;
String namePrefix;
String author;
Icon icon;
Class<? extends IDIYComponent> instanceClass = (Class<? extends IDIYComponent>) clazz;
ComponentLayer layer;
if (annotation == null) {
name = clazz.getSimpleName();
description = "";
category = "Uncategorized";
namePrefix = "Unknown";
author = "Unknown";
layer = ComponentLayer.COMPONENT;
} else {
name = annotation.name();
description = annotation.desciption();
category = annotation.category();
namePrefix = annotation.instanceNamePrefix();
author = annotation.author();
layer = annotation.componentLayer();
}
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);
List<ComponentType> nestedList;
if (componentTypes.containsKey(componentType.getCategory())) { if (componentTypes.containsKey(componentType.getCategory())) {
nestedList = componentTypes.get(componentType.getCategory()); nestedList = componentTypes.get(componentType.getCategory());
} else { } else {
nestedList = new ArrayList<IComponentType>(); nestedList = new ArrayList<ComponentType>();
componentTypes.put(componentType.getCategory(), nestedList); componentTypes.put(componentType.getCategory(), nestedList);
} }
nestedList.add(componentType); nestedList.add(componentType);
} catch (InstantiationException e) {
LOG.warn("Could not instantiate: " + clazz.getName());
} catch (IllegalAccessException e) {
LOG.warn("Could not access: " + clazz.getName());
} }
} }
} }
@@ -249,10 +291,10 @@ public class Presenter implements IPlugInPort {
// g2dWrapper.resetTx(); // g2dWrapper.resetTx();
List<IComponentInstance> components = getCurrentProject().getComponents(); List<IDIYComponent> components = getCurrentProject().getComponents();
componentAreaMap.clear(); componentAreaMap.clear();
if (components != null) { if (components != null) {
for (IComponentInstance component : components) { for (IDIYComponent component : components) {
g2dWrapper.startedDrawingComponent(); g2dWrapper.startedDrawingComponent();
ComponentState state = ComponentState.NORMAL; ComponentState state = ComponentState.NORMAL;
if (drawOptions.contains(DrawOption.SELECTION) if (drawOptions.contains(DrawOption.SELECTION)
@@ -343,9 +385,9 @@ public class Presenter implements IPlugInPort {
* *
* @return * @return
*/ */
private List<IComponentInstance> findComponentsAt(Point point) { private List<IDIYComponent> findComponentsAt(Point point) {
List<IComponentInstance> components = new ArrayList<IComponentInstance>(); List<IDIYComponent> components = new ArrayList<IDIYComponent>();
for (Map.Entry<IComponentInstance, Area> entry : componentAreaMap.entrySet()) { for (Map.Entry<IDIYComponent, Area> entry : componentAreaMap.entrySet()) {
if (entry.getValue().contains(point)) { if (entry.getValue().contains(point)) {
components.add(entry.getKey()); components.add(entry.getKey());
} }
@@ -360,20 +402,20 @@ public class Presenter implements IPlugInPort {
Point scaledPoint = scalePoint(point); Point scaledPoint = scalePoint(point);
if (componentSlot != null) { if (componentSlot != null) {
try { try {
instantiateComponent(componentSlot.getComponentInstanceClass(), componentSlot instantiateComponent(componentSlot.getInstanceClass(), componentSlot.getName(),
.getName(), componentSlot.getInstanceNamePrefix(), scaledPoint); componentSlot.getNamePrefix(), scaledPoint);
} catch (Exception e) { } catch (Exception e) {
LOG.error("Error instatiating component of type: " LOG.error("Error instatiating component of type: "
+ componentSlot.getComponentInstanceClass().getName()); + componentSlot.getInstanceClass().getName());
} }
setNewComponentSlot(null); setNewComponentSlot(null);
} else { } else {
List<IComponentInstance> components = findComponentsAt(scaledPoint); List<IDIYComponent> components = findComponentsAt(scaledPoint);
// If there's nothing under mouse cursor deselect all. // If there's nothing under mouse cursor deselect all.
if (components.isEmpty()) { if (components.isEmpty()) {
selectedComponents.clear(); selectedComponents.clear();
} else { } else {
IComponentInstance component = components.get(0); IDIYComponent component = components.get(0);
// If ctrl is pressed just toggle the component under mouse // If ctrl is pressed just toggle the component under mouse
// cursor. // cursor.
if (ctrlDown) { if (ctrlDown) {
@@ -395,9 +437,9 @@ public class Presenter implements IPlugInPort {
@Override @Override
public void mouseMoved(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown) { public void mouseMoved(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown) {
Map<IComponentInstance, ControlPointWrapper> components = new HashMap<IComponentInstance, ControlPointWrapper>(); Map<IDIYComponent, ControlPointWrapper> components = new HashMap<IDIYComponent, ControlPointWrapper>();
Point scaledPoint = scalePoint(point); Point scaledPoint = scalePoint(point);
for (IComponentInstance component : currentProject.getComponents()) { for (IDIYComponent component : currentProject.getComponents()) {
List<ControlPointWrapper> currentPoints = ComponentProcessor.getInstance() List<ControlPointWrapper> currentPoints = ComponentProcessor.getInstance()
.extractControlPoints(component.getClass()); .extractControlPoints(component.getClass());
for (ControlPointWrapper controlPoint : currentPoints) { for (ControlPointWrapper controlPoint : currentPoints) {
@@ -431,7 +473,7 @@ public class Presenter implements IPlugInPort {
} }
@Override @Override
public Area getComponentArea(IComponentInstance component) { public Area getComponentArea(IDIYComponent component) {
return componentAreaMap.get(component); return componentAreaMap.get(component);
} }
@@ -447,7 +489,7 @@ public class Presenter implements IPlugInPort {
preDragProject = cloner.deepClone(currentProject); preDragProject = cloner.deepClone(currentProject);
Point scaledPoint = scalePoint(point); Point scaledPoint = scalePoint(point);
previousDragPoint = scaledPoint; previousDragPoint = scaledPoint;
List<IComponentInstance> components = findComponentsAt(scaledPoint); List<IDIYComponent> components = findComponentsAt(scaledPoint);
if (!componentControlPointMap.isEmpty()) { if (!componentControlPointMap.isEmpty()) {
// If we're dragging control points reset selection. // If we're dragging control points reset selection.
selectedComponents.clear(); selectedComponents.clear();
@@ -460,7 +502,7 @@ public class Presenter implements IPlugInPort {
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents); messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
messageDispatcher.dispatchMessage(EventType.REPAINT); messageDispatcher.dispatchMessage(EventType.REPAINT);
} else { } else {
IComponentInstance component = components.get(0); IDIYComponent component = components.get(0);
// If the component under the cursor is not already selected, make // If the component under the cursor is not already selected, make
// it into the only selected component. // it into the only selected component.
if (!selectedComponents.contains(component)) { if (!selectedComponents.contains(component)) {
@@ -479,8 +521,8 @@ public class Presenter implements IPlugInPort {
} }
Point scaledPoint = scalePoint(point); Point scaledPoint = scalePoint(point);
if (!componentControlPointMap.isEmpty()) { if (!componentControlPointMap.isEmpty()) {
// We're dragging control point(s). // We're dragging control point(s).
IComponentInstance firstComponent = componentControlPointMap.keySet().iterator().next(); IDIYComponent firstComponent = componentControlPointMap.keySet().iterator().next();
ControlPointWrapper controlPoint = componentControlPointMap.get(firstComponent); ControlPointWrapper controlPoint = componentControlPointMap.get(firstComponent);
if (controlPoint == null) { if (controlPoint == null) {
LOG.warn("Control point not found in the map!"); LOG.warn("Control point not found in the map!");
@@ -499,11 +541,11 @@ public class Presenter implements IPlugInPort {
/ Constants.GRID) * Constants.GRID); / Constants.GRID) * Constants.GRID);
previousDragPoint.setLocation(x, y); previousDragPoint.setLocation(x, y);
for (Entry<IComponentInstance, ControlPointWrapper> entry : componentControlPointMap for (Entry<IDIYComponent, ControlPointWrapper> entry : componentControlPointMap
.entrySet()) { .entrySet()) {
try { try {
controlPoint = entry.getValue(); controlPoint = entry.getValue();
IComponentInstance component = entry.getKey(); IDIYComponent component = entry.getKey();
controlPoint.readFrom(component); controlPoint.readFrom(component);
if (controlPoint.isEditable()) { if (controlPoint.isEditable()) {
controlPoint.getValue().setLocation(x, y); controlPoint.getValue().setLocation(x, y);
@@ -543,7 +585,7 @@ public class Presenter implements IPlugInPort {
this.selectionRect = Utils.createRectangle(scaledPoint, previousDragPoint); this.selectionRect = Utils.createRectangle(scaledPoint, previousDragPoint);
} }
selectedComponents.clear(); selectedComponents.clear();
for (IComponentInstance component : currentProject.getComponents()) { for (IDIYComponent component : currentProject.getComponents()) {
Area area = componentAreaMap.get(component); Area area = componentAreaMap.get(component);
if ((area != null) && area.intersects(selectionRect)) { if ((area != null) && area.intersects(selectionRect)) {
selectedComponents.add(component); selectedComponents.add(component);
@@ -571,7 +613,7 @@ public class Presenter implements IPlugInPort {
int dx = (int) (Math.round((toPoint.x - fromPoint.x) / zoomLevel / Constants.GRID) * Constants.GRID); int dx = (int) (Math.round((toPoint.x - fromPoint.x) / zoomLevel / Constants.GRID) * Constants.GRID);
int dy = (int) (Math.round((toPoint.y - fromPoint.y) / zoomLevel / Constants.GRID) * Constants.GRID); int dy = (int) (Math.round((toPoint.y - fromPoint.y) / zoomLevel / Constants.GRID) * Constants.GRID);
fromPoint.translate(dx, dy); fromPoint.translate(dx, dy);
for (IComponentInstance component : selectedComponents) { for (IDIYComponent component : selectedComponents) {
List<ControlPointWrapper> controlPoints = ComponentProcessor.getInstance() List<ControlPointWrapper> controlPoints = ComponentProcessor.getInstance()
.extractControlPoints(component.getClass()); .extractControlPoints(component.getClass());
for (ControlPointWrapper controlPoint : controlPoints) { for (ControlPointWrapper controlPoint : controlPoints) {
@@ -595,7 +637,7 @@ public class Presenter implements IPlugInPort {
} }
@Override @Override
public void addComponents(List<IComponentInstance> components, Point preferredPoint) { public void addComponents(List<IDIYComponent> components, Point preferredPoint) {
LOG.debug(String.format("addComponents(%s)", components)); LOG.debug(String.format("addComponents(%s)", components));
Project oldProject = cloner.deepClone(currentProject); Project oldProject = cloner.deepClone(currentProject);
currentProject.getComponents().addAll(components); currentProject.getComponents().addAll(components);
@@ -634,7 +676,7 @@ public class Presenter implements IPlugInPort {
@Override @Override
// @Override // @Override
// public Object getDefaultPropertyValue( // public Object getDefaultPropertyValue(
// Class<? extends IComponentInstance> componentClass, // Class<? extends IDIYComponent> componentClass,
// String propertyName) { // String propertyName) {
// return configuration.getDefaultPropertyValues().get( // return configuration.getDefaultPropertyValues().get(
// componentClass.getName() + ":" + propertyName); // componentClass.getName() + ":" + propertyName);
@@ -647,7 +689,7 @@ public class Presenter implements IPlugInPort {
defaultMap = new HashMap<String, Object>(); defaultMap = new HashMap<String, Object>();
ConfigurationManager.getInstance().setConfigurationItem(DEFAULTS_KEY, defaultMap); ConfigurationManager.getInstance().setConfigurationItem(DEFAULTS_KEY, defaultMap);
} }
for (IComponentInstance component : selectedComponents) { for (IDIYComponent component : selectedComponents) {
String className = component.getClass().getName(); String className = component.getClass().getName();
LOG.debug("Default property value set for " + className + ":" + propertyName); LOG.debug("Default property value set for " + className + ":" + propertyName);
defaultMap.put(className + ":" + propertyName, value); defaultMap.put(className + ":" + propertyName, value);
@@ -655,14 +697,14 @@ public class Presenter implements IPlugInPort {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void instantiateComponent(Class<? extends IComponentInstance> componentClass, private void instantiateComponent(Class<? extends IDIYComponent> componentClass,
String componentTypeName, String namePrefix, Point point) throws Exception { String componentTypeName, String namePrefix, Point point) throws Exception {
LOG.info("Instatiating component of type: " + componentClass.getName()); LOG.info("Instatiating component of type: " + componentClass.getName());
Project oldProject = cloner.deepClone(currentProject); Project oldProject = cloner.deepClone(currentProject);
// Instantiate the component. // Instantiate the component.
IComponentInstance component = componentClass.newInstance(); IDIYComponent component = componentClass.newInstance();
// Find the next available name for the component. // Find the next available name for the component.
int i = 0; int i = 0;
@@ -671,7 +713,7 @@ public class Presenter implements IPlugInPort {
i++; i++;
String name = namePrefix + i; String name = namePrefix + i;
exists = false; exists = false;
for (IComponentInstance c : currentProject.getComponents()) { for (IDIYComponent c : currentProject.getComponents()) {
String cName = ComponentProcessor.getInstance().extractBomName(c); String cName = ComponentProcessor.getInstance().extractBomName(c);
if (cName.equals(name)) { if (cName.equals(name)) {
exists = true; exists = true;
@@ -735,7 +777,7 @@ public class Presenter implements IPlugInPort {
LOG.debug(String.format("applyPropertiesToSelection(%s)", properties)); LOG.debug(String.format("applyPropertiesToSelection(%s)", properties));
Project oldProject = cloner.deepClone(currentProject); Project oldProject = cloner.deepClone(currentProject);
try { try {
for (IComponentInstance component : selectedComponents) { for (IDIYComponent component : selectedComponents) {
for (PropertyWrapper property : properties) { for (PropertyWrapper property : properties) {
property.writeTo(component); property.writeTo(component);
} }
@@ -751,7 +793,7 @@ public class Presenter implements IPlugInPort {
} }
@Override @Override
public void setNewComponentSlot(IComponentType componentType) { public void setNewComponentSlot(ComponentType componentType) {
LOG.debug(String.format("setNewComponentSlot(%s)", componentType == null ? null LOG.debug(String.format("setNewComponentSlot(%s)", componentType == null ? null
: componentType.getName())); : componentType.getName()));
this.componentSlot = componentType; this.componentSlot = componentType;
@@ -771,8 +813,7 @@ public class Presenter implements IPlugInPort {
(int) (point.y / zoomLevel)); (int) (point.y / zoomLevel));
} }
private boolean shouldShowControlPoint(ControlPointWrapper controlPoint, private boolean shouldShowControlPoint(ControlPointWrapper controlPoint, IDIYComponent component) {
IComponentInstance component) {
return controlPoint.getVisibilityPolicy().equals(VisibilityPolicy.ALWAYS) return controlPoint.getVisibilityPolicy().equals(VisibilityPolicy.ALWAYS)
|| ((controlPoint.getVisibilityPolicy().equals(VisibilityPolicy.WHEN_SELECTED)) && (getSelectedComponents() || ((controlPoint.getVisibilityPolicy().equals(VisibilityPolicy.WHEN_SELECTED)) && (getSelectedComponents()
.contains(component))); .contains(component)));
@@ -5,8 +5,8 @@ import static org.junit.Assert.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.diylc.components.MockComponentInstance; import org.diylc.components.MockDIYComponent;
import org.diylc.core.IComponentInstance; import org.diylc.core.IDIYComponent;
import org.diylc.core.measures.Capacitance; import org.diylc.core.measures.Capacitance;
import org.diylc.core.measures.CapacitanceUnit; import org.diylc.core.measures.CapacitanceUnit;
import org.diylc.plugins.file.BomEntry; import org.diylc.plugins.file.BomEntry;
@@ -18,12 +18,12 @@ public class BomMakerTest {
@Test @Test
public void testCreateBom() { public void testCreateBom() {
List<IComponentInstance> components = new ArrayList<IComponentInstance>(); List<IDIYComponent> components = new ArrayList<IDIYComponent>();
MockComponentInstance c1 = new MockComponentInstance(); MockDIYComponent c1 = new MockDIYComponent();
c1.setC(new Capacitance(3d, CapacitanceUnit.pF)); c1.setC(new Capacitance(3d, CapacitanceUnit.pF));
components.add(c1); components.add(c1);
components.add(new MockComponentInstance()); components.add(new MockDIYComponent());
components.add(new MockComponentInstance()); components.add(new MockDIYComponent());
List<BomEntry> bom = BomMaker.getInstance().createBom(components); List<BomEntry> bom = BomMaker.getInstance().createBom(components);
assertNotNull(bom); assertNotNull(bom);
assertEquals(2, bom.size()); assertEquals(2, bom.size());
@@ -7,7 +7,7 @@ import java.util.List;
import org.diylc.common.ControlPointWrapper; import org.diylc.common.ControlPointWrapper;
import org.diylc.common.PropertyWrapper; import org.diylc.common.PropertyWrapper;
import org.diylc.components.MockComponentInstance; import org.diylc.components.MockDIYComponent;
import org.diylc.presenter.ComponentProcessor; import org.diylc.presenter.ComponentProcessor;
import org.junit.Test; import org.junit.Test;
@@ -17,7 +17,7 @@ public class ClassProcessorTest {
@Test @Test
public void testExtractProperties() { public void testExtractProperties() {
List<PropertyWrapper> properties = ComponentProcessor.getInstance().extractProperties( List<PropertyWrapper> properties = ComponentProcessor.getInstance().extractProperties(
MockComponentInstance.class); MockDIYComponent.class);
assertNotNull(properties); assertNotNull(properties);
assertEquals(4, properties.size()); assertEquals(4, properties.size());
}; };
@@ -25,7 +25,7 @@ public class ClassProcessorTest {
@Test @Test
public void testExtractControlPoints() { public void testExtractControlPoints() {
List<ControlPointWrapper> controlPoints = ComponentProcessor.getInstance() List<ControlPointWrapper> controlPoints = ComponentProcessor.getInstance()
.extractControlPoints(MockComponentInstance.class); .extractControlPoints(MockDIYComponent.class);
assertNotNull(controlPoints); assertNotNull(controlPoints);
assertEquals(1, controlPoints.size()); assertEquals(1, controlPoints.size());
}; };