1f9cf8653e618ed4c106f9aa8a1bbadb090321ee
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / uses / UsesViewPart.java
1 package eu.etaxonomy.taxeditor.editor.view.uses;
2
3 import java.net.URI;
4 import java.net.URISyntaxException;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.UUID;
8
9 import org.eclipse.core.runtime.IProgressMonitor;
10 import org.eclipse.jface.action.Action;
11 import org.eclipse.jface.action.GroupMarker;
12 import org.eclipse.jface.action.IToolBarManager;
13 import org.eclipse.jface.action.MenuManager;
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.StructuredSelection;
17 import org.eclipse.jface.viewers.TreeViewer;
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.dnd.DND;
21 import org.eclipse.swt.dnd.Transfer;
22 import org.eclipse.swt.graphics.ImageData;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Menu;
26 import org.eclipse.swt.widgets.Tree;
27 import org.eclipse.ui.IEditorInput;
28 import org.eclipse.ui.IEditorPart;
29 import org.eclipse.ui.IMemento;
30 import org.eclipse.ui.IWorkbenchActionConstants;
31 import org.eclipse.ui.IWorkbenchPart;
32 import org.springframework.transaction.PlatformTransactionManager;
33 import org.springframework.transaction.TransactionStatus;
34 import org.springframework.transaction.support.DefaultTransactionDefinition;
35
36 import eu.etaxonomy.cdm.model.common.CdmBase;
37 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
38 import eu.etaxonomy.cdm.model.common.MarkerType;
39 import eu.etaxonomy.cdm.model.common.TermVocabulary;
40 import eu.etaxonomy.cdm.model.common.VocabularyEnum;
41 import eu.etaxonomy.cdm.model.description.DescriptionBase;
42 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
43 import eu.etaxonomy.cdm.model.description.Feature;
44 import eu.etaxonomy.cdm.model.description.Modifier;
45 import eu.etaxonomy.cdm.model.description.State;
46 import eu.etaxonomy.cdm.model.description.TaxonDescription;
47 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
48 import eu.etaxonomy.cdm.api.service.IDescriptionService;
49 import eu.etaxonomy.cdm.api.service.IMarkerService;
50 import eu.etaxonomy.cdm.api.service.ITermService;
51 import eu.etaxonomy.cdm.api.service.IVocabularyService;
52 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
53 import eu.etaxonomy.taxeditor.editor.EditorUtil;
54 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
55 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
56 import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermEditor;
57 import eu.etaxonomy.taxeditor.editor.key.AbstractGraphKeyEditor;
58 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptionElementDragListener;
59 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptionElementDropAdapter;
60 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptionElementTransfer;
61 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveContentProvider;
62 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveLabelProvider;
63 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewerSorter;
64 import eu.etaxonomy.taxeditor.model.ContextListenerAdapter;
65 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
66 import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
67 import eu.etaxonomy.taxeditor.model.IContextListener;
68 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
69 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
70 import eu.etaxonomy.taxeditor.model.ImageResources;
71 import eu.etaxonomy.taxeditor.newWizard.NewInstitutionWizard;
72 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
73 import eu.etaxonomy.taxeditor.store.CdmStore;
74 import eu.etaxonomy.taxeditor.store.StoreUtil;
75 import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
76
77
78
79 public class UsesViewPart extends AbstractCdmEditorViewPart implements IPartContentHasDetails, IPartContentHasSupplementalData {
80
81
82 private class ContextListener extends ContextListenerAdapter{
83 /* (non-Javadoc)
84 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
85 */
86
87 @Override
88 public void contextRefresh(IProgressMonitor monitor) {
89 monitor.subTask("Refreshing the Uses View");
90 StoreUtil.warn(getClass(), "Refreshing Uses View warn");
91 setupNecessaryItems(monitor);
92 }
93
94
95 @Override
96 public void contextStart(IMemento memento, IProgressMonitor monitor) {
97 monitor.subTask("Starting the Uses View");
98 StoreUtil.warn(getClass(), "Starting Uses View warn");
99 setupNecessaryItems(monitor);
100
101
102 }
103
104 private void setupNecessaryItems(IProgressMonitor monitor) {
105 MarkerType existingMarkertype = (MarkerType) CdmStore.getService(ITermService.class).find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
106 TermVocabulary<State> stateVocabulary = (TermVocabulary<State>) CdmStore.getService(IVocabularyService.class).find(UUID.fromString("67430d7c-fd43-4e9d-af5e-d0dca3f74931"));
107 Feature featureUseRecord = (Feature) CdmStore.getService(ITermService.class).find(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
108 TermVocabulary<Modifier> countryVocabulary = (TermVocabulary<Modifier>) CdmStore.getService(IVocabularyService.class).find(UUID.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));
109 TermVocabulary<Modifier> plantPartVocabulary = (TermVocabulary<Modifier>) CdmStore.getService(IVocabularyService.class).find(UUID.fromString("369914fe-d54b-4063-99ce-abc81d30ad35"));
110 TermVocabulary<Modifier> humanGroupVocabulary = (TermVocabulary<Modifier>) CdmStore.getService(IVocabularyService.class).find(UUID.fromString("ca46cea5-bdf7-438d-9cd8-e2793d2178dc"));
111 ConversationHolder conversation = CdmStore.createConversation();
112 if (existingMarkertype == null) {
113 //existingMarkertype.set("use")
114 //existingMarkertype.setLabel("use");
115 //MarkerType useMarkerType = MarkerType.NewInstance("use", "use", null);
116 //ConversationHolder conversation = CdmStore.createConversation();
117 existingMarkertype = MarkerType.NewInstance("use", "use", null);
118 existingMarkertype.setUuid( UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
119 //CdmStore.getService(ITermService.class).save(existingMarkertype);
120 //saveOrUpdate(existingMarkertype);
121 //TermVocabulary<MarkerType> markerTypeVocabulary = (TermVocabulary<MarkerType>)CdmStore.getService(IVocabularyService.class).getVocabulary(VocabularyEnum.MarkerType);
122 TermVocabulary<MarkerType> markerTypeVocabulary = (TermVocabulary<MarkerType>)CdmStore.getService(IVocabularyService.class).find((UUID.fromString("19dffff7-e142-429c-a420-5d28e4ebe305")));
123 //(UUID.fromString("19dffff7-e142-429c-a420-5d28e4ebe305"));
124 //CdmStore.
125
126 markerTypeVocabulary.addTerm(existingMarkertype);
127 CdmStore.getService(IVocabularyService.class).saveOrUpdate(markerTypeVocabulary);
128 conversation.commit(true);
129 //conversation.close();
130 }
131 if (stateVocabulary == null) {
132 monitor.subTask("stateVocabulary empty");
133 URI termSourceUri = null;
134 try {
135 termSourceUri = new URI("eu.etaxonomy.cdm.model.description.State");
136 } catch (URISyntaxException e) {
137 // TODO Auto-generated catch block
138 e.printStackTrace();
139 }
140 stateVocabulary = TermVocabulary.NewInstance("Use Category", "Use Category", null, termSourceUri);
141 stateVocabulary.setUuid(UUID.fromString("67430d7c-fd43-4e9d-af5e-d0dca3f74931"));
142 State newDummyState = State.NewInstance("Dummy Use Cat", "Dummy Use Cat", null);
143 State newDummySubCat = State.NewInstance("Dummy Use Sub Cat", "Dummy Use Sub Cat", null);
144 newDummyState.addIncludes(newDummySubCat);
145 stateVocabulary.addTerm(newDummyState);
146 CdmStore.getService(IVocabularyService.class).saveOrUpdate(stateVocabulary);
147 conversation.commit(true);
148 }
149 if (countryVocabulary == null) {
150 monitor.subTask("countryVocabulary empty");
151 URI termSourceUri = null;
152 try {
153 termSourceUri = new URI("eu.etaxonomy.cdm.model.description.Modifier");
154 } catch (URISyntaxException e) {
155 // TODO Auto-generated catch block
156 e.printStackTrace();
157 }
158 countryVocabulary = TermVocabulary.NewInstance("Country", "Country", null, termSourceUri);
159 countryVocabulary.setUuid(UUID.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));
160
161 Modifier newDummyModifier = Modifier.NewInstance("Dummy Country", "Dummy Country", null);
162 countryVocabulary.addTerm(newDummyModifier);
163 CdmStore.getService(IVocabularyService.class).saveOrUpdate(countryVocabulary);
164 conversation.commit(true);
165 }
166 if (plantPartVocabulary == null) {
167 monitor.subTask("plantPartVocabulary empty");
168 URI termSourceUri = null;
169 try {
170 termSourceUri = new URI("eu.etaxonomy.cdm.model.description.Modifier");
171 } catch (URISyntaxException e) {
172 // TODO Auto-generated catch block
173 e.printStackTrace();
174 }
175 plantPartVocabulary = TermVocabulary.NewInstance("Plant Part", "Plant Part", null, termSourceUri);
176 plantPartVocabulary.setUuid(UUID.fromString("369914fe-d54b-4063-99ce-abc81d30ad35"));
177 Modifier newDummyModifier = Modifier.NewInstance("Dummy Plant Part", "Dummy Plant Part", null);
178 plantPartVocabulary.addTerm(newDummyModifier);
179 CdmStore.getService(IVocabularyService.class).saveOrUpdate(plantPartVocabulary);
180 conversation.commit(true);
181 }
182 if (humanGroupVocabulary == null) {
183 monitor.subTask("humanGroupVocabulary empty");
184 URI termSourceUri = null;
185 try {
186 termSourceUri = new URI("eu.etaxonomy.cdm.model.description.Modifier");
187 } catch (URISyntaxException e) {
188 // TODO Auto-generated catch block
189 e.printStackTrace();
190 }
191 humanGroupVocabulary = TermVocabulary.NewInstance("Human Group", "Human Group", null, termSourceUri);
192 humanGroupVocabulary.setUuid(UUID.fromString("ca46cea5-bdf7-438d-9cd8-e2793d2178dc"));
193 Modifier newDummyModifier = Modifier.NewInstance("Dummy Human Group", "Dummy Human Group", null);
194 Modifier newSubDummyModifier = Modifier.NewInstance("Dummy Ethnic Group", "Dummy Ethnic Group", null);
195 newDummyModifier.addIncludes(newSubDummyModifier);
196 humanGroupVocabulary.addTerm(newDummyModifier);
197 CdmStore.getService(IVocabularyService.class).saveOrUpdate(humanGroupVocabulary);
198 conversation.commit(true);
199 }
200 if(featureUseRecord == null) {
201 //Feature usesFeature = (Feature) CdmStore.getService(ITermService.class).find(UUID.fromString("e5374d39-b210-47c7-bec1-bee05b5f1cb6"));
202 featureUseRecord = Feature.NewInstance("Use Record", "Use Record", null);
203 featureUseRecord.setUuid(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
204 featureUseRecord.setSupportsCategoricalData(true);
205 //featureUseRecord
206 //featureUseRecord.setLabel("Use Record");
207 //featureUseRecord.
208 TermVocabulary<Feature> featureVocabulary = (TermVocabulary<Feature>)CdmStore.getService(IVocabularyService.class).find((UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
209 featureVocabulary.addTerm(featureUseRecord);
210 CdmStore.getService(IVocabularyService.class).saveOrUpdate(featureVocabulary);
211 conversation.commit(true);
212
213 }
214
215
216 conversation.close();
217 }
218 /*public void contextStop(IMemento memento, IProgressMonitor monitor) {
219 monitor.subTask("Getting rid of search results");
220 NavigationUtil.hideView(SearchResultView.this);
221 }*/
222 }
223
224 public static final String ID = "eu.etaxonomy.taxeditor.editor.view.uses";
225 private TreeViewer viewer;
226 private Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache = new HashMap<TaxonDescription, FeatureNodeContainerTree>();
227 private ToggleDescriptionAction showAllElementsAction;
228 private ToggleDescriptionAction hideAllElementsAction;
229 private IContextListener contextListener;
230
231 private int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
232
233 /*@Override
234 public void createPartControl(Composite parent){
235 contextListener = new ContextListener();
236 CdmStore.getContextManager().addContextListener(contextListener);
237 }*/
238
239
240 /** {@inheritDoc} */
241 @Override
242 public void createViewer(Composite parent) {
243
244 contextListener = new ContextListener();
245 CdmStore.getContextManager().addContextListener(contextListener);
246
247 viewer = new TreeViewer(new Tree(parent, SWT.MULTI | SWT.H_SCROLL
248 | SWT.V_SCROLL | SWT.FULL_SELECTION));
249
250 viewer.setContentProvider(new UsesContentProvider(featureNodeContainerCache));
251 //viewer.setContentProvider(new DescriptiveContentProvider(featureNodeContainerCache));
252
253 //viewer.setInput(descService.listTaxonDescriptions(Taxon, null, null, null, null, null, null));
254
255 viewer.setLabelProvider(new UsesLabelProvider());
256
257 //viewer.setSorter(new DescriptiveViewerSorter());
258
259 viewer.setAutoExpandLevel(2);
260
261
262
263 Transfer[] transfers = new Transfer[] { DescriptionElementTransfer.getInstance() };
264 /*viewer.addDragSupport(dndOperations, transfers, new DescriptionElementDragListener(
265 this));
266 viewer.addDropSupport(dndOperations, transfers,
267 new DescriptionElementDropAdapter(viewer));*/
268
269 // Propagate selection from viewer
270 getSite().setSelectionProvider(viewer);
271
272 showAllElementsAction = new ToggleDescriptionAction(false);
273 hideAllElementsAction = new ToggleDescriptionAction(true);
274
275 // Add context menu to tree
276 createMenu();
277
278 createToolbar();
279
280 // set initial input
281 // if(getEditor() != null){
282 // viewer.setInput(getEditor().getEditorInput());
283 // }
284 }
285
286
287 /* (non-Javadoc)
288 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
289 */
290 /** {@inheritDoc} */
291 @Override
292 protected ISelection getInitialSelection() {
293 if(getEditor() != null){
294 return new StructuredSelection(getEditor().getEditorInput());
295 }
296
297 return super.getInitialSelection();
298 }
299
300 private void createToolbar() {
301 IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
302 toolBarManager.add(showAllElementsAction);
303 toolBarManager.add(hideAllElementsAction);
304 }
305
306
307 private void createMenu(){
308 MenuManager menuManager = new MenuManager();
309 menuManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
310
311 getSite().registerContextMenu(menuManager, viewer);
312
313 // FIXME for some reason this is not working
314 menuManager.add(showAllElementsAction);
315 menuManager.add(hideAllElementsAction);
316
317 Control control = viewer.getControl();
318 Menu menu = menuManager.createContextMenu(control);
319
320 control.setMenu(menu);
321 }
322
323
324
325 @Override
326 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
327 if(EditorUtil.getActiveEditor() == null){
328 showEmptyPage();
329 return;
330 }
331
332 if(part instanceof BulkEditor){
333 showEmptyPage();
334 return;
335 }
336
337 if(part instanceof AbstractGraphKeyEditor){
338 showEmptyPage();
339 return;
340 }
341
342 /*if(part instanceof DefinedTermEditor){
343 showEmptyPage();
344 return;
345 }*/
346
347 if(part instanceof MultiPageTaxonEditor){
348 if(! part.equals(this.part)){
349 //TaxonEditorInput input = (TaxonEditorInput)((IEditorPart) part).getEditorInput();
350 IEditorInput input = ((IEditorPart) part).getEditorInput();
351 featureNodeContainerCache.clear();
352 //showViewer(part, new StructuredSelection(CdmStore.getService(IDescriptionService.class).listTaxonDescriptions(input.getTaxonNode().getTaxon(), null, null, null, null, null, null)));
353 showViewer(part, new StructuredSelection(input));
354 }
355 showViewer();
356 }
357
358 }
359
360
361
362 /*@Override
363 public void showViewer(IWorkbenchPart part, IStructuredSelection selection) {
364 this.part = part;
365
366 Object element = selection.getFirstElement();
367
368 getViewer().setInput(descService.listTaxonDescriptions((Taxon) element, null, null, null, null, null, null));
369
370 showViewer();
371 }*/
372
373
374
375 private class ToggleDescriptionAction extends Action{
376 private boolean expanded;
377
378 public ToggleDescriptionAction(boolean expand){
379 super(null, Action.AS_PUSH_BUTTON);
380 expanded = expand;
381 setImageAndTooltip();
382 }
383
384 private void setImageAndTooltip(){
385 setImageDescriptor(new ImageDescriptor() {
386 @Override
387 public ImageData getImageData() {
388 setText(expanded ? "Collapse All" : "Expand All");
389 String resource = expanded ? ImageResources.COLLAPSE_ALL : ImageResources.EXPAND_ALL;
390 return ImageResources.getImage(resource).getImageData();
391 }
392 });
393
394 String toolTipText = expanded ? "Collapse all" : "Show all descriptive data";
395 setToolTipText(toolTipText);
396 }
397
398 @Override
399 public void run() {
400 if(expanded){
401 viewer.collapseAll();
402 }else{
403 viewer.expandAll();
404 }
405 setImageAndTooltip();
406 }
407 }
408
409 /** {@inheritDoc} */
410 @Override
411 public boolean postOperation(CdmBase objectAffectedByOperation) {
412 viewer.refresh();
413 return super.postOperation(objectAffectedByOperation);
414 }
415
416 /** {@inheritDoc} */
417 @Override
418 public void changed(Object object) {
419 for(FeatureNodeContainerTree containerTree : featureNodeContainerCache.values()){
420 containerTree.buildTree();
421 }
422 viewer.refresh();
423
424 if(object instanceof DescriptionElementBase){
425 DescriptionElementBase descriptionElement = (DescriptionElementBase) object;
426 DescriptionBase description = descriptionElement.getInDescription();
427 FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
428
429 FeatureNodeContainer featureNodeContainer = containerTree.getFeatureNodeContainerForDescriptionElement(descriptionElement);
430 viewer.expandToLevel(featureNodeContainer, 1);
431
432 }
433
434 if(object != null){
435 StructuredSelection selection = new StructuredSelection(object);
436 viewer.setSelection(selection, true);
437 }
438
439 super.changed(object);
440 }
441
442
443 /** {@inheritDoc} */
444 @Override
445 public Viewer getViewer() {
446 return viewer;
447 }
448
449 /**
450 * <p>onComplete</p>
451 *
452 * @return a boolean.
453 */
454 @Override
455 public boolean onComplete() {
456 return false;
457 }
458
459 }