Java Code Examples for org.netbeans.editor.BaseDocument#addPostModificationDocumentListener()

The following examples show how to use org.netbeans.editor.BaseDocument#addPostModificationDocumentListener() . 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 File: LineTranslations.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void attach() throws IOException {
    DataObject dobj;
    synchronized (this) {
        dobj = this.dataObject;
    }
    LineCookie lc = dobj.getLookup().lookup (LineCookie.class);
    if (lc == null) {
        return ;
    }
    lb.addPropertyChangeListener(this);
    try {
        final Line lineNew = lc.getLineSet().getCurrent(lb.getLineNumber() - 1);
        synchronized (this) {
            if (line != null) {
                line.removePropertyChangeListener(this);
            }
            this.line = lineNew;
        }
        lineNew.addPropertyChangeListener(this);
        StyledDocument document = NbDocument.getDocument(new Lookup.Provider() {
                                      @Override
                                      public Lookup getLookup() {
                                          return lineNew.getLookup();
                                      }
                                  });
        if (document instanceof BaseDocument) {
            BaseDocument bd = (BaseDocument) document;
            bd.addPostModificationDocumentListener(this);
        }
    } catch (IndexOutOfBoundsException ioobex) {
        // ignore document changes for BP with bad line number
    }
}
 
Example 2
Source File: TextRegionManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void activate() {
    if (editGroups.size() == 0) {
        if (doc instanceof BaseDocument) {
            BaseDocument bdoc = (BaseDocument)doc;
            // Add the listener to allow doc syncing modifications
            // The listener is never removed (since this object is a property of the document)
            bdoc.addPostModificationDocumentListener(DocListener.INSTANCE);
            bdoc.addUpdateDocumentListener(UpdateDocListener.INSTANCE);
        }
    }
}
 
Example 3
Source File: InstantRefactoringPerformer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public InstantRefactoringPerformer(final JTextComponent target, int caretOffset, InstantRefactoringUI ui) {
    releaseAll();
    this.target = target;
    this.ui = ui;
    doc = target.getDocument();

    MutablePositionRegion mainRegion = null;
    List<MutablePositionRegion> regions = new ArrayList<>(ui.getRegions().size());

    for (MutablePositionRegion current : ui.getRegions()) {
        // TODO: type parameter name is represented as ident -> ignore surrounding <> in rename
        if (isIn(current, caretOffset)) {
            mainRegion = current;
        } else {
            regions.add(current);
        }
    }

    if (mainRegion == null) {
        throw new IllegalArgumentException("No highlight contains the caret.");
    }

    regions.add(0, mainRegion);

    region = new SyncDocumentRegion(doc, regions);

    if (doc instanceof BaseDocument) {
        BaseDocument bdoc = ((BaseDocument) doc);
        bdoc.addPostModificationDocumentListener(this);
        
        UndoableWrapper wrapper = MimeLookup.getLookup("text/x-java").lookup(UndoableWrapper.class);
        if(wrapper != null) {
            wrapper.setActive(true, this);
        }

        UndoableEdit undo = new CancelInstantRenameUndoableEdit(this);
        for (UndoableEditListener l : bdoc.getUndoableEditListeners()) {
            l.undoableEditHappened(new UndoableEditEvent(doc, undo));
        }
    }

    target.addKeyListener(this);

    target.putClientProperty(InstantRefactoringPerformer.class, this);
    target.putClientProperty("NetBeansEditor.navigateBoundaries", mainRegion); // NOI18N
	
    requestRepaint();
    
    target.select(mainRegion.getStartOffset(), mainRegion.getEndOffset());
    
    span = region.getFirstRegionLength();
    compl = new CompletionLayout(this);
    compl.setEditorComponent(target);
    final KeyStroke OKKS = ui.getKeyStroke();
    target.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(OKKS, OKActionKey);
    Action OKAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            if(registry.contains(InstantRefactoringPerformer.this)) {
                doFullRefactoring();
                target.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).remove(OKKS);
                target.getActionMap().remove(OKActionKey);
            } else {
                target.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).remove(OKKS);
                target.getActionMap().remove(OKActionKey);
            }
        }
    };
    target.getActionMap().put(OKActionKey, OKAction);
    final KeyStroke keyStroke = ui.getKeyStroke();
    compl.showCompletion(ui.getOptions(), caretOffset, Bundle.INFO_PressAgain(getKeyStrokeAsText(keyStroke)));
    
    registry.add(this);
}