Project

General

Profile

Download (4.65 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 javax.inject.Inject;
13

    
14
import org.eclipse.e4.core.contexts.IEclipseContext;
15
import org.eclipse.jface.viewers.StructuredSelection;
16
import org.eclipse.jface.viewers.Viewer;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.widgets.Composite;
19
import org.eclipse.swt.widgets.Control;
20
import org.eclipse.swt.widgets.Display;
21
import org.eclipse.ui.forms.IFormPart;
22
import org.eclipse.ui.forms.ManagedForm;
23
import org.eclipse.ui.forms.widgets.ScrolledForm;
24

    
25
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
26
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
27
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
28
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
29
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
30
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
31
import eu.etaxonomy.taxeditor.ui.element.RootElement;
32
import eu.etaxonomy.taxeditor.ui.section.occurrence.EmptySection;
33
import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
34

    
35
/**
36
 *
37
 * @author pplitzner
38
 * @date 18.07.2017
39
 *
40
 */
41
public abstract class AbstractCdmDataViewerE4 extends Viewer implements IConversationEnabled{
42

    
43
	protected ManagedForm managedForm;
44

    
45
	protected CdmFormFactory formFactory;
46

    
47
	protected ScrolledForm scrolledForm;
48

    
49
	private Composite body;
50

    
51
	protected RootElement rootElement;
52

    
53
	@Inject
54
	protected IEclipseContext context;
55

    
56
	//TODO: create a super class for this?
57
	private Object input;
58

    
59
	protected IDirtyMarkable part;
60

    
61
	@Inject
62
	public AbstractCdmDataViewerE4() {
63

    
64
	}
65

    
66
	public void init(Composite parent, IDirtyMarkable part) {
67
	    this.part = part;
68

    
69
		managedForm = new ManagedForm(parent){
70

    
71
			@Override
72
			public void dirtyStateChanged() {
73
			    markViewPartDirty();
74
			}
75
		};
76

    
77
		createFormFactory();
78

    
79
		scrolledForm = managedForm.getForm();
80

    
81
		body = scrolledForm.getBody();
82

    
83
		body.setLayout(LayoutConstants.LAYOUT());
84

    
85
		rootElement = new RootElement(formFactory, body);
86
	}
87

    
88

    
89
    protected void createEmptySection(RootElement parent) {
90
        destroySections();
91

    
92
        EmptySection emptySection = formFactory.createEmptySection(formFactory, parent, SWT.NONE);
93

    
94
        addPart(emptySection);
95
    }
96

    
97
    public void showEmptyPage(){
98
        destroySections();
99
        createEmptySection(rootElement);
100
    }
101

    
102
    protected void markViewPartDirty(){
103
        part.changed(input);
104
    }
105

    
106
	protected void createFormFactory() {
107
		if(formFactory != null){
108
			formFactory.dispose();
109
			formFactory = null;
110
		}
111
		formFactory = new CdmFormFactory(Display.getCurrent(), this);
112
	}
113

    
114
	/** {@inheritDoc} */
115
	@Override
116
	public Control getControl() {
117
		if(body.isDisposed()){
118
			return null;
119
		}
120
		for(Control child : body.getChildren()){
121
			return child;
122
		}
123

    
124
		return body;
125
	}
126

    
127
	/** {@inheritDoc} */
128
	@Override
129
	public void setInput(Object input) {
130
		this.input = input;
131
		if(input!=null){
132
		    // reset selection
133
		    setSelection(new StructuredSelection(input));
134
		    refresh();
135
		}
136
	}
137

    
138
	/** {@inheritDoc} */
139
	@Override
140
	public Object getInput() {
141
		return input;
142
	}
143

    
144
	/** {@inheritDoc} */
145
	@Override
146
	public void refresh() {
147
		showParts();
148

    
149
		managedForm.setInput(input);
150

    
151
		managedForm.refresh();
152

    
153
		managedForm.reflow(true);
154
	}
155

    
156
	public void layout(){
157
		body.layout();
158
	}
159

    
160
	protected abstract void showParts();
161

    
162
	public void destroySections() {
163
		for (IFormPart formPart : managedForm.getParts()){
164
			removePart((CdmSectionPart<?>) formPart);
165
		}
166

    
167
		managedForm.setInput(null);
168

    
169
		formFactory.destroyElement(rootElement);
170

    
171
		createFormFactory();
172

    
173
		rootElement = new RootElement(formFactory, body);
174

    
175
		for(Control control : body.getChildren()){
176
			control.dispose();
177
			control = null;
178
		}
179
	}
180

    
181
	public void setFocus(){
182
		// we have to set focus to a control of this viewer
183
		// otherwise, after opening a dialog from the details, the focus will not be
184
		// given back to the details view but to the editor
185
		for(Control child : body.getChildren()){
186
			child.setFocus();
187
			break;
188
		}
189
	}
190

    
191
	public void reflow(){
192
		managedForm.reflow(true);
193
	}
194

    
195
	protected void removePart(CdmSectionPart<?> sectionPart){
196
		managedForm.removePart(sectionPart);
197
		formFactory.removePropertyChangeListener(sectionPart);
198
	}
199

    
200
	protected void addPart(AbstractFormSection<?> section){
201
		CdmSectionPart<?> sectionPart = new CdmSectionPart<>(section);
202
		managedForm.addPart(sectionPart);
203
		formFactory.addPropertyChangeListener(sectionPart);
204
	}
205

    
206
    @Override
207
    public ConversationHolder getConversationHolder() {
208
        return null;
209
	}
210

    
211
}
(1-1/2)