3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.featuretree
;
13 import java
.util
.Collection
;
15 import org
.eclipse
.core
.runtime
.IStatus
;
16 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
17 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
18 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
19 import org
.eclipse
.jface
.viewers
.TreeViewer
;
20 import org
.eclipse
.jface
.viewers
.Viewer
;
21 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
22 import org
.eclipse
.jface
.wizard
.WizardDialog
;
23 import org
.eclipse
.jface
.wizard
.WizardPage
;
24 import org
.eclipse
.swt
.SWT
;
25 import org
.eclipse
.swt
.dnd
.DND
;
26 import org
.eclipse
.swt
.dnd
.DragSourceAdapter
;
27 import org
.eclipse
.swt
.dnd
.DragSourceEvent
;
28 import org
.eclipse
.swt
.dnd
.Transfer
;
29 import org
.eclipse
.swt
.dnd
.TransferData
;
30 import org
.eclipse
.swt
.events
.ModifyEvent
;
31 import org
.eclipse
.swt
.events
.ModifyListener
;
32 import org
.eclipse
.swt
.events
.SelectionAdapter
;
33 import org
.eclipse
.swt
.events
.SelectionEvent
;
34 import org
.eclipse
.swt
.layout
.GridData
;
35 import org
.eclipse
.swt
.layout
.GridLayout
;
36 import org
.eclipse
.swt
.widgets
.Button
;
37 import org
.eclipse
.swt
.widgets
.Composite
;
38 import org
.eclipse
.swt
.widgets
.Label
;
39 import org
.eclipse
.swt
.widgets
.Text
;
41 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureNodeService
;
42 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
43 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
44 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
45 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
46 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
50 * EditFeatureTreeWizardPage class.
54 * @created Aug 5, 2010
57 public class EditFeatureTreeWizardPage
extends WizardPage
implements
58 ModifyListener
, ISelectionChangedListener
{
60 private TreeViewer viewer
;
61 private Label label_title
;
62 private Text text_title
;
63 private Button button_add
;
64 private Button button_remove
;
65 private Label label_treeInfo
;
66 private FeatureTree featureTree
;
70 * Constructor for EditFeatureTreeWizardPage.
74 * a {@link java.lang.String} object.
76 protected EditFeatureTreeWizardPage(String pageName
) {
78 setMessage("Edit the Feature Tree.");
85 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
90 public void createControl(Composite parent
) {
91 Composite composite
= new Composite(parent
, SWT
.NULL
);
92 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
93 composite
.setLayout(new GridLayout());
95 Composite composite_treeTitle
= new Composite(composite
, SWT
.NULL
);
96 composite_treeTitle
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true,
98 composite_treeTitle
.setLayout(new GridLayout(2, false));
100 label_title
= new Label(composite_treeTitle
, SWT
.NULL
);
101 label_title
.setText("Title");
103 text_title
= new Text(composite_treeTitle
, SWT
.NULL
);
104 text_title
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false));
106 Composite composite_treeContent
= new Composite(composite
, SWT
.NULL
);
107 composite_treeContent
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
109 composite_treeContent
.setLayout(new GridLayout(2, false));
111 viewer
= new TreeViewer(composite_treeContent
);
112 viewer
.getControl().setLayoutData(
113 new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
115 Composite composite_buttons
= new Composite(composite_treeContent
,
117 composite_buttons
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.TOP
, false,
119 composite_buttons
.setLayout(new GridLayout());
121 button_add
= new Button(composite_buttons
, SWT
.PUSH
);
122 button_add
.setText("Add");
123 button_add
.setToolTipText("Add a feature to this feature treee.");
124 button_remove
= new Button(composite_buttons
, SWT
.PUSH
);
125 button_remove
.setText("Remove");
127 .setToolTipText("Remove a feature from this feature tree.");
129 label_treeInfo
= new Label(composite
, SWT
.NULL
);
131 .setText("Order and nesting of feature nodes may be changed through drag and drop.");
133 viewer
.setContentProvider(new FeatureTreeContentProvider());
134 viewer
.setLabelProvider(new FeatureTreeLabelProvider());
136 int ops
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
137 Transfer
[] transfers
= new Transfer
[] { FeatureNodeTransfer
139 viewer
.addDragSupport(ops
, transfers
, new FeatureNodeDragListener(
141 viewer
.addDropSupport(ops
, transfers
,
142 new FeatureNodeDropAdapter(viewer
));
144 viewer
.addSelectionChangedListener(this);
146 button_add
.addSelectionListener(new AddButtonListener());
147 button_remove
.addSelectionListener(new RemoveSelectionListener());
149 setControl(composite
);
158 * a {@link eu.etaxonomy.cdm.model.description.FeatureTree}
161 public void setSelectedTree(FeatureTree featureTree
) {
162 this.featureTree
= featureTree
;
163 viewer
.setInput(featureTree
);
165 text_title
.removeModifyListener(this);
166 if (featureTree
!= null){
167 text_title
.setText(featureTree
.getTitleCache());
169 text_title
.addModifyListener(this);
174 public void modifyText(ModifyEvent e
) {
175 featureTree
.setTitleCache(text_title
.getText(), true);
180 public void selectionChanged(SelectionChangedEvent event
) {
181 IStructuredSelection selection
= (IStructuredSelection
) event
184 button_add
.setEnabled(selection
.size() <= 1);
185 button_remove
.setEnabled(selection
.size() > 0);
188 private class AddButtonListener
extends SelectionAdapter
{
190 public void widgetSelected(SelectionEvent e
) {
191 AvailableFeaturesWizard wizard
= new AvailableFeaturesWizard(
193 WizardDialog dialog
= new WizardDialog(getShell(), wizard
);
195 if (dialog
.open() == IStatus
.OK
) {
196 IStructuredSelection selection
= (IStructuredSelection
) viewer
198 FeatureNode parent
= (FeatureNode
) (selection
.getFirstElement() != null ? selection
199 .getFirstElement() : ((FeatureTree
) viewer
.getInput())
201 Collection
<Feature
> additionalFeatures
= wizard
202 .getAdditionalFeatures();
203 for (Feature feature
: additionalFeatures
) {
204 FeatureNode child
= FeatureNode
.NewInstance(feature
);
205 if(CdmStore
.getCurrentSessionManager().isRemoting()) {
206 CdmStore
.getService(IFeatureNodeService
.class).merge(child
, true);
208 CdmStore
.getService(IFeatureNodeService
.class).saveOrUpdate(child
);
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
);
232 private class FeatureNodeDragListener
extends DragSourceAdapter
{
234 private final TreeViewer viewer
;
236 public FeatureNodeDragListener(TreeViewer viewer
) {
237 this.viewer
= viewer
;
241 * Method declared on DragSourceListener
244 public void dragFinished(DragSourceEvent event
) {
248 // if the featureNode was moved, remove it from the source viewer
249 if (event
.detail
== DND
.DROP_MOVE
) {
250 IStructuredSelection selection
= (IStructuredSelection
) viewer
257 * Method declared on DragSourceListener
260 public void dragSetData(DragSourceEvent event
) {
261 IStructuredSelection selection
= (IStructuredSelection
) viewer
263 FeatureNode
[] featureNodes
= (FeatureNode
[]) selection
.toList()
264 .toArray(new FeatureNode
[selection
.size()]);
265 if (FeatureNodeTransfer
.getInstance().isSupportedType(
267 event
.data
= featureNodes
;
272 * Method declared on DragSourceListener
275 public void dragStart(DragSourceEvent event
) {
276 event
.doit
= !viewer
.getSelection().isEmpty();
281 private class FeatureNodeDropAdapter
extends ViewerDropAdapter
{
283 protected FeatureNodeDropAdapter(Viewer viewer
) {
288 public boolean performDrop(Object data
) {
289 FeatureNode target
= (FeatureNode
) getCurrentTarget();
292 if (target
!= null) {
293 int location
= getCurrentLocation();
294 FeatureNode parent
= target
.getParent();
295 if (location
== LOCATION_BEFORE
) {
296 position
= Math
.max(0, parent
.getIndex(target
) - 1);
300 if (location
== LOCATION_AFTER
) {
301 position
= parent
.getIndex(target
);
306 // set target to root node if there is no target specified
307 if (target
== null) {
308 FeatureTree featureTree
= (FeatureTree
) getViewer().getInput();
309 target
= featureTree
.getRoot();
312 Object
[] droppedObjects
= (Object
[]) data
;
313 TreeViewer viewer
= (TreeViewer
) getViewer();
315 // cannot drop a feature node onto itself
316 for (Object droppedObject
: droppedObjects
) {
317 if (droppedObject
== null) {
318 MessagingUtils
.warningDialog(
319 "Operation not supported yet",
321 "We are currently unable to change the order of freshly created "
322 + "feature trees nodes. Please close and reopen the dialog to change the order of features.");
325 if (droppedObject
.equals(target
)) {
329 for (Object droppedObject
: droppedObjects
) {
330 FeatureNode droppedNode
= (FeatureNode
) droppedObject
;
331 target
.addChild(droppedNode
, position
);
332 viewer
.add(target
, droppedNode
);
333 viewer
.reveal(droppedNode
);
339 public boolean validateDrop(Object target
, int operation
,
340 TransferData transferData
) {
341 return FeatureNodeTransfer
.getInstance().isSupportedType(