org.eclipse.text.undo.IDocumentUndoManager Java Examples

The following examples show how to use org.eclipse.text.undo.IDocumentUndoManager. 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: DocumentManager.java    From ContentAssist with MIT License 6 votes vote down vote up
/**
 * Registers a document manager with an editor.
 * @param doc the document to be managed
 * @param st the styled text of the editor
 * @param dm the document manager
 */
public static void register(IDocument doc, StyledText st, DocumentManager dm) {
    if (doc != null) {
        doc.addDocumentListener(dm);
        
        DocumentUndoManagerRegistry.connect(doc);
        IDocumentUndoManager undoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(doc);
        if (undoManager != null) {
            undoManager.addDocumentUndoListener(dm);
        }
    }
    
    if (st != null) {
        st.addListener(SWT.KeyDown, dm);
        st.addListener(SWT.MouseDown, dm);
        st.addListener(SWT.MouseDoubleClick, dm);
    }
}
 
Example #2
Source File: DocumentManager.java    From ContentAssist with MIT License 6 votes vote down vote up
/**
 * Unregisters a document manager with an editor.
 * @param doc the document to be managed
 * @param st the styled text of the editor
 * @param dm the document manager
 */
public static void unregister(IDocument doc, StyledText st, DocumentManager dm) {
    if (doc != null) {
        doc.removeDocumentListener(dm);
        
        IDocumentUndoManager undoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(doc);
        DocumentUndoManagerRegistry.disconnect(doc);
        if (undoManager != null) {
            undoManager.removeDocumentUndoListener(dm);
        }
    }
    
    if (st != null) {
        st.removeListener(SWT.KeyDown, dm);
        st.removeListener(SWT.MouseDown, dm);
        st.removeListener(SWT.MouseDoubleClick, dm);
    }
}
 
Example #3
Source File: DocumentUtils.java    From typescript.java with MIT License 6 votes vote down vote up
/**
 * Method will apply all edits to document as single modification. Needs to
 * be executed in UI thread.
 * 
 * @param document
 *            document to modify
 * @param edits
 *            list of TypeScript {@link CodeEdit}.
 * @throws TypeScriptException
 * @throws BadLocationException
 * @throws MalformedTreeException
 */
public static void applyEdits(IDocument document, List<CodeEdit> codeEdits)
		throws TypeScriptException, MalformedTreeException, BadLocationException {
	if (document == null || codeEdits.isEmpty()) {
		return;
	}

	IDocumentUndoManager manager = DocumentUndoManagerRegistry.getDocumentUndoManager(document);
	if (manager != null) {
		manager.beginCompoundChange();
	}

	try {
		TextEdit edit = toTextEdit(codeEdits, document);
		// RewriteSessionEditProcessor editProcessor = new
		// RewriteSessionEditProcessor(document, edit,
		// org.eclipse.text.edits.TextEdit.NONE);
		// editProcessor.performEdits();
		edit.apply(document);
	} finally {
		if (manager != null) {
			manager.endCompoundChange();
		}
	}
}
 
Example #4
Source File: SearchReplaceMinibuffer.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Replace all occurrences
 */
private void replaceAll() {
	boolean allState = isReplaceAll(); 
	IDocumentUndoManager undoer = DocumentUndoManagerRegistry.getDocumentUndoManager(getDocument());
	try {
		paused = false;
		setReplaceAll(true);	// force flag so we don't re-enter the undoer during case replacement
		if (undoer != null) {
			undoer.beginCompoundChange();
		}
		replaceIt();
		while (findNext(getSearchStr())){
			replaceIt();
		}
	} finally {
		setReplaceAll(allState);
		if (undoer != null) {
			undoer.endCompoundChange();
		}
	}
	finish();
}
 
