90a381e3e4e63f82b579313e9694bf86057fb449
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / forms / OriginalSourceSection.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.jface.util.IPropertyChangeListener;
11 import org.eclipse.jface.util.PropertyChangeEvent;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.SelectionAdapter;
14 import org.eclipse.swt.events.SelectionEvent;
15 import org.eclipse.swt.widgets.Button;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.ui.forms.widgets.Section;
18
19 import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
20 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
21
22 /**
23 * @author n.hoffmann
24 *
25 */
26 public class OriginalSourceSection extends AbstractEntitySetSection<DescriptionElementBase> implements IPropertyChangeListener{
27
28
29 public OriginalSourceSection(Composite parent, DescriptionElementBase entity, int style){
30 super(parent, entity, Section.TWISTIE | Section.COMPACT | style);
31
32 setText("Citations");
33 }
34
35 /*
36 *
37 */
38 public void createDynamicContents() {
39
40 Set<DescriptionElementSource> sources = null;
41 if(getEntity() != null){
42 sources = getEntity().getSources();
43 }
44
45 if(sources == null || sources.size() == 0){
46 toolkit.createLabel(container, "No citations yet.");
47 }else{
48 for(DescriptionElementSource source : sources){
49 DescriptionElementSourceComposite descriptionElementSourceComposite = toolkit.createDescriptionElementSourceComposite(container, source, SWT.NULL);
50 descriptionElementSourceComposite.addPropertyChangeListener(this);
51
52 Button button_remove = toolkit.createButton(container, "Remove", SWT.PUSH);
53 button_remove.addSelectionListener(new RemoveListener(source));
54 }
55 }
56 }
57
58 private class RemoveListener extends SelectionAdapter{
59 private DescriptionElementSource source;
60
61 public RemoveListener(DescriptionElementSource source){
62 this.source = source;
63 }
64
65 /* (non-Javadoc)
66 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
67 */
68 @Override
69 public void widgetSelected(SelectionEvent e) {
70 getEntity().removeSource(source);
71 internalUpdateSection(true);
72 }
73 }
74
75 /* (non-Javadoc)
76 * @see eu.etaxonomy.taxeditor.forms.AbstractEntitySetSection#getAddAction()
77 */
78 @Override
79 protected IAction getAddAction() {
80 Action addCitation = new Action("add", Action.AS_PUSH_BUTTON){
81 /* (non-Javadoc)
82 * @see org.eclipse.jface.action.Action#run()
83 */
84 @Override
85 public void run() {
86 getEntity().addSource(DescriptionElementSource.NewInstance());
87 if(! getSection().isExpanded())
88 getSection().setExpanded(true);
89 internalUpdateSection(true);
90 }
91 };
92 addCitation.setToolTipText("Create a new citation");
93 return addCitation;
94 }
95
96 /* (non-Javadoc)
97 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
98 */
99 public void propertyChange(PropertyChangeEvent event) {
100 firePropertyChangeEvent(event);
101 }
102 }