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
.Collection
;
14 import javax
.annotation
.PostConstruct
;
15 import javax
.inject
.Inject
;
16 import javax
.inject
.Named
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.e4
.ui
.di
.Focus
;
20 import org
.eclipse
.e4
.ui
.di
.Persist
;
21 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
22 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
23 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
24 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
25 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
26 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
27 import org
.eclipse
.jface
.wizard
.WizardDialog
;
28 import org
.eclipse
.swt
.SWT
;
29 import org
.eclipse
.swt
.events
.ModifyEvent
;
30 import org
.eclipse
.swt
.events
.ModifyListener
;
31 import org
.eclipse
.swt
.events
.SelectionAdapter
;
32 import org
.eclipse
.swt
.events
.SelectionEvent
;
33 import org
.eclipse
.swt
.widgets
.Composite
;
34 import org
.eclipse
.swt
.widgets
.Shell
;
36 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
37 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureNodeService
;
38 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureTreeService
;
39 import eu
.etaxonomy
.cdm
.api
.service
.config
.FeatureNodeDeletionConfigurator
;
40 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
41 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
42 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
43 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
44 import eu
.etaxonomy
.taxeditor
.featuretree
.AvailableFeaturesWizard
;
45 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
46 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.FeatureTreeSelectionDialog
;
54 public class FeatureTreeEditor
implements
55 ModifyListener
, ISelectionChangedListener
{
57 private ConversationHolder conversation
;
60 private ESelectionService selService
;
63 private MDirtyable dirty
;
67 private FeatureTree featureTree
;
69 private FeatureTreeEditorComposite composite
;
72 public FeatureTreeEditor(@Named(IServiceConstants
.ACTIVE_SHELL
)Shell shell
) {
74 if(conversation
==null){
75 conversation
= CdmStore
.createConversation();
81 public void createControl(Composite parent
, @Named(IServiceConstants
.ACTIVE_SHELL
)Shell shell
) {
82 composite
= new FeatureTreeEditorComposite(parent
, SWT
.NULL
);
83 composite
.init(new FeatureNodeDragListener(composite
.getViewer()), new FeatureNodeDropAdapter(this, composite
.getViewer()), this, new SelectionAdapter() {
85 public void widgetSelected(SelectionEvent e
) {
86 FeatureTree tree
= FeatureTreeSelectionDialog
.select(shell
, conversation
, null);
88 setSelectedTree(tree
);
91 }, new AddButtonListener(), new RemoveSelectionListener());
92 composite
.getText_title().setEnabled(false);
95 public void setDirty(boolean isDirty
){
96 this.dirty
.setDirty(isDirty
);
99 public boolean isDirty(){
100 return dirty
.isDirty();
103 public void setSelectedTree(FeatureTree featureTree
) {
104 this.featureTree
= HibernateProxyHelper
.deproxy(featureTree
, FeatureTree
.class);
105 this.featureTree
.setRoot(HibernateProxyHelper
.deproxy(featureTree
.getRoot(), FeatureNode
.class));
106 composite
.getViewer().setInput(featureTree
);
108 composite
.getText_title().setEnabled(true);
109 composite
.getText_title().removeModifyListener(this);
110 composite
.getText_title().setText(featureTree
.getTitleCache());
111 composite
.getText_title().addModifyListener(this);
116 public void modifyText(ModifyEvent e
) {
117 featureTree
.setTitleCache(composite
.getText_title().getText(), true);
123 public void selectionChanged(SelectionChangedEvent event
) {
124 IStructuredSelection selection
= (IStructuredSelection
) event
127 composite
.getButton_add().setEnabled(selection
.size() <= 1);
128 composite
.getButton_remove().setEnabled(selection
.size() > 0);
129 //propagate selection
130 IStructuredSelection isel
= (IStructuredSelection
) event
.getSelection();
131 selService
.setSelection((isel
.size() == 1 ? isel
.getFirstElement() : isel
.toArray()));
136 composite
.getViewer().getControl().setFocus();
137 if(conversation
!=null && !conversation
.isBound()){
144 CdmStore
.getService(IFeatureTreeService
.class).saveOrUpdate(featureTree
);
148 private class AddButtonListener
extends SelectionAdapter
{
150 public void widgetSelected(SelectionEvent e
) {
151 AvailableFeaturesWizard wizard
= new AvailableFeaturesWizard(
153 WizardDialog dialog
= new WizardDialog(shell
, wizard
);
155 if (dialog
.open() == IStatus
.OK
) {
156 IStructuredSelection selection
= (IStructuredSelection
) composite
.getViewer()
158 FeatureNode parent
= (FeatureNode
) (selection
.getFirstElement() != null ? selection
159 .getFirstElement() : ((FeatureTree
) composite
.getViewer().getInput())
161 Collection
<Feature
> additionalFeatures
= wizard
162 .getAdditionalFeatures();
163 for (Feature feature
: additionalFeatures
) {
164 FeatureNode child
= FeatureNode
.NewInstance(feature
);
165 parent
.addChild(child
);
166 CdmStore
.getService(IFeatureNodeService
.class).save(child
);
169 composite
.getViewer().refresh();
175 private class RemoveSelectionListener
extends SelectionAdapter
{
177 public void widgetSelected(SelectionEvent e
) {
178 IStructuredSelection selection
= (IStructuredSelection
) composite
.getViewer()
181 for (Object selectedObject
: selection
.toArray()) {
182 FeatureNode featureNode
= (FeatureNode
) selectedObject
;
183 FeatureNode parent
= featureNode
.getParent();
184 parent
.removeChild(featureNode
);
186 CdmStore
.getService(IFeatureNodeService
.class).deleteFeatureNode(featureNode
.getUuid(), new FeatureNodeDeletionConfigurator());
190 composite
.getViewer().refresh();