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