/
FormDetailComposite.java
127 lines (99 loc) · 3.39 KB
/
FormDetailComposite.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
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
/*******************************************************************************
* Copyright (c) 2013 RCP Vision (http://www.rcp-vision.com) 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:
* Lorenzo Bettini - initial API and implementation
*******************************************************************************/
package org.eclipse.emf.parsley.composite;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import com.google.inject.Inject;
public class FormDetailComposite extends AbstractDetailComposite {
private FormControlFactory formControlFactory;
@Inject
private ILabelProvider labelProvider;
private final Composite main;
private FormToolkit toolkit;
private final ScrolledForm scrolledForm;
private FormTitleAdapter headerAdapter;
public FormDetailComposite(Composite parent, int style) {
super(parent, style);
toolkit = new FormToolkit(parent.getDisplay());
toolkit.adapt(this);
setLayout(new GridLayout(1, false));
scrolledForm = toolkit.createScrolledForm(this);
// make sure that the form takes all the space
scrolledForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
scrolledForm.getBody().setLayout(new GridLayout(2, false));
main = scrolledForm.getBody();
setBackgroundMode(SWT.INHERIT_FORCE);
}
@Override
public void dispose() {
if(headerAdapter!=null){
headerAdapter.dispose();
}
super.dispose();
toolkit.dispose();
}
@Inject
public void setFormControlFactory(FormControlFactory formControlFactory) {
this.formControlFactory = formControlFactory;
}
@Override
protected void initControlFactory(EditingDomain domain,
EObject model) {
updateTitle(model);
headerAdapter = new FormTitleAdapter(model);
model.eAdapters().add(headerAdapter);
formControlFactory.init(domain, model, main, toolkit);
}
protected void updateTitle(EObject model) {
scrolledForm.setText(labelProvider.getText(model));
scrolledForm.setImage(labelProvider.getImage(model));
}
protected ScrolledForm getScrolledForm() {
return scrolledForm;
}
@Override
protected void createControlForFeature(EClass eClass, EStructuralFeature feature) {
formControlFactory.createEditingField(feature);
}
private class FormTitleAdapter extends AdapterImpl {
private EObject model;
private boolean disposing = false;
public FormTitleAdapter(EObject model) {
this.model = model;
}
public void dispose() {
disposing = true;
model.eAdapters().remove(headerAdapter);
}
@Override
public void notifyChanged(Notification msg) {
if (!disposing) {
getDisplay().syncExec(new Runnable() {
@Override
public void run() {
updateTitle(model);
}
});
}
}
}
}