Example #5
Source File: EmbeddedEditorFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected OperationHistoryListener installUndoRedoSupport(SourceViewer viewer, IDocument document, final EmbeddedEditorActions actions) {
	IDocumentUndoManager undoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(document);
	final IUndoContext context = undoManager.getUndoContext();
	IOperationHistory operationHistory = PlatformUI.getWorkbench().getOperationSupport().getOperationHistory();
	OperationHistoryListener operationHistoryListener = new OperationHistoryListener(context, new IUpdate() {
		@Override
		public void update() {
			actions.updateAction(ITextEditorActionConstants.REDO);
			actions.updateAction(ITextEditorActionConstants.UNDO);
		}
	});
	operationHistory.addOperationHistoryListener(operationHistoryListener);
	return operationHistoryListener;
}
 
Example #6
Source File: WaitForRefactoringCondition.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected IUndoContext getUndoContext() {
  IUndoContext _xblockexpression = null;
  {
    IEditorPart _editor = this.editor.getReference().getEditor(true);
    final ITextEditor ed = ((ITextEditor) _editor);
    final IDocument document = ed.getDocumentProvider().getDocument(ed.getEditorInput());
    final IDocumentUndoManager undoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(document);
    _xblockexpression = undoManager.getUndoContext();
  }
  return _xblockexpression;
}
 
Example #7
Source File: ConsoleCmdHandler.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
protected void setUpUndo(TextConsoleViewer viewer) {
		 IDocumentUndoManager undoer = DocumentUndoManagerRegistry.getDocumentUndoManager(viewer.getDocument());
		 if (undoer == null) {
			 DocumentUndoManagerRegistry.connect(viewer.getDocument());
//			 undoer = DocumentUndoManagerRegistry.getDocumentUndoManager(viewer.getDocument());
//			 viewer.setUndoManager((IUndoManager) undoer);
		 }
	}
 
Example #8
Source File: SearchReplaceMinibuffer.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Case-based replacement - after the initial find has already happened
 * 
 * @param replacer - the replacement string (may be regexp)
 * @param index - offset of find
 * @param all - all if true, else initial
 * @return - the replacement region
 * 
 * @throws BadLocationException
 */
private IRegion caseReplace(String replacer, int index, boolean all) throws BadLocationException {
	IRegion result = null;
	IDocumentUndoManager undoer = DocumentUndoManagerRegistry.getDocumentUndoManager(getDocument());
	try {
		if (!isReplaceAll() && undoer != null) {
			undoer.beginCompoundChange();
		}
		IFindReplaceTarget target = getTarget();
		// first replace with (possible regexp) string
		((IFindReplaceTargetExtension3)target).replaceSelection(replacer, isRegexp());
		// adjust case of actual replacement string
		replacer = target.getSelectionText();
		String caseReplacer = replacer;
		if (all) {
			caseReplacer = caseReplacer.toUpperCase();
		} else {
			caseReplacer = caseReplacer.trim();
			caseReplacer = Character.toUpperCase(caseReplacer.charAt(0)) + 
			caseReplacer.substring(1,caseReplacer.length()).toString();
			// now update the replacement string with the re-cased part
			caseReplacer = replacer.replaceFirst(replacer.trim(), caseReplacer);
		}
		int ind = target.findAndSelect(index, replacer, true, false, false);
		if (ind > -1) {
			target.replaceSelection(caseReplacer);
		}
	}  finally {
		if (!isReplaceAll() && undoer != null) {
			undoer.endCompoundChange();
		}
	}
	return result;
}
 
