2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.featuretree
.e4
;
12 import java
.util
.Arrays
;
13 import java
.util
.HashMap
;
14 import java
.util
.List
;
17 import javax
.annotation
.PostConstruct
;
18 import javax
.annotation
.PreDestroy
;
19 import javax
.inject
.Inject
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.e4
.ui
.di
.Focus
;
23 import org
.eclipse
.e4
.ui
.di
.Persist
;
24 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
25 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
26 import org
.eclipse
.e4
.ui
.services
.EMenuService
;
27 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
28 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
29 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
30 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
31 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
32 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
33 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
34 import org
.eclipse
.jface
.viewers
.TreeViewer
;
35 import org
.eclipse
.swt
.dnd
.DND
;
36 import org
.eclipse
.swt
.dnd
.Transfer
;
37 import org
.eclipse
.swt
.layout
.FillLayout
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.ui
.IMemento
;
41 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
42 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
43 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureTreeService
;
44 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
45 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
46 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
47 import eu
.etaxonomy
.taxeditor
.editor
.definedterm
.TermTransfer
;
48 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureNodeTransfer
;
49 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureTreeContentProvider
;
50 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureTreeLabelProvider
;
51 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
52 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
53 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
54 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
55 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
56 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
57 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
58 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
59 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
60 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4ViewerPart
;
68 public class FeatureTreeEditor
implements ICdmEntitySessionEnabled
, ISelectionChangedListener
,
69 IE4ViewerPart
, IE4SavablePart
, IPartContentHasDetails
, IPartContentHasSupplementalData
,
70 IContextListener
, IConversationEnabled
, IDirtyMarkable
, IPostOperationEnabled
{
72 private ConversationHolder conversation
;
74 private ICdmEntitySession cdmEntitySession
;
77 private ESelectionService selService
;
80 private MDirtyable dirty
;
83 private MPart thisPart
;
85 private TreeViewer viewer
;
88 public FeatureTreeEditor() {
89 CdmStore
.getContextManager().addContextListener(this);
93 public void createControl(Composite parent
, EMenuService menuService
){
94 if (CdmStore
.isActive()){
100 parent
.setLayout(new FillLayout());
101 viewer
= new TreeViewer(parent
);
102 viewer
.setContentProvider(new FeatureTreeContentProvider());
103 viewer
.setLabelProvider(new FeatureTreeLabelProvider());
105 int ops
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
106 Transfer
[] transfers
= new Transfer
[] {
107 FeatureNodeTransfer
.getInstance(),
108 TermTransfer
.getInstance(),
109 LocalSelectionTransfer
.getTransfer()};
110 viewer
.addDragSupport(ops
, transfers
, new FeatureNodeDragListener(viewer
));
111 viewer
.addDropSupport(ops
, transfers
, new FeatureNodeDropAdapter(dirty
, this, viewer
));
112 viewer
.addSelectionChangedListener(this);
114 List
<FeatureTree
> trees
= CdmStore
.getService(IFeatureTreeService
.class).list(FeatureTree
.class, null, null, null, null);
115 viewer
.setInput(trees
);
117 //create context menu
118 menuService
.registerContextMenu(viewer
.getControl(), "eu.etaxonomy.taxeditor.store.popupmenu.featureTreeEditor");
121 private void initSession(){
122 if(conversation
== null){
123 conversation
= CdmStore
.createConversation();
125 if(cdmEntitySession
==null){
126 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
130 private void clearSession() {
131 if(conversation
!=null){
132 conversation
.close();
135 if(cdmEntitySession
!= null) {
136 cdmEntitySession
.dispose();
137 cdmEntitySession
= null;
139 dirty
.setDirty(false);
142 public void setDirty(boolean isDirty
){
143 this.dirty
.setDirty(isDirty
);
147 public boolean isDirty(){
148 return dirty
.isDirty();
152 public void selectionChanged(SelectionChangedEvent event
) {
153 //propagate selection
154 selService
.setSelection(event
.getSelection());
160 viewer
.getControl().setFocus();
162 if(conversation
!=null && !conversation
.isBound()){
165 if(cdmEntitySession
!= null) {
166 cdmEntitySession
.bind();
171 public void refresh(){
175 public TreeViewer
getViewer(){
180 public IStructuredSelection
getSelection() {
181 return (IStructuredSelection
) viewer
.getSelection();
185 public ConversationHolder
getConversationHolder() {
191 public void save(IProgressMonitor monitor
){
192 if (!conversation
.isBound()) {
196 // commit the conversation and start a new transaction immediately
197 conversation
.commit(true);
199 CdmStore
.getService(IFeatureTreeService
.class).saveOrUpdate(getRootEntities());
201 initialiseRootEntities();
203 this.setDirty(false);
206 private void initialiseRootEntities() {
207 Object
[] expandedElements
= viewer
.getExpandedElements();
208 viewer
.getTree().removeAll();
209 List
<FeatureTree
> trees
= CdmStore
.getService(IFeatureTreeService
.class).list(FeatureTree
.class, null, null, null, null);
210 viewer
.setInput(trees
);
211 viewer
.setExpandedElements(expandedElements
);
215 public void dispose(){
216 selService
.setSelection(null);
221 public ICdmEntitySession
getCdmEntitySession() {
222 return cdmEntitySession
;
226 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
227 List
<String
> propertyPaths
= Arrays
.asList(new String
[] {
228 "children", //$NON-NLS-1$
229 "feature", //$NON-NLS-1$
230 "featureTree", //$NON-NLS-1$
232 Map
<Object
, List
<String
>> propertyPathMap
=
233 new HashMap
<Object
, List
<String
>>();
234 propertyPathMap
.put(FeatureNode
.class,propertyPaths
);
235 return propertyPathMap
;
239 public List
<FeatureTree
> getRootEntities() {
240 return (List
<FeatureTree
>) viewer
.getInput();
244 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
248 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
249 //close view when workbench closes
251 thisPart
.getContext().get(EPartService
.class).hidePart(thisPart
);
259 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
263 public void contextRefresh(IProgressMonitor monitor
) {
267 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {
271 public void update(CdmDataChangeMap arg0
) {
275 public void changed(Object element
) {
276 dirty
.setDirty(true);
281 public void forceDirty() {
282 dirty
.setDirty(true);
286 public boolean postOperation(Object objectAffectedByOperation
) {
287 initialiseRootEntities();
289 if(objectAffectedByOperation
instanceof FeatureNode
){
290 FeatureNode node
= (FeatureNode
)objectAffectedByOperation
;
291 viewer
.expandToLevel(node
.getFeatureTree(), 1);
292 StructuredSelection selection
= new StructuredSelection(objectAffectedByOperation
);
293 viewer
.setSelection(selection
);
299 public boolean onComplete() {