mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
simplified component framework
This commit is contained in:
+11
-5
@@ -4,8 +4,6 @@ import java.awt.Graphics2D;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.diylc.core.annotations.ComponentDescriptor;
|
||||
import org.diylc.core.annotations.ComponentName;
|
||||
import org.diylc.core.annotations.ComponentValue;
|
||||
import org.diylc.core.annotations.ControlPoint;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
|
||||
@@ -18,8 +16,6 @@ import org.diylc.core.annotations.EditableProperty;
|
||||
* <ul>
|
||||
* <li>Must have an empty constructor.</li>
|
||||
* <li>Class should be annotated with {@link ComponentDescriptor}.</li>
|
||||
* <li>Exactly one getter should be annotated with {@link ComponentName}.</li>
|
||||
* <li>Exactly one getter should be annotated with {@link ComponentValue}.</li>
|
||||
* <li>Control point getters need to be annotated with {@link ControlPoint}.</li>
|
||||
* <li>Getters for properties editable by users should be annotated with
|
||||
* {@link EditableProperty}.</li>
|
||||
@@ -28,8 +24,18 @@ import org.diylc.core.annotations.EditableProperty;
|
||||
* </ul>
|
||||
*
|
||||
* @author Branislav Stojkovic
|
||||
*
|
||||
* @param <T>
|
||||
* type of component values, e.g. Resistance for resistors or String
|
||||
* for transistors.
|
||||
*/
|
||||
public interface IDIYComponent extends Serializable {
|
||||
public interface IDIYComponent<T> extends Serializable {
|
||||
|
||||
String getName();
|
||||
|
||||
void setName(String name);
|
||||
|
||||
T getValue();
|
||||
|
||||
/**
|
||||
* Draws the component onto the {@link Graphics2D}.
|
||||
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
package org.diylc.core.annotations;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
/**
|
||||
* Annotates getter for the component name that will be shown in the BOM. Only
|
||||
* one getter per class may be tagged with this annotation and, must return
|
||||
* string type and there has to be a matching setter.
|
||||
*
|
||||
* @author Branislav Stojkovic
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ComponentName {
|
||||
|
||||
}
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
package org.diylc.core.annotations;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
/**
|
||||
* Annotates getter for the component value that will be shown in the BOM. Only
|
||||
* one getter per class may be tagged with this annotation.
|
||||
*
|
||||
* @author Branislav Stojkovic
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ComponentValue {
|
||||
|
||||
}
|
||||
+2
-4
@@ -8,8 +8,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.diylc.core.IDIYComponent;
|
||||
import org.diylc.presenter.ComponentProcessor;
|
||||
|
||||
|
||||
public class BomMaker {
|
||||
|
||||
@@ -28,8 +26,8 @@ public class BomMaker {
|
||||
public List<BomEntry> createBom(List<IDIYComponent> components) {
|
||||
Map<String, BomEntry> entryMap = new HashMap<String, BomEntry>();
|
||||
for (IDIYComponent component : components) {
|
||||
String name = ComponentProcessor.getInstance().extractComponentName(component);
|
||||
String value = ComponentProcessor.getInstance().extractComponentValue(component);
|
||||
String name = component.getName();
|
||||
String value = component.getValue() == null ? "" : component.getValue().toString();
|
||||
if ((name != null) && (value != null)) {
|
||||
String key = name + "|" + value;
|
||||
if (entryMap.containsKey(key)) {
|
||||
|
||||
+1
-3
@@ -25,7 +25,6 @@ import org.diylc.common.IPlugIn;
|
||||
import org.diylc.common.IPlugInPort;
|
||||
import org.diylc.core.IDIYComponent;
|
||||
import org.diylc.presenter.ComparatorFactory;
|
||||
import org.diylc.presenter.ComponentProcessor;
|
||||
|
||||
import com.diyfever.gui.MemoryBar;
|
||||
import com.diyfever.gui.miscutils.PercentageListCellRenderer;
|
||||
@@ -186,8 +185,7 @@ public class StatusBar extends JPanel implements IPlugIn {
|
||||
if (n > 1) {
|
||||
formattedNames += ", ";
|
||||
}
|
||||
formattedNames += ComponentProcessor.getInstance().extractComponentName(
|
||||
component);//
|
||||
formattedNames += component.getName();//
|
||||
// +
|
||||
// " (<b><font color=\"blue\">"
|
||||
// +
|
||||
|
||||
+2
-2
@@ -23,8 +23,8 @@ public class ComparatorFactory {
|
||||
|
||||
@Override
|
||||
public int compare(IDIYComponent o1, IDIYComponent o2) {
|
||||
String name1 = ComponentProcessor.getInstance().extractComponentName(o1);
|
||||
String name2 = ComponentProcessor.getInstance().extractComponentName(o2);
|
||||
String name1 = o1.getName();
|
||||
String name2 = o2.getName();
|
||||
if (name1 == null || name2 == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
-66
@@ -11,8 +11,6 @@ import org.apache.log4j.Logger;
|
||||
import org.diylc.common.ControlPointWrapper;
|
||||
import org.diylc.common.PropertyWrapper;
|
||||
import org.diylc.core.IDIYComponent;
|
||||
import org.diylc.core.annotations.ComponentName;
|
||||
import org.diylc.core.annotations.ComponentValue;
|
||||
import org.diylc.core.annotations.ControlPoint;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
|
||||
@@ -174,68 +172,4 @@ public class ComponentProcessor {
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the method annotated with {@link ComponentName} annotations and
|
||||
* calls it to retrieve component name.
|
||||
*
|
||||
* @param component
|
||||
* @return
|
||||
*/
|
||||
public String extractComponentName(IDIYComponent component) {
|
||||
String name = null;
|
||||
for (Method method : component.getClass().getMethods()) {
|
||||
if (method.isAnnotationPresent(ComponentName.class)) {
|
||||
try {
|
||||
name = method.invoke(component).toString();
|
||||
} catch (Exception e) {
|
||||
name = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the method annotated with {@link ComponentName} and it's matching
|
||||
* setter and calls the setter to update the component name.
|
||||
*
|
||||
* @param component
|
||||
* @param newName
|
||||
*/
|
||||
public void writeComponentName(IDIYComponent component, String newName) {
|
||||
for (Method method : component.getClass().getMethods()) {
|
||||
if (method.isAnnotationPresent(ComponentName.class)) {
|
||||
try {
|
||||
Method setter = component.getClass().getMethod(
|
||||
method.getName().replaceFirst("get", "set"), String.class);
|
||||
setter.invoke(component, newName);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Could not update component name on a component of type "
|
||||
+ component.getClass().getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the method annotated with {@link ComponentValue} annotations and
|
||||
* calls it to retrieve component value.
|
||||
*
|
||||
* @param component
|
||||
* @return
|
||||
*/
|
||||
public String extractComponentValue(IDIYComponent component) {
|
||||
String value = null;
|
||||
for (Method method : component.getClass().getMethods()) {
|
||||
if (method.isAnnotationPresent(ComponentValue.class)) {
|
||||
try {
|
||||
value = method.invoke(component).toString();
|
||||
} catch (Exception e) {
|
||||
value = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -716,14 +716,13 @@ public class Presenter implements IPlugInPort {
|
||||
String name = namePrefix + i;
|
||||
exists = false;
|
||||
for (IDIYComponent c : currentProject.getComponents()) {
|
||||
String cName = ComponentProcessor.getInstance().extractComponentName(c);
|
||||
if (cName.equals(name)) {
|
||||
if (c.getName().equals(name)) {
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ComponentProcessor.getInstance().writeComponentName(component, namePrefix + i);
|
||||
component.setName(namePrefix + i);
|
||||
|
||||
// Add it to the project.
|
||||
currentProject.getComponents().add(component);
|
||||
|
||||
+3
@@ -2,6 +2,7 @@ package org.diylc.utils;
|
||||
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.Stroke;
|
||||
import java.awt.Toolkit;
|
||||
|
||||
@@ -12,6 +13,8 @@ public class Constants {
|
||||
public static final Color CANVAS_COLOR = Color.white;
|
||||
public static final Color GRID_COLOR = new Color(240, 240, 240);
|
||||
|
||||
public static Font LABEL_FONT = new Font("Tahoma", Font.BOLD, 11);
|
||||
|
||||
public static final Stroke dashedStroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
|
||||
BasicStroke.JOIN_MITER, 2.0f, new float[] { 2.0f }, 0.0f);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user