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