package eu.etaxonomy.taxeditor.featuretree.e4;
-import java.util.Collection;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
import javax.inject.Inject;
-import javax.inject.Named;
-import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.di.Persist;
+import org.eclipse.e4.ui.di.UISynchronize;
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
-import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.services.EMenuService;
+import org.eclipse.e4.ui.workbench.modeling.EPartService;
+import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
+import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.StructuredSelection;
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.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.events.SelectionListener;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.swt.widgets.Text;
-import org.eclipse.swt.widgets.Tree;
+import org.eclipse.ui.IMemento;
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
-import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
+import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
-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.featuretree.AvailableFeaturesWizard;
+import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
+import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
import eu.etaxonomy.taxeditor.featuretree.FeatureTreeContentProvider;
import eu.etaxonomy.taxeditor.featuretree.FeatureTreeLabelProvider;
-import eu.etaxonomy.taxeditor.model.ImageResources;
-import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.model.IContextListener;
+import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
+import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
+import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
+import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
+import eu.etaxonomy.taxeditor.store.AppModelId;
import eu.etaxonomy.taxeditor.store.CdmStore;
-import eu.etaxonomy.taxeditor.ui.dialog.selection.FeatureTreeSelectionDialog;
+import eu.etaxonomy.taxeditor.workbench.part.IE4ViewerPart;
/**
*
* @date 06.06.2017
*
*/
-public class FeatureTreeEditor implements
- ModifyListener, ISelectionChangedListener {
-
- private TreeViewer viewer;
- private Label label_title;
- private Button button_add;
- private Button button_remove;
- private FeatureTree featureTree;
- private Text text_title;
- private Button btnOpenFeatureTree;
+public class FeatureTreeEditor implements IFeatureTreeEditor, ISelectionChangedListener,
+ IE4ViewerPart, IPartContentHasDetails, IPartContentHasSupplementalData,
+ IContextListener, IConversationEnabled, IDirtyMarkable {
private ConversationHolder conversation;
+
+ private ICdmEntitySession cdmEntitySession;
+
+ @Inject
+ private ESelectionService selService;
+
@Inject
private MDirtyable dirty;
- private Shell shell;
+ @Inject
+ private UISynchronize sync;
@Inject
- public FeatureTreeEditor(@Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
- this.shell = shell;
- if(conversation==null){
- conversation = CdmStore.createConversation();
- }
- }
+ private MPart thisPart;
- /** {@inheritDoc} */
- @PostConstruct
- public void createControl(Composite parent, @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
- parent.setLayout(new GridLayout());
- Composite composite = new Composite(parent, SWT.NULL);
- composite.setLayout(new GridLayout());
- composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
-
- Composite composite_treeTitle = new Composite(composite, SWT.NULL);
- composite_treeTitle.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
- false));
- composite_treeTitle.setLayout(new GridLayout(3, false));
-
- label_title = new Label(composite_treeTitle, SWT.NULL);
- label_title.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
- label_title.setText("Title");
-
- text_title = new Text(composite_treeTitle, SWT.BORDER);
- text_title.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
-
- btnOpenFeatureTree = new Button(composite_treeTitle, SWT.NONE);
- btnOpenFeatureTree.setToolTipText("Open Tree");
- btnOpenFeatureTree.setImage(ImageResources.getImage(ImageResources.BROWSE_ICON));
-
- 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);
- Tree tree = viewer.getTree();
- tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
- 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.setToolTipText("Add a feature to this feature tree.");
- button_add.setImage(ImageResources.getImage(ImageResources.ADD_EDIT));
- button_remove = new Button(composite_buttons, SWT.PUSH);
- button_remove.setToolTipText("Remove a feature from this feature tree.");
- button_remove.setImage(ImageResources.getImage(ImageResources.TRASH_ICON));
-
- init(shell);
- }
+ private TreeViewer viewer;
- private void setDirty(boolean isDirty){
- this.dirty.setDirty(isDirty);
- }
+ @Inject
+ public FeatureTreeEditor() {
+ CdmStore.getContextManager().addContextListener(this);
+ }
- private void init(Shell shell) {
+ @PostConstruct
+ public void createControl(Composite parent, EMenuService menuService){
+ if (CdmStore.isActive()){
+ initSession();
+ }
+ else{
+ return;
+ }
+ parent.setLayout(new FillLayout());
+ viewer = new TreeViewer(parent);
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));
-
+ Transfer[] transfers = new Transfer[] {
+ FeatureNodeTransfer.getInstance(),
+ TermTransfer.getInstance(),
+ LocalSelectionTransfer.getTransfer()};
+ viewer.addDragSupport(ops, transfers, new FeatureNodeDragListener(viewer));
+ viewer.addDropSupport(ops, transfers, new FeatureTreeDropAdapter(this, viewer, sync));
viewer.addSelectionChangedListener(this);
- button_add.addSelectionListener(new AddButtonListener());
- button_remove.addSelectionListener(new RemoveSelectionListener());
-
- btnOpenFeatureTree.addSelectionListener(new SelectionListener() {
+ List<FeatureTree> trees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
+ viewer.setInput(trees);
- @Override
- public void widgetSelected(SelectionEvent e) {
- FeatureTree tree = FeatureTreeSelectionDialog.select(shell, conversation, null);
- if(tree!=null){
- setSelectedTree(tree);
- }
- }
+ //create context menu
+ menuService.registerContextMenu(viewer.getControl(), AppModelId.POPUPMENU_EU_ETAXONOMY_TAXEDITOR_STORE_POPUPMENU_FEATURETREEEDITOR);
+ }
- @Override
- public void widgetDefaultSelected(SelectionEvent e) {
- }
- });
+ private void initSession(){
+ if(conversation == null){
+ conversation = CdmStore.createConversation();
+ }
+ if(cdmEntitySession==null){
+ cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
+ }
}
- public void setSelectedTree(FeatureTree featureTree) {
- this.featureTree = HibernateProxyHelper.deproxy(featureTree, FeatureTree.class);
- this.featureTree.setRoot(HibernateProxyHelper.deproxy(featureTree.getRoot(), FeatureNode.class));
- viewer.setInput(featureTree);
+ private void clearSession() {
+ if(conversation!=null){
+ conversation.close();
+ conversation = null;
+ }
+ if(cdmEntitySession != null) {
+ cdmEntitySession.dispose();
+ cdmEntitySession = null;
+ }
+ dirty.setDirty(false);
+ }
- text_title.removeModifyListener(this);
- text_title.setText(featureTree.getTitleCache());
- text_title.addModifyListener(this);
+ public void setDirty(boolean isDirty){
+ this.dirty.setDirty(isDirty);
}
- /** {@inheritDoc} */
@Override
- public void modifyText(ModifyEvent e) {
- featureTree.setTitleCache(text_title.getText(), true);
- setDirty(true);
+ public boolean isDirty(){
+ return dirty.isDirty();
}
- /** {@inheritDoc} */
@Override
public void selectionChanged(SelectionChangedEvent event) {
- IStructuredSelection selection = (IStructuredSelection) event
- .getSelection();
+ //propagate selection
+ selService.setSelection(event.getSelection());
+ }
+
+ @Focus
+ public void focus(){
+ if(viewer!=null){
+ viewer.getControl().setFocus();
+ }
+ if(conversation!=null && !conversation.isBound()){
+ conversation.bind();
+ }
+ if(cdmEntitySession != null) {
+ cdmEntitySession.bind();
+ }
+ }
- button_add.setEnabled(selection.size() <= 1);
- button_remove.setEnabled(selection.size() > 0);
+ @Override
+ public void refresh(){
+ viewer.refresh();
}
- @Persist
- public void save(){
- CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(featureTree);
- setDirty(false);
+ public TreeViewer getViewer(){
+ return viewer;
}
- private class AddButtonListener extends SelectionAdapter {
- @Override
- public void widgetSelected(SelectionEvent e) {
- AvailableFeaturesWizard wizard = new AvailableFeaturesWizard(
- featureTree);
- WizardDialog dialog = new WizardDialog(shell, 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);
- }
- setDirty(true);
- viewer.refresh();
- }
- }
+ @Override
+ public IStructuredSelection getSelection() {
+ return (IStructuredSelection) viewer.getSelection();
+ }
+ @Override
+ public ConversationHolder getConversationHolder() {
+ return conversation;
}
- private class RemoveSelectionListener extends SelectionAdapter {
- @Override
- public void widgetSelected(SelectionEvent e) {
- IStructuredSelection selection = (IStructuredSelection) viewer
- .getSelection();
+ @Override
+ @Persist
+ public void save(IProgressMonitor monitor){
+ if (!conversation.isBound()) {
+ conversation.bind();
+ }
- for (Object selectedObject : selection.toArray()) {
- FeatureNode featureNode = (FeatureNode) selectedObject;
- FeatureNode parent = featureNode.getParent();
- parent.removeChild(featureNode);
+ // commit the conversation and start a new transaction immediately
+ conversation.commit(true);
- CdmStore.getService(IFeatureNodeService.class).deleteFeatureNode(featureNode.getUuid(), new FeatureNodeDeletionConfigurator());
+ CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(getRootEntities());
- }
- setDirty(true);
- viewer.refresh();
- }
+ initializeTrees();
+
+ this.setDirty(false);
}
- 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 void initializeTrees() {
+ Object[] expandedElements = viewer.getExpandedElements();
+ viewer.getTree().removeAll();
+ List<FeatureTree> trees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
+ viewer.setInput(trees);
+ viewer.setExpandedElements(expandedElements);
+ }
+ @PreDestroy
+ public void dispose(){
+ selService.setSelection(null);
+ clearSession();
}
- 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);
- }
+ @Override
+ public ICdmEntitySession getCdmEntitySession() {
+ return cdmEntitySession;
+ }
+
+ @Override
+ public Map<Object, List<String>> getPropertyPathsMap() {
+ List<String> propertyPaths = Arrays.asList(new String[] {
+ "children", //$NON-NLS-1$
+ "feature", //$NON-NLS-1$
+ "featureTree", //$NON-NLS-1$
+ });
+ Map<Object, List<String>> propertyPathMap =
+ new HashMap<Object, List<String>>();
+ propertyPathMap.put(FeatureNode.class,propertyPaths);
+ return propertyPathMap;
+ }
+
+ @Override
+ public List<FeatureTree> getRootEntities() {
+ return (List<FeatureTree>) viewer.getInput();
+ }
+
+ @Override
+ public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
+ }
+
+ @Override
+ public void contextStop(IMemento memento, IProgressMonitor monitor) {
+ //close view when workbench closes
+ try{
+ thisPart.getContext().get(EPartService.class).hidePart(thisPart);
+ }
+ catch(Exception e){
+ //nothing
+ }
+ }
+
+ @Override
+ public void contextStart(IMemento memento, IProgressMonitor monitor) {
+ }
+
+ @Override
+ public void contextRefresh(IProgressMonitor monitor) {
+ }
+
+ @Override
+ public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
+ }
+
+ @Override
+ public void update(CdmDataChangeMap arg0) {
+ }
+
+ @Override
+ public void changed(Object element) {
+ dirty.setDirty(true);
+ viewer.refresh();
+ }
+
+ @Override
+ public void forceDirty() {
+ dirty.setDirty(true);
+ }
+
+ @Override
+ public boolean postOperation(Object objectAffectedByOperation) {
+ initializeTrees();
+ viewer.refresh();
+ if(objectAffectedByOperation instanceof FeatureNode){
+ FeatureNode node = (FeatureNode)objectAffectedByOperation;
+ viewer.expandToLevel(node.getFeatureTree(), 1);
+ StructuredSelection selection = new StructuredSelection(objectAffectedByOperation);
+ viewer.setSelection(selection);
+ }
+ return true;
+ }
+
+ @Override
+ public boolean onComplete() {
+ return false;
+ }
- }
}