Example #9
Source File: ERDiagramElementStateListener.java    From ermasterr with Apache License 2.0 4 votes vote down vote up
@Override
public void elementMoved(final Object originalElement, final Object movedElement) {
    if (originalElement != null && originalElement.equals(editorPart.getEditorInput())) {
        final boolean doValidationAsync = Display.getCurrent() != null;
        final Runnable r = new Runnable() {
            @Override
            public void run() {
                if (movedElement == null || movedElement instanceof IEditorInput) {

                    final String previousContent;
                    IDocumentUndoManager previousUndoManager = null;
                    IDocument changed = null;
                    final boolean wasDirty = editorPart.isDirty();
                    changed = documentProvider.getDocument(editorPart.getEditorInput());
                    if (changed != null) {
                        if (wasDirty)
                            previousContent = changed.get();
                        else
                            previousContent = null;

                        previousUndoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(changed);
                        if (previousUndoManager != null)
                            previousUndoManager.connect(this);
                    } else
                        previousContent = null;

                    editorPart.setInputWithNotify((IEditorInput) movedElement);

                    if (previousUndoManager != null) {
                        final IDocument newDocument = documentProvider.getDocument(movedElement);
                        if (newDocument != null) {
                            final IDocumentUndoManager newUndoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(newDocument);
                            if (newUndoManager != null)
                                newUndoManager.transferUndoHistory(previousUndoManager);
                        }
                        previousUndoManager.disconnect(this);
                    }

                    if (wasDirty && changed != null) {
                        final Runnable r2 = new Runnable() {
                            @Override
                            public void run() {
                                documentProvider.getDocument(editorPart.getEditorInput()).set(previousContent);

                            }
                        };
                        execute(r2, doValidationAsync);
                    }

                }
            }
        };
        execute(r, false);
    }
}
 
Example #10
Source File: TestEditor.java    From ermasterr with Apache License 2.0 4 votes vote down vote up
@Override
public void elementMoved(final Object originalElement, final Object movedElement) {
    if (originalElement != null && originalElement.equals(getEditorInput())) {
        final boolean doValidationAsync = Display.getCurrent() != null;
        final Runnable r = new Runnable() {
            @Override
            public void run() {
                enableSanityChecking(true);

                if (fSourceViewer == null)
                    return;

                if (movedElement == null || movedElement instanceof IEditorInput) {

                    final IDocumentProvider d = getDocumentProvider();
                    final String previousContent;
                    IDocumentUndoManager previousUndoManager = null;
                    IDocument changed = null;
                    final boolean wasDirty = isDirty();
                    changed = d.getDocument(getEditorInput());
                    if (changed != null) {
                        if (wasDirty)
                            previousContent = changed.get();
                        else
                            previousContent = null;

                        previousUndoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(changed);
                        if (previousUndoManager != null)
                            previousUndoManager.connect(this);
                    } else
                        previousContent = null;

                    setInput((IEditorInput) movedElement);

                    // The undo manager needs to be replaced with one
                    // for the new document.
                    // Transfer the undo history and then disconnect
                    // from the old undo manager.
                    if (previousUndoManager != null) {
                        final IDocument newDocument = getDocumentProvider().getDocument(movedElement);
                        if (newDocument != null) {
                            final IDocumentUndoManager newUndoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(newDocument);
                            if (newUndoManager != null)
                                newUndoManager.transferUndoHistory(previousUndoManager);
                        }
                        previousUndoManager.disconnect(this);
                    }

                    if (wasDirty && changed != null) {
                        final Runnable r2 = new Runnable() {
                            @Override
                            public void run() {
                                validateState(getEditorInput());
                                d.getDocument(getEditorInput()).set(previousContent);

                            }
                        };
                        execute(r2, doValidationAsync);
                    }

                }
            }
        };
        execute(r, false);
    }
}
 
