Project

General

Profile

Download (5.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
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

    
47
	protected ManagedForm managedForm;
48

    
49
	protected CdmFormFactory formFactory;
50

    
51
	protected ScrolledForm scrolledForm;
52

    
53
	private Composite body;
54

    
55
	protected RootElement rootElement;
56

    
57
	@Inject
58
	protected IEclipseContext context;
59

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

    
63
	protected IDirtyMarkable part;
64

    
65
	@Inject
66
	public AbstractCdmDataViewer() {
67

    
68
	}
69

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

    
73
		managedForm = new ManagedForm(parent){
74

    
75
			@Override
76
			public void dirtyStateChanged() {
77
			    markViewPartDirty();
78
			}
79
		};
80

    
81
		createFormFactory();
82

    
83
		scrolledForm = managedForm.getForm();
84

    
85
		body = scrolledForm.getBody();
86

    
87
		body.setLayout(LayoutConstants.LAYOUT());
88

    
89
		rootElement = new RootElement(formFactory, body);
90
	}
91

    
92

    
93
    protected void createEmptySection(String message, RootElement parent) {
94
        destroySections();
95

    
96
        EmptySection emptySection = formFactory.createEmptySection(message, formFactory, parent, SWT.NONE);
97

    
98
        addPart(emptySection);
99
    }
100

    
101
    public void showEmptyPage(String message){
102
    	destroySections();
103
        createEmptySection(message, rootElement);
104
    }
105

    
106
    protected void markViewPartDirty(){
107
        part.changed(input);
108
    }
109

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

    
119
	@Override
120
	public Control getControl() {
121
	    if(body!=null){
122
	        if(body.isDisposed()){
123
	            return null;
124
	        }
125
	        for(Control child : body.getChildren()){
126
	            return child;
127
	        }
128
	    }
129

    
130
		return body;
131
	}
132

    
133
	@Override
134
	public void setInput(Object input) {
135
		this.input = input;
136
		if(input!=null){
137
		    try{
138
		        refresh();
139
		    }catch(CdmEagerLoadingException e){
140
		        logger.error(e);
141
		    }
142
		}
143
	}
144

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

    
150
	@Override
151
	public void refresh() {
152
		showParts();
153

    
154
		if(input!=null){
155
		    managedForm.setInput(input);
156
		}
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
		try{
176
		    formFactory.destroyElement(rootElement);
177
		}catch (SWTException e){
178
		    if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
179
                MessagingUtils.errorDialog("Widget is disposed",
180
                        null,
181
                        MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
182
                        null,
183
                        e,
184
                        true);
185

    
186
            }
187
		}
188

    
189
		createFormFactory();
190

    
191
		rootElement = new RootElement(formFactory, body);
192

    
193
		for(Control control : body.getChildren()){
194
			try{
195
				if (control != null && !control.isDisposed()){
196
					control.dispose();
197
				}
198
			}catch(SWTException e){
199
				if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
200
                    MessagingUtils.errorDialog("Widget is disposed",
201
                            null,
202
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
203
                            null,
204
                            e,
205
                            true);
206
                }
207
			}
208
			control = null;
209
		}
210
	}
211

    
212
	public void setFocus(){
213
		// we have to set focus to a control of this viewer
214
		// otherwise, after opening a dialog from the details, the focus will not be
215
		// given back to the details view but to the editor
216
		for(Control child : body.getChildren()){
217
			child.setFocus();
218
			break;
219
		}
220
	}
221

    
222
	public void reflow(){
223
		managedForm.reflow(true);
224
	}
225

    
226
	protected void removePart(CdmSectionPart<?> sectionPart){
227
		managedForm.removePart(sectionPart);
228
		formFactory.removePropertyChangeListener(sectionPart);
229
	}
230

    
231
	protected void addPart(AbstractFormSection<?> section){
232
		CdmSectionPart<?> sectionPart = new CdmSectionPart<>(section);
233
		managedForm.addPart(sectionPart);
234
		formFactory.addPropertyChangeListener(sectionPart);
235
	}
236

    
237

    
238

    
239
}
(1-1/2)