Rename A diagram file and then some [message #1693105] |
Tue, 21 April 2015 18:22 |
V Set 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 |
|
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 #1693227 is a reply to message #1693218] |
Wed, 22 April 2015 13:33 |
|
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 #1695455 is a reply to message #1695330] |
Thu, 14 May 2015 19:55 |
|
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 |
V Set Messages: 20 Registered: November 2012 |
Junior Member |
|
|
I was able to resolve this properly !
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
|
|
|
Powered by
FUDForum. Page generated in 1.05646 seconds