Accessing Archi's Preferences - archimatetool/archi-scripting-plugin GitHub Wiki

Introduction

This page shows how to access Archi's internal Preference Store in order to query and set various preferences.

[!CAUTION] This is not an official API and some constants may change.
Setting preferences to unsupported values may result in unknown behaviour.

For a list of preference constants please refer to the IPreferenceConstants class.

Accessing the Archi Preference Store

To access the Archi Preference Store use the following jArchi code:

// Archi 5.6 and later
const archiPrefs = Java.type('com.archimatetool.editor.ArchiPlugin').getInstance().getPreferenceStore();

// Archi 5.5 and earlier
const archiPrefs = Java.type('com.archimatetool.editor.ArchiPlugin').PREFERENCES;

jArchi examples

Getting and setting the default ArchiMate figure fill color for a concept

// Get Archi's Preference Store
const archiPrefs = Java.type('com.archimatetool.editor.ArchiPlugin').getInstance().getPreferenceStore();

// Get current ArchiMate figure fill color for a Business Actor
console.log('Current Fill Color for Business Actor: ' + archiPrefs.getString('defaultFillColour_BusinessActor'));

// Set it
archiPrefs.setValue('defaultFillColour_BusinessActor', '#ff0000');

// Set back to default value
archiPrefs.setToDefault('defaultFillColour_BusinessActor');

Getting and setting the default ArchiMate figure width and height

// Get Archi's Preference Store
const archiPrefs = Java.type('com.archimatetool.editor.ArchiPlugin').getInstance().getPreferenceStore();

// Get default ArchiMate figure width and height
console.log('Default Width: ' + archiPrefs.getDefaultInt('defaultArchiMateFigureWidth'));
console.log('Default Height: ' + archiPrefs.getDefaultInt('defaultArchiMateFigureHeight'));

// Get current ArchiMate figure width and height
console.log('Current Width: ' + archiPrefs.getInt('defaultArchiMateFigureWidth'));
console.log('Current Height: ' + archiPrefs.getInt('defaultArchiMateFigureHeight'));

// Set ArchiMate figure width and height
archiPrefs.setValue('defaultArchiMateFigureWidth', 140);
archiPrefs.setValue('defaultArchiMateFigureHeight', 60);

// Set back to default values
archiPrefs.setToDefault('defaultArchiMateFigureWidth');
archiPrefs.setToDefault('defaultArchiMateFigureHeight');

Getting and setting the default grid size

// Get Archi's Preference Store
const archiPrefs = Java.type('com.archimatetool.editor.ArchiPlugin').getInstance().getPreferenceStore();

// Get default value of gridSize
console.log('Default gridSize: ' + archiPrefs.getDefaultInt('gridSize'));

// Get current value of gridSize
console.log('Current gridSize: ' + archiPrefs.getInt('gridSize'));

// Set value of gridSize
archiPrefs.setValue('gridSize', 16);

// Set back to default value
archiPrefs.setToDefault('gridSize');

Getting and setting the default font name, height, and style for diagram objects

// Get Archi's Preference Store
const archiPrefs = Java.type('com.archimatetool.editor.ArchiPlugin').getInstance().getPreferenceStore();

// Declare the Eclipse FontData class
// See https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/bundles/org.eclipse.swt/Eclipse%20SWT/gtk/org/eclipse/swt/graphics/FontData.java
const FontDataClass = Java.type('org.eclipse.swt.graphics.FontData');

// Get the default font string for diagram objects
var defaultViewFont = archiPrefs.getString('defaultViewFont');

// If we have it...
if(defaultViewFont) {
    // It's a long string so we need to load it into an Eclipse FontData object to get its parts
    var fontData = new FontDataClass(defaultViewFont);

    // Then we can get name, height, and style
    console.log('Font name: ' + fontData.getName());
    console.log('Font height: ' + fontData.getHeight());
    console.log('Font style: ' + fontData.getStyle());
}
// If it's the empty string then we can't parse it
else {
    console.log("Font is internal default");
}

// Create a new FontData of font name, height, and style
var newFontData = new FontDataClass('Arial', 12, 0);

// Get the FontData string
var fdString = newFontData.toString();

// Set it to the default font in preferences (if Archi is running you need to restart)
archiPrefs.setValue('defaultViewFont', fdString);

// Set back to default value
archiPrefs.setToDefault('defaultViewFont');