Last active
April 10, 2023 14:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this: