daebf7369f8f576934284b50fbeec33382aa5711
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / forms / AbstractEntitySetSection.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.forms;
5
6 import org.eclipse.jface.action.IAction;
7 import org.eclipse.jface.action.ToolBarManager;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.layout.GridLayout;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Control;
13
14 import eu.etaxonomy.cdm.model.common.VersionableEntity;
15
16 /**
17 * @author nho
18 *
19 */
20 public abstract class AbstractEntitySetSection<T extends VersionableEntity> extends AbstractEditorFormSection {
21
22 protected T entity;
23 protected Composite container;
24
25 /**
26 * @param parent
27 * @param style
28 */
29 protected AbstractEntitySetSection(Composite parent, T entity, int style) {
30 super(parent, style);
31 this.entity = entity;
32 setTextClient(createToolbar());
33
34 }
35
36 private Control createToolbar() {
37 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
38 // IMenuService menuService = (IMenuService) EditorUtil.getService(IMenuService.class);
39 // menuService.populateContributionManager(toolBarManager, "toolbar:de.md.contributions.sectionToolbar");
40
41
42 toolBarManager.add(getAddAction());
43
44 return toolBarManager.createControl(this);
45 }
46
47 /**
48 * @return
49 */
50 protected abstract IAction getAddAction();
51
52 private void createContainer(){
53 if(container != null)
54 container.dispose();
55 container = toolkit.createContainer(getClient(), SWT.NULL);
56
57 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
58 container.setLayout(new GridLayout());
59 }
60
61 /**
62 * Call this method after dynamically changing the client area.
63 * If the options changed is set to true, will also fire a state changed
64 * event to inform the user of unsaved changes.
65 *
66 * @param changed
67 */
68 protected void internalUpdateSection(boolean changed){
69 createContainer();
70 createDynamicContents();
71 this.reflow();
72 if(changed)
73 firePropertyChangeEvent(null);
74 }
75
76 /**
77 *
78 */
79 protected abstract void createDynamicContents();
80
81 /**
82 * Sets this composites entity
83 *
84 * Call this method when the page gets loaded
85 * or otherwise updated from outside
86 *
87 * @param entity the entity to set
88 */
89 public void setEntity(T entity) {
90 this.entity = entity;
91 internalUpdateSection(false);
92 }
93
94 /**
95 * @return the entity
96 */
97 public T getEntity() {
98 return entity;
99 }
100 }