Skip to content

Commit

Permalink
Factory<> is now NodeFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Jan 19, 2024
1 parent d628cff commit 56b3d35
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 84 deletions.
39 changes: 20 additions & 19 deletions src/main/java/com/marginallyclever/ro3/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.marginallyclever.ro3.listwithevents.ListWithEvents;
import com.marginallyclever.ro3.mesh.MeshFactory;
import com.marginallyclever.ro3.node.nodefactory.NodeFactory;
import com.marginallyclever.ro3.node.nodes.*;
import com.marginallyclever.ro3.node.Node;
import com.marginallyclever.ro3.node.nodes.limbplanner.LimbPlanner;
Expand All @@ -24,32 +25,32 @@ public class Registry {
public static final EventListenerList listeners = new EventListenerList();
public static final TextureFactory textureFactory = new TextureFactory();
public static final MeshFactory meshFactory = new MeshFactory();
public static final NodeFactory<Node> nodeFactory = new NodeFactory<>(Node.class);
public static final NodeFactory nodeFactory = new NodeFactory();
private static Node scene = new Node("Scene");
public static final ListWithEvents<Camera> cameras = new ListWithEvents<>();
private static Camera activeCamera = null;
public static final ListWithEvents<Node> selection = new ListWithEvents<>();

public static void start() {
nodeFactory.clear();
NodeFactory.Category<Node> nodule = nodeFactory.getRoot().add("Node", Node::new);
nodule.add("DHParameter", DHParameter::new);
nodule.add("HingeJoint", HingeJoint::new);
nodule.add("LimbSolver", LimbSolver::new);
nodule.add("MarlinRobotArm", MarlinRobotArm::new);
nodule.add("Material", Material::new);
nodule.add("MeshInstance", MeshInstance::new);
nodule.add("Motor", Motor::new);
nodule.add("LimbPlanner", LimbPlanner::new);
NodeFactory.Category<Node> pose = nodule.add("Pose", Pose::new);
pose.add("Camera", Camera::new);
pose.add("LookAt", LookAt::new);
pose.add("Limb", Limb::new);
pose.add("AttachmentPoint", AttachmentPoint::new);/*
Factory.Category<Node> behavior = nodule.add("Behavior", null);
Factory.Category<Node> action = behavior.add("Action",null);
Factory.Category<Node> control = behavior.add("Control",null);
Factory.Category<Node> decorator = behavior.add("Decorator",null);
NodeFactory.Category nodule = nodeFactory.getRoot();
nodule.add("DHParameter", DHParameter::new);
nodule.add("HingeJoint", HingeJoint::new);
nodule.add("LimbPlanner", LimbPlanner::new);
nodule.add("LimbSolver", LimbSolver::new);
nodule.add("MarlinRobotArm", MarlinRobotArm::new);
nodule.add("Material", Material::new);
nodule.add("MeshInstance", MeshInstance::new);
nodule.add("Motor", Motor::new);
NodeFactory.Category pose = nodule.add("Pose", Pose::new);
pose.add("AttachmentPoint", AttachmentPoint::new);
pose.add("Camera", Camera::new);
pose.add("Limb", Limb::new);
pose.add("LookAt", LookAt::new);/*
Factory.Category behavior = nodule.add("Behavior", null);
Factory.Category action = behavior.add("Action",null);
Factory.Category control = behavior.add("Control",null);
Factory.Category decorator = behavior.add("Decorator",null);
decorator.add("ForceFailure", ForceFailure::new);
decorator.add("ForceSuccess", ForceSuccess::new);
decorator.add("Inverter", Inverter::new);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.marginallyclever.ro3.apps.actions;

import com.marginallyclever.ro3.apps.NodeFactoryPanel;
import com.marginallyclever.ro3.node.nodefactory.NodeFactoryPanel;
import com.marginallyclever.ro3.Registry;
import com.marginallyclever.ro3.node.Node;
import com.marginallyclever.ro3.apps.UndoSystem;
Expand All @@ -15,10 +15,9 @@
* {@link AddNode} is an {@link AbstractAction} to add a new instance of a {@link Node} to every selected branch of the
* tree. It will ask the user which type of {@link Node} to add, then create an
* {@link com.marginallyclever.ro3.apps.commands.AddNode} command to add the new {@link Node}.
* @param <T> the type of {@link Node} to add
*/
public class AddNode<T extends Node> extends AbstractAction {
private static final NodeFactoryPanel<Node> nfd = new NodeFactoryPanel<>(Registry.nodeFactory);
public class AddNode extends AbstractAction {
private static final NodeFactoryPanel nfd = new NodeFactoryPanel(Registry.nodeFactory);

public AddNode() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean
} else {
setForeground(Color.BLACK);
}
setIcon(branch.getNode().getIcon());
setToolTipText(branch.getNode().getClass().getSimpleName());
return this;
}
});
tree.setToolTipText("");
}

