Project

General

Profile

Download (5.97 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.remoting.CdmEagerLoadingException;
33
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
34
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
35
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
36
import eu.etaxonomy.taxeditor.ui.element.RootElement;
37
import eu.etaxonomy.taxeditor.ui.section.occurrence.EmptySection;
38
import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
39

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

    
50
	protected ManagedForm managedForm;
51

    
52
	protected CdmFormFactory formFactory;
53

    
54
	protected ScrolledForm scrolledForm;
55

    
56
	private Composite body;
57

    
58
	protected RootElement rootElement;
59

    
60
	@Inject
61
	protected IEclipseContext context;
62

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

    
66
	protected IDirtyMarkable part;
67

    
68
	@Inject
69
	public AbstractCdmDataViewer() {
70

    
71
	}
72

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

    
76
		managedForm = new ManagedForm(parent){
77

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

    
84
		createFormFactory();
85

    
86
		scrolledForm = managedForm.getForm();
87

    
88
		body = scrolledForm.getBody();
89

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

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

    
95

    
96
    protected void createEmptySection(String message, RootElement parent) {
97
        destroySections();
98

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

    
101
        addPart(emptySection);
102
    }
103

    
104
    public void showEmptyPage(String message){
105
    	destroySections();
106
        createEmptySection(message, rootElement);
107
    }
108

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

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

    
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
	@Override
137
	public void setInput(Object input) {
138
		this.input = input;
139
		if(input!=null){
140
		    try{
141
		        refresh();
142
		    }catch(CdmEagerLoadingException e){
143
		        logger.error(e);
144
		    }
145
		}
146
	}
147

    
148
	@Override
149
	public Object getInput() {
150
		return input;
151
	}
152

    
153
	@Override
154
	public void refresh() {
155
		showParts();
156

    
157
		if(input!=null){
158
		    managedForm.setInput(input);
159
		}
160

    
161
		managedForm.refresh();
162

    
163
//		managedForm.reflow(true);
164
	}
165

    
166
	public void layout(){
167
		body.layout();
168
	}
169

    
170
	protected abstract void showParts();
171

    
172
	public void destroySections() {
173
		for (IFormPart formPart : managedForm.getParts()){
174
			removePart((CdmSectionPart<?>) formPart);
175
		}
176

    
177
		managedForm.setInput(null);
178
		try{
179
		    formFactory.destroyElement(rootElement);
180
		}catch (SWTException e){
181
		    if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
182
                MessagingUtils.errorDialog("Widget is disposed",
183
                        null,
184
                        MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
185
                        null,
186
                        e,
187
                        true);
188

    
189
            }
190
		}
191

    
192
		createFormFactory();
193

    
194
		rootElement = new RootElement(formFactory, body);
195

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

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

    
225
	public void reflow(){
226
		managedForm.reflow(true);
227
	}
228

    
229
	protected void removePart(CdmSectionPart<?> sectionPart){
230
		managedForm.removePart(sectionPart);
231
		formFactory.removePropertyChangeListener(sectionPart);
232
	}
233

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

    
240
    @Override
241
    public ConversationHolder getConversationHolder() {
242
        return null;
243
	}
244

    
245
}
(1-1/2)