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
.api
.service
.ITermService
;
51 import eu
.etaxonomy
.cdm
.model
.description
.Character
;
52 import eu
.etaxonomy
.cdm
.model
.term
.FeatureNode
;
53 import eu
.etaxonomy
.cdm
.model
.term
.FeatureTree
;
54 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
55 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
56 import eu
.etaxonomy
.taxeditor
.editor
.definedterm
.TermTransfer
;
57 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureNodeTransfer
;
58 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureTreeContentProvider
;
59 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureTreeLabelProvider
;
60 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.operation
.AddFeatureOperation
;
61 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
62 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
63 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
64 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
65 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
66 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
67 import eu
.etaxonomy
.taxeditor
.store
.AppModelId
;
68 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
69 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
70 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4ViewerPart
;
78 public class FeatureTreeEditor
implements IFeatureTreeEditor
, ISelectionChangedListener
,
79 IE4ViewerPart
, IPartContentHasDetails
, IPartContentHasSupplementalData
,
80 IContextListener
, IConversationEnabled
, IDirtyMarkable
{
82 private ConversationHolder conversation
;
84 private ICdmEntitySession cdmEntitySession
;
87 private ESelectionService selService
;
90 private MDirtyable dirty
;
93 private UISynchronize sync
;
96 private MPart thisPart
;
98 private TreeViewer viewer
;
101 public FeatureTreeEditor() {
102 CdmStore
.getContextManager().addContextListener(this);
106 public void createControl(Composite parent
, EMenuService menuService
){
107 if (CdmStore
.isActive()){
113 parent
.setLayout(new FillLayout());
114 viewer
= new TreeViewer(parent
);
115 viewer
.setContentProvider(new FeatureTreeContentProvider());
116 viewer
.setLabelProvider(new FeatureTreeLabelProvider());
118 int ops
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
119 Transfer
[] transfers
= new Transfer
[] {
120 FeatureNodeTransfer
.getInstance(),
121 TermTransfer
.getInstance(),
122 LocalSelectionTransfer
.getTransfer()};
123 viewer
.addDragSupport(ops
, transfers
, new FeatureNodeDragListener(viewer
));
124 viewer
.addDropSupport(ops
, transfers
, new FeatureTreeDropAdapter(this, viewer
, sync
));
125 viewer
.addSelectionChangedListener(this);
126 viewer
.getTree().addKeyListener(new KeyAdapter() {
128 public void keyPressed(KeyEvent e
) {
129 if(e
.stateMask
== SWT
.MOD1
&& e
.keyCode
== 'c'){
130 copy(viewer
.getStructuredSelection());
132 else if(e
.stateMask
== SWT
.MOD1
&& e
.keyCode
== 'v'){
133 paste(viewer
.getStructuredSelection());
138 List
<FeatureTree
> trees
= CdmStore
.getService(IFeatureTreeService
.class).list(FeatureTree
.class, null, null, null, null);
139 Collections
.sort(trees
, (tree1
, tree2
) -> tree1
.getTitleCache().compareTo(tree2
.getTitleCache()));
140 viewer
.setInput(trees
);
142 //create context menu
143 menuService
.registerContextMenu(viewer
.getControl(), AppModelId
.POPUPMENU_EU_ETAXONOMY_TAXEDITOR_STORE_POPUPMENU_FEATURETREEEDITOR
);
146 public void paste(IStructuredSelection selection
) {
147 if (StoreUtil
.promptCheckIsDirty(this)) {
151 ISelection clipBoardSelection
= LocalSelectionTransfer
.getTransfer().getSelection();
152 Object firstElement
= selection
.getFirstElement();
153 FeatureNode parentNode
= null;
154 if(firstElement
instanceof FeatureNode
){
155 parentNode
= (FeatureNode
) firstElement
;
157 else if(firstElement
instanceof FeatureTree
){
158 parentNode
= ((FeatureTree
)firstElement
).getRoot();
160 if(parentNode
!=null){
161 FeatureNode copiedNode
= (FeatureNode
) ((IStructuredSelection
)clipBoardSelection
).getFirstElement();
163 AddFeatureOperation operation
= new AddFeatureOperation(copiedNode
.getTerm().getUuid(), parentNode
, this, this);
164 AbstractUtility
.executeOperation(operation
, sync
);
169 public void copy(IStructuredSelection selection
) {
170 LocalSelectionTransfer
.getTransfer().setSelection(selection
);
173 private void initSession(){
174 if(conversation
== null){
175 conversation
= CdmStore
.createConversation();
177 if(cdmEntitySession
==null){
178 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
182 private void clearSession() {
183 if(conversation
!=null){
184 conversation
.close();
187 if(cdmEntitySession
!= null) {
188 cdmEntitySession
.dispose();
189 cdmEntitySession
= null;
191 dirty
.setDirty(false);
194 public void setDirty(boolean isDirty
){
195 this.dirty
.setDirty(isDirty
);
199 public boolean isDirty(){
200 return dirty
.isDirty();
204 public void selectionChanged(SelectionChangedEvent event
) {
205 //propagate selection
206 selService
.setSelection(event
.getSelection());
212 viewer
.getControl().setFocus();
214 if(conversation
!=null && !conversation
.isBound()){
217 if(cdmEntitySession
!= null) {
218 cdmEntitySession
.bind();
223 public void refresh(){
227 public TreeViewer
getViewer(){
232 public IStructuredSelection
getSelection() {
233 return (IStructuredSelection
) viewer
.getSelection();
237 public ConversationHolder
getConversationHolder() {
243 public void save(IProgressMonitor monitor
){
244 if (!conversation
.isBound()) {
248 // commit the conversation and start a new transaction immediately
249 conversation
.commit(true);
251 CdmStore
.getService(IFeatureTreeService
.class).saveOrUpdate(getRootEntities());
253 List
<FeatureTree
> rootEntities
= getRootEntities();
254 for (FeatureTree featureTree
: rootEntities
) {
255 if(featureTree
.getTermType().equals(TermType
.Character
)){
256 FeatureTree
<Character
> characterTree
= featureTree
;
257 //save characters because they can be modified in this editor
258 characterTree
.getDistinctFeatures().forEach(character
->CdmStore
.getService(ITermService
.class).merge(character
,true));
264 this.setDirty(false);
267 private void initializeTrees() {
268 Object
[] expandedElements
= viewer
.getExpandedElements();
269 viewer
.getTree().removeAll();
270 List
<FeatureTree
> trees
= CdmStore
.getService(IFeatureTreeService
.class).list(FeatureTree
.class, null, null, null, null);
271 viewer
.setInput(trees
);
272 viewer
.setExpandedElements(expandedElements
);
276 public void dispose(){
277 selService
.setSelection(null);
282 public ICdmEntitySession
getCdmEntitySession() {
283 return cdmEntitySession
;
287 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
288 List
<String
> propertyPaths
= Arrays
.asList(new String
[] {
289 "children", //$NON-NLS-1$
290 "feature", //$NON-NLS-1$
291 "featureTree", //$NON-NLS-1$
293 Map
<Object
, List
<String
>> propertyPathMap
=
294 new HashMap
<Object
, List
<String
>>();
295 propertyPathMap
.put(FeatureNode
.class,propertyPaths
);
296 return propertyPathMap
;
300 public List
<FeatureTree
> getRootEntities() {
301 return (List
<FeatureTree
>) viewer
.getInput();
305 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
309 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
310 //close view when workbench closes
312 thisPart
.getContext().get(EPartService
.class).hidePart(thisPart
);
320 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
324 public void contextRefresh(IProgressMonitor monitor
) {
328 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {
332 public void update(CdmDataChangeMap arg0
) {
336 public void changed(Object element
) {
337 dirty
.setDirty(true);
342 public void forceDirty() {
343 dirty
.setDirty(true);
347 public boolean postOperation(Object objectAffectedByOperation
) {
350 if(objectAffectedByOperation
instanceof FeatureNode
){
351 FeatureNode node
= (FeatureNode
)objectAffectedByOperation
;
352 viewer
.expandToLevel(node
.getFeatureTree(), 1);
354 if(objectAffectedByOperation
!=null){
355 StructuredSelection selection
= new StructuredSelection(objectAffectedByOperation
);
356 viewer
.setSelection(selection
);
362 public boolean onComplete() {