Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d015667134232bed03ceabd690d96ab91df5f1c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.sse.ui.internal.actions;

import java.util.ResourceBundle;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.TextEditorAction;
import org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupProcessor;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
import org.eclipse.wst.sse.ui.internal.SSEUIMessages;

public abstract class CleanupAction extends TextEditorAction {
	protected Dialog fCleanupDialog;

	public CleanupAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
		super(bundle, prefix, editor);
	}

	protected abstract Dialog getCleanupDialog(Shell shell);

	protected abstract IStructuredCleanupProcessor getCleanupProcessor();

	public void run() {
		if (getTextEditor() instanceof StructuredTextEditor) {
			final StructuredTextEditor editor = (StructuredTextEditor) getTextEditor();
			Dialog cleanupDialog = getCleanupDialog(editor.getSite().getShell());
			if (cleanupDialog != null) {
				if (cleanupDialog.open() == Window.OK) {
					// setup runnable
					Runnable runnable = new Runnable() {
						public void run() {
							IStructuredCleanupProcessor cleanupProcessor = getCleanupProcessor();
							if (cleanupProcessor != null)
								cleanupProcessor.cleanupModel(editor.getModel());
						}
					};

					// TODO: make independent of 'model'.
					IStructuredModel model = editor.getModel();
					if (model != null) {
						try {
							// begin recording
							ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
							model.beginRecording(this, SSEUIMessages.Cleanup_Document_UI_, SSEUIMessages.Cleanup_Document_UI_, selection.getOffset(), selection.getLength()); //$NON-NLS-1$ //$NON-NLS-2$

							// tell the model that we are about to make a big
							// model change
							model.aboutToChangeModel();

							// run
							BusyIndicator.showWhile(editor.getTextViewer().getControl().getDisplay(), runnable);
						} finally {
							// 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());
						}
					}
				}

			}
		}
	}
}

Back to the top