4cffe35bb8f079cdbf2282cae9a2d222167b6ff5
[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.application.CdmApplicationState;
17 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy;
18 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
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.Messages;
32 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
33 import eu.etaxonomy.taxeditor.model.AbstractUtility;
34 import eu.etaxonomy.taxeditor.model.MessagingUtils;
35 import eu.etaxonomy.taxeditor.store.CdmStore;
36 import eu.etaxonomy.taxeditor.ui.dialog.selection.MediaSelectionDialog;
37
38 /**
39 * Context menu for the creation of derivates in the derivate hierarchy.
40 *
41 */
42 public class CreateDerivateContextMenu extends CompoundContributionItem {
43
44 private static final String SINGLE_READ = Messages.CreateDerivateContextMenu_SINGLE_READ;
45 private static final String CONSENSUS_SEQUENCE = Messages.CreateDerivateContextMenu_SEQUENCE;
46 private static final String MEDIA_SPECIMEN = Messages.CreateDerivateContextMenu_MEDIA_SPECIMEN;
47 private static final String DNA_SAMPLE = Messages.CreateDerivateContextMenu_DNA_SAMPLE;
48 private static final String TISSUE_SAMPLE = Messages.CreateDerivateContextMenu_TISSUE_SAMPLE;
49 private static final String SPECIMEN = Messages.CreateDerivateContextMenu_SPECIMEN;
50
51 @Override
52 protected IContributionItem[] getContributionItems() {
53 IContributionItem[] contributionItems = new IContributionItem[] {
54 new ContributionItem() {
55 @Override
56 public void fill(Menu menu, int index) {
57 MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
58 addItem.setText(Messages.CreateDerivateContextMenu_ADD);
59 Menu addMenu = new Menu(menu);
60 addItem.setMenu(addMenu);
61 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
62 final ISelection selection = window.getActivePage().getSelection();
63 TreeNode selectedTreeNode = EditorUtil.getTreeNodeOfSelection(selection);
64 if(selectedTreeNode!=null){
65 //context menu for FieldUnit
66 if(selectedTreeNode.getValue() instanceof FieldUnit){
67 createMenuItem(addMenu, SPECIMEN, selectedTreeNode, DerivedUnit.class, SpecimenOrObservationType.PreservedSpecimen, DerivationEventType.GATHERING_IN_SITU());
68 new MenuItem(addMenu, SWT.SEPARATOR);
69 createMenuItem(addMenu, TISSUE_SAMPLE, selectedTreeNode, DnaSample.class, SpecimenOrObservationType.TissueSample, DerivationEventType.PREPARATION());
70 createMenuItem(addMenu, DNA_SAMPLE, selectedTreeNode, DnaSample.class, SpecimenOrObservationType.DnaSample, DerivationEventType.DNA_EXTRACTION());
71 createMenuItem(addMenu, MEDIA_SPECIMEN, selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.Media, DerivationEventType.ACCESSIONING());
72 return;
73 }
74 //context menu for Specimen
75 else if(selectedTreeNode.getValue() instanceof DerivedUnit && ((DerivedUnit)selectedTreeNode.getValue()).getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
76
77 createMenuItem(addMenu, TISSUE_SAMPLE, selectedTreeNode, DnaSample.class, SpecimenOrObservationType.TissueSample, DerivationEventType.PREPARATION());
78 createMenuItem(addMenu, DNA_SAMPLE, selectedTreeNode, DnaSample.class, SpecimenOrObservationType.DnaSample, DerivationEventType.DNA_EXTRACTION());
79 createMenuItem(addMenu, MEDIA_SPECIMEN, selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.Media, DerivationEventType.ACCESSIONING());
80 return;
81 }
82 //context menu for TissueSample
83 else if(selectedTreeNode.getValue() instanceof DnaSample && ((DnaSample)selectedTreeNode.getValue()).getRecordBasis()==SpecimenOrObservationType.TissueSample){
84 createMenuItem(addMenu, DNA_SAMPLE, selectedTreeNode, DnaSample.class, SpecimenOrObservationType.DnaSample, DerivationEventType.DNA_EXTRACTION());
85 createMenuItem(addMenu, MEDIA_SPECIMEN, selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.Media, DerivationEventType.ACCESSIONING());
86 return;
87 }
88 //context menu for DnaSample
89 else if(selectedTreeNode.getValue() instanceof DnaSample && ((DnaSample)selectedTreeNode.getValue()).getRecordBasis()==SpecimenOrObservationType.DnaSample){
90 createMenuItem(addMenu, CONSENSUS_SEQUENCE, selectedTreeNode, Sequence.class, null, null);
91 createMenuItem(addMenu, MEDIA_SPECIMEN, selectedTreeNode, MediaSpecimen.class, SpecimenOrObservationType.Media, DerivationEventType.ACCESSIONING());
92 return;
93 }
94 //context menu for Sequence
95 else if(selectedTreeNode.getValue() instanceof Sequence){
96 createMenuItem(addMenu, SINGLE_READ, selectedTreeNode, SingleRead.class, null, null);
97 return;
98 }
99 }
100 MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
101 menuItem.setText(Messages.CreateDerivateContextMenu_NO_CHILD_DERIVATE);
102 menuItem.setEnabled(false);
103 }
104
105
106 }
107 };
108 return contributionItems;
109 }
110
111 private void createMenuItem(Menu menu, String menuLabel, final TreeNode selectedNode, Class<?> childClass, final SpecimenOrObservationType specimenType, final DerivationEventType derivationEventType) {
112 if(specimenType!=null && specimenType.equals(SpecimenOrObservationType.Media)){
113 //media submenu
114 MenuItem mediaItem = new MenuItem(menu, SWT.CASCADE);
115 mediaItem.setText(Messages.CreateDerivateContextMenu_MEDIA);
116 Menu mediaMenu = new Menu(menu);
117 mediaItem.setMenu(mediaMenu);
118
119 //media specimen menu
120 MenuItem menuItem = new MenuItem(mediaMenu, SWT.NONE);
121 menuItem.setText(menuLabel);
122 menuItem.addSelectionListener(new WidgetSelectionListener(selectedNode, childClass, specimenType, derivationEventType));
123
124 //existing media menu
125 MenuItem existingMediaItem = new MenuItem(mediaMenu, SWT.NONE);
126 existingMediaItem.setText(Messages.CreateDerivateContextMenu_MEDIA_EXISTING);
127 existingMediaItem.addSelectionListener(new SelectionAdapter() {
128 @Override
129 public void widgetSelected(SelectionEvent e) {
130 //select media
131 Media media = MediaSelectionDialog.select(AbstractUtility.getShell(), null, null);
132 if(media!=null){
133 //create MediaSpecimen
134 MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(specimenType);
135 mediaSpecimen.setMediaSpecimen(media);
136
137 //attach to parent
138 if(selectedNode.getValue() instanceof SpecimenOrObservationBase){
139 SpecimenOrObservationBase<?> specimenOrObservationBase = (SpecimenOrObservationBase<?>)selectedNode.getValue();
140
141 specimenOrObservationBase.addDerivationEvent(DerivationEvent.NewSimpleInstance(specimenOrObservationBase, mediaSpecimen, derivationEventType));
142 mediaSpecimen.setCacheStrategy(new DerivedUnitFacadeCacheStrategy());
143 mediaSpecimen.getTitleCache(); //update title cache
144 if(CdmStore.getCurrentSessionManager().isRemoting()) {
145 CdmStore.getService(IOccurrenceService.class).merge(specimenOrObservationBase, true);
146 }
147 }
148
149 //refresh view
150 DerivateView derivateView = null;
151 if(AbstractUtility.getActivePart() instanceof DerivateView){
152 derivateView = (DerivateView) AbstractUtility.getActivePart();
153 }
154 if(derivateView!=null){
155
156 derivateView.getConversationHolder().commit();
157 derivateView.refreshTree(mediaSpecimen);
158 }
159 }
160 }
161 });
162 }
163 else{
164 MenuItem menuItem = new MenuItem(menu, SWT.NONE);
165 menuItem.setText(menuLabel);
166 menuItem.addSelectionListener(new WidgetSelectionListener(selectedNode, childClass, specimenType, derivationEventType));
167 }
168 }
169
170 private class WidgetSelectionListener extends SelectionAdapter{
171
172 private final TreeNode selectedNode;
173 //TODO: we need a common interface for the generic like e.g. "IDerivateHierarchyNode"
174 private final Class<?> childClass;
175 private final SpecimenOrObservationType specimenType;
176 private final DerivationEventType derivationEventType;
177
178 /**
179 * @param selectedNode
180 * @param specimenType
181 */
182 public WidgetSelectionListener(TreeNode selectedNode, Class<?> childClass, SpecimenOrObservationType specimenType, DerivationEventType derivationEventType) {
183 this.selectedNode = selectedNode;
184 this.childClass = childClass;
185 this.specimenType = specimenType;
186 this.derivationEventType = derivationEventType;
187 }
188
189 @Override
190 public void widgetSelected(SelectionEvent e) {
191 DerivateView derivateView = null;
192 if(AbstractUtility.getActivePart() instanceof DerivateView){
193 derivateView = (DerivateView) AbstractUtility.getActivePart();
194 }
195 if(derivateView!=null){
196 if(derivateView.isDirty()){
197 e.doit = false;
198 MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
199 return;
200 }
201 }
202
203 Object createdElement = null;
204 Object selectedDerivate = selectedNode.getValue();
205 if(selectedDerivate instanceof DnaSample && ((DnaSample) selectedDerivate).getRecordBasis()==SpecimenOrObservationType.DnaSample){
206 DnaSample dnaSample = (DnaSample)selectedDerivate;
207 if(childClass.equals(Sequence.class)){
208 Sequence newInstance = Sequence.NewInstance(dnaSample, "", 0); //$NON-NLS-1$
209 createdElement = newInstance;
210 }
211 if(CdmStore.getCurrentSessionManager().isRemoting()) {
212 CdmStore.getService(IOccurrenceService.class).merge((SpecimenOrObservationBase) selectedDerivate, true);
213 }
214 }
215 else if(selectedDerivate instanceof Sequence){
216 SingleRead newInstance = SingleRead.NewInstance();
217 ((Sequence) selectedDerivate).addSingleRead(newInstance);
218 createdElement = newInstance;
219 if(CdmStore.getCurrentSessionManager().isRemoting()) {
220 CdmApplicationState.getCurrentAppConfig().getSequenceService().merge((Sequence)selectedDerivate, true);
221 }
222 }
223 if(selectedDerivate instanceof SpecimenOrObservationBase && DerivedUnit.class.isAssignableFrom(childClass)){
224 SpecimenOrObservationBase<?> specimenOrObservationBase = (SpecimenOrObservationBase<?>)selectedDerivate;
225 DerivedUnit derivedUnit;
226 if(childClass.equals(DnaSample.class)){
227 derivedUnit = DnaSample.NewInstance();
228 if(specimenType==SpecimenOrObservationType.DnaSample){
229 derivedUnit.setRecordBasis(SpecimenOrObservationType.DnaSample);
230 }
231 else if(specimenType==SpecimenOrObservationType.TissueSample){
232 derivedUnit.setRecordBasis(SpecimenOrObservationType.TissueSample);
233 }
234 }
235 else if(childClass.equals(MediaSpecimen.class)){
236 derivedUnit = MediaSpecimen.NewInstance(specimenType);
237 //a MediaSpecimen with no media attached does not make sense. Hence we add one
238 ((MediaSpecimen)derivedUnit).setMediaSpecimen(Media.NewInstance());
239 }
240 else{
241 derivedUnit = DerivedUnit.NewInstance(specimenType);
242 }
243 specimenOrObservationBase.addDerivationEvent(DerivationEvent.NewSimpleInstance(specimenOrObservationBase, derivedUnit, derivationEventType));
244 derivedUnit.setCacheStrategy(new DerivedUnitFacadeCacheStrategy());
245 derivedUnit.getTitleCache(); //update title cache
246 createdElement = derivedUnit;
247 if(CdmStore.getCurrentSessionManager().isRemoting()) {
248 CdmStore.getService(IOccurrenceService.class).merge((SpecimenOrObservationBase) selectedDerivate, true);
249 }
250 }
251
252 //refresh view
253 if(derivateView!=null){
254 derivateView.getConversationHolder().commit();
255 derivateView.refreshTree(createdElement);
256 }
257 }
258 }
259
260 }