Search in sources :

Example 1 with AbstractStructuredCleanupProcessor

use of org.eclipse.wst.sse.core.internal.cleanup.AbstractStructuredCleanupProcessor 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)

Aggregations

ITextSelection (org.eclipse.jface.text.ITextSelection)1 IEditorPart (org.eclipse.ui.IEditorPart)1 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)1 AbstractStructuredCleanupProcessor (org.eclipse.wst.sse.core.internal.cleanup.AbstractStructuredCleanupProcessor)1 IStructuredCleanupProcessor (org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupProcessor)1 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)1 StructuredTextEditor (org.eclipse.wst.sse.ui.StructuredTextEditor)1