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 public class UsesViewPart
extends AbstractCdmEditorViewPart
implements IPartContentHasDetails
, IPartContentHasSupplementalData
{
82 private class ContextListener
extends ContextListenerAdapter
{
84 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
88 public void contextRefresh(IProgressMonitor monitor
) {
89 monitor
.subTask("Refreshing the Uses View");
90 StoreUtil
.warn(getClass(), "Refreshing Uses View warn");
91 setupNecessaryItems(monitor
);
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
);
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"));
126 markerTypeVocabulary
.addTerm(existingMarkertype
);
127 CdmStore
.getService(IVocabularyService
.class).saveOrUpdate(markerTypeVocabulary
);
128 conversation
.commit(true);
129 //conversation.close();
131 if (stateVocabulary
== null) {
132 monitor
.subTask("stateVocabulary empty");
133 URI termSourceUri
= null;
135 termSourceUri
= new URI("eu.etaxonomy.cdm.model.description.State");
136 } catch (URISyntaxException e
) {
137 // TODO Auto-generated catch block
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);
149 if (countryVocabulary
== null) {
150 monitor
.subTask("countryVocabulary empty");
151 URI termSourceUri
= null;
153 termSourceUri
= new URI("eu.etaxonomy.cdm.model.description.Modifier");
154 } catch (URISyntaxException e
) {
155 // TODO Auto-generated catch block
158 countryVocabulary
= TermVocabulary
.NewInstance("Country", "Country", null, termSourceUri
);
159 countryVocabulary
.setUuid(UUID
.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));
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);
166 if (plantPartVocabulary
== null) {
167 monitor
.subTask("plantPartVocabulary empty");
168 URI termSourceUri
= null;
170 termSourceUri
= new URI("eu.etaxonomy.cdm.model.description.Modifier");
171 } catch (URISyntaxException e
) {
172 // TODO Auto-generated catch block
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);
182 if (humanGroupVocabulary
== null) {
183 monitor
.subTask("humanGroupVocabulary empty");
184 URI termSourceUri
= null;
186 termSourceUri
= new URI("eu.etaxonomy.cdm.model.description.Modifier");
187 } catch (URISyntaxException e
) {
188 // TODO Auto-generated catch block
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);
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);
206 //featureUseRecord.setLabel("Use Record");
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);
216 conversation
.close();
218 /*public void contextStop(IMemento memento, IProgressMonitor monitor) {
219 monitor.subTask("Getting rid of search results");
220 NavigationUtil.hideView(SearchResultView.this);
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
;
231 private int dndOperations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
234 public void createPartControl(Composite parent){
235 contextListener = new ContextListener();
236 CdmStore.getContextManager().addContextListener(contextListener);
242 public void createViewer(Composite parent
) {
244 contextListener
= new ContextListener();
245 CdmStore
.getContextManager().addContextListener(contextListener
);
247 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
248 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
250 viewer
.setContentProvider(new UsesContentProvider(featureNodeContainerCache
));
251 //viewer.setContentProvider(new DescriptiveContentProvider(featureNodeContainerCache));
253 //viewer.setInput(descService.listTaxonDescriptions(Taxon, null, null, null, null, null, null));
255 viewer
.setLabelProvider(new UsesLabelProvider());
257 //viewer.setSorter(new DescriptiveViewerSorter());
259 viewer
.setAutoExpandLevel(2);
263 Transfer
[] transfers
= new Transfer
[] { DescriptionElementTransfer
.getInstance() };
264 /*viewer.addDragSupport(dndOperations, transfers, new DescriptionElementDragListener(
266 viewer.addDropSupport(dndOperations, transfers,
267 new DescriptionElementDropAdapter(viewer));*/
269 // Propagate selection from viewer
270 getSite().setSelectionProvider(viewer
);
272 showAllElementsAction
= new ToggleDescriptionAction(false);
273 hideAllElementsAction
= new ToggleDescriptionAction(true);
275 // Add context menu to tree
281 // if(getEditor() != null){
282 // viewer.setInput(getEditor().getEditorInput());
288 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
292 protected ISelection
getInitialSelection() {
293 if(getEditor() != null){
294 return new StructuredSelection(getEditor().getEditorInput());
297 return super.getInitialSelection();
300 private void createToolbar() {
301 IToolBarManager toolBarManager
= getViewSite().getActionBars().getToolBarManager();
302 toolBarManager
.add(showAllElementsAction
);
303 toolBarManager
.add(hideAllElementsAction
);
307 private void createMenu(){
308 MenuManager menuManager
= new MenuManager();
309 menuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
311 getSite().registerContextMenu(menuManager
, viewer
);
313 // FIXME for some reason this is not working
314 menuManager
.add(showAllElementsAction
);
315 menuManager
.add(hideAllElementsAction
);
317 Control control
= viewer
.getControl();
318 Menu menu
= menuManager
.createContextMenu(control
);
320 control
.setMenu(menu
);
326 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
327 if(EditorUtil
.getActiveEditor() == null){
332 if(part
instanceof BulkEditor
){
337 if(part
instanceof AbstractGraphKeyEditor
){
342 /*if(part instanceof DefinedTermEditor){
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
));
363 public void showViewer(IWorkbenchPart part, IStructuredSelection selection) {
366 Object element = selection.getFirstElement();
368 getViewer().setInput(descService.listTaxonDescriptions((Taxon) element, null, null, null, null, null, null));
375 private class ToggleDescriptionAction
extends Action
{
376 private boolean expanded
;
378 public ToggleDescriptionAction(boolean expand
){
379 super(null, Action
.AS_PUSH_BUTTON
);
381 setImageAndTooltip();
384 private void setImageAndTooltip(){
385 setImageDescriptor(new ImageDescriptor() {
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();
394 String toolTipText
= expanded ?
"Collapse all" : "Show all descriptive data";
395 setToolTipText(toolTipText
);
401 viewer
.collapseAll();
405 setImageAndTooltip();
411 public boolean postOperation(CdmBase objectAffectedByOperation
) {
413 return super.postOperation(objectAffectedByOperation
);
418 public void changed(Object object
) {
419 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
420 containerTree
.buildTree();
424 if(object
instanceof DescriptionElementBase
){
425 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
426 DescriptionBase description
= descriptionElement
.getInDescription();
427 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
429 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
430 viewer
.expandToLevel(featureNodeContainer
, 1);
435 StructuredSelection selection
= new StructuredSelection(object
);
436 viewer
.setSelection(selection
, true);
439 super.changed(object
);
445 public Viewer
getViewer() {
455 public boolean onComplete() {