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