Merge branch 'develop' into detailsE4
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / e4 / details / 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.details;
11
12 import org.eclipse.jface.viewers.StructuredSelection;
13 import org.eclipse.jface.viewers.Viewer;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.ui.forms.IFormPart;
18 import org.eclipse.ui.forms.ManagedForm;
19 import org.eclipse.ui.forms.widgets.ScrolledForm;
20
21 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
22 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
23 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
24 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
25 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
26 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
27 import eu.etaxonomy.taxeditor.ui.element.RootElement;
28 import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
29
30 /**
31 *
32 * @author pplitzner
33 * @date 18.07.2017
34 *
35 */
36 public abstract class AbstractCdmDataViewerE4 extends Viewer implements IConversationEnabled{
37
38 protected ManagedForm managedForm;
39
40 protected CdmFormFactory formFactory;
41
42 protected ScrolledForm scrolledForm;
43
44 private final Composite body;
45
46 protected RootElement rootElement;
47
48 //TODO: create a super class for this?
49 private Object input;
50
51 private final IDirtyMarkable part;
52
53
54 public AbstractCdmDataViewerE4(Composite parent, IDirtyMarkable part) {
55 this.part = part;
56
57 managedForm = new ManagedForm(parent){
58
59 @Override
60 public void dirtyStateChanged() {
61 markViewPartDirty();
62 }
63 };
64
65 createFormFactory();
66
67 scrolledForm = managedForm.getForm();
68
69 body = scrolledForm.getBody();
70
71 body.setLayout(LayoutConstants.LAYOUT());
72
73 rootElement = new RootElement(formFactory, body);
74 }
75
76 protected void markViewPartDirty(){
77 part.changed(input);
78 }
79
80 protected void createFormFactory() {
81 if(formFactory != null){
82 formFactory.dispose();
83 formFactory = null;
84 }
85 formFactory = new CdmFormFactory(Display.getCurrent(), this);
86 }
87
88 /** {@inheritDoc} */
89 @Override
90 public Control getControl() {
91 if(body.isDisposed()){
92 return null;
93 }
94 for(Control child : body.getChildren()){
95 return child;
96 }
97
98 return body;
99 }
100
101 /** {@inheritDoc} */
102 @Override
103 public void setInput(Object input) {
104 this.input = input;
105 // reset selection
106 setSelection(new StructuredSelection(input));
107 refresh();
108 }
109
110 /** {@inheritDoc} */
111 @Override
112 public Object getInput() {
113 return input;
114 }
115
116 /** {@inheritDoc} */
117 @Override
118 public void refresh() {
119 showParts();
120
121 managedForm.setInput(input);
122
123 managedForm.refresh();
124
125 managedForm.reflow(true);
126 }
127
128 public void layout(){
129 body.layout();
130 }
131
132 protected abstract void showParts();
133
134 protected void destroySections() {
135 for (IFormPart formPart : managedForm.getParts()){
136 removePart((CdmSectionPart<?>) formPart);
137 }
138
139 managedForm.setInput(null);
140
141 formFactory.destroyElement(rootElement);
142
143 createFormFactory();
144
145 rootElement = new RootElement(formFactory, body);
146
147 for(Control control : body.getChildren()){
148 control.dispose();
149 control = null;
150 }
151 }
152
153 public void setFocus(){
154 // we have to set focus to a control of this viewer
155 // otherwise, after opening a dialog from the details, the focus will not be
156 // given back to the details view but to the editor
157 for(Control child : body.getChildren()){
158 child.setFocus();
159 break;
160 }
161 }
162
163 public void reflow(){
164 managedForm.reflow(true);
165 }
166
167 protected void removePart(CdmSectionPart<?> sectionPart){
168 managedForm.removePart(sectionPart);
169 formFactory.removePropertyChangeListener(sectionPart);
170 }
171
172 protected void addPart(AbstractFormSection<?> section){
173 CdmSectionPart<?> sectionPart = new CdmSectionPart<>(section);
174 managedForm.addPart(sectionPart);
175 formFactory.addPropertyChangeListener(sectionPart);
176 }
177
178 @Override
179 public ConversationHolder getConversationHolder() {
180 return null;
181 }
182
183 }