|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2005 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
* |
| 11 |
*******************************************************************************/ |
| 12 |
|
| 13 |
package org.eclipse.wst.html.ui.internal.edit.ui; |
| 14 |
|
| 15 |
import org.eclipse.jface.action.IAction; |
| 16 |
import org.eclipse.jface.text.IDocument; |
| 17 |
import org.eclipse.jface.text.ITextSelection; |
| 18 |
import org.eclipse.jface.viewers.ISelection; |
| 19 |
import org.eclipse.jface.window.Window; |
| 20 |
import org.eclipse.swt.custom.BusyIndicator; |
| 21 |
import org.eclipse.swt.widgets.Event; |
| 22 |
import org.eclipse.ui.IActionDelegate2; |
| 23 |
import org.eclipse.ui.IEditorActionDelegate; |
| 24 |
import org.eclipse.ui.IEditorPart; |
| 25 |
import org.eclipse.ui.IViewActionDelegate; |
| 26 |
import org.eclipse.ui.IViewPart; |
| 27 |
import org.eclipse.ui.texteditor.ITextEditor; |
| 28 |
import org.eclipse.wst.html.core.internal.cleanup.HTMLCleanupProcessorImpl; |
| 29 |
import org.eclipse.wst.html.ui.internal.HTMLUIMessages; |
| 30 |
import org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupProcessor; |
| 31 |
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
| 32 |
import org.eclipse.wst.sse.core.internal.provisional.StructuredModelManager; |
| 33 |
import org.eclipse.wst.sse.ui.internal.SSEUIMessages; |
| 34 |
import org.eclipse.wst.sse.ui.internal.StructuredTextEditor; |
| 35 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument; |
| 36 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel; |
| 37 |
|
| 38 |
/** |
| 39 |
* Cleanup action delegate for HTML editor |
| 40 |
*/ |
| 41 |
public class CleanupActionHTMLDelegate implements IEditorActionDelegate, IActionDelegate2, IViewActionDelegate { |
| 42 |
private IEditorPart fEditor; |
| 43 |
private IStructuredCleanupProcessor fCleanupProcessor; |
| 44 |
|
| 45 |
public void setActiveEditor(IAction action, IEditorPart targetEditor) { |
| 46 |
fEditor = targetEditor; |
| 47 |
} |
| 48 |
|
| 49 |
public void dispose() { |
| 50 |
// nulling out just in case |
| 51 |
fEditor = null; |
| 52 |
fCleanupProcessor = null; |
| 53 |
} |
| 54 |
|
| 55 |
public void init(IAction action) { |
| 56 |
if (action != null) { |
| 57 |
action.setText(HTMLUIMessages.CleanupDocument_label); |
| 58 |
action.setToolTipText(HTMLUIMessages.CleanupDocument_tooltip); |
| 59 |
action.setDescription(HTMLUIMessages.CleanupDocument_description); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public void runWithEvent(IAction action, Event event) { |
| 64 |
run(action); |
| 65 |
} |
| 66 |
|
| 67 |
public void init(IViewPart view) { |
| 68 |
// do nothing |
| 69 |
} |
| 70 |
|
| 71 |
public void run(IAction action) { |
| 72 |
if (fEditor instanceof StructuredTextEditor) { |
| 73 |
final StructuredTextEditor editor = (StructuredTextEditor) fEditor; |
| 74 |
CleanupDialogHTML cleanupDialog = new CleanupDialogHTML(editor.getSite().getShell()); |
| 75 |
cleanupDialog.setisXHTMLType(isXHTML()); |
| 76 |
if (cleanupDialog.open() == Window.OK) { |
| 77 |
// setup runnable |
| 78 |
Runnable runnable = new Runnable() { |
| 79 |
public void run() { |
| 80 |
IStructuredCleanupProcessor cleanupProcessor = getCleanupProcessor(); |
| 81 |
if (cleanupProcessor != null) { |
| 82 |
IStructuredModel model = null; |
| 83 |
try { |
| 84 |
model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput())); |
| 85 |
if (model != null) |
| 86 |
cleanupProcessor.cleanupModel(model); |
| 87 |
} |
| 88 |
finally { |
| 89 |
if (model != null) |
| 90 |
model.releaseFromEdit(); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
// TODO: make independent of 'model'. |
| 97 |
IStructuredModel model = null; |
| 98 |
try { |
| 99 |
model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput())); |
| 100 |
if (model != null) { |
| 101 |
// begin recording |
| 102 |
ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); |
| 103 |
model.beginRecording(this, SSEUIMessages.Cleanup_Document_UI_, SSEUIMessages.Cleanup_Document_UI_, selection.getOffset(), selection.getLength()); //$NON-NLS-1$ //$NON-NLS-2$ |
| 104 |
|
| 105 |
// tell the model that we are about to make a big |
| 106 |
// model change |
| 107 |
model.aboutToChangeModel(); |
| 108 |
|
| 109 |
// run |
| 110 |
BusyIndicator.showWhile(editor.getTextViewer().getControl().getDisplay(), runnable); |
| 111 |
} |
| 112 |
} |
| 113 |
finally { |
| 114 |
if (model != null) { |
| 115 |
// tell the model that we are done with the big |
| 116 |
// model |
| 117 |
// change |
| 118 |
model.changedModel(); |
| 119 |
|
| 120 |
// end recording |
| 121 |
ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); |
| 122 |
model.endRecording(this, selection.getOffset(), selection.getLength()); |
| 123 |
model.releaseFromEdit(); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public void selectionChanged(IAction action, ISelection selection) { |
| 131 |
// do nothing |
| 132 |
} |
| 133 |
|
| 134 |
IStructuredCleanupProcessor getCleanupProcessor() { |
| 135 |
if (fCleanupProcessor == null) |
| 136 |
fCleanupProcessor = new HTMLCleanupProcessorImpl(); |
| 137 |
|
| 138 |
return fCleanupProcessor; |
| 139 |
} |
| 140 |
|
| 141 |
private boolean isXHTML() { |
| 142 |
boolean isxhtml = false; |
| 143 |
if (fEditor instanceof ITextEditor) { |
| 144 |
ITextEditor textEditor = (ITextEditor) fEditor; |
| 145 |
IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); |
| 146 |
IStructuredModel model = null; |
| 147 |
try { |
| 148 |
model = StructuredModelManager.getModelManager().getExistingModelForRead(document); |
| 149 |
if (model instanceof IDOMModel) { |
| 150 |
IDOMDocument domDocument = ((IDOMModel) model).getDocument(); |
| 151 |
if (domDocument != null) |
| 152 |
isxhtml = domDocument.isXMLType(); |
| 153 |
} |
| 154 |
} |
| 155 |
finally { |
| 156 |
if (model != null) { |
| 157 |
model.releaseFromRead(); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
return isxhtml; |
| 162 |
} |
| 163 |
} |