use of org.eclipse.ui.editors.text.IStorageDocumentProvider in project eclipse.platform.text by eclipse.
the class LastSaveReferenceProvider method readDocument.
/**
* Reads in the saved document into <code>fReference</code>.
*
* @param monitor a progress monitor, or <code>null</code>
* @param force <code>true</code> if the reference document should also
* be read if the current document is <code>null</code>,<code>false</code>
* if it should only be updated if it already existed.
*/
private void readDocument(IProgressMonitor monitor, boolean force) {
// protect against concurrent disposal
IDocumentProvider prov = fDocumentProvider;
IEditorInput inp = fEditorInput;
IDocument doc = fReference;
ITextEditor editor = fEditor;
if (prov instanceof IStorageDocumentProvider && inp instanceof IStorageEditorInput) {
IStorageEditorInput input = (IStorageEditorInput) inp;
IStorageDocumentProvider provider = (IStorageDocumentProvider) prov;
if (doc == null)
if (force || fDocumentRead)
doc = new Document();
else
return;
IJobManager jobMgr = Job.getJobManager();
try {
IStorage storage = input.getStorage();
// check for null for backward compatibility (we used to check before...)
if (storage == null)
return;
fProgressMonitor = monitor;
ISchedulingRule rule = getSchedulingRule(storage);
// delay for any other job requiring the lock on file
try {
lockDocument(monitor, jobMgr, rule);
String encoding;
if (storage instanceof IEncodedStorage)
encoding = ((IEncodedStorage) storage).getCharset();
else
encoding = null;
boolean skipUTF8BOM = isUTF8BOM(encoding, storage);
setDocumentContent(doc, storage, encoding, monitor, skipUTF8BOM);
} finally {
unlockDocument(jobMgr, rule);
fProgressMonitor = null;
}
} catch (CoreException e) {
return;
}
if (monitor != null && monitor.isCanceled())
return;
// update state
synchronized (fLock) {
if (fDocumentProvider == provider && fEditorInput == input) {
// only update state if our provider / input pair has not
// been updated in between (dispose or setActiveEditor)
fReference = doc;
fDocumentRead = true;
addElementStateListener(editor, prov);
}
}
}
}
use of org.eclipse.ui.editors.text.IStorageDocumentProvider in project eclipse.platform.text by eclipse.
the class AbstractDecoratedTextEditor method openSaveErrorDialog.
/**
* Presents an error dialog to the user when a problem happens during save.
* <p>
* Overrides the default behavior by showing a more advanced error dialog in case of encoding
* problems.
* </p>
*
* @param title the dialog title
* @param message the message to display
* @param exception the exception to handle
* @since 3.6
*/
@Override
protected void openSaveErrorDialog(String title, String message, CoreException exception) {
IStatus status = exception.getStatus();
final IDocumentProvider documentProvider = getDocumentProvider();
if (!(status.getCode() == IFileBufferStatusCodes.CHARSET_MAPPING_FAILED && documentProvider instanceof IStorageDocumentProvider)) {
super.openSaveErrorDialog(title, message, exception);
return;
}
final int saveAsUTF8ButtonId = IDialogConstants.OK_ID + IDialogConstants.CANCEL_ID + 1;
final int selectUnmappableCharButtonId = saveAsUTF8ButtonId + 1;
final Charset charset = getCharset();
ErrorDialog errorDialog = new ErrorDialog(getSite().getShell(), title, message, status, IStatus.ERROR) {
@Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
createButton(parent, saveAsUTF8ButtonId, TextEditorMessages.AbstractDecoratedTextEditor_save_error_Dialog_button_saveAsUTF8, false);
if (charset != null)
createButton(parent, selectUnmappableCharButtonId, TextEditorMessages.AbstractDecoratedTextEditor_save_error_Dialog_button_selectUnmappable, false);
}
@Override
protected void buttonPressed(int id) {
if (id == saveAsUTF8ButtonId || id == selectUnmappableCharButtonId) {
setReturnCode(id);
close();
} else
super.buttonPressed(id);
}
@Override
protected boolean shouldShowDetailsButton() {
return false;
}
};
int returnCode = errorDialog.open();
if (returnCode == saveAsUTF8ButtonId) {
// $NON-NLS-1$
((IStorageDocumentProvider) documentProvider).setEncoding(getEditorInput(), "UTF-8");
IProgressMonitor monitor = getProgressMonitor();
try {
doSave(monitor);
} finally {
monitor.done();
}
} else if (returnCode == selectUnmappableCharButtonId) {
CharsetEncoder encoder = charset.newEncoder();
IDocument document = getDocumentProvider().getDocument(getEditorInput());
int documentLength = document.getLength();
int offset = 0;
BreakIterator charBreakIterator = BreakIterator.getCharacterInstance();
charBreakIterator.setText(document.get());
while (offset < documentLength) {
try {
int next = charBreakIterator.next();
String ch = document.get(offset, next - offset);
if (!encoder.canEncode(ch)) {
selectAndReveal(offset, next - offset);
return;
}
offset = next;
} catch (BadLocationException ex) {
EditorsPlugin.log(ex);
// Skip this character. Showing yet another dialog here is overkill
}
}
}
}
Aggregations