Search in sources :

Example 56 with StructuredTextEditor

use of org.eclipse.wst.sse.ui.StructuredTextEditor in project webtools.sourceediting by eclipse.

the class TestStructuredTextEditor method testModificationReconciling.

/**
 * Test that modifications to the editor's content will notify reconciling listeners
 */
public void testModificationReconciling() throws Exception {
    IFile file = getOrCreateFile(PROJECT_NAME + "/" + "reconcilingmodificationtest.xml");
    IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    final int[] state = new int[2];
    Arrays.fill(state, -1);
    ISourceReconcilingListener listener = new ISourceReconcilingListener() {

        int mod = 0;

        public void reconciled(IDocument document, IAnnotationModel model, boolean forced, IProgressMonitor progressMonitor) {
            state[1] = mod++;
        }

        public void aboutToBeReconciled() {
            state[0] = mod++;
        }
    };
    IEditorPart editor = IDE.openEditor(activePage, file, "org.eclipse.wst.sse.ui.StructuredTextEditor.test");
    try {
        assertTrue("Not a StructuredTextEditor", editor instanceof StructuredTextEditor);
        addReconcilingListener((StructuredTextEditor) editor, listener);
        waitForReconcile(state);
        assertTrue("Initial: Reconciling did not complete in a timely fashion", state[0] != -1 && state[1] != -1);
        assertTrue("Initial: aboutToBeReconciled not invoked first (" + state[0] + ")", state[0] == 0);
        assertTrue("Initial: reconciled not invoked after aboutToBeReconciled (" + state[1] + ")", state[1] == 1);
        IDocument document = ((StructuredTextEditor) editor).getDocumentProvider().getDocument(editor.getEditorInput());
        assertTrue("Editor doesn't have a document", document != null);
        document.set("<?xml version=\"1.0\" ?>");
        Arrays.fill(state, -1);
        waitForReconcile(state);
        assertTrue("Modified: Reconciling did not complete in a timely fashion", state[0] != -1 && state[1] != -1);
        assertTrue("Modified: aboutToBeReconciled not invoked first (" + state[0] + ")", state[0] == 2);
        assertTrue("Modified: reconciled not invoked after aboutToBeReconciled (" + state[1] + ")", state[1] == 3);
    } finally {
        if (editor != null && activePage != null) {
            activePage.closeEditor(editor, false);
        }
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ISourceReconcilingListener(org.eclipse.wst.sse.ui.reconcile.ISourceReconcilingListener) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEditorPart(org.eclipse.ui.IEditorPart) StructuredTextEditor(org.eclipse.wst.sse.ui.StructuredTextEditor) IDocument(org.eclipse.jface.text.IDocument)

Example 57 with StructuredTextEditor

use of org.eclipse.wst.sse.ui.StructuredTextEditor in project webtools.sourceediting by eclipse.

the class TestStructuredTextEditor method testGetSetEditorPart.

public void testGetSetEditorPart() {
    fEditor.setEditorPart(null);
    assertTrue("Unable to set editor part", true);
    IEditorPart part = fEditor.getEditorPart();
    assertTrue("Did not get expected editor part", part instanceof StructuredTextEditor);
}
Also used : IEditorPart(org.eclipse.ui.IEditorPart) StructuredTextEditor(org.eclipse.wst.sse.ui.StructuredTextEditor)

Example 58 with StructuredTextEditor

use of org.eclipse.wst.sse.ui.StructuredTextEditor in project webtools.sourceediting by eclipse.

the class CleanupDocumentHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
    ITextEditor textEditor = null;
    if (editorPart instanceof ITextEditor)
        textEditor = (ITextEditor) editorPart;
    else {
        Object o = editorPart.getAdapter(ITextEditor.class);
        if (o != null)
            textEditor = (ITextEditor) o;
    }
    if (textEditor != null) {
        final ITextEditor editor = textEditor;
        CleanupDialogHTML cleanupDialog = new CleanupDialogHTML(editor.getSite().getShell());
        cleanupDialog.setisXHTMLType(isXHTML(editor));
        if (cleanupDialog.open() == Window.OK) {
            // setup runnable
            Runnable runnable = new Runnable() {

                public void run() {
                    IStructuredCleanupProcessor cleanupProcessor = getCleanupProcessor();
                    if (cleanupProcessor != null) {
                        IStructuredModel model = null;
                        try {
                            model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput()));
                            if (model != null) {
                                if ((cleanupProcessor instanceof AbstractStructuredCleanupProcessor) && (editor instanceof StructuredTextEditor)) {
                                    ((AbstractStructuredCleanupProcessor) cleanupProcessor).cleanupModel(model, ((StructuredTextEditor) editor).getTextViewer());
                                } else {
                                    cleanupProcessor.cleanupModel(model);
                                }
                            }
                        } finally {
                            if (model != null)
                                model.releaseFromEdit();
                        }
                    }
                }
            };
            // TODO: make independent of 'model'.
            IStructuredModel model = null;
            try {
                model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput()));
                if (model != null) {
                    // begin recording
                    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
                    // $NON-NLS-1$ //$NON-NLS-2$
                    model.beginRecording(this, SSEUIMessages.Cleanup_Document_UI_, SSEUIMessages.Cleanup_Document_UI_, selection.getOffset(), selection.getLength());
                    // tell the model that we are about to make a big
                    // model change
                    model.aboutToChangeModel();
                    // run
                    BusyIndicator.showWhile(editor.getEditorSite().getWorkbenchWindow().getShell().getDisplay(), runnable);
                }
            } finally {
                if (model != null) {
                    // tell the model that we are done with the big
                    // model
                    // change
                    model.changedModel();
                    // end recording
                    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
                    model.endRecording(this, selection.getOffset(), selection.getLength());
                    model.releaseFromEdit();
                }
            }
        }
    }
    return null;
}
Also used : IStructuredCleanupProcessor(org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupProcessor) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) AbstractStructuredCleanupProcessor(org.eclipse.wst.sse.core.internal.cleanup.AbstractStructuredCleanupProcessor) IEditorPart(org.eclipse.ui.IEditorPart) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) StructuredTextEditor(org.eclipse.wst.sse.ui.StructuredTextEditor) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 59 with StructuredTextEditor

