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
.ArrayList
;
13 import java
.util
.Arrays
;
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
.core
.runtime
.NullProgressMonitor
;
24 import org
.eclipse
.e4
.ui
.di
.Focus
;
25 import org
.eclipse
.e4
.ui
.di
.Persist
;
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
.viewers
.ISelectionChangedListener
;
32 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
33 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
34 import org
.eclipse
.jface
.viewers
.TreeViewer
;
35 import org
.eclipse
.swt
.SWT
;
36 import org
.eclipse
.swt
.events
.ModifyEvent
;
37 import org
.eclipse
.swt
.events
.ModifyListener
;
38 import org
.eclipse
.swt
.events
.SelectionAdapter
;
39 import org
.eclipse
.swt
.events
.SelectionEvent
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.ui
.IMemento
;
43 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
44 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
45 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureTreeService
;
46 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
47 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
48 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
49 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
50 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
51 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
52 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
53 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
54 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
55 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
56 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
57 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.FeatureTreeSelectionDialog
;
58 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
59 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4ViewerPart
;
67 public class FeatureTreeEditor
implements ICdmEntitySessionEnabled
, ModifyListener
, ISelectionChangedListener
,
68 IE4ViewerPart
, IE4SavablePart
, IPartContentHasDetails
, IPartContentHasSupplementalData
, IContextListener
, IConversationEnabled
{
70 private ConversationHolder conversation
;
72 private ICdmEntitySession cdmEntitySession
;
75 private ESelectionService selService
;
78 private MDirtyable dirty
;
80 private FeatureTreeEditorComposite composite
;
83 private MPart thisPart
;
86 public FeatureTreeEditor() {
87 CdmStore
.getContextManager().addContextListener(this);
92 public void createControl(Composite parent
, EMenuService menuService
){
93 if (CdmStore
.isActive()){
94 if(conversation
== null){
95 conversation
= CdmStore
.createConversation();
97 if(cdmEntitySession
!=null){
98 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
104 composite
= new FeatureTreeEditorComposite(parent
, SWT
.NULL
);
105 composite
.init(new FeatureNodeDragListener(composite
.getViewer()),
106 new FeatureNodeDropAdapter(dirty
, composite
.getViewer()), this, new SelectionAdapter() {
108 public void widgetSelected(SelectionEvent e
) {
110 if(MessagingUtils
.confirmDialog("Editor has to be saved", "You have to save before loading another feature tree. Save now?")){
111 save(new NullProgressMonitor());
118 FeatureTree tree
= FeatureTreeSelectionDialog
.select(composite
.getDisplay().getActiveShell(), //conversation,
121 composite
.setSelectedTree(tree
, FeatureTreeEditor
.this);
126 //create context menu
127 menuService
.registerContextMenu(composite
.getViewer().getControl(), "eu.etaxonomy.taxeditor.store.popupmenu.featureTreeEditor");
130 public void setDirty(boolean isDirty
){
131 this.dirty
.setDirty(isDirty
);
134 public boolean isDirty(){
135 return dirty
.isDirty();
138 public FeatureTree
getSelectedFeatureTree(){
139 return composite
.getFeatureTree();
144 public void modifyText(ModifyEvent e
) {
145 composite
.getFeatureTree().setTitleCache(composite
.getText_title().getText(), true);
151 public void selectionChanged(SelectionChangedEvent event
) {
152 //propagate selection
153 selService
.setSelection(AbstractUtility
.getElementsFromSelectionChangedEvent(event
));
159 composite
.getViewer().getControl().setFocus();
161 if(conversation
!=null && !conversation
.isBound()){
164 if(cdmEntitySession
!= null) {
165 cdmEntitySession
.bind();
170 public void refresh(){
171 composite
.getViewer().refresh();
174 public TreeViewer
getViewer(){
175 return composite
.getViewer();
182 public IStructuredSelection
getSelection() {
183 return (IStructuredSelection
) composite
.getViewer().getSelection();
190 public ConversationHolder
getConversationHolder() {
196 public void save(IProgressMonitor monitor
){
197 if (!conversation
.isBound()) {
201 // commit the conversation and start a new transaction immediately
202 conversation
.commit(true);
204 CdmStore
.getService(IFeatureTreeService
.class).saveOrUpdate(composite
.getFeatureTree());
206 this.setDirty(false);
210 public void dispose(){
211 if(conversation
!=null){
212 conversation
.close();
215 if(cdmEntitySession
!= null) {
216 cdmEntitySession
.dispose();
217 cdmEntitySession
= null;
222 public ICdmEntitySession
getCdmEntitySession() {
223 return cdmEntitySession
;
227 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
228 List
<String
> propertyPaths
= Arrays
.asList(new String
[] {
229 "children", //$NON-NLS-1$
230 "feature", //$NON-NLS-1$
231 "featureTree", //$NON-NLS-1$
233 Map
<Object
, List
<String
>> propertyPathMap
=
234 new HashMap
<Object
, List
<String
>>();
235 propertyPathMap
.put(FeatureNode
.class,propertyPaths
);
236 return propertyPathMap
;
243 public List
<FeatureTree
> getRootEntities() {
244 List
<FeatureTree
> root
= new ArrayList
<>();
245 root
.add(composite
.getFeatureTree());
253 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
260 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
261 //close view when workbench closes
263 thisPart
.getContext().get(EPartService
.class).hidePart(thisPart
);
274 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
281 public void contextRefresh(IProgressMonitor monitor
) {
288 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {
295 public void update(CdmDataChangeMap arg0
) {