Skip to main content
summaryrefslogtreecommitdiffstats
blob: e2c10bd2d020e24dde96309585cf3594bb8abf92 (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
/*******************************************************************************
 * Contributors:
 *     Steven Spungin <steven@spungin.tv> - Bug 431735, Bug 391089
 *******************************************************************************/

package org.eclipse.e4.tools.emf.ui.internal.common.xml;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.e4.tools.emf.ui.common.IModelResource;
import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.empty.E;
import org.eclipse.e4.ui.internal.workbench.E4XMIResource;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.IRegion;
import org.xml.sax.InputSource;

public class EMFDocumentResourceMediator {
	private IModelResource modelResource;
	private Document document;
	private boolean updateFromEMF;
	private List<Diagnostic> errorList = new ArrayList<Diagnostic>();
	private Runnable documentValidationChanged;

	public EMFDocumentResourceMediator(final IModelResource modelResource) {
		this.modelResource = modelResource;
		this.document = new Document();
		this.document.addDocumentListener(new IDocumentListener() {

			@Override
			public void documentChanged(DocumentEvent event) {
				if (updateFromEMF) {
					return;
				}

				String doc = document.get();
				E4XMIResource res = new E4XMIResource();
				try {
					res.load(new InputSource(new StringReader(doc)), null);
					modelResource.replaceRoot(res.getContents().get(0));
					errorList.clear();
					if (documentValidationChanged != null) {
						documentValidationChanged.run();
					}
				} catch (IOException e) {
					errorList = res.getErrors();
					if (documentValidationChanged != null) {
						documentValidationChanged.run();
					}

				}
			}

			@Override
			public void documentAboutToBeChanged(DocumentEvent event) {

			}
		});
		updateFromEMF();
	}

	public void setValidationChangedCallback(Runnable runnable) {
		documentValidationChanged = runnable;
	}

	public List<Diagnostic> getErrorList() {
		return Collections.unmodifiableList(errorList);
	}

	public void updateFromEMF() {
		try {
			updateFromEMF = true;
			this.document.set(toXMI((EObject) modelResource.getRoot().get(0)));
		} finally {
			updateFromEMF = false;
		}
	}

	public Document getDocument() {
		return document;
	}

	private String toXMI(EObject root) {
		E4XMIResource resource = (E4XMIResource) root.eResource();
		// resource.getContents().add(EcoreUtil.copy(root));
		StringWriter writer = new StringWriter();
		try {
			resource.save(writer, null);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return writer.toString();
	}

	/**
	 * @param object
	 * @return The region for the start tag of the EObject, or null if not
	 *         found.
	 */
	public IRegion findStartTag(EObject object) {
		if (object == null) {
			return null;
		}
		E4XMIResource root = (E4XMIResource) ((EObject) modelResource.getRoot().get(0)).eResource();
		String xmiId = root.getID(object);

		FindReplaceDocumentAdapter find = new FindReplaceDocumentAdapter(document);
		IRegion region;
		try {
			// TODO This will not work if the element has '<' or '>' in an
			// attribute value
			region = find.find(0, "<.*?" + xmiId + ".*?>", true, true, false, true); //$NON-NLS-1$ //$NON-NLS-2$
			return region;
		} catch (BadLocationException e) {
			return null;
		}
	}

	/**
	 * @param object
	 * @return The region for the start of the text, or null if not found or the
	 *         text is empty.
	 */
	public IRegion findText(String text, int startOffset) {
		if (E.isEmpty(text)) {
			return null;
		}

		FindReplaceDocumentAdapter find = new FindReplaceDocumentAdapter(document);
		IRegion region;
		try {
			region = find.find(startOffset, text, true, true, false, false);
			return region;
		} catch (BadLocationException e) {
			return null;
		}
	}

}

Back to the top