Project

General

Profile

Download (4.78 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.ContextInjectionFactory;
15
import org.eclipse.e4.core.contexts.IEclipseContext;
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
		ContextInjectionFactory.inject(formFactory, context);
113
	}
114

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

    
127
		return body;
128
	}
129

    
130
	/** {@inheritDoc} */
131
	@Override
132
	public void setInput(Object input) {
133
		this.input = input;
134
		if(input!=null){
135
		    refresh();
136
		}
137
	}
138

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

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

    
150
		managedForm.setInput(input);
151

    
152
		managedForm.refresh();
153

    
154
		managedForm.reflow(true);
155
	}
156

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

    
161
	protected abstract void showParts();
162

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

    
168
		managedForm.setInput(null);
169

    
170
		formFactory.destroyElement(rootElement);
171

    
172
		createFormFactory();
173

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

    
176
		for(Control control : body.getChildren()){
177
		    if (control != null && !control.isDisposed()){
178
		        control.dispose();
179
		    }
180
			control = null;
181
		}
182
	}
183

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

    
194
	public void reflow(){
195
		managedForm.reflow(true);
196
	}
197

    
198
	protected void removePart(CdmSectionPart<?> sectionPart){
199
		managedForm.removePart(sectionPart);
200
		formFactory.removePropertyChangeListener(sectionPart);
201
	}
202

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

    
209
    @Override
210
    public ConversationHolder getConversationHolder() {
211
        return null;
212
	}
213

    
214
}
(1-1/2)