automated build configuration is on its way
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / featuretree / EditFeatureTreeWizardPage.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.featuretree;
12
13 import java.util.Collection;
14
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;
40
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;
47
48 /**
49 * <p>EditFeatureTreeWizardPage class.</p>
50 *
51 * @author n.hoffmann
52 * @created Aug 5, 2010
53 * @version 1.0
54 */
55 public class EditFeatureTreeWizardPage extends WizardPage implements ModifyListener, ISelectionChangedListener{
56
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;
64
65 /**
66 * <p>Constructor for EditFeatureTreeWizardPage.</p>
67 *
68 * @param pageName a {@link java.lang.String} object.
69 */
70 protected EditFeatureTreeWizardPage(String pageName) {
71 super(pageName);
72 setMessage("Edit the Feature Tree.");
73 }
74
75 /*
76 * (non-Javadoc)
77 *
78 * @see
79 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
80 * .Composite)
81 */
82 /** {@inheritDoc} */
83 @Override
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());
88
89 Composite composite_treeTitle = new Composite(composite, SWT.NULL);
90 composite_treeTitle.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
91 false));
92 composite_treeTitle.setLayout(new GridLayout(2, false));
93
94 label_title = new Label(composite_treeTitle, SWT.NULL);
95 label_title.setText("Title");
96
97 text_title = new Text(composite_treeTitle, SWT.NULL);
98 text_title.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
99
100 Composite composite_treeContent = new Composite(composite, SWT.NULL);
101 composite_treeContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
102 true, true));
103 composite_treeContent.setLayout(new GridLayout(2, false));
104
105 viewer = new TreeViewer(composite_treeContent);
106 viewer.getControl().setLayoutData(
107 new GridData(SWT.FILL, SWT.FILL, true, true));
108
109 Composite composite_buttons = new Composite(composite_treeContent,
110 SWT.NULL);
111 composite_buttons.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false,
112 false));
113 composite_buttons.setLayout(new GridLayout());
114
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");
120 button_remove
121 .setToolTipText("Remove a feature from this feature tree.");
122
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.");
125
126 viewer.setContentProvider(new FeatureTreeContentProvider());
127 viewer.setLabelProvider(new FeatureTreeLabelProvider());
128
129
130
131 int ops = DND.DROP_COPY | DND.DROP_MOVE;
132 Transfer[] transfers = new Transfer[] { FeatureNodeTransfer
133 .getInstance() };
134 viewer.addDragSupport(ops, transfers, new FeatureNodeDragListener(
135 viewer));
136 viewer.addDropSupport(ops, transfers,
137 new FeatureNodeDropAdapter(viewer));
138
139 viewer.addSelectionChangedListener(this);
140
141 button_add.addSelectionListener(new AddButtonListener());
142 button_remove.addSelectionListener(new RemoveSelectionListener());
143
144 setControl(composite);
145 }
146
147 /**
148 * <p>setSelectedTree</p>
149 *
150 * @param featureTree a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
151 */
152 public void setSelectedTree(FeatureTree featureTree) {
153 this.featureTree = featureTree;
154 viewer.setInput(featureTree);
155
156 text_title.removeModifyListener(this);
157 text_title.setText(featureTree.getTitleCache());
158 text_title.addModifyListener(this);
159 }
160
161 /** {@inheritDoc} */
162 @Override
163 public void modifyText(ModifyEvent e) {
164 featureTree.setTitleCache(text_title.getText(), true);
165 }
166
167 /** {@inheritDoc} */
168 @Override
169 public void selectionChanged(SelectionChangedEvent event) {
170 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
171
172 button_add.setEnabled(selection.size() <= 1);
173 button_remove.setEnabled(selection.size() > 0);
174 }
175
176 private class AddButtonListener extends SelectionAdapter{
177 @Override
178 public void widgetSelected(SelectionEvent e) {
179 AvailableFeaturesWizard wizard = new AvailableFeaturesWizard(featureTree);
180 WizardDialog dialog = new WizardDialog(getShell(), wizard);
181
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);
190 }
191 viewer.refresh();
192 }
193 }
194 }
195
196
197 private class RemoveSelectionListener extends SelectionAdapter{
198 @Override
199 public void widgetSelected(SelectionEvent e) {
200 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
201
202 for(Object selectedObject : selection.toArray()){
203 FeatureNode featureNode = (FeatureNode) selectedObject;
204 FeatureNode parent = featureNode.getParent();
205 parent.removeChild(featureNode);
206 }
207 viewer.refresh();
208 }
209 }
210
211 private class FeatureNodeDragListener extends DragSourceAdapter {
212
213 private TreeViewer viewer;
214
215 public FeatureNodeDragListener(TreeViewer viewer) {
216 this.viewer = viewer;
217 }
218
219 /**
220 * Method declared on DragSourceListener
221 */
222 public void dragFinished(DragSourceEvent event) {
223 if (!event.doit)
224 return;
225 // if the featureNode was moved, remove it from the source viewer
226 if (event.detail == DND.DROP_MOVE) {
227 IStructuredSelection selection = (IStructuredSelection) viewer
228 .getSelection();
229 viewer.refresh();
230 }
231 }
232
233 /**
234 * Method declared on DragSourceListener
235 */
236 public void dragSetData(DragSourceEvent event) {
237 IStructuredSelection selection = (IStructuredSelection) viewer
238 .getSelection();
239 FeatureNode[] featureNodes = (FeatureNode[]) selection.toList().toArray(new FeatureNode[selection.size()]);
240 if (FeatureNodeTransfer.getInstance().isSupportedType(
241 event.dataType)) {
242 event.data = featureNodes;
243 }
244 }
245
246 /**
247 * Method declared on DragSourceListener
248 */
249 public void dragStart(DragSourceEvent event) {
250 event.doit = !viewer.getSelection().isEmpty();
251 }
252
253 }
254
255 private class FeatureNodeDropAdapter extends ViewerDropAdapter {
256
257 protected FeatureNodeDropAdapter(Viewer viewer) {
258 super(viewer);
259 }
260
261 @Override
262 public boolean performDrop(Object data) {
263 FeatureNode target = (FeatureNode) getCurrentTarget();
264 int position = 0;
265
266 if (target != null) {
267 int location = getCurrentLocation();
268 FeatureNode parent = target.getParent();
269 if (location == LOCATION_BEFORE){
270 position = parent.getIndex(target) - 1;
271 target = parent;
272 }
273
274 if(location == LOCATION_AFTER) {
275 position = parent.getIndex(target);
276 target = parent;
277 }
278 }
279
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();
284 }
285
286 FeatureNode[] droppedNodes = (FeatureNode[]) data;
287 TreeViewer viewer = (TreeViewer) getViewer();
288
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.");
294 return false;
295 }
296 if (droppedNode.equals(target)) {
297 return false;
298 }
299 }
300 for (FeatureNode droppedNode : droppedNodes) {
301 target.addChild(droppedNode, position);
302 viewer.add(target, droppedNode);
303 viewer.reveal(droppedNode);
304 }
305 return true;
306 }
307
308 @Override
309 public boolean validateDrop(Object target, int operation,
310 TransferData transferData) {
311 return FeatureNodeTransfer.getInstance().isSupportedType(
312 transferData);
313 }
314
315 }
316 }