Project

General

Profile

Download (10.7 KB) Statistics
| Branch: | Tag: | Revision:
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
import org.hibernate.proxy.HibernateProxy;
41

    
42
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
43
import eu.etaxonomy.cdm.api.service.config.FeatureNodeDeletionConfigurator;
44
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
45
import eu.etaxonomy.cdm.model.description.Feature;
46
import eu.etaxonomy.cdm.model.description.FeatureNode;
47
import eu.etaxonomy.cdm.model.description.FeatureTree;
48
import eu.etaxonomy.taxeditor.model.MessagingUtils;
49
import eu.etaxonomy.taxeditor.store.CdmStore;
50

    
51
/**
52
 * <p>
53
 * EditFeatureTreeWizardPage class.
54
 * </p>
55
 *
56
 * @author n.hoffmann
57
 * @created Aug 5, 2010
58
 * @version 1.0
59
 */
60
public class EditFeatureTreeWizardPage extends WizardPage implements
61
		ModifyListener, ISelectionChangedListener {
62

    
63
	private TreeViewer viewer;
64
	private Label label_title;
65
	private Text text_title;
66
	private Button button_add;
67
	private Button button_remove;
68
	private Label label_treeInfo;
69
	private FeatureTree featureTree;
70

    
71
	/**
72
	 * <p>
73
	 * Constructor for EditFeatureTreeWizardPage.
74
	 * </p>
75
	 *
76
	 * @param pageName
77
	 *            a {@link java.lang.String} object.
78
	 */
79
	protected EditFeatureTreeWizardPage(String pageName) {
80
		super(pageName);
81
		setMessage("Edit the Feature Tree.");
82
	}
83

    
84
	/*
85
	 * (non-Javadoc)
86
	 *
87
	 * @see
88
	 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
89
	 * .Composite)
90
	 */
91
	/** {@inheritDoc} */
92
	@Override
93
	public void createControl(Composite parent) {
94
		Composite composite = new Composite(parent, SWT.NULL);
95
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
96
		composite.setLayout(new GridLayout());
97

    
98
		Composite composite_treeTitle = new Composite(composite, SWT.NULL);
99
		composite_treeTitle.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
100
				false));
101
		composite_treeTitle.setLayout(new GridLayout(2, false));
102

    
103
		label_title = new Label(composite_treeTitle, SWT.NULL);
104
		label_title.setText("Title");
105

    
106
		text_title = new Text(composite_treeTitle, SWT.NULL);
107
		text_title.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
108

    
109
		Composite composite_treeContent = new Composite(composite, SWT.NULL);
110
		composite_treeContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
111
				true, true));
112
		composite_treeContent.setLayout(new GridLayout(2, false));
113

    
114
		viewer = new TreeViewer(composite_treeContent);
115
		viewer.getControl().setLayoutData(
116
				new GridData(SWT.FILL, SWT.FILL, true, true));
117

    
118
		Composite composite_buttons = new Composite(composite_treeContent,
119
				SWT.NULL);
120
		composite_buttons.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false,
121
				false));
122
		composite_buttons.setLayout(new GridLayout());
123

    
124
		button_add = new Button(composite_buttons, SWT.PUSH);
125
		button_add.setText("Add");
126
		button_add.setToolTipText("Add a feature to this feature treee.");
127
		button_remove = new Button(composite_buttons, SWT.PUSH);
128
		button_remove.setText("Remove");
129
		button_remove
130
				.setToolTipText("Remove a feature from this feature tree.");
131

    
132
		label_treeInfo = new Label(composite, SWT.NULL);
133
		label_treeInfo
134
				.setText("Order and nesting of feature nodes may be changed through drag and drop.");
135

    
136
		viewer.setContentProvider(new FeatureTreeContentProvider());
137
		viewer.setLabelProvider(new FeatureTreeLabelProvider());
138

    
139
		int ops = DND.DROP_COPY | DND.DROP_MOVE;
