-/**
- * Copyright (C) 2007 EDIT
- * European Distributed Institute of Taxonomy
- * http://www.e-taxonomy.eu
- *
- * The contents of this file are subject to the Mozilla Public License Version 1.1
- * See LICENSE.TXT at the top of this package for the full license terms.
- */
-
-package eu.etaxonomy.taxeditor.featuretree;
-
-import java.util.Collection;
-
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.jface.viewers.ISelectionChangedListener;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.viewers.SelectionChangedEvent;
-import org.eclipse.jface.viewers.TreeViewer;
-import org.eclipse.jface.viewers.Viewer;
-import org.eclipse.jface.viewers.ViewerDropAdapter;
-import org.eclipse.jface.wizard.WizardDialog;
-import org.eclipse.jface.wizard.WizardPage;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.dnd.DND;
-import org.eclipse.swt.dnd.DragSourceAdapter;
-import org.eclipse.swt.dnd.DragSourceEvent;
-import org.eclipse.swt.dnd.Transfer;
-import org.eclipse.swt.dnd.TransferData;
-import org.eclipse.swt.events.ModifyEvent;
-import org.eclipse.swt.events.ModifyListener;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Button;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Text;
-import org.hibernate.proxy.HibernateProxy;
-
-import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
-import eu.etaxonomy.cdm.api.service.config.FeatureNodeDeletionConfigurator;
-import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
-import eu.etaxonomy.cdm.model.description.Feature;
-import eu.etaxonomy.cdm.model.description.FeatureNode;
-import eu.etaxonomy.cdm.model.description.FeatureTree;
-import eu.etaxonomy.taxeditor.model.MessagingUtils;
-import eu.etaxonomy.taxeditor.store.CdmStore;
-
-/**
- * <p>
- * EditFeatureTreeWizardPage class.
- * </p>
- *
- * @author n.hoffmann
- * @created Aug 5, 2010
- * @version 1.0
- */
-public class EditFeatureTreeWizardPage extends WizardPage implements
- ModifyListener, ISelectionChangedListener {
-
- private TreeViewer viewer;
- private Label label_title;
- private Text text_title;
- private Button button_add;
- private Button button_remove;
- private Label label_treeInfo;
- private FeatureTree featureTree;
-
- /**
- * <p>
- * Constructor for EditFeatureTreeWizardPage.
- * </p>
- *
- * @param pageName
- * a {@link java.lang.String} object.
- */
- protected EditFeatureTreeWizardPage(String pageName) {
- super(pageName);
- setMessage("Edit the Feature Tree.");
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
- * .Composite)
- */
- /** {@inheritDoc} */
- @Override
- public void createControl(Composite parent) {
- Composite composite = new Composite(parent, SWT.NULL);
- composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
- composite.setLayout(new GridLayout());
-
- Composite composite_treeTitle = new Composite(composite, SWT.NULL);
- composite_treeTitle.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
- false));
- composite_treeTitle.setLayout(new GridLayout(2, false));
-
- label_title = new Label(composite_treeTitle, SWT.NULL);
- label_title.setText("Title");
-
- text_title = new Text(composite_treeTitle, SWT.NULL);
- text_title.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
-
- Composite composite_treeContent = new Composite(composite, SWT.NULL);
- composite_treeContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
- true, true));
- composite_treeContent.setLayout(new GridLayout(2, false));
-
- viewer = new TreeViewer(composite_treeContent);
- viewer.getControl().setLayoutData(
- new GridData(SWT.FILL, SWT.FILL, true, true));
-
- Composite composite_buttons = new Composite(composite_treeContent,
- SWT.NULL);
- composite_buttons.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false,
- false));
- composite_buttons.setLayout(new GridLayout());
-
- button_add = new Button(composite_buttons, SWT.PUSH);
- button_add.setText("Add");
- button_add.setToolTipText("Add a feature to this feature treee.");
- button_remove = new Button(composite_buttons, SWT.PUSH);
- button_remove.setText("Remove");
- button_remove
- .setToolTipText("Remove a feature from this feature tree.");
-
- label_treeInfo = new Label(composite, SWT.NULL);
- label_treeInfo
- .setText("Order and nesting of feature nodes may be changed through drag and drop.");
-
- viewer.setContentProvider(new FeatureTreeContentProvider());
- viewer.setLabelProvider(new FeatureTreeLabelProvider());
-
- int ops = DND.DROP_COPY | DND.DROP_MOVE;
- Transfer[] transfers = new Transfer[] { FeatureNodeTransfer
- .getInstance() };
- viewer.addDragSupport(ops, transfers, new FeatureNodeDragListener(
- viewer));
- viewer.addDropSupport(ops, transfers,
- new FeatureNodeDropAdapter(viewer));
-
- viewer.addSelectionChangedListener(this);
-
- button_add.addSelectionListener(new AddButtonListener());
- button_remove.addSelectionListener(new RemoveSelectionListener());
-
- setControl(composite);
- }
-
- /**
- * <p>
- * setSelectedTree
- * </p>
- *
- * @param featureTree
- * a {@link eu.etaxonomy.cdm.model.description.FeatureTree}
- * object.
- */
- public void setSelectedTree(FeatureTree featureTree) {
- this.featureTree = HibernateProxyHelper.deproxy(featureTree, FeatureTree.class);
- this.featureTree.setRoot(HibernateProxyHelper.deproxy(featureTree.getRoot(), FeatureNode.class));
- viewer.setInput(featureTree);
-
- text_title.removeModifyListener(this);
- if (featureTree != null){
- text_title.setText(featureTree.getTitleCache());
- }
- text_title.addModifyListener(this);
- }
-
- /** {@inheritDoc} */
- @Override
- public void modifyText(ModifyEvent e) {
- featureTree.setTitleCache(text_title.getText(), true);
- }
-
- /** {@inheritDoc} */
- @Override
- public void selectionChanged(SelectionChangedEvent event) {
- IStructuredSelection selection = (IStructuredSelection) event
- .getSelection();
-
- button_add.setEnabled(selection.size() <= 1);
- button_remove.setEnabled(selection.size() > 0);
- }
-
- private class AddButtonListener extends SelectionAdapter {
- @Override
- public void widgetSelected(SelectionEvent e) {
- AvailableFeaturesWizard wizard = new AvailableFeaturesWizard(
- featureTree);
- WizardDialog dialog = new WizardDialog(getShell(), wizard);
-
- if (dialog.open() == IStatus.OK) {
- IStructuredSelection selection = (IStructuredSelection) viewer
- .getSelection();
- FeatureNode parent = (FeatureNode) (selection.getFirstElement() != null ? selection
- .getFirstElement() : ((FeatureTree) viewer.getInput())
- .getRoot());
- Collection<Feature> additionalFeatures = wizard
- .getAdditionalFeatures();
- for (Feature feature : additionalFeatures) {
- FeatureNode child = FeatureNode.NewInstance(feature);
- CdmStore.getService(IFeatureNodeService.class).merge(child, true);
-
- parent.addChild(child);
- }
- viewer.refresh();
- }
- }
- }
-
- private class RemoveSelectionListener extends SelectionAdapter {
- @Override
- public void widgetSelected(SelectionEvent e) {
- IStructuredSelection selection = (IStructuredSelection) viewer
- .getSelection();
-
- for (Object selectedObject : selection.toArray()) {
- FeatureNode featureNode = (FeatureNode) selectedObject;
- FeatureNode parent = featureNode.getParent();
- parent.removeChild(featureNode);
-
- CdmStore.getService(IFeatureNodeService.class).deleteFeatureNode(featureNode.getUuid(), new FeatureNodeDeletionConfigurator());
-
- }
- viewer.refresh();
- }
- }
-
- private class FeatureNodeDragListener extends DragSourceAdapter {
-
- private final TreeViewer viewer;
-
- public FeatureNodeDragListener(TreeViewer viewer) {
- this.viewer = viewer;
- }
-
- /**
- * Method declared on DragSourceListener
- */
- @Override
- public void dragFinished(DragSourceEvent event) {
- if (!event.doit) {
- return;
- }
- // if the featureNode was moved, remove it from the source viewer
- if (event.detail == DND.DROP_MOVE) {
- IStructuredSelection selection = (IStructuredSelection) viewer
- .getSelection();
- viewer.refresh();
- }
- }
-
- /**
- * Method declared on DragSourceListener
- */
- @Override
- public void dragSetData(DragSourceEvent event) {
- IStructuredSelection selection = (IStructuredSelection) viewer
- .getSelection();
- FeatureNode[] featureNodes = (FeatureNode[]) selection.toList()
- .toArray(new FeatureNode[selection.size()]);
- if (FeatureNodeTransfer.getInstance().isSupportedType(
- event.dataType)) {
- event.data = featureNodes;
- }
- }
-
- /**
- * Method declared on DragSourceListener
- */
- @Override
- public void dragStart(DragSourceEvent event) {
- event.doit = !viewer.getSelection().isEmpty();
- }
-
- }
-
- private class FeatureNodeDropAdapter extends ViewerDropAdapter {
-
- protected FeatureNodeDropAdapter(Viewer viewer) {
- super(viewer);
- }
-
- @Override
- public boolean performDrop(Object data) {
- FeatureNode target = (FeatureNode) getCurrentTarget();
- int position = 0;
-
- if (target != null) {
- int location = getCurrentLocation();
- FeatureNode parent = target.getParent();
- if (location == LOCATION_BEFORE) {
- position = Math.max(0, parent.getIndex(target) - 1);
- target = parent;
- }
-
- if (location == LOCATION_AFTER) {
- position = parent.getIndex(target);
- target = parent;
- }
- }
-
- // set target to root node if there is no target specified
- if (target == null) {
- FeatureTree featureTree = (FeatureTree) getViewer().getInput();
- target = featureTree.getRoot();
- }
-
- Object[] droppedObjects = (Object[]) data;
- TreeViewer viewer = (TreeViewer) getViewer();
-
- // cannot drop a feature node onto itself
- for (Object droppedObject : droppedObjects) {
- if (droppedObject == null) {
- MessagingUtils.warningDialog(
- "Operation not supported yet",
- this,
- "We are currently unable to change the order of freshly created "
- + "feature trees nodes. Please close and reopen the dialog to change the order of features.");
- return false;
- }
- if (droppedObject.equals(target)) {
- return false;
- }
- }
- for (Object droppedObject : droppedObjects) {
- FeatureNode droppedNode = (FeatureNode) droppedObject;
- target.addChild(droppedNode, position);
- viewer.add(target, droppedNode);
- viewer.reveal(droppedNode);
- }
- return true;
- }
-
- @Override
- public boolean validateDrop(Object target, int operation,
- TransferData transferData) {
- return FeatureNodeTransfer.getInstance().isSupportedType(
- transferData);
- }
-
- }
-}