Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 936e4ec4d062a39c1abc1684c6591c513c566a33 (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
/*******************************************************************************
 * Copyright (c) 2012 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.core.ui.editor;

import java.util.List;

import org.eclipse.etrice.core.common.ui.editor.IValidatingEditor;
import org.eclipse.etrice.core.common.ui.editor.SaveOnFocusLostListener;
import org.eclipse.etrice.core.common.validation.IssueUtils;
import org.eclipse.etrice.core.ui.preferences.RoomPreferenceConstants;
import org.eclipse.help.IContextProvider;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.util.CancelIndicator;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.IResourceValidator;
import org.eclipse.xtext.validation.Issue;

import com.google.inject.Inject;

/**
 * @author Henrik Rentz-Reichert
 *
 */
public class RoomEditor extends XtextEditor implements IValidatingEditor {

	@Inject
	protected IResourceValidator resourceValidator;

	private SaveOnFocusLostListener partListener;
	
	/* (non-Javadoc)
	 * @see org.eclipse.xtext.ui.editor.XtextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);
		
		partListener = new SaveOnFocusLostListener(this, "org.eclipse.etrice.ui.common.base", RoomPreferenceConstants.SAVE_TEXT_ON_FOCUS_LOST);
		getSite().getPage().addPartListener(partListener);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.xtext.ui.editor.XtextEditor#dispose()
	 */
	@Override
	public void dispose() {
		getSite().getPage().removePartListener(partListener);
		
		super.dispose();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.etrice.core.ui.editor.IValidatingEditor#isValid()
	 */
	@Override
	public boolean isValid() {
		return getDocument().readOnly(new IUnitOfWork<Boolean, XtextResource>() {
			@Override
			public Boolean exec(XtextResource resource) throws Exception {
				List<Issue> result = resourceValidator.validate(resource, CheckMode.NORMAL_AND_FAST, new CancelIndicator() {
					public boolean isCanceled() {
						return false;
					}
				});
				if (!result.isEmpty()) {
					for (Issue issue : result) {
						if (issue.isSyntaxError()) {
							return false;
						}
						if (IssueUtils.isBlocking(issue)) {
							return false;
						}
					}
				}
				return true;
			}
		});
	}
	
	@Override
	public <T> T getAdapter(Class<T> adapter) {
		if (IContextProvider.class.isAssignableFrom(adapter)) {
			return adapter.cast(new SelectedModelHelpProvider(this));
		}
		return super.getAdapter(adapter);

	}
}

Back to the top