implementation and generalization of a form framework to be used in the tabbed proper...
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / forms / AnnotationSection.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.forms;
5
6 import java.util.Set;
7
8 import org.eclipse.jface.action.Action;
9 import org.eclipse.jface.action.IAction;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.SelectionAdapter;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.widgets.Button;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.ui.forms.widgets.Section;
16
17 import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
18 import eu.etaxonomy.cdm.model.common.Annotation;
19 import eu.etaxonomy.cdm.model.common.AnnotationType;
20 import eu.etaxonomy.cdm.model.common.Language;
21 import eu.etaxonomy.taxeditor.store.CdmStore;
22
23 /**
24 * @author n.hoffmann
25 *
26 */
27 public class AnnotationSection extends AbstractEntitySetSection<AnnotatableEntity> {
28
29 /**
30 * @param parent
31 * @param style
32 */
33 protected AnnotationSection(Composite parent, AnnotatableEntity annotatableEntity, int style) {
34 super(parent, annotatableEntity, Section.TWISTIE | Section.COMPACT | style);
35
36 setText("Annotations");
37 }
38
39
40 /*
41 * (non-Javadoc)
42 * @see eu.etaxonomy.taxeditor.forms.AbstractEntitySetSection#createDynamicContents()
43 */
44 @Override
45 public void createDynamicContents() {
46
47 Set<Annotation> annotations = null;
48 if(getEntity() != null){
49 annotations = getEntity().getAnnotations();
50 }
51
52 if(annotations == null || annotations.size() == 0){
53 toolkit.createLabel(container, "No annotations yet.");
54 }else{
55 for(Annotation annotation : annotations){
56
57 toolkit.createAnnotationComposite(container, annotation, SWT.NULL);
58
59 Button button_remove = toolkit.createButton(container, "Remove", SWT.PUSH);
60 button_remove.addSelectionListener(new RemoveListener(annotation));
61 }
62 }
63 }
64
65 private class RemoveListener extends SelectionAdapter{
66 private Annotation annotation;
67
68 public RemoveListener(Annotation annotation){
69 this.annotation = annotation;
70 }
71
72 /* (non-Javadoc)
73 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
74 */
75 @Override
76 public void widgetSelected(SelectionEvent e) {
77 getEntity().removeAnnotation(annotation);
78 internalUpdateSection(true);
79 }
80 }
81
82
83
84 /* (non-Javadoc)
85 * @see eu.etaxonomy.taxeditor.forms.AbstractEntitySetSection#getAddAction()
86 */
87 @Override
88 protected IAction getAddAction() {
89 Action addAnnotation = new Action("add", Action.AS_PUSH_BUTTON){
90 /* (non-Javadoc)
91 * @see org.eclipse.jface.action.Action#run()
92 */
93 @Override
94 public void run() {
95 String text = "";
96 AnnotationType type = AnnotationType.EDITORIAL();
97 Language language = CdmStore.getDefaultLanguage();
98
99 Annotation annotation = Annotation.NewInstance(text, type, language);
100 getEntity().addAnnotation(annotation);
101 internalUpdateSection(true);
102 if(! getSection().isExpanded())
103 getSection().setExpanded(true);
104 internalUpdateSection(true);
105 }
106 };
107 addAnnotation.setToolTipText("Create a new annotation");
108 return addAnnotation;
109 }
110 }