jArchi - how to get the filename of the current open model

Started by Xavier Mayeur, February 15, 2019, 12:24:46 PM

Previous topic - Next topic

Xavier Mayeur

What is the method to call to retrieve the full path name of the document under which the current model is saved?

:P

Phil Beauvoir

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.
If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.

Hervé

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é

Phil Beauvoir

"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.
If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.

Xavier Mayeur

Thanks, it is a good enough workaround, waiting for a better structural solution. It works.

Phil Beauvoir

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.
If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.

Xavier Mayeur

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?

Phil Beauvoir

> Is there another method to do so?


Not at the moment. I'll add model.getPath() to the next version.
If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.