Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Rename model and diagram files programmatically
Rename model and diagram files programmatically [message #188082] Wed, 21 May 2008 02:28 Go to next message
Thomas Beyer is currently offline Thomas Beyer
Messages: 47
Registered: July 2009
Member
Hello Community,


I pastet a post from the former eclipse.technology.gmf newsgroup in here.
The post targets at programmatically changing the model file and the
resulting inconsistant references (attribut <href> for the child
<element> )in the corresponding diagram file.

I tried the solution below, but the TransactionChangeRecorder prohibits
< newResource.getContent().addAll(domainResource.getContent()) >
by throwing
"IllegalStateException: Cannot modify resource set without a write
transaction".

I managed to change the href of the root of the Diagram-File,
which is an instance of Diagram, by setting the ProxyURI to the new
file-name.

When I save the resource though, only the Diagram-element is persisted
and all information about the children is lost.

Isn't there an easier way to update the diagram-file upon renaming the
model file?

In our use-case, we need both files, diagram and model for following
model transformations.
So we hide the model file in the Explorer-View and synchronize the
workspace via the ResourceChangeListener.
Model-file and Diagram-files are always in the same path and have the
same name (except the file-extension of course). So if the user renames
the diagram-file, the resourcechangelistener renames the model-files as
supposed. But then, the references in the diagram-file a broken.


Thanks in advance.


Regards Thomas



######original post

Thank you!

With your suggestion it is working. I'm first checking for the existing
files with these names, then starting the rename threads in 'asyncExec',
after that I'm creating the 'Resource' with 'resourceSet.createResource'
then setting all the contents from the old resource to the new one with
'newModelResource.getContents().addAll(modelResource.getCont ents());'
and at
the end saving the 'diagramResource' and the 'modelResource'. Everything is
done in the - 'DocumentProvider.saveDocumentToFile' method.
If I change any of this sequence I get an IllegalArgumentException:

java.lang.IllegalArgumentException: Attempted to beginRule: F/b1/s1, does
not match outer scope rule:
MultiRule[L/b1/s1/m11.bts_diagram,L/b1/s1/m11.bts]
at org.eclipse.core.runtime.Assert.isLegal(Assert.java:62)
at
org.eclipse.core.internal.jobs.ThreadJob.illegalPush(ThreadJ ob.java:116)
at org.eclipse.core.internal.jobs.ThreadJob.push(ThreadJob.java :225)
at org.eclipse.core.internal.jobs.ImplicitJobs.begin(ImplicitJo bs.java:58)
at
org.eclipse.core.internal.jobs.JobManager.beginRule(JobManag er.java:219)
at
org.eclipse.core.internal.resources.WorkManager.checkIn(Work Manager.java:96)
at
org.eclipse.core.internal.resources.Workspace.prepareOperati on(Workspace.java:1684)
at org.eclipse.core.internal.resources.Resource.move(Resource.j ava:1324)

But with the described steps and sequence everything is working perfectly.

Thanks,
Svet Rusev


"Artem Tikhomirov" <artem.tikhomirov@xxxxxxxxxxx> wrote in message
news:e9lo75$92j$1@xxxxxxxxxxxxxxxxxxxx
> What if load both diagram and domain resources into same resource set,
> create another resource (with new name), move content from old
resource to
> the new - i.e.
> newResource.getContent().addAll(domainResource.getContent()) and save?
>
> Artem
>


> "Svet Rusev" <sv_rusev@xxxxxxxxx> wrote in message
> news:e9lk0e$h34$1@xxxxxxxxxxxxxxxxxxxx
>> Hi,
>>
>> I need to implement rename functionality of my GMF editor files. The
>> rename of the Diagram file is not a problem since it is not referred
from
>> the model file, but if I rename the model file all information about
the
>> attributes of the model element are lost. I found that in the
EObject the
>> reference is kept in the URI - 'eProxyURI' . But when the model
file is
>> loaded this reference is 'null' and the EObject.eResource is containing
>> the reference to the Resource where the EObject is stored.
>> So my question is - how to change the reference from the Diagram Node
>> EObject to the model EObject?
>> And in what is the best place for this change when you save the diagram
>> in the DocumentProvider.saveDocumentToFile method?
>>
>> Thank you !
>> Svet Rusev
>>
>>
>
>
Re: Rename model and diagram files programmatically [message #188133 is a reply to message #188082] Wed, 21 May 2008 05:40 Go to previous message
Thomas Beyer is currently offline Thomas Beyer
Messages: 47
Registered: July 2009
Member
I found a way, that works for my use-case.
However, I am not sure, if this was the intended way to do it.
I'd be grateful for any comments or improvements on this.



public static void updateDiagramFile(IPath newDiagramPath,
IPath oldModelPath, IPath newModelPath) {

// create EditingDomain
TransactionalEditingDomain editingDomain =
GMFEditingDomainFactory.getInstance().createEditingDomain();
// create a resourceSet from it
ResourceSet resourceSet = editingDomain.getResourceSet();
// create the resource from the resource set
Resource resourceDiagram =
resourceSet.getResource(URI.createPlatformResourceURI(newDia gramPath.toString(),
true), true);
//create hashset to for elements toupdate
HashSet<EObject> eObjectsToUpdate = new HashSet<EObject>();

// get diagram root
Diagram dia = (Diagram) resourceDiagram.getContents().get(0);

// add root to hashset
eObjectsToUpdate.add(dia.getElement());
//add edges to hashset
EList<Edge> edges = dia.getEdges();
for (Edge edge : edges) {
eObjectsToUpdate.add(edge.getElement());
}

// iterate over tree and get affected elements
findChildren(eObjectsToUpdate, dia);

//update the proxy uri
processUpdates(eObjectsToUpdate, newModelPath);

//save the resource
try {
//finally save the resource
resourceDiagram.save(null);
} catch (IOException e) {
// // TODO Auto-generated catch block
e.printStackTrace();
}
}

private static void findChildren(HashSet<EObject> set, EObject parent) {
HashSet<EObject> children = getSemanticChildren(parent);
if (children != null && children.size() != 0) {
for (Object child : children) {
if (child instanceof Node) {
Node node = (Node) child;
set.add(node.getElement());
findChildren(set, node);
}
}
}
return;
}


private static HashSet<EObject> getSemanticChildren(EObject parent) {
HashSet<EObject> set = new HashSet<EObject>();
if (parent instanceof Diagram) {
Diagram diagram = (Diagram) parent;
set.addAll(diagram.getChildren());
} else if (parent instanceof Node) {
set.addAll(((Node)parent).getChildren());
}
return set;
}

private static void processUpdates(HashSet<EObject> eObjectsToUpdate,
IPath newModelPath){
for (EObject object : eObjectsToUpdate) {
if(object instanceof InternalEObject && object.eIsProxy()){
InternalEObject ieo = (InternalEObject) object;
//get the proxy uri
URI uri = ieo.eProxyURI();
//remove last segment
URI trimmedURI = uri.trimSegments(1);
//add new name as last segment
URI resultURI = trimmedURI.appendSegment(newModelPath.lastSegment());
//set the new uri
ieo.eSetProxyURI(resultURI);
}
}

}
Previous Topic:Some sort of filtering in the property sheets
Next Topic:Killing "Core" tab property
Goto Forum:
  


Current Time: Thu Aug 28 05:26:53 EDT 2014

Powered by FUDForum. Page generated in 0.10348 seconds