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
.Collection
;
15 import java
.util
.HashMap
;
16 import java
.util
.List
;
19 import javax
.annotation
.PostConstruct
;
20 import javax
.annotation
.PreDestroy
;
21 import javax
.inject
.Inject
;
22 import javax
.inject
.Named
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
26 import org
.eclipse
.e4
.ui
.di
.Focus
;
27 import org
.eclipse
.e4
.ui
.di
.Persist
;
28 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
29 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
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
.wizard
.WizardDialog
;
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
.swt
.widgets
.Shell
;
43 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
44 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureNodeService
;
45 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureTreeService
;
46 import eu
.etaxonomy
.cdm
.api
.service
.config
.FeatureNodeDeletionConfigurator
;
47 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
48 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
49 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
50 import eu
.etaxonomy
.taxeditor
.featuretree
.AvailableFeaturesWizard
;
51 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
52 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
53 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
54 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
55 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
56 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.FeatureTreeSelectionDialog
;
64 public class FeatureTreeEditor
implements ICdmEntitySessionEnabled
,
65 ModifyListener
, ISelectionChangedListener
{
67 private ConversationHolder conversation
;
69 private ICdmEntitySession cdmEntitySession
;
72 private ESelectionService selService
;
75 private MDirtyable dirty
;
77 private FeatureTreeEditorComposite composite
;
80 public FeatureTreeEditor() {
85 public void createControl(Composite parent
, @Optional@Named(IServiceConstants
.ACTIVE_SHELL
) Shell shell
){
86 if (CdmStore
.isActive()){
87 if(conversation
== null){
88 conversation
= CdmStore
.createConversation();
90 if(cdmEntitySession
!=null){
91 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
97 composite
= new FeatureTreeEditorComposite(parent
, SWT
.NULL
);
98 composite
.init(new FeatureNodeDragListener(composite
.getViewer()),
99 new FeatureNodeDropAdapter(this, composite
.getViewer()), this, new SelectionAdapter() {
101 public void widgetSelected(SelectionEvent e
) {
103 if(MessagingUtils
.confirmDialog("Editor has to be saved", "You have to save before loading another feature tree. Save now?")){
111 FeatureTree tree
= FeatureTreeSelectionDialog
.select(shell
, conversation
, null);
113 composite
.setSelectedTree(tree
, FeatureTreeEditor
.this);
116 }, new AddButtonListener(), new RemoveSelectionListener(), new FeatureTreeExportListener(shell
, composite
));
119 public void setDirty(boolean isDirty
){
120 this.dirty
.setDirty(isDirty
);
123 public boolean isDirty(){
124 return dirty
.isDirty();
127 public FeatureTree
getSelectedFeatureTree(){
128 return composite
.getFeatureTree();
133 public void modifyText(ModifyEvent e
) {
134 composite
.getFeatureTree().setTitleCache(composite
.getText_title().getText(), true);
140 public void selectionChanged(SelectionChangedEvent event
) {
141 IStructuredSelection selection
= (IStructuredSelection
) event
144 composite
.getBtnAdd().setEnabled(selection
.size() <= 1);
145 composite
.getBtnRemove().setEnabled(selection
.size() > 0);
146 //propagate selection
147 selService
.setSelection(AbstractUtility
.getElementsFromSelectionChangedEvent(event
));
153 composite
.getViewer().getControl().setFocus();
155 if(conversation
!=null && !conversation
.isBound()){
158 if(cdmEntitySession
!= null) {
159 cdmEntitySession
.bind();
165 if (!conversation
.isBound()) {
169 // commit the conversation and start a new transaction immediately
170 conversation
.commit(true);
172 CdmStore
.getService(IFeatureTreeService
.class).saveOrUpdate(composite
.getFeatureTree());
174 this.setDirty(false);
178 public void dispose(){
179 if(conversation
!=null){
180 conversation
.close();
182 if(cdmEntitySession
!= null) {
183 cdmEntitySession
.dispose();
188 public ICdmEntitySession
getCdmEntitySession() {
189 return cdmEntitySession
;
193 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
194 List
<String
> propertyPaths
= Arrays
.asList(new String
[] {
195 "children", //$NON-NLS-1$
196 "feature", //$NON-NLS-1$
197 "featureTree", //$NON-NLS-1$
199 Map
<Object
, List
<String
>> propertyPathMap
=
200 new HashMap
<Object
, List
<String
>>();
201 propertyPathMap
.put(FeatureNode
.class,propertyPaths
);
202 return propertyPathMap
;
209 public List
<FeatureTree
> getRootEntities() {
210 List
<FeatureTree
> root
= new ArrayList
<>();
211 root
.add(composite
.getFeatureTree());
215 private class AddButtonListener
extends SelectionAdapter
{
217 public void widgetSelected(SelectionEvent e
) {
218 AvailableFeaturesWizard wizard
= new AvailableFeaturesWizard();
219 WizardDialog dialog
= new WizardDialog(e
.widget
.getDisplay().getActiveShell(), wizard
);
221 if (dialog
.open() == IStatus
.OK
) {
222 FeatureNode parent
= ((FeatureTree
) composite
.getViewer().getInput()).getRoot();
223 Collection
<Feature
> additionalFeatures
= wizard
.getAdditionalFeatures();
224 for (Feature feature
: additionalFeatures
) {
225 if(!getSelectedFeatureTree().getDistinctFeatures().contains(feature
)){
226 CdmStore
.getService(IFeatureNodeService
.class).addChildFeatureNode(parent
.getUuid(), feature
.getUuid());
230 composite
.getViewer().refresh();
231 composite
.getViewer().expandToLevel(parent
, 1);
237 private class RemoveSelectionListener
extends SelectionAdapter
{
239 public void widgetSelected(SelectionEvent e
) {
240 IStructuredSelection selection
= (IStructuredSelection
) composite
.getViewer()
243 for (Object selectedObject
: selection
.toArray()) {
244 FeatureNode featureNode
= (FeatureNode
) selectedObject
;
245 CdmStore
.getService(IFeatureNodeService
.class).deleteFeatureNode(featureNode
.getUuid(), new FeatureNodeDeletionConfigurator());
249 composite
.getViewer().refresh();