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