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