Project

General

Profile

Download (4.51 KB) Statistics
| Branch: | Tag: | Revision:
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 org.eclipse.jface.viewers.StructuredSelection;
13
import org.eclipse.jface.viewers.Viewer;
14
import org.eclipse.swt.SWT;
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.model.IDirtyMarkable;
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.ui.section.occurrence.EmptySection;
30
import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
31

    
32
/**
33
 *
34
 * @author pplitzner
35
 * @date 18.07.2017
36
 *
37
 */
38
public abstract class AbstractCdmDataViewerE4 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
	protected final IDirtyMarkable part;
54

    
55

    
56
	public AbstractCdmDataViewerE4(Composite parent, IDirtyMarkable part) {
57
	    this.part = part;
58

    
59
		managedForm = new ManagedForm(parent){
60

    
61
			@Override
62
			public void dirtyStateChanged() {
63
			    markViewPartDirty();
64
			}
65
		};
66

    
67
		createFormFactory();
68

    
69
		scrolledForm = managedForm.getForm();
70

    
71
		body = scrolledForm.getBody();
72

    
73
		body.setLayout(LayoutConstants.LAYOUT());
74

    
75
		rootElement = new RootElement(formFactory, body);
76
	}
77

    
78

    
79
    protected void createEmptySection(RootElement parent) {
80
        destroySections();
81

    
82
        EmptySection emptySection = formFactory.createEmptySection(formFactory, parent, SWT.NONE);
83

    
84
        addPart(emptySection);
85
    }
86

    
87
    public void showEmptyPage(){
88
        destroySections();
89
        createEmptySection(rootElement);
90
    }
91

    
92
    protected void markViewPartDirty(){
93
        part.changed(input);
94
    }
95

    
96
	protected void createFormFactory() {
97
		if(formFactory != null){
98
			formFactory.dispose();
99
			formFactory = null;
100
		}
101
		formFactory = new CdmFormFactory(Display.getCurrent(), this);
102
	}
103

    
104
	/** {@inheritDoc} */
105
	@Override
106
	public Control getControl() {
107
		if(body.isDisposed()){
108
			return null;
109
		}
110
		for(Control child : body.getChildren()){
111
			return child;
112
		}
113

    
114
		return body;
115
	}
116

    
117
	/** {@inheritDoc} */
118
	@Override
119
	public void setInput(Object input) {
120
		this.input = input;
121
		if(input!=null){
122
		    // reset selection
123
		    setSelection(new StructuredSelection(input));
124
		    refresh();
125
		}
126
	}
127

    
128
	/** {@inheritDoc} */
129
	@Override
130
	public Object getInput() {
131
		return input;
132
	}
133

    
134
	/** {@inheritDoc} */
135
	@Override
136
	public void refresh() {
137
		showParts();
138

    
139
		managedForm.setInput(input);
140

    
141
		managedForm.refresh();
142

    
143
		managedForm.reflow(true);
144
	}
145

    
146
	public void layout(){
147
		body.layout();
148
	}
149

    
150
	protected abstract void showParts();
151

    
152
	protected void destroySections() {
153
		for (IFormPart formPart : managedForm.getParts()){
154
			removePart((CdmSectionPart<?>) formPart);
155
		}
156

    
157
		managedForm.setInput(null);
158

    
159
		formFactory.destroyElement(rootElement);
160

    
161
		createFormFactory();
162

    
163
		rootElement = new RootElement(formFactory, body);
164

    
165
		for(Control control : body.getChildren()){
166
			control.dispose();
167
			control = null;
168
		}
169
	}
170

    
171
	public void setFocus(){
172
		// we have to set focus to a control of this viewer
173
		// otherwise, after opening a dialog from the details, the focus will not be
174
		// given back to the details view but to the editor
175
		for(Control child : body.getChildren()){
176
			child.setFocus();
177
			break;
178
		}
179
	}
180

    
181
	public void reflow(){
182
		managedForm.reflow(true);
183
	}
184

    
185
	protected void removePart(CdmSectionPart<?> sectionPart){
186
		managedForm.removePart(sectionPart);
187
		formFactory.removePropertyChangeListener(sectionPart);
188
	}
189

    
190
	protected void addPart(AbstractFormSection<?> section){
191
		CdmSectionPart<?> sectionPart = new CdmSectionPart<>(section);
192
		managedForm.addPart(sectionPart);
193
		formFactory.addPropertyChangeListener(sectionPart);
194
	}
195

    
196
    @Override
197
    public ConversationHolder getConversationHolder() {
198
        return null;
199
	}
200

    
201
}
(1-1/2)