a39b9ff525836a4753f2ff3babfe259afd50dfa4
[taxeditor.git] / eu.etaxonomy.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.model.MessagingUtils;
46 import eu.etaxonomy.taxeditor.store.CdmStore;
47
48 /**
49 * <p>
50 * EditFeatureTreeWizardPage class.
51 * </p>
52 *
53 * @author n.hoffmann
54 * @created Aug 5, 2010
55 * @version 1.0
56 */
57 public class EditFeatureTreeWizardPage extends WizardPage implements
58 ModifyListener, ISelectionChangedListener {
59
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;
67
68 /**
69 * <p>
70 * Constructor for EditFeatureTreeWizardPage.
71 * </p>
72 *
73 * @param pageName
74 * a {@link java.lang.String} object.
75 */
76 protected EditFeatureTreeWizardPage(String pageName) {
77 super(pageName);
78 setMessage("Edit the Feature Tree.");
79 }
80
81 /*
82 * (non-Javadoc)
83 *
84 * @see
85 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
86 * .Composite)
87 */
88 /** {@inheritDoc} */
89 @Override
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());
94
95 Composite composite_treeTitle = new Composite(composite, SWT.NULL);
96 composite_treeTitle.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
97 false));
98 composite_treeTitle.setLayout(new GridLayout(2, false));
99
100 label_title = new Label(composite_treeTitle, SWT.NULL);
101 label_title.setText("Title");
102
103 text_title = new Text(composite_treeTitle, SWT.NULL);
104 text_title.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
105
106 Composite composite_treeContent = new Composite(composite, SWT.NULL);
107 composite_treeContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
108 true, true));
109 composite_treeContent.setLayout(new GridLayout(2, false));
110
111 viewer = new TreeViewer(composite_treeContent);
112 viewer.getControl().setLayoutData(
113 new GridData(SWT.FILL, SWT.FILL, true, true));
114
115 Composite composite_buttons = new Composite(composite_treeContent,
116 SWT.NULL);
117 composite_buttons.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false,
118 false));
119 composite_buttons.setLayout(new GridLayout());
120
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");
126 button_remove
127 .setToolTipText("Remove a feature from this feature tree.");
128
129 label_treeInfo = new Label(composite, SWT.NULL);
130 label_treeInfo
131 .setText("Order and nesting of feature nodes may be changed through drag and drop.");
132
133 viewer.setContentProvider(new FeatureTreeContentProvider());
134 viewer.setLabelProvider(new FeatureTreeLabelProvider());
135
136 int ops = DND.DROP_COPY | DND.DROP_MOVE;
137 Transfer[] transfers = new Transfer[] { FeatureNodeTransfer
138 .getInstance() };
139 viewer.addDragSupport(ops, transfers, new FeatureNodeDragListener(
140 viewer));
141 viewer.addDropSupport(ops, transfers,
142 new FeatureNodeDropAdapter(viewer));
143
144 viewer.addSelectionChangedListener(this);
145
146 button_add.addSelectionListener(new AddButtonListener());
147 button_remove.addSelectionListener(new RemoveSelectionListener());
148
149 setControl(composite);
150 }
151
152 /**
153 * <p>
154 * setSelectedTree
155 * </p>
156 *
157 * @param featureTree
158 * a {@link eu.etaxonomy.cdm.model.description.FeatureTree}
159 * object.
160 */
161 public void setSelectedTree(FeatureTree featureTree) {
162 this.featureTree = featureTree;
163 viewer.setInput(featureTree);
164
165 text_title.removeModifyListener(this);
166 text_title.setText(featureTree.getTitleCache());
167 text_title.addModifyListener(this);
168 }
169
170 /** {@inheritDoc} */
171 @Override
172 public void modifyText(ModifyEvent e) {
173 featureTree.setTitleCache(text_title.getText(), true);
174 }
175
176 /** {@inheritDoc} */
177 @Override
178 public void selectionChanged(SelectionChangedEvent event) {
179 IStructuredSelection selection = (IStructuredSelection) event
180 .getSelection();
181
182 button_add.setEnabled(selection.size() <= 1);
183 button_remove.setEnabled(selection.size() > 0);
184 }
185
186 private class AddButtonListener extends SelectionAdapter {
187 @Override
188 public void widgetSelected(SelectionEvent e) {
189 AvailableFeaturesWizard wizard = new AvailableFeaturesWizard(
190 featureTree);
191 WizardDialog dialog = new WizardDialog(getShell(), wizard);
192
193 if (dialog.open() == IStatus.OK) {
194 IStructuredSelection selection = (IStructuredSelection) viewer
195 .getSelection();
196 FeatureNode parent = (FeatureNode) (selection.getFirstElement() != null ? selection
197 .getFirstElement() : ((FeatureTree) viewer.getInput())
198 .getRoot());
199 Collection<Feature> additionalFeatures = wizard
200 .getAdditionalFeatures();
201 for (Feature feature : additionalFeatures) {
202 FeatureNode child = FeatureNode.NewInstance(feature);
203 if(CdmStore.getCurrentSessionManager().isRemoting()) {
204 CdmStore.getService(IFeatureNodeService.class).merge(child, true);
205 } else {
206 CdmStore.getService(IFeatureNodeService.class).saveOrUpdate(child);
207 }
208 parent.addChild(child);
209 }
210 viewer.refresh();
211 }
212 }
213 }
214
215 private class RemoveSelectionListener extends SelectionAdapter {
216 @Override
217 public void widgetSelected(SelectionEvent e) {
218 IStructuredSelection selection = (IStructuredSelection) viewer
219 .getSelection();
220
221 for (Object selectedObject : selection.toArray()) {
222 FeatureNode featureNode = (FeatureNode) selectedObject;
223 FeatureNode parent = featureNode.getParent();
224 parent.removeChild(featureNode);
225 }
226 viewer.refresh();
227 }
228 }
229
230 private class FeatureNodeDragListener extends DragSourceAdapter {
231
232 private final TreeViewer viewer;
233
234 public FeatureNodeDragListener(TreeViewer viewer) {
235 this.viewer = viewer;
236 }
237
238 /**
239 * Method declared on DragSourceListener
240 */
241 @Override
242 public void dragFinished(DragSourceEvent event) {
243 if (!event.doit) {
244 return;
245 }
246 // if the featureNode was moved, remove it from the source viewer
247 if (event.detail == DND.DROP_MOVE) {
248 IStructuredSelection selection = (IStructuredSelection) viewer
249 .getSelection();
250 viewer.refresh();
251 }
252 }
253
254 /**
255 * Method declared on DragSourceListener
256 */
257 @Override
258 public void dragSetData(DragSourceEvent event) {
259 IStructuredSelection selection = (IStructuredSelection) viewer
260 .getSelection();
261 FeatureNode[] featureNodes = (FeatureNode[]) selection.toList()
262 .toArray(new FeatureNode[selection.size()]);
263 if (FeatureNodeTransfer.getInstance().isSupportedType(
264 event.dataType)) {
265 event.data = featureNodes;
266 }
267 }
268
269 /**
270 * Method declared on DragSourceListener
271 */
272 @Override
273 public void dragStart(DragSourceEvent event) {
274 event.doit = !viewer.getSelection().isEmpty();
275 }
276
277 }
278
279 private class FeatureNodeDropAdapter extends ViewerDropAdapter {
280
281 protected FeatureNodeDropAdapter(Viewer viewer) {
282 super(viewer);
283 }
284
285 @Override
286 public boolean performDrop(Object data) {
287 FeatureNode target = (FeatureNode) getCurrentTarget();
288 int position = 0;
289
290 if (target != null) {
291 int location = getCurrentLocation();
292 FeatureNode parent = target.getParent();
293 if (location == LOCATION_BEFORE) {
294 position = Math.max(0, parent.getIndex(target) - 1);
295 target = parent;
296 }
297
298 if (location == LOCATION_AFTER) {
299 position = parent.getIndex(target);
300 target = parent;
301 }
302 }
303
304 // set target to root node if there is no target specified
305 if (target == null) {
306 FeatureTree featureTree = (FeatureTree) getViewer().getInput();
307 target = featureTree.getRoot();
308 }
309
310 Object[] droppedObjects = (Object[]) data;
311 TreeViewer viewer = (TreeViewer) getViewer();
312
313 // cannot drop a feature node onto itself
314 for (Object droppedObject : droppedObjects) {
315 if (droppedObject == null) {
316 MessagingUtils.warningDialog(
317 "Operation not supported yet",
318 this,
319 "We are currently unable to change the order of freshly created "
320 + "feature trees nodes. Please close and reopen the dialog to change the order of features.");
321 return false;
322 }
323 if (droppedObject.equals(target)) {
324 return false;
325 }
326 }
327 for (Object droppedObject : droppedObjects) {
328 FeatureNode droppedNode = (FeatureNode) droppedObject;
329 target.addChild(droppedNode, position);
330 viewer.add(target, droppedNode);
331 viewer.reveal(droppedNode);
332 }
333 return true;
334 }
335
336 @Override
337 public boolean validateDrop(Object target, int operation,
338 TransferData transferData) {
339 return FeatureNodeTransfer.getInstance().isSupportedType(
340 transferData);
341 }
342
343 }
344 }