Example #11
Source File: ERDiagramElementStateListener.java    From erflute with Apache License 2.0 4 votes vote down vote up
@Override
public void elementMoved(final Object originalElement, final Object movedElement) {
    if (originalElement != null && originalElement.equals(editorPart.getEditorInput())) {
        final boolean doValidationAsync = Display.getCurrent() != null;
        final Runnable r = new Runnable() {

            @Override
            public void run() {
                if (movedElement == null || movedElement instanceof IEditorInput) {
                    final String previousContent;
                    IDocumentUndoManager previousUndoManager = null;
                    IDocument changed = null;
                    final boolean wasDirty = editorPart.isDirty();
                    changed = documentProvider.getDocument(editorPart.getEditorInput());
                    if (changed != null) {
                        if (wasDirty) {
                            previousContent = changed.get();
                        } else {
                            previousContent = null;
                        }
                        previousUndoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(changed);
                        if (previousUndoManager != null) {
                            previousUndoManager.connect(this);
                        }
                    } else {
                        previousContent = null;
                    }
                    editorPart.setInputWithNotify((IEditorInput) movedElement);

                    if (previousUndoManager != null) {
                        final IDocument newDocument = documentProvider.getDocument(movedElement);
                        if (newDocument != null) {
                            final IDocumentUndoManager newUndoManager = DocumentUndoManagerRegistry.getDocumentUndoManager(newDocument);
                            if (newUndoManager != null)
                                newUndoManager.transferUndoHistory(previousUndoManager);
                        }
                        previousUndoManager.disconnect(this);
                    }

                    if (wasDirty && changed != null) {
                        final Runnable r2 = new Runnable() {

                            @Override
                            public void run() {
                                documentProvider.getDocument(editorPart.getEditorInput()).set(previousContent);
                            }
                        };
                        execute(r2, doValidationAsync);
                    }
                }
            }
        };
        execute(r, false);
    }
}
 
Example #12
Source File: JSEditor.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
public void createPartControl( Composite parent )
{
	Composite child = this.initEditorLayout( parent );

	// Script combo
	cmbExprListViewer = new ComboViewer( cmbExpList );
	JSExpListProvider provider = new JSExpListProvider( );
	cmbExprListViewer.setContentProvider( provider );
	cmbExprListViewer.setLabelProvider( provider );
	cmbExprListViewer.setData( VIEWER_CATEGORY_KEY, VIEWER_CATEGORY_CONTEXT );

	// SubFunctions combo
	JSSubFunctionListProvider subProvider = new JSSubFunctionListProvider( this );

	// also add subProvider as listener of expr viewer.
	cmbExprListViewer.addSelectionChangedListener( subProvider );

	cmbSubFunctions.addListener( CustomChooserComposite.DROPDOWN_EVENT,
			new Listener( ) {

				public void handleEvent( Event event )
				{
					cmbSubFunctions.deselectAll( );

					ScriptParser parser = new ScriptParser( getEditorText( ) );

					Collection<IScriptMethodInfo> coll = parser.getAllMethodInfo( );

					for ( Iterator<IScriptMethodInfo> itr = coll.iterator( ); itr.hasNext( ); )
					{
						IScriptMethodInfo mtd = itr.next( );

						cmbSubFunctions.markSelection( METHOD_DISPLAY_INDENT
								+ mtd.getName( ) );
					}
				}

			} );

	cmbSubFunctionsViewer = new TextComboViewer( cmbSubFunctions );
	cmbSubFunctionsViewer.setContentProvider( subProvider );
	cmbSubFunctionsViewer.setLabelProvider( subProvider );
	cmbSubFunctionsViewer.addSelectionChangedListener( subProvider );
	cmbSubFunctionsViewer.addSelectionChangedListener( propertyDefnChangeListener );

	// Initialize the model for the document.
	Object model = getModel( );
	if ( model != null )
	{
		cmbExpList.setVisible( true );
		cmbSubFunctions.setVisible( true );
		setComboViewerInput( model );
	}
	else
	{
		setComboViewerInput( Messages.getString( "JSEditor.Input.trial" ) ); //$NON-NLS-1$
	}
	cmbExprListViewer.addSelectionChangedListener( palettePage.getSupport( ) );
	cmbExprListViewer.addSelectionChangedListener( propertyDefnChangeListener );

	scriptEditor.createPartControl( child );
	scriptValidator = new ScriptValidator( getViewer( ) );

	disableEditor( );

	SourceViewer viewer = getViewer( );
	IDocument document = viewer == null ? null : viewer.getDocument( );

	if ( document != null )
	{
		IDocumentUndoManager undoManager = DocumentUndoManagerRegistry.getDocumentUndoManager( document );

		if ( undoManager != null )
		{
			undoManager.addDocumentUndoListener( undoListener );
		}
		document.addDocumentListener( documentListener );
	}
}
 
