Skip to content

Instantly share code, notes, and snippets.

@ThomasRohde
Last active April 10, 2023 14:01
Show Gist options
  • Save ThomasRohde/ce9ee7b2f94fec8e4b9687e25a2b92fa to your computer and use it in GitHub Desktop.
Save ThomasRohde/ce9ee7b2f94fec8e4b9687e25a2b92fa to your computer and use it in GitHub Desktop.
This script allows you to enter a search term. The case-insensitive, sub-string, matches will be selected in the model browser. #JArchi, #Archi
/*
Author: Thomas Klok Rohde
Description: Select all elements in the model browser that includes the search term (case insensitive)
History:
March 30, 2023 : Created
*/
const eclipseShell = Java.type('org.eclipse.swt.widgets.Shell');
const TreeItem = Java.type('org.eclipse.swt.widgets.TreeItem');
const Tree = Java.type('org.eclipse.swt.widgets.Tree');
function locateModelTree() {
let trees = [];
function findTrees(obj) {
if (obj) {
try {
let children = obj.getChildren();
children.forEach(function (child) {
if (child instanceof Tree) {
trees.push(child);
}
findTrees(child);
})
}
catch (e) {
// Ignore
}
}
}
let shells = shell.getDisplay().getShells();
for (var i = 0; i < shells.length; i++) {
if (shells[i] instanceof eclipseShell) {
let thisShell = shells[i];
findTrees(thisShell);
}
}
let modelTree = null;
trees.forEach(function (tree) {
let matches = 0;
for (const item of tree.getItems()) {
for (const folder of item.getItems()) {
let folderName = folder.getText();
if (['Application', 'Business', 'Motivation', 'Other', 'Relations'].includes(folderName))
matches += 1;
}
};
if (matches >= 5) {
modelTree = tree;
}
});
return modelTree;
}
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);
@Phillipus
Copy link

Try this:

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment