Created
January 29, 2026 13:08
-
-
Save romualdrichard/bd8b91d575f9fdd5bf7a6c71eb09e834 to your computer and use it in GitHub Desktop.
#jarchi duplicate relationship
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: Duplicate Relationships | |
| * | |
| * This script duplicates selected relationships by creating: | |
| * - A new relationship with the same type, source, target and properties | |
| * - Visual connections in all views where the original relationship appears (optional) | |
| * | |
| * Usage: Select one or more relationships and run the script | |
| */ | |
| console.clear(); | |
| console.show(); | |
| // Import SWT classes for dialog | |
| var SWT = Java.type('org.eclipse.swt.SWT'); | |
| var MessageBox = Java.type('org.eclipse.swt.widgets.MessageBox'); | |
| // Get selection | |
| var selection = $(selection); | |
| // Filter only relationships | |
| var relationships = selection.filter("relationship"); | |
| if (relationships.isEmpty()) { | |
| var shell = workbench.getActiveWorkbenchWindow().getShell(); | |
| var messageBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK); | |
| messageBox.setMessage("Please select at least one relationship."); | |
| messageBox.setText("No Selection"); | |
| messageBox.open(); | |
| } else { | |
| // Ask user if they want to add new relationships to all views | |
| var shell = workbench.getActiveWorkbenchWindow().getShell(); | |
| var messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO); | |
| messageBox.setMessage("Do you want to add the new relationships to all views where the original relationships are present?"); | |
| messageBox.setText("Add to Views"); | |
| var addToViews = messageBox.open() == SWT.YES; | |
| var nbDuplicated = 0; | |
| relationships.each(function(relationship) { | |
| try { | |
| // Get the relationship concept (not the visual object) | |
| var relationshipConcept = relationship.concept ? relationship.concept : relationship; | |
| // Get source and target elements (concepts, not visual objects) | |
| var source = relationshipConcept.source; | |
| var target = relationshipConcept.target; | |
| console.log("Processing relationship: " + relationshipConcept.name); | |
| console.log(" Type: " + relationshipConcept.type); | |
| console.log(" Source: " + source.name + " (type: " + source.type + ")"); | |
| console.log(" Target: " + target.name + " (type: " + target.type + ")"); | |
| // Create the new relationship | |
| var newRelationship = model.createRelationship( | |
| relationshipConcept.type, | |
| "new " + relationshipConcept.name || "", | |
| source, | |
| target | |
| ); | |
| // Copy documentation | |
| if (relationshipConcept.documentation) { | |
| newRelationship.documentation = relationshipConcept.documentation; | |
| } | |
| // Copy user properties | |
| if (relationshipConcept.prop()) { | |
| relationshipConcept.prop().forEach(function(key) { | |
| newRelationship.prop(key, relationshipConcept.prop(key)); | |
| }); | |
| } | |
| // Add to views if user chose to | |
| if (addToViews) { | |
| // Find all views where the original relationship is present | |
| var viewsWithRelationship = $("view").filter(function(view) { | |
| return $(view).find("relationship").filter(function(r) { | |
| var rConcept = r.concept ? r.concept : r; | |
| return rConcept.id === relationshipConcept.id; | |
| }).size() > 0; | |
| }); | |
| // Add the new relationship to all these views | |
| viewsWithRelationship.each(function(view) { | |
| // Get the visual connection of the original relationship in this view | |
| var originalConnection = $(view).find("relationship").filter(function(r) { | |
| var rConcept = r.concept ? r.concept : r; | |
| return rConcept.id === relationshipConcept.id; | |
| }).first(); | |
| // Add the new relationship to the view | |
| var newConnection = view.add(newRelationship, originalConnection.source, originalConnection.target); | |
| // Copy visual properties if available | |
| if (originalConnection.lineWidth) { | |
| newConnection.lineWidth = originalConnection.lineWidth; | |
| } | |
| if (originalConnection.lineColor) { | |
| newConnection.lineColor = originalConnection.lineColor; | |
| } | |
| if (originalConnection.fontColor) { | |
| newConnection.fontColor = originalConnection.fontColor; | |
| } | |
| if (originalConnection.textPosition) { | |
| newConnection.textPosition = originalConnection.textPosition; | |
| } | |
| console.log(" - Added to view: " + view.name); | |
| }); | |
| console.log(" Number of views: " + viewsWithRelationship.size()); | |
| } else { | |
| console.log(" Skipped adding to views (user choice)"); | |
| } | |
| nbDuplicated++; | |
| console.log("Relationship duplicated: " + relationshipConcept.name + " (" + relationshipConcept.type + ")"); | |
| console.log(""); | |
| } catch (error) { | |
| console.error("Error duplicating relationship: " + (relationship.name || relationship.concept.name)); | |
| console.error("Error message: " + error.message); | |
| console.error("Stack trace: " + error.stack); | |
| var relConcept = relationship.concept ? relationship.concept : relationship; | |
| console.error("Details - Type: " + relConcept.type + ", Source: " + relConcept.source.name + ", Target: " + relConcept.target.name); | |
| } | |
| }); | |
| console.log("================================="); | |
| console.log(nbDuplicated + " relationship(s) duplicated successfully!"); | |
| // Final confirmation dialog | |
| var finalBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK); | |
| finalBox.setMessage(nbDuplicated + " relationship(s) duplicated successfully!"); | |
| finalBox.setText("Success"); | |
| finalBox.open(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment