Java Code Examples for com.intellij.openapi.editor.Document#setReadOnly()

The following examples show how to use com.intellij.openapi.editor.Document#setReadOnly() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: consulo   File: MergeVersion.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Document createWorkingDocument(final Project project) {
  //TODO[ik]: do we really need to create copy here?
  final Document workingDocument = myDocument; //DocumentUtil.createCopy(myDocument, project);
  //LOG.assertTrue(workingDocument != myDocument);
  workingDocument.setReadOnly(false);
  final DocumentReference ref = DocumentReferenceManager.getInstance().create(workingDocument);
  myTextBeforeMerge = myDocument.getText();
  ApplicationManager.getApplication().runWriteAction(new Runnable() {
    @Override
    public void run() {
      setDocumentText(workingDocument, myOriginalText, DiffBundle.message("merge.init.merge.content.command.name"), project);
      if (project != null) {
        final UndoManager undoManager = UndoManager.getInstance(project);
        if (undoManager != null) { //idea.sh merge command
          undoManager.nonundoableActionPerformed(ref, false);
        }
      }
    }
  });
  return workingDocument;
}
 
Example 2
Source Project: floobits-intellij   File: FactoryImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void clearReadOnlyState() {
    for (String path : readOnlyBufferIds) {
        VirtualFile file = getVirtualFile(path);
        if (file == null) {
            continue;
        }
        Document document = FileDocumentManager.getInstance().getDocument(file);
        if (document == null) {
            continue;
        }
        document.setReadOnly(false);
    }
    readOnlyBufferIds.clear();
}
 
Example 3
private void showHint(final Editor editor, final Point point, final int lineNumber) {
  final JPanel panel = new JPanel(new BorderLayout());
  panel.add(createActionsToolbar(editor, lineNumber), BorderLayout.NORTH);

  final LineData lineData = getLineData(lineNumber);
  final Editor uEditor;
  if (lineData != null && lineData.getStatus() != LineCoverage.NONE && !mySubCoverageActive) {
    final EditorFactory factory = EditorFactory.getInstance();
    final Document doc = factory.createDocument(getReport(editor, lineNumber));
    doc.setReadOnly(true);
    uEditor = factory.createEditor(doc, editor.getProject());
    panel.add(EditorFragmentComponent.createEditorFragmentComponent(uEditor, 0, doc.getLineCount(), false, false), BorderLayout.CENTER);
  } else {
    uEditor = null;
  }


  final LightweightHint hint = new LightweightHint(panel){
    @Override
    public void hide() {
      if (uEditor != null) EditorFactory.getInstance().releaseEditor(uEditor);
      super.hide();

    }
  };
  HintManagerImpl.getInstanceImpl().showEditorHint(hint, editor, point,
                                                   HintManagerImpl.HIDE_BY_ANY_KEY | HintManagerImpl.HIDE_BY_TEXT_CHANGE | HintManagerImpl.HIDE_BY_OTHER_HINT | HintManagerImpl.HIDE_BY_SCROLLING, -1, false, new HintHint(editor, point));
}
 
Example 4
Source Project: consulo   File: FragmentContent.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void beforeListenersAttached(@Nonnull Document original, @Nonnull Document copy) {
  boolean writable = copy.isWritable();
  if (!writable) {
    copy.setReadOnly(false);
  }
  replaceString(copy, 0, copy.getTextLength(), subText(original, myRangeMarker.getStartOffset(), getLength()));
  copy.setReadOnly(!writable);
}
 
Example 5
Source Project: consulo   File: FragmentContent.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Document createCopy() {
  final Document originalDocument = myRangeMarker.getDocument();
  String textInRange =
    originalDocument.getCharsSequence().subSequence(myRangeMarker.getStartOffset(), myRangeMarker.getEndOffset()).toString();
  final Document result = EditorFactory.getInstance().createDocument(textInRange);
  result.setReadOnly(!originalDocument.isWritable());
  result.putUserData(ORIGINAL_DOCUMENT, originalDocument);
  return result;
}
 
Example 6
/**
 * Replaces the content of the given file with the given content. This is done on the Document
 * level.
 *
 * <p><b>NOTE:</b> This method should only be used as part of the recovery process.
 *
 * @param file the file to recover
 * @param content the new content of the file
 * @param encoding the encoding of the content
 * @param source the user that send the recovery action
 * @see Document
 */
public void handleContentRecovery(IFile file, byte[] content, String encoding, User source) {
  VirtualFile virtualFile = VirtualFileConverter.convertToVirtualFile(file);
  if (virtualFile == null) {
    log.warn(
        "Could not recover file content of "
            + file
            + " as it could not be converted to a virtual file.");

    return;
  }

  Project project = ((IntellijReferencePoint) file.getReferencePoint()).getProject();

  Document document = DocumentAPI.getDocument(virtualFile);
  if (document == null) {
    log.warn(
        "Could not recover file content of "
            + file
            + " as no valid document representation was returned by the Intellij API.");

    return;
  }

  int documentLength = document.getTextLength();

  annotationManager.removeAnnotations(file);

  String text;
  try {
    text = new String(content, encoding);
  } catch (UnsupportedEncodingException e) {
    log.warn("Could not decode text using given encoding. Using default instead.", e);

    text = new String(content);

    String qualifiedResource =
        file.getReferencePoint().getName() + " - " + file.getReferencePointRelativePath();

    NotificationPanel.showWarning(
        String.format(
            Messages.LocalEditorManipulator_incompatible_encoding_message,
            qualifiedResource,
            encoding,
            Charset.defaultCharset()),
        Messages.LocalEditorManipulator_incompatible_encoding_title);
  }

  boolean wasReadOnly = !document.isWritable();
  boolean wasDocumentListenerEnabled = manager.isDocumentModificationHandlerEnabled();

  try {
    // TODO distinguish if read-only was set by the StopManager, abort otherwise
    if (wasReadOnly) {
      document.setReadOnly(false);
    }

    if (wasDocumentListenerEnabled) {
      manager.setLocalDocumentModificationHandlersEnabled(false);
    }

    DocumentAPI.replaceText(project, document, 0, documentLength, text);

  } finally {

    if (wasDocumentListenerEnabled) {
      manager.setLocalDocumentModificationHandlersEnabled(true);
    }

    if (wasReadOnly) {
      document.setReadOnly(true);
    }
  }

  Editor editor = null;
  if (ProjectAPI.isOpen(project, virtualFile)) {
    editor = ProjectAPI.openEditor(project, virtualFile, false);

    if (editor == null) {
      log.warn("Could not obtain text editor for open, recovered file " + virtualFile);

      return;
    }
  }

  annotationManager.addContributionAnnotation(source, file, 0, documentLength, editor);
}