} else {
resourceName = input.getName();
}
}
final IDocumentProvider docProvider = sourcePage.getDocumentProvider();
IDocument document = docProvider.getDocument(input);
try {
model.loadFrom(new IDocumentWrapper(document));
model.setBndResourceName(resourceName);
if (resource != null) {
model.setBndResource(resource.getLocation().toFile());
}
// model.addPropertyChangeListener(modelListener);
} catch (IOException e) {
throw new PartInitException("Error reading editor input.", e);
}
// Ensure the field values are updated if the file content is replaced
docProvider.addElementStateListener(new IElementStateListener() {
String savedString = null;
public void elementMoved(Object originalElement, Object movedElement) {}
public void elementDirtyStateChanged(Object element, boolean isDirty) {}
public void elementDeleted(Object element) {}
public void elementContentReplaced(Object element) {
try {
IDocumentWrapper idoc = new IDocumentWrapper(docProvider.getDocument(element));
if (!saving.get()) {
model.loadFrom(idoc);
} else {
if (savedString != null) {
logger.logInfo("Putting back content that we almost lost!", null);
try {
idoc.replace(0, idoc.getLength(), savedString);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
} catch (IOException e) {
logger.logError("Error loading model from document.", e);
} finally {
savedString = null;
}
}
public void elementContentAboutToBeReplaced(Object element) {
// [cs] This check is here to attempt to save content that would be thrown away by a (misbehaving?) version control plugin.
// Scenario: File is checked out by Perforce plugin.
// This causes elementContentAboutToBeReplaced and elementContentReplaced callbacks to be fired.
// However -- by the time that elementContentReplaced is called, the content inside of the IDocumentWrapper
// is already replaced with the contents of the perforce file being checked out.
// To avoid losing changes, we need to save the content here, then put that content BACK on to the document
// in elementContentReplaced
if (saving.get()) {
logger.logInfo("Content about to be replaced... Save it.", null);
savedString = new IDocumentWrapper(docProvider.getDocument(element)).get();
}
}
});
}