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());
94 public void setDirty(boolean isDirty
){
95 this.dirty
.setDirty(isDirty
);
98 public void setSelectedTree(FeatureTree featureTree
) {
99 this.featureTree
= HibernateProxyHelper
.deproxy(featureTree
, FeatureTree
.class);
100 this.featureTree
.setRoot(HibernateProxyHelper
.deproxy(featureTree
.getRoot(), FeatureNode
.class));
101 composite
.getViewer().setInput(featureTree
);
103 composite
.getText_title().removeModifyListener(this);
104 composite
.getText_title().setText(featureTree
.getTitleCache());
105 composite
.getText_title().addModifyListener(this);
110 public void modifyText(ModifyEvent e
) {
111 featureTree
.setTitleCache(composite
.getText_title().getText(), true);
117 public void selectionChanged(SelectionChangedEvent event
) {
118 IStructuredSelection selection
= (IStructuredSelection
) event
121 composite
.getButton_add().setEnabled(selection
.size() <= 1);
122 composite
.getButton_remove().setEnabled(selection
.size() > 0);
123 //propagate selection
124 IStructuredSelection isel
= (IStructuredSelection
) event
.getSelection();
125 selService
.setSelection((isel
.size() == 1 ? isel
.getFirstElement() : isel
.toArray()));
130 composite
.getViewer().getControl().setFocus();
131 if(conversation
!=null && !conversation
.isBound()){
138 CdmStore
.getService(IFeatureTreeService
.class).saveOrUpdate(featureTree
);
142 private class AddButtonListener
extends SelectionAdapter
{
144 public void widgetSelected(SelectionEvent e
) {
145 AvailableFeaturesWizard wizard
= new AvailableFeaturesWizard(
147 WizardDialog dialog
= new WizardDialog(shell
, wizard
);
149 if (dialog
.open() == IStatus
.OK
) {
150 IStructuredSelection selection
= (IStructuredSelection
) composite
.getViewer()
152 FeatureNode parent
= (FeatureNode
) (selection
.getFirstElement() != null ? selection
153 .getFirstElement() : ((FeatureTree
) composite
.getViewer().getInput())
155 Collection
<Feature
> additionalFeatures
= wizard
156 .getAdditionalFeatures();
157 for (Feature feature
: additionalFeatures
) {
158 FeatureNode child
= FeatureNode
.NewInstance(feature
);
159 CdmStore
.getService(IFeatureNodeService
.class).merge(child
, true);
161 parent
.addChild(child
);
164 composite
.getViewer().refresh();
170 private class RemoveSelectionListener
extends SelectionAdapter
{
172 public void widgetSelected(SelectionEvent e
) {
173 IStructuredSelection selection
= (IStructuredSelection
) composite
.getViewer()
176 for (Object selectedObject
: selection
.toArray()) {
177 FeatureNode featureNode
= (FeatureNode
) selectedObject
;
178 FeatureNode parent
= featureNode
.getParent();
179 parent
.removeChild(featureNode
);
181 CdmStore
.getService(IFeatureNodeService
.class).deleteFeatureNode(featureNode
.getUuid(), new FeatureNodeDeletionConfigurator());
185 composite
.getViewer().refresh();