adapt details view to pro parte synonyms
[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(RootElement parent) {
97 destroySections();
98
99 EmptySection emptySection = formFactory.createEmptySection(formFactory, parent, SWT.NONE);
100
101 addPart(emptySection);
102 }
103
104 public void showEmptyPage(){
105 destroySections();
106 createEmptySection(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 managedForm.setInput(input);
162
163 managedForm.refresh();
164
165 managedForm.reflow(true);
166 }
167
168 public void layout(){
169 body.layout();
170 }
171
172 protected abstract void showParts();
173
174 public void destroySections() {
175 for (IFormPart formPart : managedForm.getParts()){
176 removePart((CdmSectionPart<?>) formPart);
177 }
178
179 managedForm.setInput(null);
180
181 formFactory.destroyElement(rootElement);
182
183 createFormFactory();
184
185 rootElement = new RootElement(formFactory, body);
186
187 for(Control control : body.getChildren()){
188 try{
189 if (control != null && !control.isDisposed()){
190 control.dispose();
191 }
192 }catch(SWTException e){
193 if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
194 MessagingUtils.errorDialog("Widget is disposed",
195 null,
196 MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
197 null,
198 e,
199 true);
200
201 }
202 }
203 control = null;
204 }
205 }
206
207 public void setFocus(){
208 // we have to set focus to a control of this viewer
209 // otherwise, after opening a dialog from the details, the focus will not be
210 // given back to the details view but to the editor
211 for(Control child : body.getChildren()){
212 child.setFocus();
213 break;
214 }
215 }
216
217 public void reflow(){
218 managedForm.reflow(true);
219 }
220
221 protected void removePart(CdmSectionPart<?> sectionPart){
222 managedForm.removePart(sectionPart);
223 formFactory.removePropertyChangeListener(sectionPart);
224 }
225
226 protected void addPart(AbstractFormSection<?> section){
227 CdmSectionPart<?> sectionPart = new CdmSectionPart<>(section);
228 managedForm.addPart(sectionPart);
229 formFactory.addPropertyChangeListener(sectionPart);
230 }
231
232 @Override
233 public ConversationHolder getConversationHolder() {
234 return null;
235 }
236
237 }