|
Added
Link Here
|
| 1 |
package org.eclipse.wst.css.ui.internal.edit.ui; |
| 2 |
|
| 3 |
import org.eclipse.jface.action.IAction; |
| 4 |
import org.eclipse.jface.dialogs.Dialog; |
| 5 |
import org.eclipse.jface.text.ITextSelection; |
| 6 |
import org.eclipse.jface.viewers.ISelection; |
| 7 |
import org.eclipse.jface.window.Window; |
| 8 |
import org.eclipse.swt.custom.BusyIndicator; |
| 9 |
import org.eclipse.swt.widgets.Event; |
| 10 |
import org.eclipse.ui.IActionDelegate2; |
| 11 |
import org.eclipse.ui.IEditorActionDelegate; |
| 12 |
import org.eclipse.ui.IEditorPart; |
| 13 |
import org.eclipse.ui.IViewActionDelegate; |
| 14 |
import org.eclipse.ui.IViewPart; |
| 15 |
import org.eclipse.wst.css.core.internal.cleanup.CleanupProcessorCSS; |
| 16 |
import org.eclipse.wst.css.ui.internal.CSSUIMessages; |
| 17 |
import org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupProcessor; |
| 18 |
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
| 19 |
import org.eclipse.wst.sse.core.internal.provisional.StructuredModelManager; |
| 20 |
import org.eclipse.wst.sse.ui.internal.SSEUIMessages; |
| 21 |
import org.eclipse.wst.sse.ui.internal.StructuredTextEditor; |
| 22 |
|
| 23 |
/** |
| 24 |
* Cleanup action delegate for CSS editor |
| 25 |
*/ |
| 26 |
public class CleanupActionCSSDelegate implements IEditorActionDelegate, IActionDelegate2, IViewActionDelegate { |
| 27 |
private IEditorPart fEditor; |
| 28 |
private IStructuredCleanupProcessor fCleanupProcessor; |
| 29 |
|
| 30 |
public void setActiveEditor(IAction action, IEditorPart targetEditor) { |
| 31 |
fEditor = targetEditor; |
| 32 |
} |
| 33 |
|
| 34 |
public void dispose() { |
| 35 |
// nulling out just in case |
| 36 |
fEditor = null; |
| 37 |
fCleanupProcessor = null; |
| 38 |
} |
| 39 |
|
| 40 |
public void init(IAction action) { |
| 41 |
if (action != null) { |
| 42 |
action.setText(CSSUIMessages.CleanupDocument_label); |
| 43 |
action.setToolTipText(CSSUIMessages.CleanupDocument_tooltip); |
| 44 |
action.setDescription(CSSUIMessages.CleanupDocument_description); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public void runWithEvent(IAction action, Event event) { |
| 49 |
run(action); |
| 50 |
} |
| 51 |
|
| 52 |
public void init(IViewPart view) { |
| 53 |
// do nothing |
| 54 |
} |
| 55 |
|
| 56 |
public void run(IAction action) { |
| 57 |
if (fEditor instanceof StructuredTextEditor) { |
| 58 |
final StructuredTextEditor editor = (StructuredTextEditor) fEditor; |
| 59 |
Dialog cleanupDialog = new CleanupDialogCSS(editor.getSite().getShell()); |
| 60 |
if (cleanupDialog.open() == Window.OK) { |
| 61 |
// setup runnable |
| 62 |
Runnable runnable = new Runnable() { |
| 63 |
public void run() { |
| 64 |
IStructuredCleanupProcessor cleanupProcessor = getCleanupProcessor(); |
| 65 |
if (cleanupProcessor != null) { |
| 66 |
IStructuredModel model = null; |
| 67 |
try { |
| 68 |
model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput())); |
| 69 |
if (model != null) |
| 70 |
cleanupProcessor.cleanupModel(model); |
| 71 |
} |
| 72 |
finally { |
| 73 |
if (model != null) |
| 74 |
model.releaseFromEdit(); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
// TODO: make independent of 'model'. |
| 81 |
IStructuredModel model = null; |
| 82 |
try { |
| 83 |
model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput())); |
| 84 |
if (model != null) { |
| 85 |
// begin recording |
| 86 |
ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); |
| 87 |
model.beginRecording(this, SSEUIMessages.Cleanup_Document_UI_, SSEUIMessages.Cleanup_Document_UI_, selection.getOffset(), selection.getLength()); //$NON-NLS-1$ //$NON-NLS-2$ |
| 88 |
|
| 89 |
// tell the model that we are about to make a big |
| 90 |
// model change |
| 91 |
model.aboutToChangeModel(); |
| 92 |
|
| 93 |
// run |
| 94 |
BusyIndicator.showWhile(editor.getTextViewer().getControl().getDisplay(), runnable); |
| 95 |
} |
| 96 |
} |
| 97 |
finally { |
| 98 |
if (model != null) { |
| 99 |
// tell the model that we are done with the big |
| 100 |
// model |
| 101 |
// change |
| 102 |
model.changedModel(); |
| 103 |
|
| 104 |
// end recording |
| 105 |
ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); |
| 106 |
model.endRecording(this, selection.getOffset(), selection.getLength()); |
| 107 |
model.releaseFromEdit(); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public void selectionChanged(IAction action, ISelection selection) { |
| 115 |
// do nothing |
| 116 |
} |
| 117 |
|
| 118 |
IStructuredCleanupProcessor getCleanupProcessor() { |
| 119 |
if (fCleanupProcessor == null) |
| 120 |
fCleanupProcessor = new CleanupProcessorCSS(); |
| 121 |
|
| 122 |
return fCleanupProcessor; |
| 123 |
} |
| 124 |
} |