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
;
12 import java
.util
.Collection
;
14 import org
.eclipse
.core
.runtime
.IStatus
;
15 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
16 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
17 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
18 import org
.eclipse
.jface
.viewers
.TreeViewer
;
19 import org
.eclipse
.jface
.viewers
.Viewer
;
20 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
21 import org
.eclipse
.jface
.wizard
.WizardDialog
;
22 import org
.eclipse
.jface
.wizard
.WizardPage
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.dnd
.DND
;
25 import org
.eclipse
.swt
.dnd
.DragSourceAdapter
;
26 import org
.eclipse
.swt
.dnd
.DragSourceEvent
;
27 import org
.eclipse
.swt
.dnd
.Transfer
;
28 import org
.eclipse
.swt
.dnd
.TransferData
;
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
.layout
.GridData
;
34 import org
.eclipse
.swt
.layout
.GridLayout
;
35 import org
.eclipse
.swt
.widgets
.Button
;
36 import org
.eclipse
.swt
.widgets
.Composite
;
37 import org
.eclipse
.swt
.widgets
.Label
;
38 import org
.eclipse
.swt
.widgets
.Text
;
39 import org
.hibernate
.proxy
.HibernateProxy
;
41 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureNodeService
;
42 import eu
.etaxonomy
.cdm
.api
.service
.config
.FeatureNodeDeletionConfigurator
;
43 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
44 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
45 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
46 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
47 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
48 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
52 * EditFeatureTreeWizardPage class.
56 * @created Aug 5, 2010
59 public class EditFeatureTreeWizardPage
extends WizardPage
implements
60 ModifyListener
, ISelectionChangedListener
{
62 private TreeViewer viewer
;
63 private Label label_title
;
64 private Text text_title
;
65 private Button button_add
;
66 private Button button_remove
;
67 private Label label_treeInfo
;
68 private FeatureTree featureTree
;
72 * Constructor for EditFeatureTreeWizardPage.
76 * a {@link java.lang.String} object.
78 protected EditFeatureTreeWizardPage(String pageName
) {
80 setMessage("Edit the Feature Tree.");
87 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
92 public void createControl(Composite parent
) {
93 Composite composite
= new Composite(parent
, SWT
.NULL
);
94 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
95 composite
.setLayout(new GridLayout());
97 Composite composite_treeTitle
= new Composite(composite
, SWT
.NULL
);
98 composite_treeTitle
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true,
100 composite_treeTitle
.setLayout(new GridLayout(2, false));
102 label_title
= new Label(composite_treeTitle
, SWT
.NULL
);
103 label_title
.setText("Title");
105 text_title
= new Text(composite_treeTitle
, SWT
.NULL
);
106 text_title
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false));
108 Composite composite_treeContent
= new Composite(composite
, SWT
.NULL
);
109 composite_treeContent
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
111 composite_treeContent
.setLayout(new GridLayout(2, false));
113 viewer
= new TreeViewer(composite_treeContent
);
114 viewer
.getControl().setLayoutData(
115 new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
117 Composite composite_buttons
= new Composite(composite_treeContent
,
119 composite_buttons
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.TOP
, false,
121 composite_buttons
.setLayout(new GridLayout());
123 button_add
= new Button(composite_buttons
, SWT
.PUSH
);
124 button_add
.setText("Add");
125 button_add
.setToolTipText("Add a feature to this feature treee.");
126 button_remove
= new Button(composite_buttons
, SWT
.PUSH
);
127 button_remove
.setText("Remove");
129 .setToolTipText("Remove a feature from this feature tree.");
131 label_treeInfo
= new Label(composite
, SWT
.NULL
);
133 .setText("Order and nesting of feature nodes may be changed through drag and drop.");
135 viewer
.setContentProvider(new FeatureTreeContentProvider());
136 viewer
.setLabelProvider(new FeatureTreeLabelProvider());
138 int ops
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
139 Transfer
[] transfers
= new Transfer
[] { FeatureNodeTransfer
141 viewer
.addDragSupport(ops
, transfers
, new FeatureNodeDragListener(
143 viewer
.addDropSupport(ops
, transfers
,
144 new FeatureNodeDropAdapter(viewer
));
146 viewer
.addSelectionChangedListener(this);
148 button_add
.addSelectionListener(new AddButtonListener());
149 button_remove
.addSelectionListener(new RemoveSelectionListener());
151 setControl(composite
);
160 * a {@link eu.etaxonomy.cdm.model.description.FeatureTree}
163 public void setSelectedTree(FeatureTree featureTree
) {
164 this.featureTree
= HibernateProxyHelper
.deproxy(featureTree
, FeatureTree
.class);
165 this.featureTree
.setRoot(HibernateProxyHelper
.deproxy(featureTree
.getRoot(), FeatureNode
.class));
166 viewer
.setInput(featureTree
);
168 text_title
.removeModifyListener(this);
169 if (featureTree
!= null){
170 text_title
.setText(featureTree
.getTitleCache());
172 text_title
.addModifyListener(this);
177 public void modifyText(ModifyEvent e
) {
178 featureTree
.setTitleCache(text_title
.getText(), true);
183 public void selectionChanged(SelectionChangedEvent event
) {
184 IStructuredSelection selection
= (IStructuredSelection
) event
187 button_add
.setEnabled(selection
.size() <= 1);
188 button_remove
.setEnabled(selection
.size() > 0);
191 private class AddButtonListener
extends SelectionAdapter
{
193 public void widgetSelected(SelectionEvent e
) {
194 AvailableFeaturesWizard wizard
= new AvailableFeaturesWizard(
196 WizardDialog dialog
= new WizardDialog(getShell(), wizard
);
198 if (dialog
.open() == IStatus
.OK
) {
199 IStructuredSelection selection
= (IStructuredSelection
) viewer
201 FeatureNode parent
= (FeatureNode
) (selection
.getFirstElement() != null ? selection
202 .getFirstElement() : ((FeatureTree
) viewer
.getInput())
204 Collection
<Feature
> additionalFeatures
= wizard
205 .getAdditionalFeatures();
206 for (Feature feature
: additionalFeatures
) {
207 FeatureNode child
= FeatureNode
.NewInstance(feature
);
208 CdmStore
.getService(IFeatureNodeService
.class).merge(child
, true);
210 parent
.addChild(child
);
217 private class RemoveSelectionListener
extends SelectionAdapter
{
219 public void widgetSelected(SelectionEvent e
) {
220 IStructuredSelection selection
= (IStructuredSelection
) viewer
223 for (Object selectedObject
: selection
.toArray()) {
224 FeatureNode featureNode
= (FeatureNode
) selectedObject
;
225 FeatureNode parent
= featureNode
.getParent();
226 parent
.removeChild(featureNode
);
228 CdmStore
.getService(IFeatureNodeService
.class).deleteFeatureNode(featureNode
.getUuid(), new FeatureNodeDeletionConfigurator());
235 private class FeatureNodeDragListener
extends DragSourceAdapter
{
237 private final TreeViewer viewer
;
239 public FeatureNodeDragListener(TreeViewer viewer
) {
240 this.viewer
= viewer
;
244 * Method declared on DragSourceListener
247 public void dragFinished(DragSourceEvent event
) {
251 // if the featureNode was moved, remove it from the source viewer
252 if (event
.detail
== DND
.DROP_MOVE
) {
253 IStructuredSelection selection
= (IStructuredSelection
) viewer
260 * Method declared on DragSourceListener
263 public void dragSetData(DragSourceEvent event
) {
264 IStructuredSelection selection
= (IStructuredSelection
) viewer
266 FeatureNode
[] featureNodes
= (FeatureNode
[]) selection
.toList()
267 .toArray(new FeatureNode
[selection
.size()]);
268 if (FeatureNodeTransfer
.getInstance().isSupportedType(
270 event
.data
= featureNodes
;
275 * Method declared on DragSourceListener
278 public void dragStart(DragSourceEvent event
) {
279 event
.doit
= !viewer
.getSelection().isEmpty();
284 private class FeatureNodeDropAdapter
extends ViewerDropAdapter
{
286 protected FeatureNodeDropAdapter(Viewer viewer
) {
291 public boolean performDrop(Object data
) {
292 FeatureNode target
= (FeatureNode
) getCurrentTarget();
295 if (target
!= null) {
296 int location
= getCurrentLocation();
297 FeatureNode parent
= target
.getParent();
298 if (location
== LOCATION_BEFORE
) {
299 position
= Math
.max(0, parent
.getIndex(target
) - 1);
303 if (location
== LOCATION_AFTER
) {
304 position
= parent
.getIndex(target
);
309 // set target to root node if there is no target specified
310 if (target
== null) {
311 FeatureTree featureTree
= (FeatureTree
) getViewer().getInput();
312 target
= featureTree
.getRoot();
315 Object
[] droppedObjects
= (Object
[]) data
;
316 TreeViewer viewer
= (TreeViewer
) getViewer();
318 // cannot drop a feature node onto itself
319 for (Object droppedObject
: droppedObjects
) {
320 if (droppedObject
== null) {
321 MessagingUtils
.warningDialog(
322 "Operation not supported yet",
324 "We are currently unable to change the order of freshly created "
325 + "feature trees nodes. Please close and reopen the dialog to change the order of features.");
328 if (droppedObject
.equals(target
)) {
332 for (Object droppedObject
: droppedObjects
) {
333 FeatureNode droppedNode
= (FeatureNode
) droppedObject
;
334 target
.addChild(droppedNode
, position
);
335 viewer
.add(target
, droppedNode
);
336 viewer
.reveal(droppedNode
);
342 public boolean validateDrop(Object target
, int operation
,
343 TransferData transferData
) {
344 return FeatureNodeTransfer
.getInstance().isSupportedType(