Code Consistency with com.archimatetool.editor.treeModelView

Started by Lieven, December 07, 2024, 13:51:55 PM

Previous topic - Next topic

Lieven

Hi,

According to me the tree com.archimatetool.editor.treeModelView view.viewer.tree is not completely built upon initialisation when opening a file.  As a consequence code like the one below produces different results.  So no consistency can be reached.

As an example, when opening the model with input "test" it produces the result of screenshot Opening Model.  When you select the first level folder "New Folder", it produces the result of screenshot Result1.  When you select the second level folder "New Folder", it produces the result of screenshot Result2.

Can you explain how this tree is built and how to fix this problem so consistency can be reached ? Thanks in advance.

Br, Lieven

https://gist.github.com/ThomasRohde/ce9ee7b2f94fec8e4b9687e25a2b92fa?permalink_comment_id=4520807#file-find-elements-ajs-L10

function locateModelTree() {
    page = Java.type('org.eclipse.ui.PlatformUI').getWorkbench().getActiveWorkbenchWindow().getActivePage();
    view = page.findView("com.archimatetool.editor.treeModelView");
    return view != null ? view.viewer.tree : null;
}

function findSearchTerm(root, term) {
  if (!root || !term) return [];
  let searchTerm = term.toLowerCase();
  let result = [];
  try {
    let candidate = root.getText().toLowerCase();
    if (candidate.includes(searchTerm)) {
      result.push(root);
    }
  } catch (error) {
    // Ignore, probably some object not responding to getText()
  }
  for (const item of root.getItems()) {
    result = [...result, ...findSearchTerm(item, term)];
  };
  return result;
};

let modelTree = locateModelTree();
let searchTerm = window.prompt("Enter a search term", "Application");
let results = findSearchTerm(modelTree, searchTerm);
modelTree.setSelection(results);

Phil Beauvoir

#1
Hi,

you shouldn't be accessing Tree Items. Tree Items may or may not be present depending on whether the Tree is expanded or not. This is because the Tree is "lazily" loaded and only creates Tree Items as needed. If you want to do this kind of thing you have to access the model in the Tree's underlying TreeModelViewer - I don't know how this would work in jArchi as this kind of internal access is not really supported. It would probably involve accessing the Content Provider and iterating through the objects.

Phil
If you value and use Archi, please consider making a donation.
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.

Lieven

Hi,

Thanks for the confirmation.

I have experienced this "lazily" loading of the tree in a couple of scripts I have written. I found a solution to drive this "lazily" loading with 'setSelection'.

Br,

Lieven