e62783ae4bed2908fa2688196f9c3a1e83b496f7
[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.IWorkbenchWindow;
13 import org.eclipse.ui.PlatformUI;
14 import org.eclipse.ui.actions.CompoundContributionItem;
15
16 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy;
17 import eu.etaxonomy.cdm.model.media.Media;
18 import eu.etaxonomy.cdm.model.molecular.DnaSample;
19 import eu.etaxonomy.cdm.model.molecular.Sequence;
20 import eu.etaxonomy.cdm.model.molecular.SingleRead;
21 import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
22 import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
23 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
24 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
25 import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
26 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
27 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
28 import eu.etaxonomy.taxeditor.editor.EditorUtil;
29 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
30 import eu.etaxonomy.taxeditor.model.AbstractUtility;
31 import eu.etaxonomy.taxeditor.model.MessagingUtils;
32 import eu.etaxonomy.taxeditor.ui.dialog.selection.MediaSelectionDialog;
33
34 /**
35 * Context menu for the creation of derivates in the derivate hierarchy.
36 *
37 */
38 public class CreateDerivateContextMenu extends CompoundContributionItem {
39
40 @Override
41 protected IContributionItem[] getContributionItems() {
42 IContributionItem[] contributionItems = new IContributionItem[] {
43 new ContributionItem() {
44 @Override
45 public void fill(Menu menu, int index) {
46 MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
47 addItem.setText("Add...");
48 Menu addMenu = new Menu(menu);
49 addItem.setMenu(addMenu);
50 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
51 final ISelection selection = window.getActivePage().getSelection();
52 TreeNode selectedTreeNode = EditorUtil.getTreeNodeOfSelection(selection);
53 if(selectedTreeNode!=null){
54 //context menu for FieldUnit
55 if(selectedTreeNode.getValue() instanceof FieldUnit){
56 createMenuItem(addMenu, "Specimen", selectedTreeNode, DerivedUnit.class, SpecimenOrObservationType.PreservedSpecimen, DerivationEventType.GATHERING_IN_SITU());
57 new MenuItem(addMenu, SWT.SEPARATOR);
58 createMenuItem(addMenu, "Tissue Sample", selectedTreeNode, DnaSample.class, SpecimenOrObservationType.TissueSample, DerivationEventType.PREPARATION());
59 createMenuItem(addMenu, "DNA Sample", selectedTreeNode, DnaSample.class, SpecimenOrObservationType.DnaSample, DerivationEventType.DNA_EXTRACTION());
60 createMenuItem(addMenu, "Media Specimen", selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.Media, DerivationEventType.ACCESSIONING());
61 }
62 //context menu for Specimen
63 else if(selectedTreeNode.getValue() instanceof DerivedUnit && ((DerivedUnit)selectedTreeNode.getValue()).getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
64
65 createMenuItem(addMenu, "Tissue Sample", selectedTreeNode, DnaSample.class, SpecimenOrObservationType.TissueSample, DerivationEventType.PREPARATION());
66 createMenuItem(addMenu, "DNA Sample", selectedTreeNode, DnaSample.class, SpecimenOrObservationType.DnaSample, DerivationEventType.DNA_EXTRACTION());
67 createMenuItem(addMenu, "Media Specimen", selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.Media, DerivationEventType.ACCESSIONING());
68 }
69 //context menu for TissueSample
70 else if(selectedTreeNode.getValue() instanceof DnaSample && ((DnaSample)selectedTreeNode.getValue()).getRecordBasis()==SpecimenOrObservationType.TissueSample){
71 createMenuItem(addMenu, "DnaSample", selectedTreeNode, DnaSample.class, SpecimenOrObservationType.DnaSample, DerivationEventType.DNA_EXTRACTION());
72 createMenuItem(addMenu, "Media Specimen", selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.Media, DerivationEventType.ACCESSIONING());
73 }
74 //context menu for DnaSample
75 else if(selectedTreeNode.getValue() instanceof DnaSample && ((DnaSample)selectedTreeNode.getValue()).getRecordBasis()==SpecimenOrObservationType.DnaSample){
76 createMenuItem(addMenu, "Consensus Sequence", selectedTreeNode, Sequence.class, null, null);
77 createMenuItem(addMenu, "Media Specimen", selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.Media, DerivationEventType.ACCESSIONING());
78 }
79 //context menu for Sequence
80 else if(selectedTreeNode.getValue() instanceof Sequence){
81 createMenuItem(addMenu, "Single Read", selectedTreeNode, SingleRead.class, null, null);
82 }
83 //default
84 else{
85 MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
86 menuItem.setText("No child derivates");
87 menuItem.setEnabled(false);
88 }
89 }
90 }
91
92
93 }
94 };
95 return contributionItems;
96 }
97
98 private void createMenuItem(Menu menu, String menuLabel, final TreeNode selectedNode, Class<?> childClass, final SpecimenOrObservationType specimenType, final DerivationEventType derivationEventType) {
99 if(specimenType!=null && specimenType.equals(SpecimenOrObservationType.Media)){
100 //media submenu
101 MenuItem mediaItem = new MenuItem(menu, SWT.CASCADE);
102 mediaItem.setText("Media...");
103 Menu mediaMenu = new Menu(menu);
104 mediaItem.setMenu(mediaMenu);
105
106 //media specimen menu
107 MenuItem menuItem = new MenuItem(mediaMenu, SWT.NONE);
108 menuItem.setText(menuLabel);
109 menuItem.addSelectionListener(new WidgetSelectionListener(selectedNode, childClass, specimenType, derivationEventType));
110
111 //existing media menu
112 MenuItem existingMediaItem = new MenuItem(mediaMenu, SWT.NONE);
113 existingMediaItem.setText("Media Specimen with existing media");
114 existingMediaItem.addSelectionListener(new SelectionAdapter() {
115 @Override
116 public void widgetSelected(SelectionEvent e) {
117 //select media
118 Media media = MediaSelectionDialog.select(AbstractUtility.getShell(), null, null);
119 //create MediaSpecimen
120 MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(specimenType);
121 mediaSpecimen.setMediaSpecimen(media);
122
123 //attach to parent
124 if(selectedNode.getValue() instanceof SpecimenOrObservationBase){
125 SpecimenOrObservationBase<?> specimenOrObservationBase = (SpecimenOrObservationBase<?>)selectedNode.getValue();
126
127 specimenOrObservationBase.addDerivationEvent(DerivationEvent.NewSimpleInstance(specimenOrObservationBase, mediaSpecimen, derivationEventType));
128 mediaSpecimen.setCacheStrategy(new DerivedUnitFacadeCacheStrategy());
129 mediaSpecimen.getTitleCache(); //update title cache
130 }
131
132 //refresh view
133 DerivateView derivateView = null;
134 if(AbstractUtility.getActivePart() instanceof DerivateView){
135 derivateView = (DerivateView) AbstractUtility.getActivePart();
136 }
137 if(derivateView!=null){
138 derivateView.getConversationHolder().commit();
139 derivateView.refreshTree(mediaSpecimen);
140 }
141 }
142 });
143 }
144 else{
145 MenuItem menuItem = new MenuItem(menu, SWT.NONE);
146 menuItem.setText(menuLabel);
147 menuItem.addSelectionListener(new WidgetSelectionListener(selectedNode, childClass, specimenType, derivationEventType));
148 }
149 }
150
151 private class WidgetSelectionListener extends SelectionAdapter{
152
153 private final TreeNode selectedNode;
154 //TODO: we need a common interface for the generic like e.g. "IDerivateHierarchyNode"
155 private final Class<?> childClass;
156 private final SpecimenOrObservationType specimenType;
157 private final DerivationEventType derivationEventType;
158
159 /**
160 * @param selectedNode
161 * @param specimenType
162 */
163 public WidgetSelectionListener(TreeNode selectedNode, Class<?> childClass, SpecimenOrObservationType specimenType, DerivationEventType derivationEventType) {
164 this.selectedNode = selectedNode;
165 this.childClass = childClass;
166 this.specimenType = specimenType;
167 this.derivationEventType = derivationEventType;
168 }
169
170 @Override
171 public void widgetSelected(SelectionEvent e) {
172 DerivateView derivateView = null;
173 if(AbstractUtility.getActivePart() instanceof DerivateView){
174 derivateView = (DerivateView) AbstractUtility.getActivePart();
175 }
176 if(derivateView!=null){
177 if(derivateView.isDirty()){
178 e.doit = false;
179 MessagingUtils.warningDialog("View has unsaved changes", this, "You need to save before performing this action");
180 return;
181 }
182 }
183
184 Object createdElement = null;
185 Object selectedDerivate = selectedNode.getValue();
186 if(selectedDerivate instanceof DnaSample && ((DnaSample) selectedDerivate).getRecordBasis()==SpecimenOrObservationType.DnaSample){
187 DnaSample dnaSample = (DnaSample)selectedDerivate;
188 if(childClass.equals(Sequence.class)){
189 Sequence newInstance = Sequence.NewInstance(dnaSample, "", 0);
190 createdElement = newInstance;
191 }
192 }
193 else if(selectedDerivate instanceof Sequence){
194 SingleRead newInstance = SingleRead.NewInstance();
195 ((Sequence) selectedDerivate).addSingleRead(newInstance);
196 createdElement = newInstance;
197 }
198 else if(selectedDerivate instanceof SpecimenOrObservationBase && DerivedUnit.class.isAssignableFrom(childClass)){
199 SpecimenOrObservationBase<?> specimenOrObservationBase = (SpecimenOrObservationBase<?>)selectedDerivate;
200 DerivedUnit derivedUnit;
201 if(childClass.equals(DnaSample.class)){
202 derivedUnit = DnaSample.NewInstance();
203 if(specimenType==SpecimenOrObservationType.DnaSample){
204 derivedUnit.setRecordBasis(SpecimenOrObservationType.DnaSample);
205 }
206 else if(specimenType==SpecimenOrObservationType.TissueSample){
207 derivedUnit.setRecordBasis(SpecimenOrObservationType.TissueSample);
208 }
209 }
210 else if(childClass.equals(MediaSpecimen.class)){
211 derivedUnit = MediaSpecimen.NewInstance(specimenType);
212 //a MediaSpecimen with no media attached does not make sense. Hence we add one
213 ((MediaSpecimen)derivedUnit).setMediaSpecimen(Media.NewInstance());
214 }
215 else{
216 derivedUnit = DerivedUnit.NewInstance(specimenType);
217 }
218 specimenOrObservationBase.addDerivationEvent(DerivationEvent.NewSimpleInstance(specimenOrObservationBase, derivedUnit, derivationEventType));
219 derivedUnit.setCacheStrategy(new DerivedUnitFacadeCacheStrategy());
220 derivedUnit.getTitleCache(); //update title cache
221 createdElement = derivedUnit;
222 }
223
224 //refresh view
225 if(derivateView!=null){
226 derivateView.getConversationHolder().commit();
227 derivateView.refreshTree(createdElement);
228 }
229 }
230 }
231
232 }