Skip to content

Commit

Permalink
Task #226 Creating a Class for Importing Data from Comet
Browse files Browse the repository at this point in the history
  • Loading branch information
ngat_di authored and ngat_di committed May 14, 2024
1 parent 69f87b3 commit 0d290d3
Showing 1 changed file with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@
package de.dlr.sc.virsat.model.extension.cefx.ui.importWizards;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ISharedImages;
import cdp4common.dto.Thing;
import java.util.List;

/**
* Utility class to populate an SWT Tree with data represented by Thing objects.
*/

public class TreePopulator {

// Constants
private static final String ROOT_ITEM_TEXT = "Comet Data";
private static Image nodeImage; // Image used for non-leaf tree nodes

/**
* Private constructor to prevent instantiation of this utility class.
Expand All @@ -32,6 +35,13 @@ private TreePopulator() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

/**
* Initializes images for the tree nodes from Eclipse's shared image repository.
*/
private static void initImages() {
nodeImage = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
}

/**
* Populates the given SWT Tree with data from a list of Thing objects.
* It creates a root item and attaches child items representing each Thing.
Expand All @@ -40,10 +50,17 @@ private TreePopulator() {
* @param things The list of Thing objects retrieved from the Comet server.
*/
public static void populateTree(Tree tree, List<Thing> things) {
initImages(); // Initialize images before populating the tree
tree.removeAll(); // Clear previous items if any
TreeItem rootItem = new TreeItem(tree, SWT.NONE);
rootItem.setText(ROOT_ITEM_TEXT);
things.forEach(thing -> createTreeItem(rootItem, thing));
expandAll(tree.getItems());
rootItem.setImage(nodeImage); // Set the image for the root node
for (Thing thing : things) {
TreeItem item = createTreeItem(rootItem, thing);
// Recursively add child items
populateTreeItem(item, thing);
}
expandAll(tree.getItems()); // Optionally expand all nodes.
}

/**
Expand All @@ -52,10 +69,51 @@ public static void populateTree(Tree tree, List<Thing> things) {
*
* @param parentItem The parent TreeItem under which the new item will be created.
* @param thing The Thing object to create a tree item for.
* @return The created TreeItem.
*/
private static void createTreeItem(TreeItem parentItem, Thing thing) {
private static TreeItem createTreeItem(TreeItem parentItem, Thing thing) {
TreeItem item = new TreeItem(parentItem, SWT.NONE);
item.setText(thing.toString());
item.setText(thing.toString());
if (!isLeaf(thing)) {
item.setImage(nodeImage); // Set the image for non-leaf nodes
}
return item;
}

/**
* Populates a tree item with child items representing the contained Things.
*
* @param parentItem The parent TreeItem.
* @param parentThing The parent Thing object.
*/
private static void populateTreeItem(TreeItem parentItem, Thing parentThing) {
for (List<?> containerList : parentThing.getContainerLists()) {
for (Object obj : containerList) {
if (obj instanceof Thing) {
Thing childThing = (Thing) obj;
TreeItem childItem = createTreeItem(parentItem, childThing);
// Recursively add child items
populateTreeItem(childItem, childThing);
}
}
}
}

/**
* Determines if this Thing is a leaf node in the hierarchy.
* A leaf node is defined as not having any other Thing in its container properties.
*
* @param thing The Thing object to check.
* @return true if this Thing is a leaf, false otherwise.
*/
private static boolean isLeaf(Thing thing) {
// Check if all container lists are empty, which would indicate no children
for (List<?> containerList : thing.getContainerLists()) {
if (!containerList.isEmpty()) {
return false; // Found a non-empty container list, hence it's not a leaf
}
}
return true; // No children found, it's a leaf
}

/**
Expand All @@ -72,4 +130,13 @@ private static void expandAll(TreeItem[] items) {
}
}
}

/**
* Disposes of the resources used by the TreePopulator.
*/
public static void dispose() {
if (nodeImage != null) {
nodeImage.dispose();
}
}
}

0 comments on commit 0d290d3

Please sign in to comment.