Example #13
Source File: ERDiagramElementStateListener.java    From ermaster-b with Apache License 2.0 4 votes vote down vote up
public void elementMoved(final Object originalElement,
		final Object movedElement) {
	if (originalElement != null
			&& originalElement.equals(editorPart.getEditorInput())) {
		final boolean doValidationAsync = Display.getCurrent() != null;
		Runnable r = new Runnable() {
			public void run() {
				if (movedElement == null
						|| movedElement instanceof IEditorInput) {

					final String previousContent;
					IDocumentUndoManager previousUndoManager = null;
					IDocument changed = null;
					boolean wasDirty = editorPart.isDirty();
					changed = documentProvider.getDocument(editorPart
							.getEditorInput());
					if (changed != null) {
						if (wasDirty)
							previousContent = changed.get();
						else
							previousContent = null;

						previousUndoManager = DocumentUndoManagerRegistry
								.getDocumentUndoManager(changed);
						if (previousUndoManager != null)
							previousUndoManager.connect(this);
					} else
						previousContent = null;

					editorPart
							.setInputWithNotify((IEditorInput) movedElement);

					if (previousUndoManager != null) {
						IDocument newDocument = documentProvider
								.getDocument(movedElement);
						if (newDocument != null) {
							IDocumentUndoManager newUndoManager = DocumentUndoManagerRegistry
									.getDocumentUndoManager(newDocument);
							if (newUndoManager != null)
								newUndoManager
										.transferUndoHistory(previousUndoManager);
						}
						previousUndoManager.disconnect(this);
					}

					if (wasDirty && changed != null) {
						Runnable r2 = new Runnable() {
							public void run() {
								documentProvider.getDocument(
										editorPart.getEditorInput()).set(
										previousContent);

							}
						};
						execute(r2, doValidationAsync);
					}

				}
			}
		};
		execute(r, false);
	}
}
 
Example #14
Source File: TestEditor.java    From ermaster-b with Apache License 2.0 4 votes vote down vote up
public void elementMoved(final Object originalElement,
		final Object movedElement) {
	if (originalElement != null
			&& originalElement.equals(getEditorInput())) {
		final boolean doValidationAsync = Display.getCurrent() != null;
		Runnable r = new Runnable() {
			public void run() {
				enableSanityChecking(true);

				if (fSourceViewer == null)
					return;

				if (movedElement == null
						|| movedElement instanceof IEditorInput) {

					final IDocumentProvider d = getDocumentProvider();
					final String previousContent;
					IDocumentUndoManager previousUndoManager = null;
					IDocument changed = null;
					boolean wasDirty = isDirty();
					changed = d.getDocument(getEditorInput());
					if (changed != null) {
						if (wasDirty)
							previousContent = changed.get();
						else
							previousContent = null;

						previousUndoManager = DocumentUndoManagerRegistry
								.getDocumentUndoManager(changed);
						if (previousUndoManager != null)
							previousUndoManager.connect(this);
					} else
						previousContent = null;

					setInput((IEditorInput) movedElement);

					// The undo manager needs to be replaced with one
					// for the new document.
					// Transfer the undo history and then disconnect
					// from the old undo manager.
					if (previousUndoManager != null) {
						IDocument newDocument = getDocumentProvider()
								.getDocument(movedElement);
						if (newDocument != null) {
							IDocumentUndoManager newUndoManager = DocumentUndoManagerRegistry
									.getDocumentUndoManager(newDocument);
							if (newUndoManager != null)
								newUndoManager
										.transferUndoHistory(previousUndoManager);
						}
						previousUndoManager.disconnect(this);
					}

					if (wasDirty && changed != null) {
						Runnable r2 = new Runnable() {
							public void run() {
								validateState(getEditorInput());
								d.getDocument(getEditorInput()).set(
										previousContent);

							}
						};
						execute(r2, doValidationAsync);
					}

				}
			}
		};
		execute(r, false);
	}
}