From f07a5d27a3cdaba0a4660e842a32961d1d0f53ab Mon Sep 17 00:00:00 2001
From: Jordan Deyton <jordan.deyton@gmail.com>
Date: Mon, 5 Jan 2015 13:24:14 -0500
Subject: [PATCH] Adding a new class to add template parameters that are
 defined in the exemplars. Currently, it just finds the exemplar parameters
 and prints their names out to the console. Next up, we should expose these to
 the user somehow so they can pick one to add.

Signed-off-by: Jordan Deyton <jordan.deyton@gmail.com>
---
 .../widgets/properties/AddPropertyAction.java |  12 +-
 .../properties/AddTemplatePropertyAction.java | 116 ++++++++++++++++++
 .../properties/TreePropertySection.java       |   5 +
 3 files changed, 132 insertions(+), 1 deletion(-)
 create mode 100644 src/org.eclipse.ice.client.widgets/src/org/eclipse/ice/client/widgets/properties/AddTemplatePropertyAction.java

diff --git a/src/org.eclipse.ice.client.widgets/src/org/eclipse/ice/client/widgets/properties/AddPropertyAction.java b/src/org.eclipse.ice.client.widgets/src/org/eclipse/ice/client/widgets/properties/AddPropertyAction.java
index 97be8c544..bef96b49a 100644
--- a/src/org.eclipse.ice.client.widgets/src/org/eclipse/ice/client/widgets/properties/AddPropertyAction.java
+++ b/src/org.eclipse.ice.client.widgets/src/org/eclipse/ice/client/widgets/properties/AddPropertyAction.java
@@ -76,6 +76,16 @@ public class AddPropertyAction extends Action {
 		setEnabled((canAdd(tree) != null ? true : false));
 	}
 
+	/**
+	 * Gets the current {@link TreeComposite} to which properties (in the form
+	 * of {@link Entry}s) can be added.
+	 * 
+	 * @return The associated tree. This may be null.
+	 */
+	protected TreeComposite getTree() {
+		return tree;
+	}
+
 	/**
 	 * Gets whether or not the specified <code>TreeComposite</code> can have an
 	 * <code>Entry</code> (aka property or parameter) added to it. The
@@ -88,7 +98,7 @@ public class AddPropertyAction extends Action {
 	 * @return The tree's data node that can receive a new property or
 	 *         parameter, or null if one cannot be added.
 	 */
