Project

General

Profile

Download (6.04 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
package eu.etaxonomy.taxeditor.view.e4;
10

    
11
import javax.inject.Inject;
12

    
13
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
15
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
16
import org.eclipse.e4.core.contexts.IEclipseContext;
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.taxeditor.model.IDirtyMarkable;
28
import eu.etaxonomy.taxeditor.model.MessagingUtils;
29
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
30
import eu.etaxonomy.taxeditor.remoting.CdmEagerLoadingException;
31
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
32
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
33
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
34
import eu.etaxonomy.taxeditor.ui.element.RootElement;
35
import eu.etaxonomy.taxeditor.ui.section.occurrence.EmptySection;
36
import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
37

    
38
/**
39
 * @author pplitzner
40
 * @date 18.07.2017
41
 */
42
public abstract class AbstractCdmDataViewer extends Viewer {
43

    
44
	private static final Logger logger = LogManager.getLogger();
45

    
46
	protected ManagedForm managedForm;
47

    
48
	protected CdmFormFactory formFactory;
49

    
50
	protected ScrolledForm scrolledForm;
51

    
52
	private Composite body;
53

    
54
	protected RootElement rootElement;
55

    
56
	@Inject
57
	protected IEclipseContext context;
58

    
59
	//TODO: create a super class for this?
60
	private Object input;
61

    
62
	protected IDirtyMarkable part;
63

    
64
	@Inject
65
	public AbstractCdmDataViewer() {
66
	}
67

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

    
71
		managedForm = new ManagedForm(parent){
72

    
73
			@Override
74
			public void dirtyStateChanged() {
75
			    markViewPartDirty();
76
			}
77
		};
78

    
79
		createFormFactory();
80

    
81
		scrolledForm = managedForm.getForm();
82

    
83
		body = scrolledForm.getBody();
84

    
85
		body.setLayout(LayoutConstants.LAYOUT());
86

    
87
		rootElement = new RootElement(formFactory, body);
88
	}
89

    
90
    protected void createEmptySection(String message, RootElement parent) {
91
        destroySections();
92

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

    
95
        addPart(emptySection);
96
    }
97

    
98
    public void showEmptyPage(String message){
99
    	destroySections();
100
        createEmptySection(message, rootElement);
101
    }
102

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

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

    
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
	@Override
131
	public void setInput(Object input) {
132
		this.input = input;
133
		if(input!=null){
134
		    try{
135
		        refresh();
136
		    }catch(CdmEagerLoadingException e){
137
		        logger.error(e);
138
		    }
139
		}
140
	}
141

    
142
	@Override
143
	public Object getInput() {
144
		return input;
145
	}
146

    
147
	@Override
148
	public void refresh() {
149
		showParts();
150

    
151
		if(input!=null){
152
		    managedForm.setInput(input);
153
		}
154

    
155
		managedForm.refresh();
156

    
157
//		managedForm.reflow(true);
158
	}
159
	
160
	
161
	public void refresh_withoutnew_build() {
162
		if(input!=null){
163
			IFormPart[] parts = managedForm.getParts();
164
			for (IFormPart part: parts) {
165
				if(part instanceof CdmSectionPart) {	
166
					((CdmSectionPart<?>)part).setFormInputWithoutUpdate(input);
167
				}
168
			}
169
		}		
170

    
171
	}
172

    
173
	public void layout(){
174
		body.layout();
175
	}
176

    
177
	protected abstract void showParts();
178

    
179
	public void destroySections() {
180
		for (IFormPart formPart : managedForm.getParts()){
181
			removePart((CdmSectionPart<?>) formPart);
182
		}
183

    
184
		managedForm.setInput(null);
185
		try{
186
		    formFactory.destroyElement(rootElement);
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
		createFormFactory();
199

    
200
		rootElement = new RootElement(formFactory, body);
201

    
202
		for(Control control : body.getChildren()){
203
			try{
204
				if (control != null && !control.isDisposed()){
205
					control.dispose();
206
				}
207
			}catch(SWTException e){
208
				if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
209
                    MessagingUtils.errorDialog("Widget is disposed",
210
                            null,
211
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
212
                            null,
213
                            e,
214
                            true);
215
                }
216
			}
217
			control = null;
218
		}
219
	}
220

    
221
	public void setFocus(){
222
		// we have to set focus to a control of this viewer
223
		// otherwise, after opening a dialog from the details, the focus will not be
224
		// given back to the details view but to the editor
225
		for(Control child : body.getChildren()){
226
			child.setFocus();
227
			break;
228
		}
229
	}
230

    
231
	public void reflow(){
232
		managedForm.reflow(true);
233
	}
234

    
235
	protected void removePart(CdmSectionPart<?> sectionPart){
236
		managedForm.removePart(sectionPart);
237
		formFactory.removePropertyChangeListener(sectionPart);
238
	}
239

    
240
	protected void addPart(AbstractFormSection<?> section){
241
		CdmSectionPart<?> sectionPart = new CdmSectionPart<>(section);
242
		managedForm.addPart(sectionPart);
243
		formFactory.addPropertyChangeListener(sectionPart);
244
	}
245
}
(1-1/2)