1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.view.e4;
|
11
|
|
12
|
import java.util.HashMap;
|
13
|
import java.util.List;
|
14
|
import java.util.Map;
|
15
|
|
16
|
import javax.inject.Inject;
|
17
|
|
18
|
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
|
19
|
import org.eclipse.e4.core.contexts.IEclipseContext;
|
20
|
import org.eclipse.e4.core.services.log.Logger;
|
21
|
import org.eclipse.jface.viewers.Viewer;
|
22
|
import org.eclipse.swt.SWT;
|
23
|
import org.eclipse.swt.SWTException;
|
24
|
import org.eclipse.swt.widgets.Composite;
|
25
|
import org.eclipse.swt.widgets.Control;
|
26
|
import org.eclipse.swt.widgets.Display;
|
27
|
import org.eclipse.ui.forms.IFormPart;
|
28
|
import org.eclipse.ui.forms.ManagedForm;
|
29
|
import org.eclipse.ui.forms.widgets.ScrolledForm;
|
30
|
|
31
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
32
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
33
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
34
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
35
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
36
|
import eu.etaxonomy.taxeditor.remoting.CdmEagerLoadingException;
|
37
|
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
|
38
|
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
|
39
|
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
|
40
|
import eu.etaxonomy.taxeditor.ui.element.RootElement;
|
41
|
import eu.etaxonomy.taxeditor.ui.section.occurrence.EmptySection;
|
42
|
import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
|
43
|
|
44
|
/**
|
45
|
*
|
46
|
* @author pplitzner
|
47
|
* @date 18.07.2017
|
48
|
*
|
49
|
*/
|
50
|
public abstract class AbstractCdmDataViewerE4 extends Viewer implements IConversationEnabled{
|
51
|
@Inject
|
52
|
private Logger logger;
|
53
|
|
54
|
protected Map<String, List<AbstractFormSection>> entityToSectionsMap = new HashMap<>();
|
55
|
|
56
|
protected ManagedForm managedForm;
|
57
|
|
58
|
protected CdmFormFactory formFactory;
|
59
|
|
60
|
protected ScrolledForm scrolledForm;
|
61
|
|
62
|
private Composite body;
|
63
|
|
64
|
protected RootElement rootElement;
|
65
|
|
66
|
@Inject
|
67
|
protected IEclipseContext context;
|
68
|
|
69
|
//TODO: create a super class for this?
|
70
|
private Object input;
|
71
|
|
72
|
protected IDirtyMarkable part;
|
73
|
|
74
|
@Inject
|
75
|
public AbstractCdmDataViewerE4() {
|
76
|
|
77
|
}
|
78
|
|
79
|
public void init(Composite parent, IDirtyMarkable part) {
|
80
|
this.part = part;
|
81
|
|
82
|
managedForm = new ManagedForm(parent){
|
83
|
|
84
|
@Override
|
85
|
public void dirtyStateChanged() {
|
86
|
markViewPartDirty();
|
87
|
}
|
88
|
};
|
89
|
|
90
|
createFormFactory();
|
91
|
|
92
|
scrolledForm = managedForm.getForm();
|
93
|
|
94
|
body = scrolledForm.getBody();
|
95
|
|
96
|
body.setLayout(LayoutConstants.LAYOUT());
|
97
|
|
98
|
rootElement = new RootElement(formFactory, body);
|
99
|
}
|
100
|
|
101
|
|
102
|
protected void createEmptySection(String message, RootElement parent) {
|
103
|
destroySections();
|
104
|
|
105
|
EmptySection emptySection = formFactory.createEmptySection(message, formFactory, parent, SWT.NONE);
|
106
|
|
107
|
addPart(emptySection);
|
108
|
}
|
109
|
|
110
|
public void showEmptyPage(String message){
|
111
|
destroySections();
|
112
|
createEmptySection(message, rootElement);
|
113
|
}
|
114
|
|
115
|
protected void markViewPartDirty(){
|
116
|
part.changed(input);
|
117
|
}
|
118
|
|
119
|
protected void createFormFactory() {
|
120
|
if(formFactory != null){
|
121
|
formFactory.dispose();
|
122
|
formFactory = null;
|
123
|
}
|
124
|
formFactory = new CdmFormFactory(Display.getCurrent(), this);
|
125
|
ContextInjectionFactory.inject(formFactory, context);
|
126
|
}
|
127
|
|
128
|
/** {@inheritDoc} */
|
129
|
@Override
|
130
|
public Control getControl() {
|
131
|
if(body!=null){
|
132
|
if(body.isDisposed()){
|
133
|
return null;
|
134
|
}
|
135
|
for(Control child : body.getChildren()){
|
136
|
return child;
|
137
|
}
|
138
|
}
|
139
|
|
140
|
return body;
|
141
|
}
|
142
|
|
143
|
/** {@inheritDoc} */
|
144
|
@Override
|
145
|
public void setInput(Object input) {
|
146
|
this.input = input;
|
147
|
if(input!=null){
|
148
|
try{
|
149
|
refresh();
|
150
|
}catch(CdmEagerLoadingException e){
|
151
|
logger.error(e);
|
152
|
}
|
153
|
}
|
154
|
}
|
155
|
|
156
|
/** {@inheritDoc} */
|
157
|
@Override
|
158
|
public Object getInput() {
|
159
|
return input;
|
160
|
}
|
161
|
|
162
|
/** {@inheritDoc} */
|
163
|
@Override
|
164
|
public void refresh() {
|
165
|
showParts();
|
166
|
|
167
|
managedForm.setInput(input);
|
168
|
|
169
|
managedForm.refresh();
|
170
|
|
171
|
managedForm.reflow(true);
|
172
|
}
|
173
|
|
174
|
public void layout(){
|
175
|
body.layout();
|
176
|
}
|
177
|
|
178
|
protected abstract void showParts();
|
179
|
|
180
|
public void destroySections() {
|
181
|
for (IFormPart formPart : managedForm.getParts()){
|
182
|
removePart((CdmSectionPart<?>) formPart);
|
183
|
}
|
184
|
|
185
|
managedForm.setInput(null);
|
186
|
|
187
|
formFactory.destroyElement(rootElement);
|
188
|
|
189
|
createFormFactory();
|
190
|
|
191
|
rootElement = new RootElement(formFactory, body);
|
192
|
|
193
|
for(Control control : body.getChildren()){
|
194
|
try{
|
195
|
if (control != null && !control.isDisposed()){
|
196
|
control.dispose();
|
197
|
}
|
198
|
}catch(SWTException e){
|
199
|
if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
|
200
|
MessagingUtils.errorDialog("Widget is disposed",
|
201
|
null,
|
202
|
MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
|
203
|
null,
|
204
|
e,
|
205
|
true);
|
206
|
|
207
|
}
|
208
|
}
|
209
|
control = null;
|
210
|
}
|
211
|
}
|
212
|
|
213
|
public void setFocus(){
|
214
|
// we have to set focus to a control of this viewer
|
215
|
// otherwise, after opening a dialog from the details, the focus will not be
|
216
|
// given back to the details view but to the editor
|
217
|
for(Control child : body.getChildren()){
|
218
|
child.setFocus();
|
219
|
break;
|
220
|
}
|
221
|
}
|
222
|
|
223
|
public void reflow(){
|
224
|
managedForm.reflow(true);
|
225
|
}
|
226
|
|
227
|
protected void removePart(CdmSectionPart<?> sectionPart){
|
228
|
managedForm.removePart(sectionPart);
|
229
|
formFactory.removePropertyChangeListener(sectionPart);
|
230
|
}
|
231
|
|
232
|
protected void addPart(AbstractFormSection<?> section){
|
233
|
CdmSectionPart<?> sectionPart = new CdmSectionPart<>(section);
|
234
|
managedForm.addPart(sectionPart);
|
235
|
formFactory.addPropertyChangeListener(sectionPart);
|
236
|
}
|
237
|
|
238
|
@Override
|
239
|
public ConversationHolder getConversationHolder() {
|
240
|
return null;
|
241
|
}
|
242
|
|
243
|
}
|