1 package eu
.etaxonomy
.taxeditor
.editor
.view
.uses
;
4 import java
.net
.URISyntaxException
;
5 import java
.util
.HashMap
;
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
;
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
;
79 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
82 * @created mar 13, 2012
85 public class UsesViewPart
extends AbstractCdmEditorViewPart
implements IPartContentHasDetails
, IPartContentHasSupplementalData
{
88 private class ContextListener
extends ContextListenerAdapter
{
90 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
94 public void contextRefresh(IProgressMonitor monitor
) {
95 monitor
.subTask("Refreshing the Uses View");
96 StoreUtil
.warn(getClass(), "Refreshing Uses View warn");
97 setupNecessaryItems(monitor
);
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
);
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);
126 if (stateVocabulary
== null) {
127 monitor
.subTask("stateVocabulary empty");
128 URI termSourceUri
= null;
130 termSourceUri
= new URI("eu.etaxonomy.cdm.model.description.State");
131 } catch (URISyntaxException e
) {
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);
143 if (countryVocabulary
== null) {
144 monitor
.subTask("countryVocabulary empty");
145 URI termSourceUri
= null;
147 termSourceUri
= new URI("eu.etaxonomy.cdm.model.description.Modifier");
148 } catch (URISyntaxException e
) {
151 countryVocabulary
= TermVocabulary
.NewInstance("Country", "Country", null, termSourceUri
);
152 countryVocabulary
.setUuid(UUID
.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));
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);
159 if (plantPartVocabulary
== null) {
160 monitor
.subTask("plantPartVocabulary empty");
161 URI termSourceUri
= null;
163 termSourceUri
= new URI("eu.etaxonomy.cdm.model.description.Modifier");
164 } catch (URISyntaxException e
) {
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);
174 if (humanGroupVocabulary
== null) {
175 monitor
.subTask("humanGroupVocabulary empty");
176 URI termSourceUri
= null;
178 termSourceUri
= new URI("eu.etaxonomy.cdm.model.description.Modifier");
179 } catch (URISyntaxException e
) {
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);
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);
203 conversation
.close();
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
;
214 private int dndOperations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
217 public void createPartControl(Composite parent){
218 contextListener = new ContextListener();
219 CdmStore.getContextManager().addContextListener(contextListener);
225 public void createViewer(Composite parent
) {
227 contextListener
= new ContextListener();
228 CdmStore
.getContextManager().addContextListener(contextListener
);
230 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
231 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
233 viewer
.setContentProvider(new UsesContentProvider(featureNodeContainerCache
));
234 //viewer.setContentProvider(new DescriptiveContentProvider(featureNodeContainerCache));
236 //viewer.setInput(descService.listTaxonDescriptions(Taxon, null, null, null, null, null, null));
238 viewer
.setLabelProvider(new UsesLabelProvider());
240 //viewer.setSorter(new DescriptiveViewerSorter());
242 viewer
.setAutoExpandLevel(2);
246 Transfer
[] transfers
= new Transfer
[] { DescriptionElementTransfer
.getInstance() };
247 /*viewer.addDragSupport(dndOperations, transfers, new DescriptionElementDragListener(
249 viewer.addDropSupport(dndOperations, transfers,
250 new DescriptionElementDropAdapter(viewer));*/
252 // Propagate selection from viewer
253 getSite().setSelectionProvider(viewer
);
255 showAllElementsAction
= new ToggleDescriptionAction(false);
256 hideAllElementsAction
= new ToggleDescriptionAction(true);
258 // Add context menu to tree
264 // if(getEditor() != null){
265 // viewer.setInput(getEditor().getEditorInput());
271 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
275 protected ISelection
getInitialSelection() {
276 if(getEditor() != null){
277 return new StructuredSelection(getEditor().getEditorInput());
280 return super.getInitialSelection();
283 private void createToolbar() {
284 IToolBarManager toolBarManager
= getViewSite().getActionBars().getToolBarManager();
285 toolBarManager
.add(showAllElementsAction
);
286 toolBarManager
.add(hideAllElementsAction
);
290 private void createMenu(){
291 MenuManager menuManager
= new MenuManager();
292 menuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
294 getSite().registerContextMenu(menuManager
, viewer
);
296 // FIXME for some reason this is not working
297 menuManager
.add(showAllElementsAction
);
298 menuManager
.add(hideAllElementsAction
);
300 Control control
= viewer
.getControl();
301 Menu menu
= menuManager
.createContextMenu(control
);
303 control
.setMenu(menu
);
309 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
310 if(EditorUtil
.getActiveEditor() == null){
315 if(part
instanceof BulkEditor
){
320 if(part
instanceof AbstractGraphKeyEditor
){
325 /*if(part instanceof DefinedTermEditor){
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
));
346 public void showViewer(IWorkbenchPart part, IStructuredSelection selection) {
349 Object element = selection.getFirstElement();
351 getViewer().setInput(descService.listTaxonDescriptions((Taxon) element, null, null, null, null, null, null));
358 private class ToggleDescriptionAction
extends Action
{
359 private boolean expanded
;
361 public ToggleDescriptionAction(boolean expand
){
362 super(null, Action
.AS_PUSH_BUTTON
);
364 setImageAndTooltip();
367 private void setImageAndTooltip(){
368 setImageDescriptor(new ImageDescriptor() {
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();
377 String toolTipText
= expanded ?
"Collapse all" : "Show all descriptive data";
378 setToolTipText(toolTipText
);
384 viewer
.collapseAll();
388 setImageAndTooltip();
394 public boolean postOperation(CdmBase objectAffectedByOperation
) {
396 return super.postOperation(objectAffectedByOperation
);
401 public void changed(Object object
) {
402 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
403 containerTree
.buildTree();
407 if(object
instanceof DescriptionElementBase
){
408 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
409 DescriptionBase description
= descriptionElement
.getInDescription();
410 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
412 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
413 viewer
.expandToLevel(featureNodeContainer
, 1);
418 StructuredSelection selection
= new StructuredSelection(object
);
419 viewer
.setSelection(selection
, true);
422 super.changed(object
);
428 public Viewer
getViewer() {
438 public boolean onComplete() {