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
.store
.CdmStore
;
46 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
49 * <p>EditFeatureTreeWizardPage class.</p>
52 * @created Aug 5, 2010
55 public class EditFeatureTreeWizardPage
extends WizardPage
implements ModifyListener
, ISelectionChangedListener
{
57 private TreeViewer viewer
;
58 private Label label_title
;
59 private Text text_title
;
60 private Button button_add
;
61 private Button button_remove
;
62 private Label label_treeInfo
;
63 private FeatureTree featureTree
;
66 * <p>Constructor for EditFeatureTreeWizardPage.</p>
68 * @param pageName a {@link java.lang.String} object.
70 protected EditFeatureTreeWizardPage(String pageName
) {
72 setMessage("Edit the Feature Tree.");
79 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
84 public void createControl(Composite parent
) {
85 Composite composite
= new Composite(parent
, SWT
.NULL
);
86 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
87 composite
.setLayout(new GridLayout());
89 Composite composite_treeTitle
= new Composite(composite
, SWT
.NULL
);
90 composite_treeTitle
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true,
92 composite_treeTitle
.setLayout(new GridLayout(2, false));
94 label_title
= new Label(composite_treeTitle
, SWT
.NULL
);
95 label_title
.setText("Title");
97 text_title
= new Text(composite_treeTitle
, SWT
.NULL
);
98 text_title
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false));
100 Composite composite_treeContent
= new Composite(composite
, SWT
.NULL
);
101 composite_treeContent
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
103 composite_treeContent
.setLayout(new GridLayout(2, false));
105 viewer
= new TreeViewer(composite_treeContent
);
106 viewer
.getControl().setLayoutData(
107 new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
109 Composite composite_buttons
= new Composite(composite_treeContent
,
111 composite_buttons
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.TOP
, false,
113 composite_buttons
.setLayout(new GridLayout());
115 button_add
= new Button(composite_buttons
, SWT
.PUSH
);
116 button_add
.setText("Add");
117 button_add
.setToolTipText("Add a feature to this feature treee.");
118 button_remove
= new Button(composite_buttons
, SWT
.PUSH
);
119 button_remove
.setText("Remove");
121 .setToolTipText("Remove a feature from this feature tree.");
123 label_treeInfo
= new Label(composite
, SWT
.NULL
);
124 label_treeInfo
.setText("Order and nesting of feature nodes may be changed through drag and drop.");
126 viewer
.setContentProvider(new FeatureTreeContentProvider());
127 viewer
.setLabelProvider(new FeatureTreeLabelProvider());
131 int ops
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
132 Transfer
[] transfers
= new Transfer
[] { FeatureNodeTransfer
134 viewer
.addDragSupport(ops
, transfers
, new FeatureNodeDragListener(
136 viewer
.addDropSupport(ops
, transfers
,
137 new FeatureNodeDropAdapter(viewer
));
139 viewer
.addSelectionChangedListener(this);
141 button_add
.addSelectionListener(new AddButtonListener());
142 button_remove
.addSelectionListener(new RemoveSelectionListener());
144 setControl(composite
);
148 * <p>setSelectedTree</p>
150 * @param featureTree a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
152 public void setSelectedTree(FeatureTree featureTree
) {
153 this.featureTree
= featureTree
;
154 viewer
.setInput(featureTree
);
156 text_title
.removeModifyListener(this);
157 text_title
.setText(featureTree
.getTitleCache());
158 text_title
.addModifyListener(this);
163 public void modifyText(ModifyEvent e
) {
164 featureTree
.setTitleCache(text_title
.getText(), true);
169 public void selectionChanged(SelectionChangedEvent event
) {
170 IStructuredSelection selection
= (IStructuredSelection
) event
.getSelection();
172 button_add
.setEnabled(selection
.size() <= 1);
173 button_remove
.setEnabled(selection
.size() > 0);
176 private class AddButtonListener
extends SelectionAdapter
{
178 public void widgetSelected(SelectionEvent e
) {
179 AvailableFeaturesWizard wizard
= new AvailableFeaturesWizard(featureTree
);
180 WizardDialog dialog
= new WizardDialog(getShell(), wizard
);
182 if(dialog
.open() == IStatus
.OK
){
183 IStructuredSelection selection
= (IStructuredSelection
) viewer
.getSelection();
184 FeatureNode parent
= (FeatureNode
) (selection
.getFirstElement() != null ? selection
.getFirstElement() : ((FeatureTree
) viewer
.getInput()).getRoot());
185 Collection
<Feature
> additionalFeatures
= wizard
.getAdditionalFeatures();
186 for(Feature feature
: additionalFeatures
){
187 FeatureNode child
= FeatureNode
.NewInstance(feature
);
188 CdmStore
.getService(IFeatureNodeService
.class).saveOrUpdate(child
);
189 parent
.addChild(child
);
197 private class RemoveSelectionListener
extends SelectionAdapter
{
199 public void widgetSelected(SelectionEvent e
) {
200 IStructuredSelection selection
= (IStructuredSelection
) viewer
.getSelection();
202 for(Object selectedObject
: selection
.toArray()){
203 FeatureNode featureNode
= (FeatureNode
) selectedObject
;
204 FeatureNode parent
= featureNode
.getParent();
205 parent
.removeChild(featureNode
);
211 private class FeatureNodeDragListener
extends DragSourceAdapter
{
213 private TreeViewer viewer
;
215 public FeatureNodeDragListener(TreeViewer viewer
) {
216 this.viewer
= viewer
;
220 * Method declared on DragSourceListener
222 public void dragFinished(DragSourceEvent event
) {
225 // if the featureNode was moved, remove it from the source viewer
226 if (event
.detail
== DND
.DROP_MOVE
) {
227 IStructuredSelection selection
= (IStructuredSelection
) viewer
234 * Method declared on DragSourceListener
236 public void dragSetData(DragSourceEvent event
) {
237 IStructuredSelection selection
= (IStructuredSelection
) viewer
239 FeatureNode
[] featureNodes
= (FeatureNode
[]) selection
.toList().toArray(new FeatureNode
[selection
.size()]);
240 if (FeatureNodeTransfer
.getInstance().isSupportedType(
242 event
.data
= featureNodes
;
247 * Method declared on DragSourceListener
249 public void dragStart(DragSourceEvent event
) {
250 event
.doit
= !viewer
.getSelection().isEmpty();
255 private class FeatureNodeDropAdapter
extends ViewerDropAdapter
{
257 protected FeatureNodeDropAdapter(Viewer viewer
) {
262 public boolean performDrop(Object data
) {
263 FeatureNode target
= (FeatureNode
) getCurrentTarget();
266 if (target
!= null) {
267 int location
= getCurrentLocation();
268 FeatureNode parent
= target
.getParent();
269 if (location
== LOCATION_BEFORE
){
270 position
= parent
.getIndex(target
) - 1;
274 if(location
== LOCATION_AFTER
) {
275 position
= parent
.getIndex(target
);
280 // set target to root node if there is no target specified
281 if (target
== null) {
282 FeatureTree featureTree
= (FeatureTree
) getViewer().getInput();
283 target
= featureTree
.getRoot();
286 FeatureNode
[] droppedNodes
= (FeatureNode
[]) data
;
287 TreeViewer viewer
= (TreeViewer
) getViewer();
289 // cannot drop a feature node onto itself
290 for (FeatureNode droppedNode
: droppedNodes
) {
291 if (droppedNode
== null){
292 StoreUtil
.warningDialog("Operation not supported yet", this, "We are currently unable to change the order of freshly created " +
293 "feature trees nodes. Please close and reopen the dialog to change the order of features.");
296 if (droppedNode
.equals(target
)) {
300 for (FeatureNode droppedNode
: droppedNodes
) {
301 target
.addChild(droppedNode
, position
);
302 viewer
.add(target
, droppedNode
);
303 viewer
.reveal(droppedNode
);
309 public boolean validateDrop(Object target
, int operation
,
310 TransferData transferData
) {
311 return FeatureNodeTransfer
.getInstance().isSupportedType(