Updating the Model File reference in Diagram FIle [message #143696] |
Tue, 24 July 2007 04:52  |
Eclipse User |
|
|
|
Originally posted by: vlalan.csc.com
Hi All,
I want to implement the Save As functionality for a Diagram File.
The procedure I am following is:
1. Create a new Diagram File having the same contents as the old one.
2. Create a new Model File having the same contents as the old one.
3. Traverse the newly created Diagram File and update the references of
old model file with new model file.
The problem I am facing is that when I save the Diagram in the same
folder, everything is running fine but when I save the Diagram in another
folder it gives me error in step 3.
The code that handles this is:
final Resource diagramResource =
editingDomain.getResourceSet().getResource(newDiagramFileUri , true);
GMFResource gmfResource = (GMFResource) diagramResource;
final DiagramImpl diagram = (DiagramImpl) gmfResource.getContents().get(0);
try {
editingDomain.getCommandStack().execute(
new RecordingCommand(editingDomain) {
protected void doExecute() {
diagram.setName(dupDiagramFile.getName());
}
});
diagram.getElement().eResource().setURI(newModelFileUri);
<<<<......recursily traverse the diagram and setURI at each node>>>
Further debugging let to the conclusion that when we save the new Diagram
File in same folder the last line diagram.getElement().eResource() works
fine as it finds the old model file present in this folder.
However when we save the Diagram in a new Folder the same line
diagram.getElement().eResource() gives a NullPointerException as there is
no old model file here.
Please help if anyone has come across the same issue.
Also please suggest any other way to update the model reference if any.
Thanks in Advance,
Vipul
|
|
|
Re: Updating the Model File reference in Diagram FIle [message #143697 is a reply to message #143696] |
Tue, 24 July 2007 04:57  |
Eclipse User |
|
|
|
Originally posted by: philippeweber57.yahoo.fr
Hi Vipul,
here is the code that I used to duplicate a model and its diagram within
UML2Tools, as it is a GMF-based tool, you could reuse the same
principle. The trick is to load the 2 files in the same ResourceSet and
use a CrossReferenceAdapter to handle the changes for you.
The xxxHelper classes are only an help to load and save the file
UMLResourceHelper umlResourceHelper = new UMLResourceHelper();
DiagramResourceHelper diagramResourceHelper = new DiagramResourceHelper();
Diagram diagram =
diagramResourceHelper.loadUMLClassDiagram(inputDiagramURI);
ResourceSet resourceSet = diagram.eResource().getResourceSet();
Model inputModel =
umlResourceHelper.loadUMLModel(inputModelURI,resourceSet);
//Initialize CrossReference Adapter to resolve references in diagram
CrossReferenceAdapter.getCrossReferenceAdapter(resourceSet);
umlResourceHelper.saveUMLModel(inputModel, outputModelURI);
//Save Diagram
try {
Resource resource = diagram.eResource();
resource.setURI(outputDiagramURI);
resource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Hope it helps
Regards
Philippe
Vipul wrote:
> Hi All,
>
> I want to implement the Save As functionality for a Diagram File.
>
> The procedure I am following is:
> 1. Create a new Diagram File having the same contents as the old one.
> 2. Create a new Model File having the same contents as the old one.
> 3. Traverse the newly created Diagram File and update the references of
> old model file with new model file.
>
> The problem I am facing is that when I save the Diagram in the same
> folder, everything is running fine but when I save the Diagram in
> another folder it gives me error in step 3.
>
> The code that handles this is:
> final Resource diagramResource =
> editingDomain.getResourceSet().getResource(newDiagramFileUri , true);
> GMFResource gmfResource = (GMFResource) diagramResource;
> final DiagramImpl diagram = (DiagramImpl) gmfResource.getContents().get(0);
> try {
> editingDomain.getCommandStack().execute(
> new RecordingCommand(editingDomain) {
> protected void doExecute() {
> diagram.setName(dupDiagramFile.getName());
> }
> });
>
> diagram.getElement().eResource().setURI(newModelFileUri);
> <<<<......recursily traverse the diagram and setURI at each node>>>
>
> Further debugging let to the conclusion that when we save the new
> Diagram File in same folder the last line
> diagram.getElement().eResource() works fine as it finds the old model
> file present in this folder.
>
> However when we save the Diagram in a new Folder the same line
> diagram.getElement().eResource() gives a NullPointerException as there
> is no old model file here.
>
> Please help if anyone has come across the same issue.
>
> Also please suggest any other way to update the model reference if any.
>
> Thanks in Advance,
> Vipul
>
|
|
|
Powered by
FUDForum. Page generated in 0.02536 seconds