Project

General

Profile

Download (5.35 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.view;
12

    
13
import org.eclipse.jface.viewers.StructuredSelection;
14
import org.eclipse.jface.viewers.Viewer;
15
import org.eclipse.swt.widgets.Composite;
16
import org.eclipse.swt.widgets.Control;
17
import org.eclipse.swt.widgets.Display;
18
import org.eclipse.ui.forms.IFormPart;
19
import org.eclipse.ui.forms.ManagedForm;
20
import org.eclipse.ui.forms.events.ExpansionEvent;
21
import org.eclipse.ui.forms.events.IExpansionListener;
22
import org.eclipse.ui.forms.widgets.ScrolledForm;
23
import org.eclipse.ui.forms.widgets.Section;
24

    
25
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
26
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
27
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
28
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
29
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
30
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
31
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
32
import eu.etaxonomy.taxeditor.ui.element.RootElement;
33
import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
34

    
35
/**
36
 * @author n.hoffmann
37
 * @created Feb 9, 2010
38
 * @version 1.0
39
 */
40
public abstract class AbstractCdmDataViewer extends Viewer implements IConversationEnabled{
41

    
42
	protected ManagedForm managedForm;
43

    
44
	protected CdmFormFactory formFactory;
45

    
46
	protected ScrolledForm scrolledForm;
47

    
48
	private final Composite body;
49

    
50
	protected RootElement rootElement;
51

    
52
	//TODO: create a super class for this?
53
	private Object input;
54

    
55
	private final AbstractCdmViewPart viewPart;
56

    
57

    
58
	public AbstractCdmDataViewer(Composite parent, AbstractCdmViewPart viewPart) {
59
		this.viewPart = viewPart;
60

    
61
		managedForm = new ManagedForm(parent){
62

    
63
			@Override
64
			public void dirtyStateChanged() {
65
				markViewPartDirty();
66
			}
67
		};
68

    
69
		createFormFactory();
70

    
71
		scrolledForm = managedForm.getForm();
72

    
73
		body = scrolledForm.getBody();
74

    
75
		body.setLayout(LayoutConstants.LAYOUT());
76

    
77
		rootElement = new RootElement(formFactory, body);
78
	}
79

    
80
	protected void createFormFactory() {
81
		if(formFactory != null){
82
			formFactory.dispose();
83
			formFactory = null;
84
		}
85
		formFactory = new CdmFormFactory(Display.getCurrent(), this);
86
	}
87

    
88
	protected void markViewPartDirty(){
89
		viewPart.changed(input);
90
	}
91

    
92
	protected AbstractCdmViewPart getViewPart() {
93
		return viewPart;
94
	}
95

    
96
	/** {@inheritDoc} */
97
	@Override
98
	public Control getControl() {
99
		if(body.isDisposed()){
100
			return null;
101
		}
102
		for(Control child : body.getChildren()){
103
			return child;
104
		}
105

    
106
		return body;
107
	}
108
	
109
	/** {@inheritDoc} */
110
	@Override
111
	public void setInput(Object input) {
112
		this.input = input;
113
		// reset selection
114
		setSelection(new StructuredSelection(input));
115
		refresh();
116
	}
117

    
118
	/** {@inheritDoc} */
119
	@Override
120
	public Object getInput() {
121
		return input;
122
	}
123

    
124
	/** {@inheritDoc} */
125
	@Override
126
	public void refresh() {
127
		showParts();
128

    
129
		managedForm.setInput(input);
130

    
131
		managedForm.refresh();
132

    
133
		managedForm.reflow(true);
134
	}
135

    
136
	public void layout(){
137
		body.layout();
138
	}
139

    
140
	protected abstract void showParts();
141

    
142
	protected void destroySections() {
143
		for (IFormPart formPart : managedForm.getParts()){
144
			removePart((CdmSectionPart<?>) formPart);
145
		}
146

    
147
		managedForm.setInput(null);
148

    
149
		formFactory.destroyElement(rootElement);
150

    
151
		createFormFactory();
152

    
153
		rootElement = new RootElement(formFactory, body);
154

    
155
		for(Control control : body.getChildren()){
156
			control.dispose();
157
			control = null;
158
		}
159
	}
160

    
161
	public void setFocus(){
162
		// we have to set focus to a control of this viewer
163
		// otherwise, after opening a dialog from the details, the focus will not be
164
		// given back to the details view but to the editor
165
		for(Control child : body.getChildren()){
166
			child.setFocus();
167
			break;
168
		}
169
	}
170

    
171
	public void reflow(){
172
		managedForm.reflow(true);
173
	}
174

    
175
	protected void removePart(CdmSectionPart<?> sectionPart){
176
		managedForm.removePart(sectionPart);
177
		formFactory.removePropertyChangeListener(sectionPart);
178
	}
179

    
180
	protected void addPart(AbstractFormSection<?> section, boolean isDefaultExpanded){
181
		CdmSectionPart<?> sectionPart = new CdmSectionPart<>(section);
182
		managedForm.addPart(sectionPart);
183
		formFactory.addPropertyChangeListener(sectionPart);
184
		PreferencesUtil.getPreferenceStore().setDefault(section.getClass().getCanonicalName()+";"+getInput().getClass().getCanonicalName(), isDefaultExpanded);
185
		section.setExpanded(PreferencesUtil.getPreferenceStore().getBoolean(section.getClass().getCanonicalName()+";"+getInput().getClass().getCanonicalName()));
186
		section.addExpansionListener(new ExpandListener(section));
187
	}
188

    
189
	@Override
190
    public ConversationHolder getConversationHolder() {
191
		return viewPart.getConversationHolder();
192
	}
193

    
194
	/** {@inheritDoc} */
195
	@Override
196
    public void update(CdmDataChangeMap changeEvents) {}
197
	
198
    private class ExpandListener implements IExpansionListener{
199
    	
200
    	private Section section;
201
    	
202
		public ExpandListener(Section section) {
203
			super();
204
			this.section = section;
205
		}
206

    
207
		@Override
208
		public void expansionStateChanging(ExpansionEvent e) {
209
		}
210

    
211
		@Override
212
		public void expansionStateChanged(ExpansionEvent e) {
213
			PreferencesUtil.getPreferenceStore().setValue(section.getClass().getCanonicalName()+";"+getInput().getClass().getCanonicalName(), e.getState());
214
		}
215
    	
216
    }
217

    
218
}
(2-2/7)