Project

General

Profile

« Previous | Next » 

Revision ac12d86b

Added by Patrick Plitzner almost 11 years ago

  • added java doc
    • formatted code

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/AbstractEntityCollectionSection.java
1 1
/**
2
 * 
2
 *
3 3
 */
4 4
package eu.etaxonomy.taxeditor.ui.section;
5 5

  
6 6
import java.util.Collection;
7 7

  
8 8
import org.eclipse.jface.action.Action;
9
import org.eclipse.jface.action.IAction;
9 10
import org.eclipse.jface.action.ToolBarManager;
10 11
import org.eclipse.jface.resource.ImageDescriptor;
11 12
import org.eclipse.swt.SWT;
......
19 20
import org.eclipse.swt.widgets.Label;
20 21
import org.eclipse.ui.forms.events.ExpansionEvent;
21 22
import org.eclipse.ui.forms.events.IExpansionListener;
22
import org.eclipse.ui.forms.widgets.Section;
23
import org.eclipse.ui.forms.widgets.ExpandableComposite;
23 24

  
24 25
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
25 26
import eu.etaxonomy.cdm.common.CdmUtils;
27
import eu.etaxonomy.taxeditor.model.AbstractUtility;
26 28
import eu.etaxonomy.taxeditor.model.ImageResources;
27 29
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
28 30
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
29 31
import eu.etaxonomy.taxeditor.preference.Resources;
30
import eu.etaxonomy.taxeditor.store.StoreUtil;
31 32
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
32 33
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
33 34
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
34 35

  
35 36
/**
36
 * <p>Abstract AbstractEntityCollectionSection class.</p>
37
 * This class visualizes an CDM entity of type ENTITY and additionally provides the functionality to add
38
 * other elements of type ELEMENT to them.
39
 *
40
 * @param <ENTITY> A CDM entity which should be visualized by this section.
41
 * @param <ELEMENT> An element that can be added (multiple times) to this entity.
37 42
 *
38 43
 * @author n.hoffmann
39 44
 * @version $Id: $
40 45
 */
46

  
41 47
public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends AbstractFormSection<ENTITY> implements IExpansionListener{
42
		
48

  
43 49
	protected Composite container;
44
	
50

  
45 51
	private Label label_empty;
46 52

  
47 53
	private String title;
48
	
54

  
49 55
	/**
50 56
	 * <p>Constructor for AbstractEntityCollectionSection.</p>
51 57
	 *
52
	 * @param conversation 
58
	 * @param conversation
53 59
	 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
54 60
	 * @param style a int.
55 61
	 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
......
58 64
	 * @param <ELEMENT> a ELEMENT object.
59 65
	 */
60 66
	public AbstractEntityCollectionSection(CdmFormFactory formFactory, ConversationHolder conversation, ICdmFormElement parentElement, String title, int style) {
61
		super(formFactory, parentElement, Section.CLIENT_INDENT | style);
67
		super(formFactory, parentElement, ExpandableComposite.CLIENT_INDENT | style);
62 68
		this.title = title;
63 69
		this.setText(getTitleString());
64 70
		showToolbar();
65
		
71

  
66 72
		addExpansionListener(this);
67 73
	}
68
	
74

  
69 75
	protected Control createToolbar() {
70 76
		ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
71
		
72
		Action addAction = new Action("add", Action.AS_PUSH_BUTTON){
77

  
78
		Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
73 79
			/* (non-Javadoc)
74 80
			 * @see org.eclipse.jface.action.Action#run()
75 81
			 */
......
78 84
				ELEMENT element = createNewElement();
79 85
				if(element != null){
80 86
					addElement(element);
81
					if(! getSection().isExpanded())
82
						getSection().setExpanded(true);
87
					if(! getSection().isExpanded()) {
88
                        getSection().setExpanded(true);
89
                    }
83 90
					internalUpdateSection(true);
84 91
				}
85 92
			}
86 93
		};
87 94
		addAction.setImageDescriptor(new ImageDescriptor() {
88
			
95

  
89 96
			@Override
90 97
			public ImageData getImageData() {
91 98
				return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
92 99
			}
93 100
		});
94 101
		addAction.setToolTipText(getTooltipString());
95
		
102

  
96 103
		toolBarManager.add(addAction);
97
		
104

  
98 105
		return toolBarManager.createControl(this);
99 106
	}
100
	
107

  
101 108
	public void showToolbar(){
102 109
		setTextClient(createToolbar());
103 110
	}
104
	
111

  
105 112
	public void removeToolbar(){
106 113
		setTextClient(null);
107 114
	}
108
	
115

  
109 116
	/**
110 117
	 * <p>setEntity</p>
111 118
	 *
......
119 126
		}
120 127
		setSectionTitle();
121 128
		layout();
122
	};
123
		
129
	}
130

  
124 131
	/**
125 132
	 * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
126 133
	 * Override in subclasses if you want to have a different behaviour.
......
132 139
			this.setText(getTitleString());
133 140
		}
134 141
	}
135
	
142

  
136 143
	/**
137 144
	 * Removes all content from the container
138 145
	 */
......
143 150
		}
144 151
		removeElements();
145 152
	}
146
	
153

  
147 154
	/**
148 155
	 * Call this method after dynamically changing the client area.
149 156
	 * If the options changed is set to true, will also fire a state changed
......
153 160
	 */
