Skip to main content
summaryrefslogtreecommitdiffstats
blob: b1a504fa460d9583d7a3da4219a2f99b0d3ab459 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.commands;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.gef.commands.Command;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jst.jsf.common.ui.internal.logging.Logger;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.utils.StructuredModelUtil;
import org.eclipse.wst.html.core.internal.format.HTMLFormatProcessorImpl;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
import org.eclipse.wst.sse.ui.internal.provisional.extensions.ISourceEditingTextTools;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.eclipse.wst.xml.ui.internal.provisional.IDOMSourceEditingTextTools;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
 * @author mengbo
 */
public abstract class SourceViewerCommand extends Command {
	/**
	 * the structured text editor containing the viewer
	 */
	protected final StructuredTextEditor _editor;

	private Document _document;

	private Logger _log = PDPlugin.getLogger(SourceViewerCommand.class);

	/**
	 * @param label 
	 * @param editor 
	 */
	public SourceViewerCommand(String label, StructuredTextEditor editor) {
		super();
		_editor = editor;
		IDOMSourceEditingTextTools tools = getSourceEditingTextTools();
		_document = tools.getDOMDocument();
	}

	/**
	 * @return the text tools
	 */
	protected IDOMSourceEditingTextTools getSourceEditingTextTools() {
		IDOMSourceEditingTextTools tools = (IDOMSourceEditingTextTools) _editor
				.getAdapter(ISourceEditingTextTools.class);
		return tools;
	}

	/**
	 * preExecute and postExecute is a pair. () SHOULD NOT throw any exception,
	 * if it throw any exception, it should catch itself and return false to
	 * indicate not continue.
	 * @return true if preExec succeeded
	 */
	protected final boolean preExecute() {
		int position = 0;
		int length = 0;
		ISelection selection = _editor.getTextViewer().getSelection();
		if (selection instanceof TextSelection) {
			position = ((TextSelection) selection).getOffset();
			length = ((TextSelection) selection).getLength();
		}
		getModel().beginRecording(this, getLabel(), position, length);
		getModel().aboutToChangeModel();
		return true;
	}

	/**
	 * if preExecute() return true, then this method will always be called even
	 * preExecute()/doExecute() and postExecute() fail.
	 */
	protected final void postExecute() {
		getModel().changedModel();
		getModel().endRecording(this);
		setSelection();
	}

	/**
	 * format the specified node in source code. Utility method that can be
	 * called by child classes
	 * 
	 * @param node
	 */
	public final void formatNode(Node node) {
		new HTMLFormatProcessorImpl().formatNode(node);
	}

	/**
	 * Notifies the team framework of an edit to the model's underlying file.
	 */
	public void notifyTeamFrameworkOfEdit() {
		IFile file = StructuredModelUtil.getFileFor(getModel());
		if (file != null) {
			IWorkspace workspace = file.getWorkspace();
			if (workspace != null) {
				IStatus status = workspace.validateEdit(new IFile[]{file}, null);
				if (!status.isOK()) {
					_log.info(status.getMessage());
				}
			}
		}
	}

	/**
	 * @return the dom model
	 */
	protected IDOMModel getModel() {
		Assert.isTrue(_document != null && _document instanceof IDOMNode);
		return ((IDOMNode) _document).getModel();
	}

	public final void execute() {
		boolean ok = preExecute();
		if (ok) {
			try {
				doExecute();
			} catch (Exception ex) {
				// "Error in command execution"
				_log.error("Error.SourceViewerCommand.Execution", ex); //$NON-NLS-1$
			} finally {
				postExecute();
			}
		}
	}

	/**
	 * execute
	 */
	public abstract void doExecute();

	/**
	 * set the selection
	 */
	public abstract void setSelection();
}

Back to the top