Archi Forum

Archi Plug-ins => jArchi => Topic started by: Xavier Mayeur on February 15, 2019, 12:24:46 PM

Title: jArchi - how to get the filename of the current open model
Post by: Xavier Mayeur on February 15, 2019, 12:24:46 PM
What is the method to call to retrieve the full path name of the document under which the current model is saved?

:P
Title: Re: jArchi - how to get the filename of the current open model
Post by: Phil Beauvoir on February 15, 2019, 12:38:59 PM
Actually, we don't have a method for that so maybe we should.

Can you get it from the context of when you load it or save it? i.e you'll know what it is if you load it and when you save it.
Title: Re: jArchi - how to get the filename of the current open model
Post by: Hervé on February 15, 2019, 13:04:39 PM
Hi Xavier,

Until the method is natively provided, you may use reflection to get if (I know it's bad  :-[)
// we check that a model or a model's component is selected
if ( model == null || model.id == null )
fatal("You must select a model or some Archimate elements.");

try {
// jArchi provides a ArchimateModelProxy class instead of a ArchimateModel class
// unfortunately, the getEObject() method that provides the underlying ArchimateModel class is protected
// so we use reflection to invoke this method.
var method = Packages.com.archimatetool.script.dom.model.ArchimateModelProxy.class.getDeclaredMethod("getEObject");
method.setAccessible(true);
var m = method.invoke(model);
console.log(m.getFile())
} catch (e) {
console.error("Failed to get the model's filename.");
}


if the m variable is null, this means that the model has not got any file attached.

Best regards
Hervé
Title: Re: jArchi - how to get the filename of the current open model
Post by: Phil Beauvoir on February 15, 2019, 13:09:50 PM
"unfortunately, the getEObject() method that provides the underlying ArchimateModel class is protected"

For a very good reason. If you set anything on an underlying EObject you will break the underlying undo/redo stack and possibly corrupt the model.

The proxy classes are there to supply undo/redo actions and to protect against model damage.
Title: Re: jArchi - how to get the filename of the current open model
Post by: Xavier Mayeur on February 15, 2019, 14:38:02 PM
Thanks, it is a good enough workaround, waiting for a better structural solution. It works.
Title: Re: jArchi - how to get the filename of the current open model
Post by: Phil Beauvoir on February 15, 2019, 15:11:04 PM
I'll add model.getPath() to the next version.

But I'm interested to know when you would need this given that you know what the file path is before loading a model, and when saving it.

BTW - don't set any fields using Java reflection as it could mess up the stack.
Title: Re: jArchi - how to get the filename of the current open model
Post by: Xavier Mayeur on February 15, 2019, 19:44:13 PM
I am enhancing Steven Mileham's script that export the model views's to a MarkDOwn file. I want to export the view of the model I am currently working on to .md files in the same directory that the open document. Actually, I don't open the model using the script - I activate the script from the model and ideally automatically at model closure if possible (would need a kind of 'onClose' event at model close...) Therefore, I need to retrieve from somewhere the model file path to save the export in the same directory... Is there another method to do so?
Title: Re: jArchi - how to get the filename of the current open model
Post by: Phil Beauvoir on February 15, 2019, 20:00:21 PM
> Is there another method to do so?


Not at the moment. I'll add model.getPath() to the next version.