Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Clone GEF file
Clone GEF file [message #242519] Wed, 23 April 2008 10:51
Eclipse UserFriend
Originally posted by: coolbird.gmail.com

<b>I want to add a button in the toolbar
When I click the button,
the graph will be copied and saved as a new file.

For example, there is a file named diagram-01.
When you click the button, this file would be copied and saved as diagram-02 automatically.
There is a rectangular in the diagram-02.This is correct,
But if I were to draw a circle, and then clicking the button to copy as diagram-03.
There are a rectangular and a circle in the diagram-03. It's correct, too.

But if I open the diagram-02, will find that there is only a rectangular in the diagram-02.
It seems diagram-02 didn't save before open diagram-03.
I try to call doSave() first, but the same result.

follows are code,if anything wrong?</b>


public void doFastCopy() {

doSave();

ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(contentsModel);
oos.close();

// get now file name and add 1;
IFile file = ((IFileEditorInput) getEditorInput()).getFile();
setInput(new FileEditorInput(file));
IPath path= file.getFullPath();

String oldFileName = path.removeFileExtension().lastSegment();


int fileNumber=Integer.parseInt(oldFileName.substring(oldFileNam e.length()-1,oldFileName.length()));


String newFileName = oldFileName.replace(String.valueOf(fileNumber), String.valueOf(fileNumber+1));

IPath path2=path.removeLastSegments(1);

path2=path2.append(newFileName);

path2=path2.addFileExtension("sketch");

// create new file
final IFile file2 = ResourcesPlugin.getWorkspace().getRoot().getFile(path2);

file2.create(
new ByteArrayInputStream(out.toByteArray()), // contents
true, // keep saving, even if IFile is out of sync with the Workspace
null); // progress monitor
setInput(new FileEditorInput(file2));
getCommandStack().markSaveLocation();
}
catch (CoreException ce) {
ce.printStackTrace();
}
catch (Exception ioe) {
ioe.printStackTrace();
}
}

public void doSave(IProgressMonitor monitor) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(getModel());
oos.close();
IFile file = ((IFileEditorInput) getEditorInput()).getFile();
setInput(new FileEditorInput(file));

file.setContents(new ByteArrayInputStream(out.toByteArray()), true,true,monitor);
getCommandStack().markSaveLocation();

} catch (CoreException ce) {
ce.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
Previous Topic:2 problems with PolylineConnection
Next Topic:Is there now one command framework in the platform that EMF & GEF can use?
Goto Forum:
  


Current Time: Thu Apr 25 09:17:59 GMT 2024

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

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

Back to the top