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