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.ExpandableComposite;
|
30
|
import org.eclipse.ui.forms.widgets.ScrolledForm;
|
31
|
|
32
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
33
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
34
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
35
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
36
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
37
|
import eu.etaxonomy.taxeditor.remoting.CdmEagerLoadingException;
|
38
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
39
|
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
|
40
|
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
|
41
|
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
|
42
|
import eu.etaxonomy.taxeditor.ui.element.RootElement;
|
43
|
import eu.etaxonomy.taxeditor.ui.section.occurrence.EmptySection;
|
44
|
import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
|
45
|
|
46
|
/**
|
47
|
*
|
48
|
* @author pplitzner
|
49
|
* @date 18.07.2017
|
50
|
*
|
51
|
*/
|
52
|
public abstract class AbstractCdmDataViewerE4 extends Viewer implements IConversationEnabled{
|
53
|
@Inject
|
54
|
private Logger logger;
|
55
|
|
56
|
protected Map<String, List<AbstractFormSection>> entityToSectionsMap = new HashMap<>();
|
57
|
|
58
|
protected ManagedForm managedForm;
|
59
|
|
60
|
protected CdmFormFactory formFactory;
|
61
|
|
62
|
protected ScrolledForm scrolledForm;
|
63
|
|
64
|
private Composite body;
|
65
|
|
66
|
protected RootElement rootElement;
|
67
|
|
68
|
@Inject
|
69
|
protected IEclipseContext context;
|
70
|
|
71
|
//TODO: create a super class for this?
|
72
|
private Object input;
|
73
|
|
74
|
protected IDirtyMarkable part;
|
75
|
|
76
|
@Inject
|
77
|
public AbstractCdmDataViewerE4() {
|
78
|
|
79
|
}
|
80
|
|
81
|
public void init(Composite parent, IDirtyMarkable part) {
|
82
|
this.part = part;
|
83
|
|
84
|
managedForm = new ManagedForm(parent){
|
85
|
|
86
|
@Override
|
87
|
public void dirtyStateChanged() {
|
88
|
markViewPartDirty();
|
89
|
}
|
90
|
};
|
91
|
|
92
|
createFormFactory();
|
93
|
|
94
|
scrolledForm = managedForm.getForm();
|
95
|
|
96
|
body = scrolledForm.getBody();
|
97
|
|
98
|
body.setLayout(LayoutConstants.LAYOUT());
|
99
|
|
100
|
rootElement = new RootElement(formFactory, body);
|
101
|
}
|
102
|
|
103
|
|
104
|
protected boolean isCached(String id){
|
105
|
List<AbstractFormSection> sections = entityToSectionsMap.get(id);
|
106
|
return sections!=null;
|
107
|
}
|
108
|
|
109
|
protected void createEmptySection(String message, RootElement parent) {
|
110
|
destroySections();
|
111
|
|
112
|
EmptySection emptySection = formFactory.createEmptySection(message, formFactory, parent, SWT.NONE);
|
113
|
|
114
|
addPart(emptySection);
|
115
|
}
|
116
|
|
117
|
public void showEmptyPage(String message){
|
118
|
destroySections();
|
119
|
createEmptySection(message, rootElement);
|
120
|
}
|
121
|
|
122
|
protected void markViewPartDirty(){
|
123
|
part.changed(input);
|
124
|
}
|
125
|
|
126
|
protected void createFormFactory() {
|
127
|
if(formFactory != null){
|
128
|
formFactory.dispose();
|
129
|
formFactory = null;
|
130
|
}
|
131
|
formFactory = new CdmFormFactory(Display.getCurrent(), this);
|
132
|
ContextInjectionFactory.inject(formFactory, context);
|
133
|
}
|
134
|
|
135
|
/** {@inheritDoc} */
|
136
|
@Override
|
137
|
public Control getControl() {
|
138
|
if(body!=null){
|
139
|
if(body.isDisposed()){
|
140
|
return null;
|
141
|
}
|
142
|
for(Control child : body.getChildren()){
|
143
|
return child;
|
144
|
}
|
145
|
}
|
146
|
|
147
|
return body;
|
148
|
}
|
149
|
|
150
|
/** {@inheritDoc} */
|
151
|
@Override
|
152
|
public void setInput(Object input) {
|
153
|
this.input = input;
|
154
|
if(input!=null){
|
155
|
try{
|
156
|
refresh();
|
157
|
}catch(CdmEagerLoadingException e){
|
158
|
logger.error(e);
|
159
|
}
|
160
|
}
|
161
|
}
|
162
|
|
163
|
/** {@inheritDoc} */
|
164
|
@Override
|
165
|
public Object getInput() {
|
166
|
return input;
|
167
|
}
|
168
|
|
169
|
/** {@inheritDoc} */
|
170
|
@Override
|
171
|
public void refresh() {
|
172
|
showParts();
|
173
|
|
174
|
managedForm.setInput(input);
|
175
|
|
176
|
managedForm.refresh();
|
177
|
|
178
|
managedForm.reflow(true);
|
179
|
}
|
180
|
|
181
|
public void layout(){
|
182
|
body.layout();
|
183
|
}
|
184
|
|
185
|
protected int getSectionStyle(Class<? extends AbstractFormSection> clazz){
|
186
|
int style = ExpandableComposite.TWISTIE;
|
187
|
if(PreferencesUtil.getPreferenceStore().getBoolean(StoreUtil.getPrefKey(clazz, getInput()))){
|
188
|
style |= ExpandableComposite.EXPANDED;
|
189
|
}
|
190
|
return style;
|
191
|
}
|
192
|
|
193
|
protected abstract void showParts();
|
194
|
|
195
|
public void destroySections() {
|
196
|
entityToSectionsMap.clear();
|
197
|
|
198
|
for (IFormPart formPart : managedForm.getParts()){
|
199
|
removePart((CdmSectionPart<?>) formPart);
|
200
|
}
|
201
|
|
202
|
managedForm.setInput(null);
|
203
|
|
204
|
formFactory.destroyElement(rootElement);
|
205
|
|
206
|
createFormFactory();
|
207
|
|
208
|
rootElement = new RootElement(formFactory, body);
|
209
|
|
210
|
for(Control control : body.getChildren()){
|
211
|
try{
|
212
|
if (control != null && !control.isDisposed()){
|
213
|
control.dispose();
|
214
|
}
|
215
|
}catch(SWTException e){
|
216
|
if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
|
217
|
MessagingUtils.errorDialog("Widget is disposed",
|
218
|
null,
|
219
|
MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
|
220
|
null,
|
221
|
e,
|
222
|
true);
|
223
|
|
224
|
}
|
225
|
}
|
226
|
control = null;
|
227
|
}
|
228
|
}
|
229
|
|
230
|
public void setFocus(){
|
231
|
// we have to set focus to a control of this viewer
|
232
|
// otherwise, after opening a dialog from the details, the focus will not be
|
233
|
// given back to the details view but to the editor
|
234
|
for(Control child : body.getChildren()){
|
235
|
child.setFocus();
|
236
|
break;
|
237
|
}
|
238
|
}
|
239
|
|
240
|
public void reflow(){
|
241
|
managedForm.reflow(true);
|
242
|
}
|
243
|
|
244
|
protected void removePart(CdmSectionPart<?> sectionPart){
|
245
|
managedForm.removePart(sectionPart);
|
246
|
formFactory.removePropertyChangeListener(sectionPart);
|
247
|
}
|
248
|
|
249
|
protected void addPart(AbstractFormSection<?> section){
|
250
|
CdmSectionPart<?> sectionPart = new CdmSectionPart<>(section);
|
251
|
managedForm.addPart(sectionPart);
|
252
|
formFactory.addPropertyChangeListener(sectionPart);
|
253
|
}
|
254
|
|
255
|
@Override
|
256
|
public ConversationHolder getConversationHolder() {
|
257
|
return null;
|
258
|
}
|
259
|
|
260
|
}
|