/**
- * 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.
- */
-
+* Copyright (C) 2019 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.e4;
-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 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.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.ISelection;
-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.swt.SWT;
-import org.eclipse.swt.dnd.DND;
-import org.eclipse.swt.dnd.Transfer;
-import org.eclipse.swt.events.KeyAdapter;
-import org.eclipse.swt.events.KeyEvent;
-import org.eclipse.swt.layout.FillLayout;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.ui.IMemento;
-
-import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
-import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
-import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
-import eu.etaxonomy.cdm.api.service.ITermService;
-import eu.etaxonomy.cdm.model.description.Character;
-import eu.etaxonomy.cdm.model.term.FeatureNode;
-import eu.etaxonomy.cdm.model.term.FeatureTree;
+import eu.etaxonomy.cdm.model.description.Feature;
import eu.etaxonomy.cdm.model.term.TermType;
-import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
-import eu.etaxonomy.taxeditor.editor.definedterm.FeatureTreeViewerComparator;
-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.featuretree.e4.operation.AddFeatureOperation;
-import eu.etaxonomy.taxeditor.model.AbstractUtility;
-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.store.StoreUtil;
-import eu.etaxonomy.taxeditor.workbench.part.IE4ViewerPart;
/**
- *
* @author pplitzner
- * @date 06.06.2017
+ * @since Aug 19, 2019
*
*/
-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;
-
- @Inject
- private UISynchronize sync;
-
- @Inject
- private MPart thisPart;
-
- private TreeViewer viewer;
-
- @Inject
- public FeatureTreeEditor() {
- CdmStore.getContextManager().addContextListener(this);
- }
-
- @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(),
- TermTransfer.getInstance(),
- LocalSelectionTransfer.getTransfer()};
- viewer.addDragSupport(ops, transfers, new FeatureNodeDragListener(viewer));
- viewer.addDropSupport(ops, transfers, new FeatureTreeDropAdapter(this, viewer, sync));
- viewer.addSelectionChangedListener(this);
- viewer.getTree().addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
- if(e.stateMask == SWT.MOD1 && e.keyCode == 'c'){
- copy(viewer.getStructuredSelection());
- }
- else if(e.stateMask == SWT.MOD1 && e.keyCode == 'v'){
- paste(viewer.getStructuredSelection());
- }
- }
- });
-
- List<FeatureTree> trees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
- viewer.setComparator(new FeatureTreeViewerComparator());
- viewer.setInput(trees);
-
- //create context menu
- menuService.registerContextMenu(viewer.getControl(), AppModelId.POPUPMENU_EU_ETAXONOMY_TAXEDITOR_STORE_POPUPMENU_FEATURETREEEDITOR);
- }
-
- public void paste(IStructuredSelection selection) {
- if (StoreUtil.promptCheckIsDirty(this)) {
- return;
- }
-
- ISelection clipBoardSelection = LocalSelectionTransfer.getTransfer().getSelection();
- Object firstElement = selection.getFirstElement();
- FeatureNode parentNode = null;
- if(firstElement instanceof FeatureNode){
- parentNode = (FeatureNode) firstElement;
- }
- else if(firstElement instanceof FeatureTree){
- parentNode = ((FeatureTree)firstElement).getRoot();
- }
- if(parentNode!=null){
- FeatureNode copiedNode = (FeatureNode) ((IStructuredSelection)clipBoardSelection).getFirstElement();
-
- AddFeatureOperation operation = new AddFeatureOperation(copiedNode.getTerm().getUuid(), parentNode, this, this);
- AbstractUtility.executeOperation(operation, sync);
- }
-
- }
-
- public void copy(IStructuredSelection selection) {
- LocalSelectionTransfer.getTransfer().setSelection(selection);
- }
-
- private void initSession(){
- if(conversation == null){
- conversation = CdmStore.createConversation();
- }
- if(cdmEntitySession==null){
- cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
- }
- }
-
- private void clearSession() {
- if(conversation!=null){
- conversation.close();
- conversation = null;
- }
- if(cdmEntitySession != null) {
- cdmEntitySession.dispose();
- cdmEntitySession = null;
- }
- dirty.setDirty(false);
- }
-
- public void setDirty(boolean isDirty){
- this.dirty.setDirty(isDirty);
- }
-
- @Override
- public boolean isDirty(){
- return dirty.isDirty();
- }
-
- @Override
- public void selectionChanged(SelectionChangedEvent event) {
- //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();
- }
- }
-
- @Override
- public void refresh(){
- viewer.refresh();
- }
-
- public TreeViewer getViewer(){
- return viewer;
- }
-
- @Override
- public IStructuredSelection getSelection() {
- return (IStructuredSelection) viewer.getSelection();
- }
-
- @Override
- public ConversationHolder getConversationHolder() {
- return conversation;
- }
-
- @Override
- @Persist
- public void save(IProgressMonitor monitor){
- if (!conversation.isBound()) {
- conversation.bind();
- }
-
- // commit the conversation and start a new transaction immediately
- conversation.commit(true);
-
- CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(getRootEntities());
-
- List<FeatureTree> rootEntities = getRootEntities();
- for (FeatureTree featureTree : rootEntities) {
- if(featureTree.getTermType().equals(TermType.Character)){
- FeatureTree<Character> characterTree = featureTree;
- //save characters because they can be modified in this editor
- characterTree.getDistinctFeatures().forEach(character->CdmStore.getService(ITermService.class).merge(character,true));
- }
- }
-
- initializeTrees();
-
- this.setDirty(false);
- }
-
- 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();
- }
-
- @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) {
- }
+public class FeatureTreeEditor extends AbstractTermTreeEditor<Feature> {
@Override
- public void contextRefresh(IProgressMonitor monitor) {
+ public TermType getTermType() {
+ return TermType.Feature;
}
- @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);
- }
- if(objectAffectedByOperation!=null){
- StructuredSelection selection = new StructuredSelection(objectAffectedByOperation);
- viewer.setSelection(selection);
- }
- return true;
- }
-
- @Override
- public boolean onComplete() {
- return false;
- }
}