140
		Transfer[] transfers = new Transfer[] { FeatureNodeTransfer
141
				.getInstance() };
142
		viewer.addDragSupport(ops, transfers, new FeatureNodeDragListener(
143
				viewer));
144
		viewer.addDropSupport(ops, transfers,
145
				new FeatureNodeDropAdapter(viewer));
146

    
147
		viewer.addSelectionChangedListener(this);
148

    
149
		button_add.addSelectionListener(new AddButtonListener());
150
		button_remove.addSelectionListener(new RemoveSelectionListener());
151

    
152
		setControl(composite);
153
	}
154

    
155
	/**
156
	 * <p>
157
	 * setSelectedTree
158
	 * </p>
159
	 *
160
	 * @param featureTree
161
	 *            a {@link eu.etaxonomy.cdm.model.description.FeatureTree}
162
	 *            object.
163
	 */
164
	public void setSelectedTree(FeatureTree featureTree) {
165
		this.featureTree = HibernateProxyHelper.deproxy(featureTree, FeatureTree.class);
166
		this.featureTree.setRoot(HibernateProxyHelper.deproxy(featureTree.getRoot(), FeatureNode.class));
167
		viewer.setInput(featureTree);
168

    
169
		text_title.removeModifyListener(this);
170
		if (featureTree != null){
171
			text_title.setText(featureTree.getTitleCache());
172
		}
173
		text_title.addModifyListener(this);
174
	}
175

    
176
	/** {@inheritDoc} */
177
	@Override
178
	public void modifyText(ModifyEvent e) {
179
		featureTree.setTitleCache(text_title.getText(), true);
180
	}
181

    
182
	/** {@inheritDoc} */
183
	@Override
184
	public void selectionChanged(SelectionChangedEvent event) {
185
		IStructuredSelection selection = (IStructuredSelection) event
186
				.getSelection();
187

    
188
		button_add.setEnabled(selection.size() <= 1);
189
		button_remove.setEnabled(selection.size() > 0);
190
	}
191

    
192
	private class AddButtonListener extends SelectionAdapter {
193
		@Override
194
		public void widgetSelected(SelectionEvent e) {
195
			AvailableFeaturesWizard wizard = new AvailableFeaturesWizard(
196
					featureTree);
197
			WizardDialog dialog = new WizardDialog(getShell(), wizard);
198

    
199
			if (dialog.open() == IStatus.OK) {
200
				IStructuredSelection selection = (IStructuredSelection) viewer
201
						.getSelection();
202
				FeatureNode parent = (FeatureNode) (selection.getFirstElement() != null ? selection
203
						.getFirstElement() : ((FeatureTree) viewer.getInput())
204
						.getRoot());
205
				Collection<Feature> additionalFeatures = wizard
206
						.getAdditionalFeatures();
207
				for (Feature feature : additionalFeatures) {
208
					FeatureNode child = FeatureNode.NewInstance(feature);
209
					if(CdmStore.getCurrentSessionManager().isRemoting()) {
210
					    CdmStore.getService(IFeatureNodeService.class).merge(child, true);
211
					} else {
212
					    CdmStore.getService(IFeatureNodeService.class).saveOrUpdate(child);
213
					}
214
					parent.addChild(child);
215
				}
216
				viewer.refresh();
217
			}
218
		}
219
	}
220

    
221
	private class RemoveSelectionListener extends SelectionAdapter {
222
		@Override
223
		public void widgetSelected(SelectionEvent e) {
224
			IStructuredSelection selection = (IStructuredSelection) viewer
225
					.getSelection();
226

    
227
			for (Object selectedObject : selection.toArray()) {
228
				FeatureNode featureNode = (FeatureNode) selectedObject;
229
				FeatureNode parent = featureNode.getParent();
230
				parent.removeChild(featureNode);
231
				
232
				CdmStore.getService(IFeatureNodeService.class).deleteFeatureNode(featureNode.getUuid(), new FeatureNodeDeletionConfigurator());
233
				
234
			}
235
			viewer.refresh();
236
		}
237
	}
