Project

General

Profile

Download (5.42 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.e4.core.services.log.Logger;
17
import org.eclipse.jface.viewers.Viewer;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.SWTException;
20
import org.eclipse.swt.widgets.Composite;
21
import org.eclipse.swt.widgets.Control;
22
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.ui.forms.IFormPart;
24
import org.eclipse.ui.forms.ManagedForm;
25
import org.eclipse.ui.forms.widgets.ScrolledForm;
26

    
27
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
28
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
29
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
30
import eu.etaxonomy.taxeditor.model.MessagingUtils;
31
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
32
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
33
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
34
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
35
import eu.etaxonomy.taxeditor.ui.element.RootElement;
36
import eu.etaxonomy.taxeditor.ui.section.occurrence.EmptySection;
37
import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
38

    
39
/**
40
 *
41
 * @author pplitzner
42
 * @date 18.07.2017
43
 *
44
 */
45
public abstract class AbstractCdmDataViewerE4 extends Viewer implements IConversationEnabled{
46
	 @Inject
47
	 private Logger logger;
48

    
49
	protected ManagedForm managedForm;
50

    
51
	protected CdmFormFactory formFactory;
52

    
53
	protected ScrolledForm scrolledForm;
54

    
55
	private Composite body;
56

    
57
	protected RootElement rootElement;
58

    
59
	@Inject
60
	protected IEclipseContext context;
61

    
62
	//TODO: create a super class for this?
63
	private Object input;
64

    
65
	protected IDirtyMarkable part;
66

    
67
	@Inject
68
	public AbstractCdmDataViewerE4() {
69

    
70
	}
71

    
72
	public void init(Composite parent, IDirtyMarkable part) {
73
	    this.part = part;
74

    
75
		managedForm = new ManagedForm(parent){
76

    
77
			@Override
78
			public void dirtyStateChanged() {
79
			    markViewPartDirty();
80
			}
81
		};
82

    
83
		createFormFactory();
84

    
85
		scrolledForm = managedForm.getForm();
86

    
87
		body = scrolledForm.getBody();
88

    
89
		body.setLayout(LayoutConstants.LAYOUT());
90

    
91
		rootElement = new RootElement(formFactory, body);
92
	}
93

    
94

    
95
    protected void createEmptySection(RootElement parent) {
96
        destroySections();
97

    
98
        EmptySection emptySection = formFactory.createEmptySection(formFactory, parent, SWT.NONE);
99

    
100
        addPart(emptySection);
101
    }
102

    
103
    public void showEmptyPage(){
104
    	destroySections();
105
        createEmptySection(rootElement);
106
    }
107

    
108
    protected void markViewPartDirty(){
109
        part.changed(input);
110
    }
111

    
112
	protected void createFormFactory() {
113
		if(formFactory != null){
114
			formFactory.dispose();
115
			formFactory = null;
116
		}
117
		formFactory =  new CdmFormFactory(Display.getCurrent(), this);
118
		ContextInjectionFactory.inject(formFactory, context);
119
	}
120

    
121
	/** {@inheritDoc} */
122
	@Override
123
	public Control getControl() {
124
	    if(body!=null){
125
	        if(body.isDisposed()){
126
	            return null;
127
	        }
128
	        for(Control child : body.getChildren()){
129
	            return child;
130
	        }
131
	    }
132

    
133
		return body;
134
	}
135

    
136
	/** {@inheritDoc} */
137
	@Override
138
	public void setInput(Object input) {
139
		this.input = input;
140
		if(input!=null){
141
		    refresh();
142
		}
143
	}
144

    
145
	/** {@inheritDoc} */
146
	@Override
147
	public Object getInput() {
148
		return input;
149
	}
150

    
151
	/** {@inheritDoc} */
152
	@Override
153
	public void refresh() {
154
		showParts();
155

    
156
		managedForm.setInput(input);
157

    
158
		managedForm.refresh();
159

    
160
		managedForm.reflow(true);
161
	}
162

    
163
	public void layout(){
164
		body.layout();
165
	}
166

    
167
	protected abstract void showParts();
168

    
169
	public void destroySections() {
170
		for (IFormPart formPart : managedForm.getParts()){
171
			removePart((CdmSectionPart<?>) formPart);
172
		}
173

    
174
		managedForm.setInput(null);
175

    
176
		formFactory.destroyElement(rootElement);
177

    
178
		createFormFactory();
179

    
180
		rootElement = new RootElement(formFactory, body);
181

    
182
		for(Control control : body.getChildren()){
183
			try{
184
				if (control != null && !control.isDisposed()){
185
					control.dispose();
186
				}
187
			}catch(SWTException e){
188
				if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
189
                    MessagingUtils.errorDialog("Widget is disposed",
190
                            null,
191
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
192
                            null,
193
                            e,
194
                            true);
195

    
196
                }
197
			}
198
			control = null;
199
		}
200
	}
201

    
202
	public void setFocus(){
203
		// we have to set focus to a control of this viewer
204
		// otherwise, after opening a dialog from the details, the focus will not be
205
		// given back to the details view but to the editor
206
		for(Control child : body.getChildren()){
207
			child.setFocus();
208
			break;
209
		}
210
	}
211

    
212
	public void reflow(){
213
		managedForm.reflow(true);
214
	}
215

    
216
	protected void removePart(CdmSectionPart<?> sectionPart){
217
		managedForm.removePart(sectionPart);
218
		formFactory.removePropertyChangeListener(sectionPart);
219
	}
220

    
221
	protected void addPart(AbstractFormSection<?> section){
222
		CdmSectionPart<?> sectionPart = new CdmSectionPart<>(section);
223
		managedForm.addPart(sectionPart);
224
		formFactory.addPropertyChangeListener(sectionPart);
225
	}
226

    
227
    @Override
228
    public ConversationHolder getConversationHolder() {
229
        return null;
230
	}
231

    
232
}
(1-1/2)