57cb798855860ffd4ba5adf4329fbd317353866d
[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.cdm.persistence.hibernate.CdmDataChangeMap;
25 import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
26 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
28 import eu.etaxonomy.taxeditor.ui.element.RootElement;
29 import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
30
31 /**
32 * <p>Abstract AbstractCdmDataViewer class.</p>
33 *
34 * @author n.hoffmann
35 * @created Feb 9, 2010
36 * @version 1.0
37 */
38 public abstract class AbstractCdmDataViewer extends Viewer implements IConversationEnabled{
39
40 protected ManagedForm managedForm;
41
42 protected CdmFormFactory formFactory;
43
44 protected ScrolledForm scrolledForm;
45
46 private final Composite body;
47
48 protected RootElement rootElement;
49
50 //TODO: create a super class for this?
51 private Object input;
52
53 private final AbstractCdmViewPart viewPart;
54
55
56 /**
57 * <p>Constructor for AbstractCdmDataViewer.</p>
58 *
59 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
60 * @param viewPart a {@link eu.etaxonomy.taxeditor.view.AbstractCdmViewPart} object.
61 */
62 public AbstractCdmDataViewer(Composite parent, AbstractCdmViewPart viewPart) {
63 this.viewPart = viewPart;
64
65 managedForm = new ManagedForm(parent){
66
67 /* (non-Javadoc)
68 * @see org.eclipse.ui.forms.ManagedForm#dirtyStateChanged()
69 */
70 @Override
71 public void dirtyStateChanged() {
72 markViewPartDirty();
73 }
74 };
75
76 createFormFactory();
77
78 scrolledForm = managedForm.getForm();
79
80 body = scrolledForm.getBody();
81
82 body.setLayout(LayoutConstants.LAYOUT());
83
84 rootElement = new RootElement(formFactory, body);
85 }
86
87 /**
88 * <p>createFormFactory</p>
89 */
90 protected void createFormFactory() {
91 if(formFactory != null){
92 formFactory.dispose();
93 formFactory = null;
94 }
95 formFactory = new CdmFormFactory(Display.getCurrent(), this);
96 }
97
98 protected void markViewPartDirty(){
99 viewPart.changed(input);
100 }
101
102
103 /**
104 * @return
105 */
106 protected AbstractCdmViewPart getViewPart() {
107 return viewPart;
108 }
109
110 /* (non-Javadoc)
111 * @see org.eclipse.jface.viewers.Viewer#getControl()
112 */
113 /** {@inheritDoc} */
114 @Override
115 public Control getControl() {
116 for(Control child : body.getChildren()){
117 return child;
118 }
119
120 return body;
121 }
122
123 /* (non-Javadoc)
124 * @see org.eclipse.jface.viewers.Viewer#setInput(java.lang.Object)
125 */
126 /** {@inheritDoc} */
127 @Override
128 public void setInput(Object input) {
129 this.input = input;
130 // reset selection
131 setSelection(new StructuredSelection(input));
132 refresh();
133 }
134
135 /* (non-Javadoc)
136 * @see org.eclipse.jface.viewers.Viewer#getInput()
137 */
138 /** {@inheritDoc} */
139 @Override
140 public Object getInput() {
141 return input;
142 }
143
144 /* (non-Javadoc)
145 * @see org.eclipse.jface.viewers.Viewer#refresh()
146 */
147 /** {@inheritDoc} */
148 @Override
149 public void refresh() {
150 showParts();
151
152 managedForm.setInput(input);
153
154 managedForm.refresh();
155
156 managedForm.reflow(true);
157 }
158
159
160 /**
161 * <p>layout</p>
162 */
163 public void layout(){
164 body.layout();
165 }
166
167 /**
168 * <p>showParts</p>
169 */
170 protected abstract void showParts();
171
172
173 /**
174 * <p>destroySections</p>
175 */
176 protected void destroySections() {
177 for (IFormPart formPart : managedForm.getParts()){
178 removePart((CdmSectionPart) formPart);
179 }
180
181 managedForm.setInput(null);
182
183 formFactory.destroyElement(rootElement);
184
185 createFormFactory();
186
187 rootElement = new RootElement(formFactory, body);
188
189 for(Control control : body.getChildren()){
190 control.dispose();
191 control = null;
192 }
193 }
194
195
196 /**
197 * <p>setFocus</p>
198 */
199 public void setFocus(){
200 // we have to set focus to a control of this viewer
201 // otherwise, after opening a dialog from the details, the focus will not be
202 // given back to the details view but to the editor
203 for(Control child : body.getChildren()){
204 child.setFocus();
205 break;
206 }
207 }
208
209 /**
210 * <p>reflow</p>
211 */
212 public void reflow(){
213 managedForm.reflow(true);
214 }
215
216 /**
217 * <p>removePart</p>
218 *
219 * @param sectionPart a {@link eu.etaxonomy.taxeditor.view.detail.CdmSectionPart} object.
220 */
221 protected void removePart(CdmSectionPart sectionPart){
222 managedForm.removePart(sectionPart);
223 formFactory.removePropertyChangeListener(sectionPart);
224 }
225
226 /**
227 * <p>addPart</p>
228 *
229 * @param section a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection} object.
230 */
231 protected void addPart(AbstractFormSection section){
232 CdmSectionPart sectionPart = new CdmSectionPart(section);
233 managedForm.addPart(sectionPart);
234 formFactory.addPropertyChangeListener(sectionPart);
235 }
236
237 /**
238 * <p>getConversationHolder</p>
239 *
240 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
241 */
242 @Override
243 public ConversationHolder getConversationHolder() {
244 return viewPart.getConversationHolder();
245 }
246
247 /** {@inheritDoc} */
248 @Override
249 public void update(CdmDataChangeMap changeEvents) {}
250
251 }