use of org.eclipse.wst.sse.ui.StructuredTextEditor in project webtools.sourceediting by eclipse.

the class ToggleLineCommentHandler method processAction.

/**
 * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractCommentHandler#processAction(
 * 	org.eclipse.ui.texteditor.ITextEditor, org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument,
 * 	org.eclipse.jface.text.ITextSelection)
 */
protected void processAction(ITextEditor textEditor, final IStructuredDocument document, ITextSelection textSelection) {
    IStructuredModel model = null;
    DocumentRewriteSession session = null;
    boolean changed = false;
    try {
        // get text selection lines info
        int selectionStartLine = textSelection.getStartLine();
        int selectionEndLine = textSelection.getEndLine();
        int selectionEndLineOffset = document.getLineOffset(selectionEndLine);
        int selectionEndOffset = textSelection.getOffset() + textSelection.getLength();
        // adjust selection end line
        if ((selectionEndLine > selectionStartLine) && (selectionEndLineOffset == selectionEndOffset)) {
            selectionEndLine--;
        }
        // save the selection position since it will be changing
        Position selectionPosition = null;
        selectionPosition = new Position(textSelection.getOffset(), textSelection.getLength());
        document.addPosition(selectionPosition);
        model = StructuredModelManager.getModelManager().getModelForEdit(document);
        if (model != null) {
            // makes it so one undo will undo all the edits to the document
            model.beginRecording(this, SSEUIMessages.ToggleComment_label, SSEUIMessages.ToggleComment_description);
            // keeps listeners from doing anything until updates are all done
            model.aboutToChangeModel();
            if (document instanceof IDocumentExtension4) {
                session = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
            }
            changed = true;
            // get the display for the editor if we can
            Display display = null;
            if (textEditor instanceof StructuredTextEditor) {
                StructuredTextViewer viewer = ((StructuredTextEditor) textEditor).getTextViewer();
                if (viewer != null) {
                    display = viewer.getControl().getDisplay();
                }
            }
            // create the toggling operation
            IRunnableWithProgress toggleCommentsRunnable = new ToggleLinesRunnable(model.getContentTypeIdentifier(), document, selectionStartLine, selectionEndLine, display);
            // if toggling lots of lines then use progress monitor else just run the operation
            if ((selectionEndLine - selectionStartLine) > TOGGLE_LINES_MAX_NO_BUSY_INDICATOR && display != null) {
                ProgressMonitorDialog dialog = new ProgressMonitorDialog(display.getActiveShell());
                dialog.run(false, true, toggleCommentsRunnable);
            } else {
                toggleCommentsRunnable.run(new NullProgressMonitor());
            }
        }
    } catch (InvocationTargetException e) {
        // $NON-NLS-1$
        Logger.logException("Problem running toggle comment progess dialog.", e);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        Logger.logException("Problem running toggle comment progess dialog.", e);
    } catch (BadLocationException e) {
        // $NON-NLS-1$ //$NON-NLS-2$
        Logger.logException("The given selection " + textSelection + " must be invalid", e);
    } finally {
        // clean everything up
        if (session != null && document instanceof IDocumentExtension4) {
            ((IDocumentExtension4) document).stopRewriteSession(session);
        }
        if (model != null) {
            model.endRecording(this);
            if (changed) {
                model.changedModel();
            }
            model.releaseFromEdit();
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Position(org.eclipse.jface.text.Position) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) StructuredTextEditor(org.eclipse.wst.sse.ui.StructuredTextEditor) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) StructuredTextViewer(org.eclipse.wst.sse.ui.internal.StructuredTextViewer) BadLocationException(org.eclipse.jface.text.BadLocationException) Display(org.eclipse.swt.widgets.Display)

Example 60 with StructuredTextEditor

use of org.eclipse.wst.sse.ui.StructuredTextEditor in project webtools.sourceediting by eclipse.

the class JSPELContentAssistTest method testELChosenProposalCompletion.

public void testELChosenProposalCompletion() throws Exception {
    IFile file = getFile(FILE_1_PATH);
    ICompletionProposal[] props = getCompletionProposals(file, TEST_1_DOC_LOC);
    verifyProposals(props, TEST_1_EXPECTED_PROPS);
    StructuredTextEditor editor = getEditor(file);
    StructuredTextViewer viewer = editor.getTextViewer();
    IDocument document = viewer.getDocument();
    props[0].apply(document);
    String inserted = document.get(TEST_1_DOC_LOC - 1, TEST_1_EXPECTED_PROPS[0].length());
    assertEquals("The completed proposal " + inserted + " does not match the expected completion " + TEST_1_EXPECTED_PROPS[0], TEST_1_EXPECTED_PROPS[0], inserted);
    editor.getSite().getPage().saveEditor(editor, false);
    editor.close(false);
}
Also used : IFile(org.eclipse.core.resources.IFile) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) StructuredTextEditor(org.eclipse.wst.sse.ui.StructuredTextEditor) StructuredTextViewer(org.eclipse.wst.sse.ui.internal.StructuredTextViewer) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

StructuredTextEditor (org.eclipse.wst.sse.ui.StructuredTextEditor)67 IFile (org.eclipse.core.resources.IFile)29 IEditorPart (org.eclipse.ui.IEditorPart)28 StructuredTextViewer (org.eclipse.wst.sse.ui.internal.StructuredTextViewer)21 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)18 IDocument (org.eclipse.jface.text.IDocument)15 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)15 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)12 Position (org.eclipse.jface.text.Position)11 ArrayList (java.util.ArrayList)10 List (java.util.List)10 BadLocationException (org.eclipse.jface.text.BadLocationException)9 ITextSelection (org.eclipse.jface.text.ITextSelection)6 PartInitException (org.eclipse.ui.PartInitException)6 XMLMultiPageEditorPart (org.eclipse.wst.xml.ui.internal.tabletree.XMLMultiPageEditorPart)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 Action (org.eclipse.jface.action.Action)4 Separator (org.eclipse.jface.action.Separator)4 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)3 IFileEditorInput (org.eclipse.ui.IFileEditorInput)3