simplified component framework

This commit is contained in:
bancika
2011-01-05 00:20:09 +00:00
parent 69acfcff2f
commit dd50ccf785
12 changed files with 55 additions and 50 deletions
@@ -87,14 +87,14 @@ public class DIL_IC implements IDIYComponent<String> {
g2d.drawRoundRect(x, y, width, height, EDGE_RADIUS, EDGE_RADIUS); g2d.drawRoundRect(x, y, width, height, EDGE_RADIUS, EDGE_RADIUS);
// Draw label. // Draw label.
g2d.setFont(Constants.LABEL_FONT); g2d.setFont(Constants.LABEL_FONT);
g2d.setColor(LABEL_COLOR); g2d.setColor(componentState == ComponentState.DRAGGING ? BORDER_COLOR : LABEL_COLOR);
FontMetrics fontMetrics = g2d.getFontMetrics(g2d.getFont()); FontMetrics fontMetrics = g2d.getFontMetrics(g2d.getFont());
Rectangle2D rect = fontMetrics.getStringBounds(getName(), g2d); Rectangle2D rect = fontMetrics.getStringBounds(getName(), g2d);
int textHeight = (int) (rect.getHeight()); int textHeight = (int) (rect.getHeight());
int textWidth = (int) (rect.getWidth()); int textWidth = (int) (rect.getWidth());
// Center text horizontally and vertically // Center text horizontally and vertically
x = (width - textWidth) / 2; x += (width - textWidth) / 2;
y = (height - textHeight) / 2 + fontMetrics.getAscent(); y += (height - textHeight) / 2 + fontMetrics.getAscent();
g2d.drawString(getName(), x, y); g2d.drawString(getName(), x, y);
} }
@@ -18,7 +18,7 @@ import org.diylc.core.IDIYComponent;
* *
* @author Branislav Stojkovic * @author Branislav Stojkovic
*/ */
public class ComponentSelection extends ArrayList<IDIYComponent> 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<IDIYComponent> implements Tran
super(); super();
} }
public ComponentSelection(Collection<IDIYComponent> selectedComponents) { public ComponentSelection(Collection<IDIYComponent<?>> selectedComponents) {
super(selectedComponents); super(selectedComponents);
} }
@@ -174,7 +174,7 @@ public interface IPlugInPort {
* *
* @return * @return
*/ */
Area getComponentArea(IDIYComponent 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.
@@ -217,7 +217,7 @@ public interface IPlugInPort {
* @param components * @param components
* @param preferredPoint * @param preferredPoint
*/ */
void addComponents(List<IDIYComponent> 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.
@@ -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<IDIYComponent> components; private List<IDIYComponent<?>> components;
public Project() { public Project() {
components = new ArrayList<IDIYComponent>(); 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<IDIYComponent> 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<IDIYComponent> getComponents() { public List<IDIYComponent<?>> getComponents() {
return components; return components;
} }
@@ -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<IDIYComponent> components = (List<IDIYComponent>) 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) {
@@ -23,9 +23,9 @@ public class BomMaker {
private BomMaker() { private BomMaker() {
} }
public List<BomEntry> createBom(List<IDIYComponent> 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 (IDIYComponent component : components) { for (IDIYComponent<?> component : components) {
String name = component.getName(); String name = component.getName();
String value = component.getValue() == null ? "" : component.getValue().toString(); String value = component.getValue() == null ? "" : component.getValue().toString();
if ((name != null) && (value != null)) { if ((name != null) && (value != null)) {
@@ -45,7 +45,7 @@ public class StatusBar extends JPanel implements IPlugIn {
// State variables // State variables
private ComponentType componentSlot; private ComponentType componentSlot;
private List<IDIYComponent> componentsUnderCursor; private List<IDIYComponent<?>> componentsUnderCursor;
public StatusBar() { public StatusBar() {
super(); super();
@@ -165,8 +165,8 @@ public class StatusBar extends JPanel implements IPlugIn {
refreshStatusText(); refreshStatusText();
break; break;
case AVAILABLE_CTRL_POINTS_CHANGED: case AVAILABLE_CTRL_POINTS_CHANGED:
componentsUnderCursor = new ArrayList<IDIYComponent>( componentsUnderCursor = new ArrayList<IDIYComponent<?>>(
((Map<IDIYComponent, ControlPointWrapper>) params[0]).keySet()); ((Map<IDIYComponent<?>, ControlPointWrapper>) params[0]).keySet());
Collections.sort(componentsUnderCursor, ComparatorFactory.getInstance() Collections.sort(componentsUnderCursor, ComparatorFactory.getInstance()
.getComponentNameComparator()); .getComponentNameComparator());
refreshStatusText(); refreshStatusText();
@@ -181,7 +181,7 @@ public class StatusBar extends JPanel implements IPlugIn {
} else { } else {
String formattedNames = ""; String formattedNames = "";
int n = 1; int n = 1;
for (IDIYComponent component : componentsUnderCursor) { for (IDIYComponent<?> component : componentsUnderCursor) {
if (n > 1) { if (n > 1) {
formattedNames += ", "; formattedNames += ", ";
} }
@@ -1,5 +1,6 @@
package org.diylc.plugins.toolbox; package org.diylc.plugins.toolbox;
import java.awt.Insets;
import java.awt.dnd.DnDConstants; import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragSource; import java.awt.dnd.DragSource;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@@ -20,6 +21,8 @@ class ComponentButton extends JButton {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static int MARGIN = 2;
private IPlugInPort plugInPort; private IPlugInPort plugInPort;
private ComponentType componentType; private ComponentType componentType;
@@ -28,6 +31,8 @@ class ComponentButton extends JButton {
this.plugInPort = plugInPort; this.plugInPort = plugInPort;
this.componentType = componentType; this.componentType = componentType;
setMargin(new Insets(MARGIN, MARGIN, MARGIN, MARGIN));
setToolTipText("<html><b>" + componentType.getName() + "</b><br>" setToolTipText("<html><b>" + componentType.getName() + "</b><br>"
+ componentType.getDescription() + "<br>Author: " + componentType.getAuthor() + componentType.getDescription() + "<br>Author: " + componentType.getAuthor()
+ "</html>"); + "</html>");
@@ -8,7 +8,7 @@ public class ComparatorFactory {
private static ComparatorFactory instance; private static ComparatorFactory instance;
private Comparator<IDIYComponent> componentNameComparator; private Comparator<IDIYComponent<?>> componentNameComparator;
public static ComparatorFactory getInstance() { public static ComparatorFactory getInstance() {
if (instance == null) { if (instance == null) {
@@ -17,12 +17,12 @@ public class ComparatorFactory {
return instance; return instance;
} }
public Comparator<IDIYComponent> getComponentNameComparator() { public Comparator<IDIYComponent<?>> getComponentNameComparator() {
if (componentNameComparator == null) { if (componentNameComparator == null) {
componentNameComparator = new Comparator<IDIYComponent>() { componentNameComparator = new Comparator<IDIYComponent<?>>() {
@Override @Override
public int compare(IDIYComponent o1, IDIYComponent o2) { public int compare(IDIYComponent<?> o1, IDIYComponent<?> o2) {
String name1 = o1.getName(); String name1 = o1.getName();
String name2 = o2.getName(); String name2 = o2.getName();
if (name1 == null || name2 == null) { if (name1 == null || name2 == null) {
@@ -133,12 +133,12 @@ public class ComponentProcessor {
* @param selectedComponents * @param selectedComponents
* @return * @return
*/ */
public List<PropertyWrapper> getMutualSelectionProperties(List<IDIYComponent> selectedComponents) { public List<PropertyWrapper> getMutualSelectionProperties(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>();
IDIYComponent 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) {
@@ -150,7 +150,7 @@ public class ComponentProcessor {
} }
} }
for (int i = 1; i < selectedComponents.size(); i++) { for (int i = 1; i < selectedComponents.size(); i++) {
IDIYComponent 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 {
@@ -71,7 +71,7 @@ public class Presenter implements IPlugInPort {
public static boolean ENABLE_ANTIALIASING = true; public static boolean ENABLE_ANTIALIASING = true;
private double zoomLevel = 1; private double zoomLevel = 1;
private Map<IDIYComponent, Area> componentAreaMap; private Map<IDIYComponent<?>, Area> componentAreaMap;
private Project currentProject; private Project currentProject;
private Map<String, List<ComponentType>> componentTypes; private Map<String, List<ComponentType>> componentTypes;
private List<IPlugIn> plugIns; private List<IPlugIn> plugIns;
@@ -79,7 +79,7 @@ public class Presenter implements IPlugInPort {
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<IDIYComponent, ControlPointWrapper> componentControlPointMap; private Map<IDIYComponent<?>, ControlPointWrapper> componentControlPointMap;
private Cloner cloner; private Cloner cloner;
@@ -104,7 +104,7 @@ public class Presenter implements IPlugInPort {
public Presenter(IView view) { public Presenter(IView view) {
super(); super();
this.view = view; this.view = view;
componentAreaMap = new HashMap<IDIYComponent, 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();
@@ -149,7 +149,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<IDIYComponent, 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);
} }
@@ -293,10 +293,10 @@ public class Presenter implements IPlugInPort {
// g2dWrapper.resetTx(); // g2dWrapper.resetTx();
List<IDIYComponent> components = getCurrentProject().getComponents(); List<IDIYComponent<?>> components = getCurrentProject().getComponents();
componentAreaMap.clear(); componentAreaMap.clear();
if (components != null) { if (components != null) {
for (IDIYComponent 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)
@@ -387,9 +387,9 @@ public class Presenter implements IPlugInPort {
* *
* @return * @return
*/ */
private List<IDIYComponent> findComponentsAt(Point point) { private List<IDIYComponent<?>> findComponentsAt(Point point) {
List<IDIYComponent> components = new ArrayList<IDIYComponent>(); List<IDIYComponent<?>> components = new ArrayList<IDIYComponent<?>>();
for (Map.Entry<IDIYComponent, 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());
} }
@@ -412,12 +412,12 @@ public class Presenter implements IPlugInPort {
} }
setNewComponentSlot(null); setNewComponentSlot(null);
} else { } else {
List<IDIYComponent> 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 {
IDIYComponent 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) {
@@ -439,9 +439,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<IDIYComponent, ControlPointWrapper> components = new HashMap<IDIYComponent, ControlPointWrapper>(); Map<IDIYComponent<?>, ControlPointWrapper> components = new HashMap<IDIYComponent<?>, ControlPointWrapper>();
Point scaledPoint = scalePoint(point); Point scaledPoint = scalePoint(point);
for (IDIYComponent 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) {
@@ -475,7 +475,7 @@ public class Presenter implements IPlugInPort {
} }
@Override @Override
public Area getComponentArea(IDIYComponent component) { public Area getComponentArea(IDIYComponent<?> component) {
return componentAreaMap.get(component); return componentAreaMap.get(component);
} }
@@ -491,7 +491,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<IDIYComponent> 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();
@@ -504,7 +504,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 {
IDIYComponent 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)) {
@@ -524,7 +524,7 @@ 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).
IDIYComponent 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!");
@@ -543,11 +543,11 @@ public class Presenter implements IPlugInPort {
/ Constants.GRID) * Constants.GRID); / Constants.GRID) * Constants.GRID);
previousDragPoint.setLocation(x, y); previousDragPoint.setLocation(x, y);
for (Entry<IDIYComponent, ControlPointWrapper> entry : componentControlPointMap for (Entry<IDIYComponent<?>, ControlPointWrapper> entry : componentControlPointMap
.entrySet()) { .entrySet()) {
try { try {
controlPoint = entry.getValue(); controlPoint = entry.getValue();
IDIYComponent 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);
@@ -587,7 +587,7 @@ public class Presenter implements IPlugInPort {
this.selectionRect = Utils.createRectangle(scaledPoint, previousDragPoint); this.selectionRect = Utils.createRectangle(scaledPoint, previousDragPoint);
} }
selectedComponents.clear(); selectedComponents.clear();
for (IDIYComponent 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);
@@ -615,7 +615,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 (IDIYComponent 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) {
@@ -639,7 +639,7 @@ public class Presenter implements IPlugInPort {
} }
@Override @Override
public void addComponents(List<IDIYComponent> 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);
@@ -778,7 +778,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 (IDIYComponent component : selectedComponents) { for (IDIYComponent<?> component : selectedComponents) {
for (PropertyWrapper property : properties) { for (PropertyWrapper property : properties) {
property.writeTo(component); property.writeTo(component);
} }
@@ -814,7 +814,7 @@ public class Presenter implements IPlugInPort {
(int) (point.y / zoomLevel)); (int) (point.y / zoomLevel));
} }
private boolean shouldShowControlPoint(ControlPointWrapper controlPoint, IDIYComponent component) { private boolean shouldShowControlPoint(ControlPointWrapper controlPoint, IDIYComponent<?> 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)));
@@ -18,7 +18,7 @@ public class BomMakerTest {
@Test @Test
public void testCreateBom() { public void testCreateBom() {
List<IDIYComponent> components = new ArrayList<IDIYComponent>(); List<IDIYComponent<?>> components = new ArrayList<IDIYComponent<?>>();
MockDIYComponent c1 = new MockDIYComponent(); MockDIYComponent c1 = new MockDIYComponent();
c1.setC(new Capacitance(3d, CapacitanceUnit.pF)); c1.setC(new Capacitance(3d, CapacitanceUnit.pF));
components.add(c1); components.add(c1);