-	private DataComponent canAdd(TreeComposite tree) {
+	protected DataComponent canAdd(TreeComposite tree) {
 
 		DataComponent dataNode = null;
 
diff --git a/src/org.eclipse.ice.client.widgets/src/org/eclipse/ice/client/widgets/properties/AddTemplatePropertyAction.java b/src/org.eclipse.ice.client.widgets/src/org/eclipse/ice/client/widgets/properties/AddTemplatePropertyAction.java
new file mode 100644
index 000000000..07453f781
--- /dev/null
+++ b/src/org.eclipse.ice.client.widgets/src/org/eclipse/ice/client/widgets/properties/AddTemplatePropertyAction.java
@@ -0,0 +1,116 @@
+package org.eclipse.ice.client.widgets.properties;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.ice.datastructures.form.DataComponent;
+import org.eclipse.ice.datastructures.form.Entry;
+import org.eclipse.ice.datastructures.form.TreeComposite;
+import org.eclipse.ice.datastructures.updateableComposite.Component;
+
+// TODO documentation
+public class AddTemplatePropertyAction extends AddPropertyAction {
+
+	private List<Entry> properties;
+
+	@Override
+	public void run() {
+		super.run();
+
+		// TODO Remove this and do something useful.
+		for (Entry entry : properties) {
+			System.out.println(entry.getName());
+		}
+	}
+
+	/**
+	 * Searches the tree's parent to find the corresponding exemplar tree node
+	 * and retrieves all properties from the exemplar node if it exists.
+	 * <p>
+	 * In this case, properties correspond to <i>ready</i> {@code Entry}s inside
+	 * the exemplar tree node's active {@code DataComponent}.
+	 * </p>
+	 * 
+	 * @param tree
+	 *            The tree node to which template properties can be added.
+	 * @return A list containing the template properties. This method returns an
+	 *         empty list if no template properties can be found.
+	 */
+	private List<Entry> findTemplateProperties(TreeComposite tree) {
+
+		List<Entry> properties = null;
+
+		// Determine the exemplar tree node corresponding to the specified tree
+		// node.
+		if (tree != null) {
+			TreeComposite parent = tree.getParent();
+			if (parent != null && parent.hasChildExemplars()) {
+				DataComponent dataNode = null;
+				List<TreeComposite> exemplars = parent.getChildExemplars();
+				// Use the ID to see if the nodes match.
+				int id = tree.getId();
+				System.out.println(id);
+				System.out.println("Found the parent's child exemplars...");
+				for (TreeComposite exemplar : exemplars) {
+					System.out.println("exemplar: " + exemplar.getName() + " " + exemplar.getId());
+					if (exemplar.getId() == id) {
+						// If we found a match, pull the templated properties
+						// from the active data node.
+						dataNode = (DataComponent) exemplar.getActiveDataNode();
+						// FIXME Shouldn't the active data node be set?
+						if (dataNode == null) {
+							List<Component> dataNodes = exemplar.getDataNodes();
+							if (!dataNodes.isEmpty()) {
+								dataNode = (DataComponent) dataNodes.get(0);
+							}
+						}
+						if (dataNode != null) {
+							properties = dataNode.retrieveAllEntries();
+							// FIXME We should be retrieving ready entries, but there are none...
+//							properties = dataNode.retrieveReadyEntries();
+						}
+						break;
+					}
+				}
+			}
+		}
+
+		// Return an empty list if there was no corresponding exemplar with
+		// templated properties.
+		if (properties == null) {
+			properties = new ArrayList<Entry>(1);
+		}
+
+		return properties;
+	}
+
+	/**
+	 * Sets the current {@link TreeComposite} to which properties (in the form
+	 * of {@link Entry}s) can be added.
+	 * 
+	 * @param tree
+	 *            The parent tree. If null, the action is disabled.
+	 */
+	@Override
+	public void setTree(TreeComposite tree) {
+		// Clear the list of properties.
+		properties = new ArrayList<Entry>(1);
+		// Set the tree.
+		super.setTree(tree);
+		// Update the list of properties and set the enabled flag.
+		properties = findTemplateProperties(tree);
+		setEnabled(!properties.isEmpty());
+
+		return;
+	}
+
+	/**
+	 * Overrides the default behavior. If there are no {@link #properties}, then
+	 * the returned {@code DataComponent} is null.
+	 */
+	@Override
+	protected DataComponent canAdd(TreeComposite tree) {
+		return properties.isEmpty() ? null : super.canAdd(tree);
+	}
+
+}
diff --git a/src/org.eclipse.ice.client.widgets/src/org/eclipse/ice/client/widgets/properties/TreePropertySection.java b/src/org.eclipse.ice.client.widgets/src/org/eclipse/ice/client/widgets/properties/TreePropertySection.java
index 25510ca41..34ab7b854 100644
--- a/src/org.eclipse.ice.client.widgets/src/org/eclipse/ice/client/widgets/properties/TreePropertySection.java
+++ b/src/org.eclipse.ice.client.widgets/src/org/eclipse/ice/client/widgets/properties/TreePropertySection.java
@@ -124,6 +124,8 @@ public class TreePropertySection extends AbstractPropertySection implements
 	 * The ToolBar action that adds new, blank properties to the {@link #tree}.
 	 */
 	private final AddPropertyAction addAction = new AddPropertyAction();
+	// TODO documentation...
+	private final AddTemplatePropertyAction addTemplatePropertyAction = new AddTemplatePropertyAction();
 	/**
 	 * The ToolBar action that removes properties that are selected in the
 	 * {@link #tableViewer}.
@@ -291,8 +293,10 @@ public class TreePropertySection extends AbstractPropertySection implements
 		}
 
 		addAction.setTree(tree);
+		addTemplatePropertyAction.setTree(tree);
 		removeAction.setTree(tree);
 
+		// TODO Change this to be like the above 3 calls.
 		// Refresh the type Combo widget.
 		refreshTypeWidgets();
 
@@ -426,6 +430,7 @@ public class TreePropertySection extends AbstractPropertySection implements
 		// TODO Make this an ActionTree whose default action is to add a new,
 		// blank property, but with an option to add pre-defined properties.
 		toolBarManager.add(addAction);
+		toolBarManager.add(addTemplatePropertyAction);
 
 		// Create the remove action.
 		tableViewer.addSelectionChangedListener(removeAction);
-- 
GitLab