Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Rename A diagram file and then some(Rename a file, first element and all its shortcuts)
Rename A diagram file and then some [message #1693105] Tue, 21 April 2015 18:22 Go to next message
V Set is currently offline V SetFriend
Messages: 20
Registered: November 2012
Junior Member
Hi,

I have a requirement where the file name (without extension) and the name of the main element in the file are the same. When the file is renamed, I would like the main element to be renamed as well. In addition all the shortcuts of that element must be renamed too.

After debugging, I found that org.eclipse.ui.actions.RenameResourceAction is used. But this class cannot be extended, so I would like to know if there is another way.

Thanks,


[Updated on: Tue, 21 April 2015 18:23]

Report message to a moderator

Re: Rename A diagram file and then some [message #1693117 is a reply to message #1693105] Tue, 21 April 2015 21:04 Go to previous messageGo to next message
Nikos Margaritis is currently offline Nikos MargaritisFriend
Messages: 65
Registered: September 2014
Member

Hello there,

In order to catch those events you need to attach a resource change listener to your editor (take a look at the xxx.editor plugin --> xxx.presentation package --> xxxEditor.java in order to see an example of how this works).

In the xxxEditor.java make sure to locate the
protected IResourceChangeListener resourceChangeListener


Based on your statement, (I quote)
Quote:
When the file is renamed, I would like the main element to be renamed as well


I understand that you want to change the name of the model root Element, which is not possible. The element name originates from the ECore file from which you generate the editor. You can only change attributes of the elements, not their class names.

Now in case that your root element has an attribute called name which is also its label (so as for that label to be displayed right next to the class name) this you can change. In order to do that, you will need to catch the resource change event and then run an element property change command.

Command example, (pretty much plug & play)
DiagramEditPart diagramEditPart = getDiagramEditPart();
//where YourRoot is the class of your root element
//where getRootElement_Name the ATTRIBUTE name of your xyz root element
//where newName is a String
YourRoot parentElement = (YourRoot) diagramEditPart.resolveSemanticElement();
		org.eclipse.emf.common.command.Command setCmd = SetCommand.create(
				diagramEditPart.getEditingDomain(),
				parentElement,
					xxxPackage.eINSTANCE.getRootElement_Name(), newName);
diagramEditPart.getEditingDomain().getCommandStack().execute(setCmd);



Lastly, what do you mean shortcuts of the diagram file? Are these shortcuts OS (operating system) related? If so, there is no way to know where those shortcuts are placed in the first place as when you create them, this is handled by the OS. So shortcuts should probably be changed manually by the user. Except if you really know where these files are located beforehand (without scanning to find them).


Best Regards,

[Updated on: Tue, 21 April 2015 21:12]

Report message to a moderator

Re: Rename A diagram file and then some [message #1693218 is a reply to message #1693117] Wed, 22 April 2015 13:00 Go to previous messageGo to next message
V Set is currently offline V SetFriend
Messages: 20
Registered: November 2012
Junior Member
Thanks for your prompt reply!

You are right...I do not want to change the name of the root element. I understand that it is generated from eCore. One 'node' in the diagram file correlates with the name of the file and that needs to be changed when the diagram file name changes. This particular node has shortcuts that are used in other diagram files. The change has to be propagated to all these shortcuts.

I will look into the resourceChangeListener and go from there.

Thank you
Re: Rename A diagram file and then some [message #1693227 is a reply to message #1693218] Wed, 22 April 2015 13:33 Go to previous messageGo to next message
Nikos Margaritis is currently offline Nikos MargaritisFriend
Messages: 65
Registered: September 2014
Member

Hello,

Then one approach is with the resource change listener and the command example. (for a single diagram).

Now for the shortcuts (I suppose you mean a name reference), if the other diagram files are placed under the same project/workspace etc, you should find a way to get their diagramEditPart, which you will use, in order to execute the command change request and change that specific element name (attribute).

You could keep a record of the shortcuts somewhere while you create them, programmatically. For example when a shortcut is placed you could have a XML file to which you will store, all these information, in order to make it easier to find where these shortcuts are located (under which files etc). You could name that a dependency file, and of course it can be hidden from the users (naming conventions play a role here in how to create hidden files).

Best.
Re: Rename A diagram file and then some [message #1695330 is a reply to message #1693227] Wed, 13 May 2015 13:42 Go to previous messageGo to next message
V Set is currently offline V SetFriend
Messages: 20
Registered: November 2012
Junior Member
Nikos,

This turned out to be more interesting than I had perceived. I had to use the 'RenameParticipant' to change references in other files. So now when I rename a file or a folder, all of the references and shortcuts in the project are updated. Similarly, I used the MoveParticipant to move files/folders and thus update all of the references in other files in the project.

But, I still have the issue of renaming the single node in my renamed file. Unfortunately, the rename process does not load the contents of the file being renamed. So I am exploring other possibilities. I looked at the resourceChangeListener. Does this get implemented only when a file editor is open? You also mentioned a solution that would 'catch the rename change event'. Can you please expand on that?

Thanks for the help,


Re: Rename A diagram file and then some [message #1695455 is a reply to message #1695330] Thu, 14 May 2015 19:55 Go to previous messageGo to next message
Nikos Margaritis is currently offline Nikos MargaritisFriend
Messages: 65
Registered: September 2014
Member

V Set,

The resource changed listener is attached to the editor class. Therefore, indeed you need to have the editor opened in order to catch the event.
However what you can do is to provide a custom implementation for the renaming and add it as an extension point to your editor. (like that when you right click the specific editor .xyz file you will get an extra menu item on the pop up window)

Hence by clicking that menu item your handler will be called. You can provide your own implementation of the renaming that will notify or update the diagrams (to do that get the diagramEditPart and execute the Command that I said in the previous post to change any node's properties).

You can find a sample extension point which will create an extra menu item saying "Rename for xyz" when you right click in any .xyz_diagram files.

You need to place that in the plugin.xml of your xxx.diagram plugin.

<extension
         point="org.eclipse.ui.commands">
      <category
            name="Sample Category"
            id="example.id.category">
      </category>
      <command
            categoryId="example.id.category"
            defaultHandler="org.example.adapter.commandhandlers.ResourceChangedContextHandler"
            id="org.example.adapter.commandhandlers.ResourceChangedContextHandlerID"
            name="Resource change listener">
      </command>
</extension>


<menuContribution
            locationURI="popup:org.eclipse.jdt.ui.PackageExplorer">
         <command
               commandId="org.example.adapter.commandhandlers.ResourceChangedContextHandlerID"
               icon="icons/sample.gif"
               id="org.example.someID"
               label="Rename for xyz"
               style="push">   
            <visibleWhen>
    <with variable="activeMenuSelection">
     <iterate
         ifEmpty="false">
            <adapt type="org.eclipse.core.resources.IResource">
             <test property="org.eclipse.core.resources.name" value="*.xyz_diagram" />
         </adapt>
        </iterate>
    </with>
          </visibleWhen>  
         </command>
</menuContribution>


Best Nikos,

[Updated on: Fri, 22 May 2015 12:20]

Report message to a moderator

Re: Rename A diagram file and then some [message #1698282 is a reply to message #1695455] Fri, 12 June 2015 19:13 Go to previous message
V Set is currently offline V SetFriend
Messages: 20
Registered: November 2012
Junior Member
I was able to resolve this properly Smile !

I added a POST_BUILD resourceChangeListener to the RenameParticipant. So after all the changes have been committed, we can edit the contents of the file just renamed. This is possible with the IResourceDeltaVisitor.

Here's the code snippet of my RenameParticipant:

class xxxRenameParticipant extends RenameParticipant {

private static String extension = ".xxx_diagram";

String newName = "";
IWorkspace workspace = ResourcesPlugin.getWorkspace();

@Override
protected boolean initialize(Object element) {

newName = getArguments().getNewName();

workspace.addResourceChangeListener(resourceChangeListener, IResourceChangeEvent.POST_BUILD);

return true;
}


@Override
public RefactoringStatus checkConditions(IProgressMonitor pm,
CheckConditionsContext context) throws OperationCanceledException {

//validation code
return new RefactoringStatus();
}



@Override
public Change createChange(IProgressMonitor pm) throws CoreException,
OperationCanceledException {
CompositeChange result = new CompositeChange(" file name reference updates");
final HashMap changes = new HashMap();
//Replace the old name in the path with the new name

//use the text search engine to find matches of the old path in xxx_diagram files

String[] fileNamePatterns = { "*"+extension };
FileTextSearchScope scope = FileTextSearchScope.newSearchScope(roots, fileNamePatterns, false);
Pattern pattern = Pattern.compile(oldPath.toPortableString()); // find the original path
//Create a list of all files that need to be changed
TextSearchRequestor collector = new TextSearchRequestor() {
@Override
public boolean acceptPatternMatch(TextSearchMatchAccess matchAccess)
throws CoreException {
IFile aFile = matchAccess.getFile();

//check to see if file is already being changed
//do not merge changes
//create new change and add to change list
//create a replace edit to replace the old path with the new
//add it to the change
return true;
}
};

TextSearchEngine.create().search(scope, collector, pattern, new NullProgressMonitor());

//add all the changes to the composite change

return result;
}

/**
* This part listens for workspace changes.
* It is a POST_BUILD listener since we update the attribute: name in the model after all the file name references are updated.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
* */

protected IResourceChangeListener resourceChangeListener =
new IResourceChangeListener() {
final ArrayList renamed = new ArrayList();

public void resourceChanged(IResourceChangeEvent event) {

final String newElementName = //derive new name
IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() {
protected ResourceSet resourceSet = editingDomain.getResourceSet();
public boolean visit(IResourceDelta delta) {
//Rename is a combination of DELETE and ADD. The latest information is in the 'added' file
if (delta.getKind() == IResourceDelta.ADDED) {
resource = resourceSet.getResource(path, true);
renamed.add(resource);
}
return true;
}
};

try {
event.getDelta().accept(visitor);
//retrieve modelelement and feature from the file.
// feature = xxxPackage.eINSTANCE.getxxx();
//We have all the info.
//removeResourceChangeListener
}

//Command to update the feature
Command setNameCmd = SetCommand.create(editingDomain, element, feature, newElementName);
editingDomain.getCommandStack().execute(setNameCmd);
try {
resource.save(getSaveOptions());
} catch () {
}
}
};
}

Thanks for pointing me in the right direction

V Set

Previous Topic:OCL and GMF graphic instance
Next Topic:Mapping uml model to XML file
Goto Forum:
  


Current Time: Sat Apr 20 03:00:18 GMT 2024

Powered by FUDForum. Page generated in 0.04482 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top