154 161
	protected void internalUpdateSection(boolean changed){
155 162
		destroyDynamicContent();
156
		if(isExpanded() || expandSectionWhenContentAvailable())
157
			renderContent(isExpanded());
158
		if(changed)
159
			firePropertyChangeEvent(this);
163
		if(isExpanded() || expandSectionWhenContentAvailable()) {
164
            renderContent(isExpanded());
165
        }
166
		if(changed) {
167
            firePropertyChangeEvent(this);
168
        }
160 169
	}
161 170

  
162 171
	/**
163
	 * Create the elements to be shown in this seciton client area 
172
	 * Create the elements to be shown in this section client area
164 173
	 */
165 174
	private void renderContent(boolean forceExpansion)
166 175
	{
167 176
		Collection<ELEMENT> elements = getCollection(getEntity());
168
		
177

  
169 178
		if(elements == null || elements.isEmpty()){
170 179
			createEmptyContent();
171 180
		}else{
172 181
			createDynamicContents(elements);
173 182
			forceExpansion = true;
174 183
		}
175
		
184

  
176 185
		this.setExpanded(forceExpansion);
177
		
186

  
178 187
		reflow();
179 188
	}
180
	
189

  
181 190
	/**
182 191
	 * <p>createEmptyContent</p>
183 192
	 */
184 193
	protected void createEmptyContent(){
185 194
		label_empty = formFactory.createLabel(getLayoutComposite(), getEmptyString());
186 195
	}
187
	
196

  
188 197
	/**
189 198
	 * Creates the widgets for the collection
190 199
	 *
191 200
	 * @param elements a {@link java.util.Collection} object.
192 201
	 */
193 202
	protected void createDynamicContents(Collection<ELEMENT> elements)
194
	{		
203
	{
195 204
		int i = 0;
196 205
		for(final ELEMENT element : elements){
197 206
			SelectionAdapter removeListener = new SelectionAdapter(){
198 207
				@Override
199 208
				public void widgetSelected(SelectionEvent e) {
200
					removeElement(element);					
209
					removeElement(element);
201 210
					internalUpdateSection(true);
202 211
				}
203 212
			};
204 213
			boolean modulo = i++%2 == 0;
205 214
			String colorResource = modulo ? Resources.COLOR_LIST_EVEN : Resources.COLOR_LIST_ODD;
206
			createElementComposite(element, removeListener, StoreUtil.getColor(colorResource));
215
			createElementComposite(element, removeListener, AbstractUtility.getColor(colorResource));
207 216
		}
208 217
	}
209 218

  
......
217 226
	protected void createElementComposite(ELEMENT element, SelectionListener removeListener, Color backgroundColor){
218 227
		formFactory.createEntityCollectionElement(this, element, removeListener, backgroundColor, SWT.NULL);
219 228
	}
220
	
229

  
221 230
	/* (non-Javadoc)
222 231
	 * @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
223 232
	 */
......
238 247
	public String getTitleString() {
239 248
		return CdmUtils.Nz(title);
240 249
	}
241
	
250

  
242 251
	/**
243 252
	 * <p>setTitleString</p>
244 253
	 *
......
249 258
		setSectionTitle();
250 259
		layout();
251 260
	}
252
	
261

  
253 262
	/** {@inheritDoc} */
254
	public void expansionStateChanging(ExpansionEvent e) {
263
	@Override
264
    public void expansionStateChanging(ExpansionEvent e) {
255 265
//		logger.warn("Expansion State Changing");
256 266
	}
257
	
267

  
258 268
	/** {@inheritDoc} */
259
	public void expansionStateChanged(ExpansionEvent e) {
269
	@Override
270
    public void expansionStateChanged(ExpansionEvent e) {
260 271
		if(isExpanded()){
261 272
			renderContent(isExpanded());
262 273
		}else{
263 274
			destroyDynamicContent();
264 275
		}
265 276
	}
266
	
277

  
267 278
	private boolean expandSectionWhenContentAvailable(){
268 279
		return PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE);
269 280
	}
270
	
281

  
271 282
	/**
272 283
	 * Get the specific collection of this entity
273 284
	 *
......
275 286
	 * @return a {@link java.util.Collection} object.
276 287
	 */
277 288
	public abstract Collection<ELEMENT> getCollection(ENTITY entity);
278
	
289

  
279 290
	/**
280 291
	 * Create a new Element for this collection
281 292
	 *
282 293
	 * @return a ELEMENT object.
283 294
	 */
284 295
	public abstract ELEMENT createNewElement();
285
	
296

  
286 297
	/**
287 298
	 * Add an element to the entities collection
288 299
	 *
289 300
	 * @param element a ELEMENT object.
290 301
	 */
291 302
	public abstract void addElement(ELEMENT element);
292
	
303

  
293 304
	/**
294 305
	 * Remove an element from the entities collection
295 306
	 *
296 307
	 * @param element a ELEMENT object.
297 308
	 */
298 309
	public abstract void removeElement(ELEMENT element);
299
	
310

  
300 311
	/**
301 312
	 * String to display when the collection is empty
302 313
	 *
303 314
	 * @return a {@link java.lang.String} object.
304 315
	 */
305 316
	public abstract String getEmptyString();
306
	
317

  
307 318
	/**
308 319
	 * <p>getTooltipString</p>
309 320
	 *
310 321
	 * @return String to display when hovering the add button
311 322
	 */
312
	protected abstract String getTooltipString();	
323
	protected abstract String getTooltipString();
313 324
}

Also available in: Unified diff