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
.model
.description
.Feature
;
42 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
43 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
44 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
45 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
48 * <p>EditFeatureTreeWizardPage class.</p>
51 * @created Aug 5, 2010
54 public class EditFeatureTreeWizardPage
extends WizardPage
implements ModifyListener
, ISelectionChangedListener
{
56 private TreeViewer viewer
;
57 private Label label_title
;
58 private Text text_title
;
59 private Button button_add
;
60 private Button button_remove
;
61 private Label label_treeInfo
;
62 private FeatureTree featureTree
;
65 * <p>Constructor for EditFeatureTreeWizardPage.</p>
67 * @param pageName a {@link java.lang.String} object.
69 protected EditFeatureTreeWizardPage(String pageName
) {
71 setMessage("Edit the Feature Tree.");
78 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
83 public void createControl(Composite parent
) {
84 Composite composite
= new Composite(parent
, SWT
.NULL
);
85 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
86 composite
.setLayout(new GridLayout());
88 Composite composite_treeTitle
= new Composite(composite
, SWT
.NULL
);
89 composite_treeTitle
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true,
91 composite_treeTitle
.setLayout(new GridLayout(2, false));
93 label_title
= new Label(composite_treeTitle
, SWT
.NULL
);
94 label_title
.setText("Title");
96 text_title
= new Text(composite_treeTitle
, SWT
.NULL
);
97 text_title
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false));
99 Composite composite_treeContent
= new Composite(composite
, SWT
.NULL
);
100 composite_treeContent
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
102 composite_treeContent
.setLayout(new GridLayout(2, false));
104 viewer
= new TreeViewer(composite_treeContent
);
105 viewer
.getControl().setLayoutData(
106 new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
108 Composite composite_buttons
= new Composite(composite_treeContent
,
110 composite_buttons
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.TOP
, false,
112 composite_buttons
.setLayout(new GridLayout());
114 button_add
= new Button(composite_buttons
, SWT
.PUSH
);
115 button_add
.setText("Add");
116 button_add
.setToolTipText("Add a feature to this feature treee.");
117 button_remove
= new Button(composite_buttons
, SWT
.PUSH
);
118 button_remove
.setText("Remove");
120 .setToolTipText("Remove a feature from this feature tree.");
122 label_treeInfo
= new Label(composite
, SWT
.NULL
);
123 label_treeInfo
.setText("Order and nesting of feature nodes may be changed through drag and drop.");
125 viewer
.setContentProvider(new FeatureTreeContentProvider());
126 viewer
.setLabelProvider(new FeatureTreeLabelProvider());
130 int ops
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
131 Transfer
[] transfers
= new Transfer
[] { FeatureNodeTransfer
133 viewer
.addDragSupport(ops
, transfers
, new FeatureNodeDragListener(
135 viewer
.addDropSupport(ops
, transfers
,
136 new FeatureNodeDropAdapter(viewer
));
138 viewer
.addSelectionChangedListener(this);
140 button_add
.addSelectionListener(new AddButtonListener());
141 button_remove
.addSelectionListener(new RemoveSelectionListener());
143 setControl(composite
);
147 * <p>setSelectedTree</p>
149 * @param featureTree a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
151 public void setSelectedTree(FeatureTree featureTree
) {
152 this.featureTree
= featureTree
;
153 viewer
.setInput(featureTree
);
155 text_title
.removeModifyListener(this);
156 text_title
.setText(featureTree
.getTitleCache());
157 text_title
.addModifyListener(this);
162 public void modifyText(ModifyEvent e
) {
163 featureTree
.setTitleCache(text_title
.getText(), true);
168 public void selectionChanged(SelectionChangedEvent event
) {
169 IStructuredSelection selection
= (IStructuredSelection
) event
.getSelection();
171 button_add
.setEnabled(selection
.size() <= 1);
172 button_remove
.setEnabled(selection
.size() > 0);
175 private class AddButtonListener
extends SelectionAdapter
{
177 public void widgetSelected(SelectionEvent e
) {
178 AvailableFeaturesWizard wizard
= new AvailableFeaturesWizard(featureTree
);
179 WizardDialog dialog
= new WizardDialog(getShell(), wizard
);
181 if(dialog
.open() == IStatus
.OK
){
182 IStructuredSelection selection
= (IStructuredSelection
) viewer
.getSelection();
183 FeatureNode parent
= (FeatureNode
) (selection
.getFirstElement() != null ? selection
.getFirstElement() : ((FeatureTree
) viewer
.getInput()).getRoot());
184 Collection
<Feature
> additionalFeatures
= wizard
.getAdditionalFeatures();
185 for(Feature feature
: additionalFeatures
){
186 FeatureNode child
= FeatureNode
.NewInstance(feature
);
187 CdmStore
.getFeatureNodeService().saveOrUpdate(child
);
188 parent
.addChild(child
);
196 private class RemoveSelectionListener
extends SelectionAdapter
{
198 public void widgetSelected(SelectionEvent e
) {
199 IStructuredSelection selection
= (IStructuredSelection
) viewer
.getSelection();
201 for(Object selectedObject
: selection
.toArray()){
202 FeatureNode featureNode
= (FeatureNode
) selectedObject
;
203 FeatureNode parent
= featureNode
.getParent();
204 parent
.removeChild(featureNode
);
209 private class FeatureNodeDragListener
extends DragSourceAdapter
{
211 private TreeViewer viewer
;
213 public FeatureNodeDragListener(TreeViewer viewer
) {
214 this.viewer
= viewer
;
218 * Method declared on DragSourceListener
220 public void dragFinished(DragSourceEvent event
) {
223 // if the featureNode was moved, remove it from the source viewer
224 if (event
.detail
== DND
.DROP_MOVE
) {
225 IStructuredSelection selection
= (IStructuredSelection
) viewer
232 * Method declared on DragSourceListener
234 public void dragSetData(DragSourceEvent event
) {
235 IStructuredSelection selection
= (IStructuredSelection
) viewer
237 FeatureNode
[] featureNodes
= (FeatureNode
[]) selection
.toList().toArray(new FeatureNode
[selection
.size()]);
238 if (FeatureNodeTransfer
.getInstance().isSupportedType(
240 event
.data
= featureNodes
;
245 * Method declared on DragSourceListener
247 public void dragStart(DragSourceEvent event
) {
248 event
.doit
= !viewer
.getSelection().isEmpty();
253 private class FeatureNodeDropAdapter
extends ViewerDropAdapter
{
255 protected FeatureNodeDropAdapter(Viewer viewer
) {
260 public boolean performDrop(Object data
) {
261 FeatureNode target
= (FeatureNode
) getCurrentTarget();
264 if (target
!= null) {
265 int location
= getCurrentLocation();
266 FeatureNode parent
= target
.getParent();
267 if (location
== LOCATION_BEFORE
){
268 position
= parent
.getIndex(target
) - 1;
272 if(location
== LOCATION_AFTER
) {
273 position
= parent
.getIndex(target
);
278 // set target to root node if there is no target specified
279 if (target
== null) {
280 FeatureTree featureTree
= (FeatureTree
) getViewer().getInput();
281 target
= featureTree
.getRoot();
284 FeatureNode
[] droppedNodes
= (FeatureNode
[]) data
;
285 TreeViewer viewer
= (TreeViewer
) getViewer();
287 // cannot drop a feature node onto itself
288 for (FeatureNode droppedNode
: droppedNodes
) {
289 if (droppedNode
== null){
290 StoreUtil
.warningDialog("Operation not supported yet", this, "We are currently unable to change the order of freshly created " +
291 "feature trees nodes. Please close and reopen the dialog to change the order of features.");
294 if (droppedNode
.equals(target
)) {
298 for (FeatureNode droppedNode
: droppedNodes
) {
299 target
.addChild(droppedNode
, position
);
300 viewer
.add(target
, droppedNode
);
301 viewer
.reveal(droppedNode
);
307 public boolean validateDrop(Object target
, int operation
,
308 TransferData transferData
) {
309 return FeatureNodeTransfer
.getInstance().isSupportedType(