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
.Persist
;
20 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
21 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
22 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
23 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
24 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
25 import org
.eclipse
.jface
.viewers
.TreeViewer
;
26 import org
.eclipse
.jface
.viewers
.Viewer
;
27 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
28 import org
.eclipse
.jface
.wizard
.WizardDialog
;
29 import org
.eclipse
.swt
.SWT
;
30 import org
.eclipse
.swt
.dnd
.DND
;
31 import org
.eclipse
.swt
.dnd
.DragSourceAdapter
;
32 import org
.eclipse
.swt
.dnd
.DragSourceEvent
;
33 import org
.eclipse
.swt
.dnd
.Transfer
;
34 import org
.eclipse
.swt
.dnd
.TransferData
;
35 import org
.eclipse
.swt
.events
.ModifyEvent
;
36 import org
.eclipse
.swt
.events
.ModifyListener
;
37 import org
.eclipse
.swt
.events
.SelectionAdapter
;
38 import org
.eclipse
.swt
.events
.SelectionEvent
;
39 import org
.eclipse
.swt
.events
.SelectionListener
;
40 import org
.eclipse
.swt
.layout
.GridData
;
41 import org
.eclipse
.swt
.layout
.GridLayout
;
42 import org
.eclipse
.swt
.widgets
.Button
;
43 import org
.eclipse
.swt
.widgets
.Composite
;
44 import org
.eclipse
.swt
.widgets
.Label
;
45 import org
.eclipse
.swt
.widgets
.Shell
;
46 import org
.eclipse
.swt
.widgets
.Text
;
47 import org
.eclipse
.swt
.widgets
.Tree
;
49 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
50 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureNodeService
;
51 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureTreeService
;
52 import eu
.etaxonomy
.cdm
.api
.service
.config
.FeatureNodeDeletionConfigurator
;
53 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
54 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
55 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
56 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
57 import eu
.etaxonomy
.taxeditor
.featuretree
.AvailableFeaturesWizard
;
58 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureNodeTransfer
;
59 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureTreeContentProvider
;
60 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureTreeLabelProvider
;
61 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
62 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
63 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
64 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.FeatureTreeSelectionDialog
;
72 public class FeatureTreeEditor
implements
73 ModifyListener
, ISelectionChangedListener
{
75 private TreeViewer viewer
;
76 private Label label_title
;
77 private Button button_add
;
78 private Button button_remove
;
79 private Label label_treeInfo
;
80 private FeatureTree featureTree
;
81 private Text text_title
;
82 private Button btnOpenFeatureTree
;
83 private Button btnNewFeatureTree
;
85 private ConversationHolder conversation
;
87 private MDirtyable dirty
;
92 public FeatureTreeEditor(@Named(IServiceConstants
.ACTIVE_SHELL
)Shell shell
) {
94 if(conversation
==null){
95 conversation
= CdmStore
.createConversation();
101 public void createControl(Composite parent
, @Named(IServiceConstants
.ACTIVE_SHELL
)Shell shell
) {
102 parent
.setLayout(new GridLayout());
103 Composite composite
= new Composite(parent
, SWT
.NULL
);
104 composite
.setLayout(new GridLayout());
105 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
107 Composite composite_treeTitle
= new Composite(composite
, SWT
.NULL
);
108 composite_treeTitle
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true,
110 composite_treeTitle
.setLayout(new GridLayout(4, false));
112 label_title
= new Label(composite_treeTitle
, SWT
.NULL
);
113 label_title
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
114 label_title
.setText("Title");
116 text_title
= new Text(composite_treeTitle
, SWT
.BORDER
);
117 text_title
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1));
119 btnOpenFeatureTree
= new Button(composite_treeTitle
, SWT
.NONE
);
120 btnOpenFeatureTree
.setImage(ImageResources
.getImage(ImageResources
.BROWSE_ICON
));
121 btnOpenFeatureTree
.setText("Open Tree");
123 btnNewFeatureTree
= new Button(composite_treeTitle
, SWT
.NONE
);
124 btnNewFeatureTree
.setText("New Feature Tree");
126 Composite composite_treeContent
= new Composite(composite
, SWT
.NULL
);
127 composite_treeContent
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
129 composite_treeContent
.setLayout(new GridLayout(2, false));
131 viewer
= new TreeViewer(composite_treeContent
);
132 Tree tree
= viewer
.getTree();
133 tree
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true, 1, 1));
134 viewer
.getControl().setLayoutData(
135 new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
137 Composite composite_buttons
= new Composite(composite_treeContent
,
139 composite_buttons
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.TOP
, false,
141 composite_buttons
.setLayout(new GridLayout());
143 button_add
= new Button(composite_buttons
, SWT
.PUSH
);
144 button_add
.setText("Add");
145 button_add
.setToolTipText("Add a feature to this feature treee.");
146 button_remove
= new Button(composite_buttons
, SWT
.PUSH
);
147 button_remove
.setText("Remove");
149 .setToolTipText("Remove a feature from this feature tree.");
151 label_treeInfo
= new Label(composite
, SWT
.NULL
);
153 .setText("Order and nesting of feature nodes may be changed through drag and drop.");
158 private void setDirty(boolean isDirty
){
159 this.dirty
.setDirty(isDirty
);
162 private void init(Shell shell
) {
163 viewer
.setContentProvider(new FeatureTreeContentProvider());
164 viewer
.setLabelProvider(new FeatureTreeLabelProvider());
166 int ops
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
167 Transfer
[] transfers
= new Transfer
[] { FeatureNodeTransfer
169 viewer
.addDragSupport(ops
, transfers
, new FeatureNodeDragListener(
171 viewer
.addDropSupport(ops
, transfers
,
172 new FeatureNodeDropAdapter(viewer
));
174 viewer
.addSelectionChangedListener(this);
176 button_add
.addSelectionListener(new AddButtonListener());
177 button_remove
.addSelectionListener(new RemoveSelectionListener());
179 btnOpenFeatureTree
.addSelectionListener(new SelectionListener() {
182 public void widgetSelected(SelectionEvent e
) {
183 FeatureTree tree
= FeatureTreeSelectionDialog
.select(shell
, conversation
, null);
184 setSelectedTree(tree
);
188 public void widgetDefaultSelected(SelectionEvent e
) {
193 public void setSelectedTree(FeatureTree featureTree
) {
194 this.featureTree
= HibernateProxyHelper
.deproxy(featureTree
, FeatureTree
.class);
195 this.featureTree
.setRoot(HibernateProxyHelper
.deproxy(featureTree
.getRoot(), FeatureNode
.class));
196 viewer
.setInput(featureTree
);
198 text_title
.removeModifyListener(this);
199 text_title
.setText(featureTree
.getTitleCache());
200 text_title
.addModifyListener(this);
205 public void modifyText(ModifyEvent e
) {
206 featureTree
.setTitleCache(text_title
.getText(), true);
212 public void selectionChanged(SelectionChangedEvent event
) {
213 IStructuredSelection selection
= (IStructuredSelection
) event
216 button_add
.setEnabled(selection
.size() <= 1);
217 button_remove
.setEnabled(selection
.size() > 0);
222 CdmStore
.getService(IFeatureTreeService
.class).saveOrUpdate(featureTree
);
226 private class AddButtonListener
extends SelectionAdapter
{
228 public void widgetSelected(SelectionEvent e
) {
229 AvailableFeaturesWizard wizard
= new AvailableFeaturesWizard(
231 WizardDialog dialog
= new WizardDialog(shell
, wizard
);
233 if (dialog
.open() == IStatus
.OK
) {
234 IStructuredSelection selection
= (IStructuredSelection
) viewer
236 FeatureNode parent
= (FeatureNode
) (selection
.getFirstElement() != null ? selection
237 .getFirstElement() : ((FeatureTree
) viewer
.getInput())
239 Collection
<Feature
> additionalFeatures
= wizard
240 .getAdditionalFeatures();
241 for (Feature feature
: additionalFeatures
) {
242 FeatureNode child
= FeatureNode
.NewInstance(feature
);
243 CdmStore
.getService(IFeatureNodeService
.class).merge(child
, true);
245 parent
.addChild(child
);
254 private class RemoveSelectionListener
extends SelectionAdapter
{
256 public void widgetSelected(SelectionEvent e
) {
257 IStructuredSelection selection
= (IStructuredSelection
) viewer
260 for (Object selectedObject
: selection
.toArray()) {
261 FeatureNode featureNode
= (FeatureNode
) selectedObject
;
262 FeatureNode parent
= featureNode
.getParent();
263 parent
.removeChild(featureNode
);
265 CdmStore
.getService(IFeatureNodeService
.class).deleteFeatureNode(featureNode
.getUuid(), new FeatureNodeDeletionConfigurator());
273 private class FeatureNodeDragListener
extends DragSourceAdapter
{
275 private final TreeViewer viewer
;
277 public FeatureNodeDragListener(TreeViewer viewer
) {
278 this.viewer
= viewer
;
282 * Method declared on DragSourceListener
285 public void dragFinished(DragSourceEvent event
) {
289 // if the featureNode was moved, remove it from the source viewer
290 if (event
.detail
== DND
.DROP_MOVE
) {
291 IStructuredSelection selection
= (IStructuredSelection
) viewer
298 * Method declared on DragSourceListener
301 public void dragSetData(DragSourceEvent event
) {
302 IStructuredSelection selection
= (IStructuredSelection
) viewer
304 FeatureNode
[] featureNodes
= (FeatureNode
[]) selection
.toList()
305 .toArray(new FeatureNode
[selection
.size()]);
306 if (FeatureNodeTransfer
.getInstance().isSupportedType(
308 event
.data
= featureNodes
;
313 * Method declared on DragSourceListener
316 public void dragStart(DragSourceEvent event
) {
317 event
.doit
= !viewer
.getSelection().isEmpty();
322 private class FeatureNodeDropAdapter
extends ViewerDropAdapter
{
324 protected FeatureNodeDropAdapter(Viewer viewer
) {
329 public boolean performDrop(Object data
) {
330 FeatureNode target
= (FeatureNode
) getCurrentTarget();
333 if (target
!= null) {
334 int location
= getCurrentLocation();
335 FeatureNode parent
= target
.getParent();
336 if (location
== LOCATION_BEFORE
) {
337 position
= Math
.max(0, parent
.getIndex(target
) - 1);
341 if (location
== LOCATION_AFTER
) {
342 position
= parent
.getIndex(target
);
347 // set target to root node if there is no target specified
348 if (target
== null) {
349 FeatureTree featureTree
= (FeatureTree
) getViewer().getInput();
350 target
= featureTree
.getRoot();
353 Object
[] droppedObjects
= (Object
[]) data
;
354 TreeViewer viewer
= (TreeViewer
) getViewer();
356 // cannot drop a feature node onto itself
357 for (Object droppedObject
: droppedObjects
) {
358 if (droppedObject
== null) {
359 MessagingUtils
.warningDialog(
360 "Operation not supported yet",
362 "We are currently unable to change the order of freshly created "
363 + "feature trees nodes. Please close and reopen the dialog to change the order of features.");
366 if (droppedObject
.equals(target
)) {
370 for (Object droppedObject
: droppedObjects
) {
371 FeatureNode droppedNode
= (FeatureNode
) droppedObject
;
372 target
.addChild(droppedNode
, position
);
373 viewer
.add(target
, droppedNode
);
374 viewer
.reveal(droppedNode
);
380 public boolean validateDrop(Object target
, int operation
,
381 TransferData transferData
) {
382 return FeatureNodeTransfer
.getInstance().isSupportedType(