Created
January 30, 2026 08:46
-
-
Save romualdrichard/35218e5c3702cbe809b944b137a69794 to your computer and use it in GitHub Desktop.
#jarchi modifiy property value
This file contains hidden or 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
| /* | |
| * JArchi Script - Modify User Property Value | |
| * | |
| * This script allows you to search and replace a property value | |
| * on all model objects that have this property. | |
| * | |
| * Uses SWT dialog boxes for user interaction. | |
| */ | |
| // Import necessary SWT classes | |
| const SWT = Java.type('org.eclipse.swt.SWT'); | |
| const Shell = Java.type('org.eclipse.swt.widgets.Shell'); | |
| const Display = Java.type('org.eclipse.swt.widgets.Display'); | |
| const Label = Java.type('org.eclipse.swt.widgets.Label'); | |
| const Text = Java.type('org.eclipse.swt.widgets.Text'); | |
| const Combo = Java.type('org.eclipse.swt.widgets.Combo'); | |
| const Button = Java.type('org.eclipse.swt.widgets.Button'); | |
| const MessageBox = Java.type('org.eclipse.swt.widgets.MessageBox'); | |
| const GridLayout = Java.type('org.eclipse.swt.layout.GridLayout'); | |
| const GridData = Java.type('org.eclipse.swt.layout.GridData'); | |
| const SelectionAdapter = Java.type('org.eclipse.swt.events.SelectionAdapter'); | |
| // Global variables to store input values | |
| var propertyName = ""; | |
| var oldValue = ""; | |
| var newValue = ""; | |
| var userConfirmed = false; | |
| var allProperties = {}; // Map: propertyName -> Set of values | |
| /** | |
| * Collects all user properties and their values from the model | |
| */ | |
| function collectAllProperties() { | |
| var properties = {}; | |
| // Loop through all concepts in the model | |
| $("concept").each(function(concept) { | |
| // Get the list of property keys | |
| var propKeys = concept.prop(); | |
| // Loop through each property key | |
| if (propKeys && Array.isArray(propKeys)) { | |
| propKeys.forEach(function(key) { | |
| if (key && key.trim() !== "") { | |
| // Get the property value | |
| var value = concept.prop(key); | |
| if (value !== null && value !== undefined) { | |
| // Initialize the value set if the property doesn't exist yet | |
| if (!properties[key]) { | |
| properties[key] = {}; | |
| } | |
| // Add the value to the set (using an object as a set) | |
| properties[key][value] = true; | |
| } | |
| } | |
| }); | |
| } | |
| }); | |
| return properties; | |
| } | |
| /** | |
| * Converts a "set" object to a sorted array | |
| */ | |
| function setToArray(set) { | |
| var array = []; | |
| for (var key in set) { | |
| if (set.hasOwnProperty(key)) { | |
| array.push(key); | |
| } | |
| } | |
| // Natural sort that handles numbers correctly | |
| array.sort(function(a, b) { | |
| return a.toString().localeCompare(b.toString(), undefined, {numeric: true, sensitivity: 'base'}); | |
| }); | |
| return array; | |
| } | |
| /** | |
| * Creates and displays the input dialog box | |
| */ | |
| function showInputDialog() { | |
| var display = Display.getCurrent(); | |
| if (display == null) { | |
| display = Display.getDefault(); | |
| } | |
| var shell = new Shell(display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); | |
| shell.setText("Modify Property Value"); | |
| shell.setSize(550, 250); | |
| var layout = new GridLayout(2, false); | |
| layout.marginWidth = 10; | |
| layout.marginHeight = 10; | |
| layout.verticalSpacing = 10; | |
| shell.setLayout(layout); | |
| // Prepare lists for combos | |
| var propertyNames = []; | |
| for (var prop in allProperties) { | |
| if (allProperties.hasOwnProperty(prop)) { | |
| propertyNames.push(prop); | |
| } | |
| } | |
| // Natural sort to handle numbers in names correctly | |
| propertyNames.sort(function(a, b) { | |
| return a.toString().localeCompare(b.toString(), undefined, {numeric: true, sensitivity: 'base'}); | |
| }); | |
| // Label and combo for property name | |
| var labelProperty = new Label(shell, SWT.NONE); | |
| labelProperty.setText("Property name:"); | |
| var gridData1 = new GridData(SWT.BEGINNING, SWT.CENTER, false, false); | |
| labelProperty.setLayoutData(gridData1); | |
| var comboProperty = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY); | |
| var gridData2 = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
| comboProperty.setLayoutData(gridData2); | |
| comboProperty.setItems(Java.to(propertyNames, "java.lang.String[]")); | |
| // Label and combo for old value | |
| var labelOldValue = new Label(shell, SWT.NONE); | |
| labelOldValue.setText("Value to replace:"); | |
| var gridData3 = new GridData(SWT.BEGINNING, SWT.CENTER, false, false); | |
| labelOldValue.setLayoutData(gridData3); | |
| var comboOldValue = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY); | |
| var gridData4 = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
| comboOldValue.setLayoutData(gridData4); | |
| comboOldValue.setEnabled(false); | |
| // Listener to update available values when a property is selected | |
| comboProperty.addSelectionListener(new SelectionAdapter() { | |
| widgetSelected: function(e) { | |
| var selectedProperty = comboProperty.getText(); | |
| if (selectedProperty && allProperties[selectedProperty]) { | |
| var values = setToArray(allProperties[selectedProperty]); | |
| comboOldValue.setItems(Java.to(values, "java.lang.String[]")); | |
| comboOldValue.setEnabled(true); | |
| comboOldValue.select(0); | |
| } else { | |
| comboOldValue.setItems(Java.to([], "java.lang.String[]")); | |
| comboOldValue.setEnabled(false); | |
| } | |
| textNewValue.setText(""); | |
| } | |
| }); | |
| // Label and field for new value | |
| var labelNewValue = new Label(shell, SWT.NONE); | |
| labelNewValue.setText("New value:"); | |
| var gridData5 = new GridData(SWT.BEGINNING, SWT.CENTER, false, false); | |
| labelNewValue.setLayoutData(gridData5); | |
| var textNewValue = new Text(shell, SWT.BORDER); | |
| var gridData6 = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
| textNewValue.setLayoutData(gridData6); | |
| // OK Button | |
| var btnOK = new Button(shell, SWT.PUSH); | |
| btnOK.setText("OK"); | |
| var gridData8 = new GridData(SWT.END, SWT.CENTER, true, false); | |
| gridData8.widthHint = 100; | |
| btnOK.setLayoutData(gridData8); | |
| btnOK.addSelectionListener(new SelectionAdapter() { | |
| widgetSelected: function(e) { | |
| propertyName = comboProperty.getText().trim(); | |
| oldValue = comboOldValue.getText(); | |
| newValue = textNewValue.getText(); | |
| if (propertyName === "") { | |
| showMessage(shell, "Error", "Please select a property.", SWT.ICON_ERROR); | |
| return; | |
| } | |
| if (oldValue === "") { | |
| showMessage(shell, "Error", "Please select a value to replace.", SWT.ICON_ERROR); | |
| return; | |
| } | |
| if (newValue === "") { | |
| showMessage(shell, "Error", "Please enter a new value.", SWT.ICON_ERROR); | |
| return; | |
| } | |
| if (oldValue === newValue) { | |
| showMessage(shell, "Error", "The old and new values are identical.", SWT.ICON_WARNING); | |
| return; | |
| } | |
| userConfirmed = true; | |
| shell.close(); | |
| } | |
| }); | |
| // Cancel Button | |
| var btnCancel = new Button(shell, SWT.PUSH); | |
| btnCancel.setText("Cancel"); | |
| var gridData9 = new GridData(SWT.END, SWT.CENTER, false, false); | |
| gridData9.widthHint = 100; | |
| btnCancel.setLayoutData(gridData9); | |
| btnCancel.addSelectionListener(new SelectionAdapter() { | |
| widgetSelected: function(e) { | |
| userConfirmed = false; | |
| shell.close(); | |
| } | |
| }); | |
| // Center the window | |
| var monitor = display.getPrimaryMonitor(); | |
| var bounds = monitor.getBounds(); | |
| var shellSize = shell.getSize(); | |
| shell.setLocation( | |
| (bounds.width - shellSize.x) / 2, | |
| (bounds.height - shellSize.y) / 2 | |
| ); | |
| shell.open(); | |
| // Event loop | |
| while (!shell.isDisposed()) { | |
| if (!display.readAndDispatch()) { | |
| display.sleep(); | |
| } | |
| } | |
| } | |
| /** | |
| * Displays a message box | |
| */ | |
| function showMessage(parent, title, message, icon) { | |
| var msgBox = new MessageBox(parent, icon | SWT.OK); | |
| msgBox.setText(title); | |
| msgBox.setMessage(message); | |
| msgBox.open(); | |
| } | |
| /** | |
| * Displays a confirmation box | |
| */ | |
| function showConfirmation(parent, title, message) { | |
| var msgBox = new MessageBox(parent, SWT.ICON_QUESTION | SWT.YES | SWT.NO); | |
| msgBox.setText(title); | |
| msgBox.setMessage(message); | |
| return msgBox.open() === SWT.YES; | |
| } | |
| /** | |
| * Searches and modifies properties in the model | |
| */ | |
| function modifyProperties() { | |
| var count = 0; | |
| var modifiedObjects = []; | |
| // Loop through all concepts in the model | |
| $("concept").each(function(concept) { | |
| var prop = concept.prop(propertyName); | |
| if (prop !== null && prop === oldValue) { | |
| concept.prop(propertyName, newValue); | |
| modifiedObjects.push(concept.name + " (" + concept.type + ")"); | |
| count++; | |
| } | |
| }); | |
| return { | |
| count: count, | |
| objects: modifiedObjects | |
| }; | |
| } | |
| /** | |
| * Main function | |
| */ | |
| function main() { | |
| console.clear(); | |
| console.show(); | |
| console.log("=== Modify Property Value ===\n"); | |
| // Check that a model is open | |
| if (!model) { | |
| window.alert("No model open. Please open an ArchiMate model."); | |
| exit(); | |
| } | |
| // Collect all properties from the model | |
| console.log("Collecting user properties..."); | |
| allProperties = collectAllProperties(); | |
| var propertyCount = 0; | |
| for (var prop in allProperties) { | |
| if (allProperties.hasOwnProperty(prop)) { | |
| propertyCount++; | |
| } | |
| } | |
| if (propertyCount === 0) { | |
| window.alert("No user properties found in the model."); | |
| exit(); | |
| } | |
| console.log(propertyCount + " user property(ies) found.\n"); | |
| // Display the input dialog box | |
| showInputDialog(); | |
| // If user cancelled | |
| if (!userConfirmed) { | |
| console.log("Operation cancelled by user."); | |
| exit(); | |
| } | |
| console.log("Property: " + propertyName); | |
| console.log("Old value: '" + oldValue + "'"); | |
| console.log("New value: '" + newValue + "'"); | |
| console.log(""); | |
| // Search for affected objects | |
| var result = modifyProperties(); | |
| // Display the result | |
| if (result.count === 0) { | |
| console.log("No objects found with property '" + propertyName + "' having value '" + oldValue + "'."); | |
| var display = Display.getCurrent() || Display.getDefault(); | |
| var shell = new Shell(display); | |
| showMessage(shell, "Result", | |
| "No objects found with this property and value.", | |
| SWT.ICON_INFORMATION); | |
| shell.dispose(); | |
| } else { | |
| console.log("=== Modified Objects (" + result.count + ") ==="); | |
| result.objects.forEach(function(obj) { | |
| console.log(" - " + obj); | |
| }); | |
| var display = Display.getCurrent() || Display.getDefault(); | |
| var shell = new Shell(display); | |
| showMessage(shell, "Result", | |
| result.count + " object(s) successfully modified.\n\nCheck the console for more details.", | |
| SWT.ICON_INFORMATION); | |
| shell.dispose(); | |
| } | |
| } | |
| // Execute the script | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment