c21f3696a373be7372d9c2ab62fec5bd0e22ab96
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / contextMenu / CreateDerivateContextMenu.java
1 package eu.etaxonomy.taxeditor.editor.view.derivate.contextMenu;
2
3 import org.eclipse.jface.action.ContributionItem;
4 import org.eclipse.jface.action.IContributionItem;
5 import org.eclipse.jface.viewers.ISelection;
6 import org.eclipse.jface.viewers.TreeNode;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.SelectionAdapter;
9 import org.eclipse.swt.events.SelectionEvent;
10 import org.eclipse.swt.widgets.Menu;
11 import org.eclipse.swt.widgets.MenuItem;
12 import org.eclipse.ui.IWorkbenchPart;
13 import org.eclipse.ui.IWorkbenchWindow;
14 import org.eclipse.ui.PlatformUI;
15 import org.eclipse.ui.actions.CompoundContributionItem;
16
17 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy;
18 import eu.etaxonomy.cdm.model.common.DefinedTerm;
19 import eu.etaxonomy.cdm.model.media.Media;
20 import eu.etaxonomy.cdm.model.molecular.DnaSample;
21 import eu.etaxonomy.cdm.model.molecular.Sequence;
22 import eu.etaxonomy.cdm.model.molecular.SingleRead;
23 import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
24 import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
25 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
26 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
27 import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
28 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
29 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
30 import eu.etaxonomy.taxeditor.editor.EditorUtil;
31 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
32 import eu.etaxonomy.taxeditor.model.AbstractUtility;
33 import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
34
35 /**
36 * Context menu for the creation of derivates in the derivate hierarchy.
37 *
38 */
39 public class CreateDerivateContextMenu extends CompoundContributionItem {
40
41 /* (non-Javadoc)
42 * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
43 */
44 @Override
45 protected IContributionItem[] getContributionItems() {
46 IContributionItem[] contributionItems = new IContributionItem[] {
47 new ContributionItem() {
48 @Override
49 public void fill(Menu menu, int index) {
50 DefinedTerm artworkTerm = DerivateLabelProvider.getArtworkTerm();
51 DefinedTerm livingPlantPhotoTerm = DerivateLabelProvider.getLivingPlantPhotoTerm();
52 DefinedTerm specimenScanTerm = DerivateLabelProvider.getSpecimenScanTerm();
53 MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
54 addItem.setText("Add...");
55 Menu addMenu = new Menu(menu);
56 addItem.setMenu(addMenu);
57 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
58 final ISelection selection = window.getActivePage().getSelection();
59 TreeNode selectedTreeNode = EditorUtil.getTreeNodeOfSelection(selection);
60 if(selectedTreeNode!=null){
61 //context menu for FieldUnit
62 if(selectedTreeNode.getValue() instanceof FieldUnit){
63 createMenuItem(addMenu, "Specimen", selectedTreeNode, DerivedUnit.class, SpecimenOrObservationType.PreservedSpecimen);
64 new MenuItem(addMenu, SWT.SEPARATOR);
65 createMenuItem(addMenu, "Tissue Sample", selectedTreeNode, DerivedUnit.class, SpecimenOrObservationType.TissueSample);
66 createMenuItem(addMenu, "DNA Sample", selectedTreeNode, DnaSample.class, SpecimenOrObservationType.DnaSample);
67 createMenuItem(addMenu, "Specimen Scan", selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.StillImage, specimenScanTerm);
68 createMenuItem(addMenu, "Artwork", selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.StillImage, artworkTerm);
69 createMenuItem(addMenu, "Living Plant Photo", selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.StillImage, livingPlantPhotoTerm);
70 }
71 //context menu for Specimen
72 else if(selectedTreeNode.getValue() instanceof DerivedUnit && ((DerivedUnit)selectedTreeNode.getValue()).getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
73
74 createMenuItem(addMenu, "Tissue Sample", selectedTreeNode, DerivedUnit.class, SpecimenOrObservationType.TissueSample);
75 createMenuItem(addMenu, "DNA Sample", selectedTreeNode, DnaSample.class, SpecimenOrObservationType.DnaSample);
76 createMenuItem(addMenu, "Specimen Scan", selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.StillImage, specimenScanTerm);
77 createMenuItem(addMenu, "Artwork", selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.StillImage, artworkTerm);
78 createMenuItem(addMenu, "Living Plant Photo", selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.StillImage, livingPlantPhotoTerm);
79 }
80 //context menu for TissueSample
81 else if(selectedTreeNode.getValue() instanceof DerivedUnit && ((DerivedUnit)selectedTreeNode.getValue()).getRecordBasis()==SpecimenOrObservationType.TissueSample){
82 createMenuItem(addMenu, "DnaSample", selectedTreeNode, DnaSample.class, null);
83 }
84 //context menu for DnaSample
85 else if(selectedTreeNode.getValue() instanceof DnaSample){
86 createMenuItem(addMenu, "Consensus Sequence", selectedTreeNode, Sequence.class, null);
87 }
88 //context menu for Sequence
89 else if(selectedTreeNode.getValue() instanceof Sequence){
90 createMenuItem(addMenu, "Single Read", selectedTreeNode, SingleRead.class, null);
91 }
92 //default
93 else{
94 MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
95 menuItem.setText("No child derivates");
96 menuItem.setEnabled(false);
97 }
98 }
99 }
100
101
102 }
103 };
104 return contributionItems;
105 }
106
107 private void createMenuItem(Menu menu, String menuLabel, TreeNode selectedNode, Class<?> childClass, SpecimenOrObservationType specimenType, DefinedTerm kindOfUnit) {
108 MenuItem menuItem = new MenuItem(menu, SWT.NONE);
109 menuItem.setText(menuLabel);
110 menuItem.addSelectionListener(new WidgetSelectionListener(selectedNode, childClass, specimenType, kindOfUnit));
111 }
112
113 private void createMenuItem(Menu menu, String menuLabel, TreeNode selectedNode, Class<?> childClass, SpecimenOrObservationType specimenType) {
114 createMenuItem(menu, menuLabel, selectedNode, childClass, specimenType, null);
115 }
116
117 private class WidgetSelectionListener extends SelectionAdapter{
118
119 private final TreeNode selectedNode;
120 //TODO: we need a common interface for the generic like e.g. "IDerivateHierarchyNode"
121 private final Class<?> childClass;
122 private final SpecimenOrObservationType specimenType;
123 private final DefinedTerm kindOfUnit;
124
125 /**
126 * @param selectedNode
127 * @param specimenType
128 * @param kindOfUnit
129 */
130 public WidgetSelectionListener(TreeNode selectedNode, Class<?> childClass, SpecimenOrObservationType specimenType, DefinedTerm kindOfUnit) {
131 this.selectedNode = selectedNode;
132 this.childClass = childClass;
133 this.specimenType = specimenType;
134 this.kindOfUnit = kindOfUnit;
135 }
136
137 /* (non-Javadoc)
138 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
139 */
140 @Override
141 public void widgetSelected(SelectionEvent e) {
142 Object selectedDerivate = selectedNode.getValue();
143 if(selectedDerivate instanceof DnaSample){
144 DnaSample dnaSample = (DnaSample)selectedDerivate;
145 if(childClass.equals(Sequence.class)){
146 dnaSample.addSequence(Sequence.NewInstance(""));//why does this not have DnaSample as constructor parameter?
147 }
148 }
149 else if(selectedDerivate instanceof Sequence){
150 ((Sequence) selectedDerivate).addSingleRead(SingleRead.NewInstance());
151 }
152 else if(selectedDerivate instanceof SpecimenOrObservationBase && DerivedUnit.class.isAssignableFrom(childClass)){
153 SpecimenOrObservationBase<?> specimenOrObservationBase = (SpecimenOrObservationBase<?>)selectedDerivate;
154 DerivedUnit derivedUnit;
155 if(childClass.equals(DnaSample.class)){
156 derivedUnit = DnaSample.NewInstance();
157 }
158 else if(childClass.equals(MediaSpecimen.class)){
159 derivedUnit = MediaSpecimen.NewInstance(specimenType);
160 //a MediaSpecimen with no media attached does not make sense. Hence we add one
161 ((MediaSpecimen)derivedUnit).setMediaSpecimen(Media.NewInstance());
162 derivedUnit.setKindOfUnit(kindOfUnit);
163 }
164 else{
165 derivedUnit = DerivedUnit.NewInstance(specimenType);
166 }
167 specimenOrObservationBase.addDerivationEvent(DerivationEvent.NewSimpleInstance(specimenOrObservationBase, derivedUnit, DerivationEventType.GATHERING_IN_SITU()));
168 derivedUnit.setCacheStrategy(new DerivedUnitFacadeCacheStrategy());
169 derivedUnit.getTitleCache(); //update title cache
170 }
171
172 //refresh view
173 IWorkbenchPart activePart = AbstractUtility.getActivePart();
174 if(activePart instanceof DerivateView){
175 DerivateView derivateView = (DerivateView)activePart;
176 derivateView.changed(null);
177 derivateView.refreshTree();
178 }
179 }
180 }
181
182 }