Archi Forum

Archi => Archi Development => Topic started by: abel on November 19, 2023, 20:49:19 PM

Title: How to get the selected model from an Archi plugin
Post by: abel on November 19, 2023, 20:49:19 PM
Hi all,
I'm trying to automatise actions with an Archi plugin over the current model, but I cannot find which one is it.

If you try to do the same using a JArchi is very easy, but I haven't found any method like "getTheCurrentModel()" or something similar. Could you give me a clue on how to do it?

Thanks in advance,
Title: Re: How to get the selected model from an Archi plugin
Post by: Phil Beauvoir on November 20, 2023, 10:35:26 AM
Hi, in the context of running Archi on the desktop, the "current" model is the one (if any) that has the focus, i.e. is selected, in the UI (Model Tree or diagram). Typically a plug-in will register itself as a selection listener in a View or menu. You can also get the current selection from the Workbench and drill into that to find what model object is selected, and the model from that.

jArchi does it as follows:

https://github.com/archimatetool/archi-scripting-plugin/blob/master/com.archimatetool.script/src/com/archimatetool/script/WorkbenchPartTracker.java

Depending on your needs you should take a look at how the Archi plug-ins manage the workbecnh selection to get the current model.
Title: Re: How to get the selected model from an Archi plugin
Post by: abel on November 21, 2023, 02:22:22 AM
Thanks Phil.

I will try to follow the JArchi behaviour in my plugin.
Title: Re: How to get the selected model from an Archi plugin
Post by: abel on November 24, 2023, 11:25:42 AM
Hi Phil,
 It runs like a charm!!

Thanks again :-)
Title: Re: How to get the selected model from an Archi plugin
Post by: Phil Beauvoir on November 24, 2023, 11:38:40 AM
If you want to access the current selected model statically you can do it this way:

IWorkbenchPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().getActivePart();
if(part != null) {
    IArchimateModel model = part.getAdapter(IArchimateModel.class);
}

(check for possible null values in that first line)