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
.Collections
;
14 import java
.util
.HashMap
;
15 import java
.util
.List
;
18 import javax
.annotation
.PostConstruct
;
19 import javax
.annotation
.PreDestroy
;
20 import javax
.inject
.Inject
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.e4
.ui
.di
.Focus
;
24 import org
.eclipse
.e4
.ui
.di
.Persist
;
25 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
26 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
27 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
28 import org
.eclipse
.e4
.ui
.services
.EMenuService
;
29 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
30 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
31 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
32 import org
.eclipse
.jface
.viewers
.ISelection
;
33 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
34 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
35 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
36 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
37 import org
.eclipse
.jface
.viewers
.TreeViewer
;
38 import org
.eclipse
.swt
.SWT
;
39 import org
.eclipse
.swt
.dnd
.DND
;
40 import org
.eclipse
.swt
.dnd
.Transfer
;
41 import org
.eclipse
.swt
.events
.KeyAdapter
;
42 import org
.eclipse
.swt
.events
.KeyEvent
;
43 import org
.eclipse
.swt
.layout
.FillLayout
;
44 import org
.eclipse
.swt
.widgets
.Composite
;
45 import org
.eclipse
.ui
.IMemento
;
47 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
48 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
49 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureTreeService
;
50 import eu
.etaxonomy
.cdm
.model
.term
.FeatureNode
;
51 import eu
.etaxonomy
.cdm
.model
.term
.FeatureTree
;
52 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
53 import eu
.etaxonomy
.taxeditor
.editor
.definedterm
.TermTransfer
;
54 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureNodeTransfer
;
55 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureTreeContentProvider
;
56 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureTreeLabelProvider
;
57 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.operation
.AddFeatureOperation
;
58 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
59 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
60 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
61 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
62 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
63 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
64 import eu
.etaxonomy
.taxeditor
.store
.AppModelId
;
65 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
66 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
67 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4ViewerPart
;
75 public class FeatureTreeEditor
implements IFeatureTreeEditor
, ISelectionChangedListener
,
76 IE4ViewerPart
, IPartContentHasDetails
, IPartContentHasSupplementalData
,
77 IContextListener
, IConversationEnabled
, IDirtyMarkable
{
79 private ConversationHolder conversation
;
81 private ICdmEntitySession cdmEntitySession
;
84 private ESelectionService selService
;
87 private MDirtyable dirty
;
90 private UISynchronize sync
;
93 private MPart thisPart
;
95 private TreeViewer viewer
;
98 public FeatureTreeEditor() {
99 CdmStore
.getContextManager().addContextListener(this);
103 public void createControl(Composite parent
, EMenuService menuService
){
104 if (CdmStore
.isActive()){
110 parent
.setLayout(new FillLayout());
111 viewer
= new TreeViewer(parent
);
112 viewer
.setContentProvider(new FeatureTreeContentProvider());
113 viewer
.setLabelProvider(new FeatureTreeLabelProvider());
115 int ops
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
116 Transfer
[] transfers
= new Transfer
[] {
117 FeatureNodeTransfer
.getInstance(),
118 TermTransfer
.getInstance(),
119 LocalSelectionTransfer
.getTransfer()};
120 viewer
.addDragSupport(ops
, transfers
, new FeatureNodeDragListener(viewer
));
121 viewer
.addDropSupport(ops
, transfers
, new FeatureTreeDropAdapter(this, viewer
, sync
));
122 viewer
.addSelectionChangedListener(this);
123 viewer
.getTree().addKeyListener(new KeyAdapter() {
125 public void keyPressed(KeyEvent e
) {
126 if(e
.stateMask
== SWT
.MOD1
&& e
.keyCode
== 'c'){
127 copy(viewer
.getStructuredSelection());
129 else if(e
.stateMask
== SWT
.MOD1
&& e
.keyCode
== 'v'){
130 paste(viewer
.getStructuredSelection());
135 List
<FeatureTree
> trees
= CdmStore
.getService(IFeatureTreeService
.class).list(FeatureTree
.class, null, null, null, null);
136 Collections
.sort(trees
, (tree1
, tree2
) -> tree1
.getTitleCache().compareTo(tree2
.getTitleCache()));
137 viewer
.setInput(trees
);
139 //create context menu
140 menuService
.registerContextMenu(viewer
.getControl(), AppModelId
.POPUPMENU_EU_ETAXONOMY_TAXEDITOR_STORE_POPUPMENU_FEATURETREEEDITOR
);
143 public void paste(IStructuredSelection selection
) {
144 if (StoreUtil
.promptCheckIsDirty(this)) {
148 ISelection clipBoardSelection
= LocalSelectionTransfer
.getTransfer().getSelection();
149 Object firstElement
= selection
.getFirstElement();
150 FeatureNode parentNode
= null;
151 if(firstElement
instanceof FeatureNode
){
152 parentNode
= (FeatureNode
) firstElement
;
154 else if(firstElement
instanceof FeatureTree
){
155 parentNode
= ((FeatureTree
)firstElement
).getRoot();
157 if(parentNode
!=null){
158 FeatureNode copiedNode
= (FeatureNode
) ((IStructuredSelection
)clipBoardSelection
).getFirstElement();
160 AddFeatureOperation operation
= new AddFeatureOperation(copiedNode
.getTerm().getUuid(), parentNode
, this, this);
161 AbstractUtility
.executeOperation(operation
, sync
);
166 public void copy(IStructuredSelection selection
) {
167 LocalSelectionTransfer
.getTransfer().setSelection(selection
);
170 private void initSession(){
171 if(conversation
== null){
172 conversation
= CdmStore
.createConversation();
174 if(cdmEntitySession
==null){
175 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
179 private void clearSession() {
180 if(conversation
!=null){
181 conversation
.close();
184 if(cdmEntitySession
!= null) {
185 cdmEntitySession
.dispose();
186 cdmEntitySession
= null;
188 dirty
.setDirty(false);
191 public void setDirty(boolean isDirty
){
192 this.dirty
.setDirty(isDirty
);
196 public boolean isDirty(){
197 return dirty
.isDirty();
201 public void selectionChanged(SelectionChangedEvent event
) {
202 //propagate selection
203 selService
.setSelection(event
.getSelection());
209 viewer
.getControl().setFocus();
211 if(conversation
!=null && !conversation
.isBound()){
214 if(cdmEntitySession
!= null) {
215 cdmEntitySession
.bind();
220 public void refresh(){
224 public TreeViewer
getViewer(){
229 public IStructuredSelection
getSelection() {
230 return (IStructuredSelection
) viewer
.getSelection();
234 public ConversationHolder
getConversationHolder() {
240 public void save(IProgressMonitor monitor
){
241 if (!conversation
.isBound()) {
245 // commit the conversation and start a new transaction immediately
246 conversation
.commit(true);
248 CdmStore
.getService(IFeatureTreeService
.class).saveOrUpdate(getRootEntities());
252 this.setDirty(false);
255 private void initializeTrees() {
256 Object
[] expandedElements
= viewer
.getExpandedElements();
257 viewer
.getTree().removeAll();
258 List
<FeatureTree
> trees
= CdmStore
.getService(IFeatureTreeService
.class).list(FeatureTree
.class, null, null, null, null);
259 viewer
.setInput(trees
);
260 viewer
.setExpandedElements(expandedElements
);
264 public void dispose(){
265 selService
.setSelection(null);
270 public ICdmEntitySession
getCdmEntitySession() {
271 return cdmEntitySession
;
275 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
276 List
<String
> propertyPaths
= Arrays
.asList(new String
[] {
277 "children", //$NON-NLS-1$
278 "feature", //$NON-NLS-1$
279 "featureTree", //$NON-NLS-1$
281 Map
<Object
, List
<String
>> propertyPathMap
=
282 new HashMap
<Object
, List
<String
>>();
283 propertyPathMap
.put(FeatureNode
.class,propertyPaths
);
284 return propertyPathMap
;
288 public List
<FeatureTree
> getRootEntities() {
289 return (List
<FeatureTree
>) viewer
.getInput();
293 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
297 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
298 //close view when workbench closes
300 thisPart
.getContext().get(EPartService
.class).hidePart(thisPart
);
308 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
312 public void contextRefresh(IProgressMonitor monitor
) {
316 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {
320 public void update(CdmDataChangeMap arg0
) {
324 public void changed(Object element
) {
325 dirty
.setDirty(true);
330 public void forceDirty() {
331 dirty
.setDirty(true);
335 public boolean postOperation(Object objectAffectedByOperation
) {
338 if(objectAffectedByOperation
instanceof FeatureNode
){
339 FeatureNode node
= (FeatureNode
)objectAffectedByOperation
;
340 viewer
.expandToLevel(node
.getFeatureTree(), 1);
342 if(objectAffectedByOperation
!=null){
343 StructuredSelection selection
= new StructuredSelection(objectAffectedByOperation
);
344 viewer
.setSelection(selection
);
350 public boolean onComplete() {