238

    
239
	private class FeatureNodeDragListener extends DragSourceAdapter {
240

    
241
		private final TreeViewer viewer;
242

    
243
		public FeatureNodeDragListener(TreeViewer viewer) {
244
			this.viewer = viewer;
245
		}
246

    
247
		/**
248
		 * Method declared on DragSourceListener
249
		 */
250
		@Override
251
		public void dragFinished(DragSourceEvent event) {
252
			if (!event.doit) {
253
                return;
254
            }
255
			// if the featureNode was moved, remove it from the source viewer
256
			if (event.detail == DND.DROP_MOVE) {
257
				IStructuredSelection selection = (IStructuredSelection) viewer
258
						.getSelection();
259
				viewer.refresh();
260
			}
261
		}
262

    
263
		/**
264
		 * Method declared on DragSourceListener
265
		 */
266
		@Override
267
		public void dragSetData(DragSourceEvent event) {
268
			IStructuredSelection selection = (IStructuredSelection) viewer
269
					.getSelection();
270
			FeatureNode[] featureNodes = (FeatureNode[]) selection.toList()
271
					.toArray(new FeatureNode[selection.size()]);
272
			if (FeatureNodeTransfer.getInstance().isSupportedType(
273
					event.dataType)) {
274
				event.data = featureNodes;
275
			}
276
		}
277

    
278
		/**
279
		 * Method declared on DragSourceListener
280
		 */
281
		@Override
282
		public void dragStart(DragSourceEvent event) {
283
			event.doit = !viewer.getSelection().isEmpty();
284
		}
285

    
286
	}
287

    
288
	private class FeatureNodeDropAdapter extends ViewerDropAdapter {
289

    
290
		protected FeatureNodeDropAdapter(Viewer viewer) {
291
			super(viewer);
292
		}
293

    
294
		@Override
295
		public boolean performDrop(Object data) {
296
			FeatureNode target = (FeatureNode) getCurrentTarget();
297
			int position = 0;
298

    
299
			if (target != null) {
300
				int location = getCurrentLocation();
301
				FeatureNode parent = target.getParent();
302
				if (location == LOCATION_BEFORE) {
303
					position = Math.max(0, parent.getIndex(target) - 1);
304
					target = parent;
305
				}
306

    
307
				if (location == LOCATION_AFTER) {
308
					position = parent.getIndex(target);
309
					target = parent;
310
				}
311
			}
312

    
313
			// set target to root node if there is no target specified
314
			if (target == null) {
315
				FeatureTree featureTree = (FeatureTree) getViewer().getInput();
316
				target = featureTree.getRoot();
317
			}
318

    
319
			Object[] droppedObjects = (Object[]) data;
320
			TreeViewer viewer = (TreeViewer) getViewer();
321

    
322
			// cannot drop a feature node onto itself
323
			for (Object droppedObject : droppedObjects) {
324
				if (droppedObject == null) {
325
					MessagingUtils.warningDialog(
326
									"Operation not supported yet",
327
									this,
328
									"We are currently unable to change the order of freshly created "
329
											+ "feature trees nodes. Please close and reopen the dialog to change the order of features.");
330
					return false;
331
				}
332
				if (droppedObject.equals(target)) {
333
					return false;
334
				}
335
			}
336
			for (Object droppedObject : droppedObjects) {
337
				FeatureNode droppedNode = (FeatureNode) droppedObject;
338
				target.addChild(droppedNode, position);
339
				viewer.add(target, droppedNode);
340
				viewer.reveal(droppedNode);
341
			}
342
			return true;
343
		}
344

    
345
		@Override
346
		public boolean validateDrop(Object target, int operation,
347
				TransferData transferData) {
348
			return FeatureNodeTransfer.getInstance().isSupportedType(
349
					transferData);
350
		}
351

    
352
	}
353
}
(3-3/10)