This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
/
AbstractComponentPrefPage2.java
90 lines (74 loc) · 2.84 KB
/
AbstractComponentPrefPage2.java
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
/*******************************************************************************
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* 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:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.ui.preferences.common;
import static melnorme.utilbox.core.Assert.AssertNamespace.assertFail;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Composite;
import melnorme.lang.tooling.data.IStatusMessage;
import melnorme.lang.tooling.data.IValidationSource;
import melnorme.lang.tooling.data.StatusLevel;
import melnorme.utilbox.collections.ArrayList2;
import melnorme.utilbox.fields.IDomainField;
public abstract class AbstractComponentPrefPage2 extends AbstractLangPreferencesPage {
private final ArrayList2<IValidationSource> validators = new ArrayList2<>();
public AbstractComponentPrefPage2() {
super(null);
}
public AbstractComponentPrefPage2(IPreferenceStore store) {
super(store);
}
@Override
public final void createControl(Composite parent) {
super.createControl(parent);
updateStatusMessage();
}
public void addValidationSource(IValidationSource validationSource) {
validators.add(validationSource);
}
public void addValidationStatusField(IDomainField<IStatusMessage> statusField) {
IValidationSource validationSource;
if(statusField instanceof IValidationSource) {
validationSource = (IValidationSource) statusField;
} else {
validationSource = new IValidationSource() {
@Override
public IStatusMessage getValidationStatus() {
return statusField.getFieldValue();
}
};
}
addValidationSource(validationSource);
statusField.addListener(() -> updateStatusMessage());
}
protected void updateStatusMessage() {
if(!isControlCreated()) {
return;
}
IStatusMessage status = IValidationSource.getHighestStatus(validators);
if(status == null) {
setMessage(null);
setValid(true);
} else {
setMessage(status.getMessage(), statusLevelToMessageType(status.getStatusLevel()));
setValid(status.getStatusLevel() != StatusLevel.ERROR);
}
}
public static int statusLevelToMessageType(StatusLevel statusLevel) {
switch (statusLevel) {
case OK: return IMessageProvider.NONE;
case INFO: return IMessageProvider.INFORMATION;
case WARNING: return IMessageProvider.WARNING;
case ERROR: return IMessageProvider.ERROR;
}
throw assertFail();
}
}