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