public void setSubject(T subject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import com.marginallyclever.ro3.apps.actions.*;
import com.marginallyclever.ro3.listwithevents.ItemAddedListener;
import com.marginallyclever.ro3.listwithevents.ItemRemovedListener;
import com.marginallyclever.ro3.node.Node;
import com.marginallyclever.ro3.node.NodeAttachListener;
import com.marginallyclever.ro3.node.NodeDetachListener;
import com.marginallyclever.ro3.node.NodeRenameListener;
import com.marginallyclever.ro3.node.*;
import com.marginallyclever.ro3.node.nodefactory.NodeTreeBranchRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -154,7 +152,7 @@ public void scanTree(Node toScan) {
}

private void buildToolBar() {
var addButton = new JButton(new AddNode<>());
var addButton = new JButton(new AddNode());
var cutButton = new JButton(cutNode);
var removeButton = new JButton(removeNode);
var copyButton = new JButton(copyNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public SearchBar(String text) {
match.setText(text);
match.getDocument().addDocumentListener(this);

isCaseSensitive.addActionListener(e -> fireMatchChange());
isRegex.addActionListener(e -> fireMatchChange());

addAncestorListener(new AncestorListener() {
@Override
public void ancestorAdded(javax.swing.event.AncestorEvent event) {
Expand Down Expand Up @@ -73,7 +76,7 @@ public void changedUpdate(DocumentEvent e) {
fireMatchChange();
}

public void fireMatchChange() {
protected void fireMatchChange() {
String newValue = match.getText();
super.firePropertyChange("match", null, newValue);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.marginallyclever.ro3;
package com.marginallyclever.ro3.node.nodefactory;

import com.marginallyclever.ro3.node.Node;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -10,35 +11,32 @@
import java.util.function.Supplier;

/**
* A factory that can be used to create objects. It does not manage the objects it creates.
* @param <T> The class of object to create.
* A factory that can be used to create Nodes. It does not manage the objects it creates.
*/
public class NodeFactory<T> {
public class NodeFactory {
private static final Logger logger = LoggerFactory.getLogger(NodeFactory.class);
private final Class<T> type;

/**
* A category of objects. These categories can be nested in a tree.
* @param <T> The class of object to create.
* Categories of Node types. These categories can be nested in a tree.
*/
public static class Category<T> {
public static class Category {
private final String name;
private final Supplier<T> supplier;
private final List<Category<T>> children = new ArrayList<>();
private Category<T> parent=null;
private final Supplier<Node> supplier;
private final List<Category> children = new ArrayList<>();
private Category parent=null;

public Category(String name,Supplier<T> supplier) {
public Category(String name,Supplier<Node> supplier) {
this.name = name;
this.supplier = supplier;
}

public void add(Category<T> c) {
public void add(Category c) {
children.add(c);
c.parent = this;
}

public Category<T> add(String name,Supplier<T> supplier) {
Category<T> item = new Category<>(name,supplier);
public Category add(String name, Supplier<Node> supplier) {
Category item = new Category(name,supplier);
add(item);
return item;
}
Expand All @@ -47,34 +45,33 @@ public String getName() {
return name;
}

public Category<T> getParent() {
public Category getParent() {
return parent;
}

public List<Category<T>> getChildren() {
public List<Category> getChildren() {
return children;
}

public Supplier<T> getSupplier() {
public Supplier<Node> getSupplier() {
return supplier;
}
}

private final Category<T> root = new Category<>("root",null);
private final Category root = new Category("Node",Node::new);

public NodeFactory(Class<T> type) {
this.type = type;
public NodeFactory() {
scan();
}

public Category<T> getRoot() {
public Category getRoot() {
return root;
}

public Supplier<T> getSupplierFor(String path) {
List<Category<T>> toCheck = new ArrayList<>(root.children);
public Supplier<Node> getSupplierFor(String path) {
List<Category> toCheck = new ArrayList<>(root.children);
while(!toCheck.isEmpty()) {
Category<T> current = toCheck.remove(0);
Category current = toCheck.remove(0);
toCheck.addAll(current.children);

if(current.name.equals(path)) {
Expand All @@ -85,8 +82,8 @@ public Supplier<T> getSupplierFor(String path) {
return null;
}

public T create(String path) {
Supplier<T> supplier = getSupplierFor(path);
public Node create(String path) {
Supplier<Node> supplier = getSupplierFor(path);
if(supplier==null) return null;
return supplier.get();
}
Expand All @@ -95,9 +92,9 @@ public void scan() {
// Create a new instance of Reflections
Reflections reflections = new Reflections("com.marginallyclever.ro3");
// Get all classes that extend T
Set<Class<? extends T>> found = reflections.getSubTypesOf(type);
Set<Class<? extends Node>> found = reflections.getSubTypesOf(Node.class);
// Now, classes contains all classes that extend T
for (Class<? extends T> clazz : found) {
for (Class<? extends Node> clazz : found) {
logger.info("Found " + clazz.getName());
}
}
Expand Down
Loading

0 comments on commit 56b3d35

Please sign in to comment.