package eu.etaxonomy.taxeditor.featuretree.e4;
-import java.util.Collection;
+import java.util.Arrays;
+import java.util.Collections;
+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.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.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.events.KeyAdapter;
+import org.eclipse.swt.events.KeyEvent;
+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.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.ui.dialog.selection.FeatureTreeSelectionDialog;
+import eu.etaxonomy.taxeditor.store.StoreUtil;
+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(2, false));
-
- Composite composite_treeTitle = new Composite(parent, SWT.NULL);
- composite_treeTitle.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
- false));
- GridLayout gl_composite_treeTitle = new GridLayout(2, false);
- gl_composite_treeTitle.marginWidth = 0;
- composite_treeTitle.setLayout(gl_composite_treeTitle);
-
- label_title = new Label(composite_treeTitle, SWT.NULL);
- label_title.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
- label_title.setText("Feature Tree");
-
- text_title = new Text(composite_treeTitle, SWT.BORDER);
- text_title.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
-
- btnOpenFeatureTree = new Button(parent, SWT.NONE);
- btnOpenFeatureTree.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
- btnOpenFeatureTree.setToolTipText("Open Tree");
- btnOpenFeatureTree.setImage(ImageResources.getImage(ImageResources.BROWSE_ICON));
-
- viewer = new TreeViewer(parent);
- 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(parent,
- 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.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
- 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.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
- 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() {
-
+ viewer.getTree().addKeyListener(new KeyAdapter() {
@Override
- public void widgetSelected(SelectionEvent e) {
- FeatureTree tree = FeatureTreeSelectionDialog.select(shell, conversation, null);
- if(tree!=null){
- setSelectedTree(tree);
+ 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());
}
- }
-
- @Override
- public void widgetDefaultSelected(SelectionEvent e) {
}
});
+
+ List<FeatureTree> trees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
+ Collections.sort(trees, (tree1, tree2) -> tree1.getTitleCache().compareTo(tree2.getTitleCache()));
+ viewer.setInput(trees);
+
+ //create context menu
+ menuService.registerContextMenu(viewer.getControl(), AppModelId.POPUPMENU_EU_ETAXONOMY_TAXEDITOR_STORE_POPUPMENU_FEATURETREEEDITOR);
}
- public void setSelectedTree(FeatureTree featureTree) {
- this.featureTree = HibernateProxyHelper.deproxy(featureTree, FeatureTree.class);
- this.featureTree.setRoot(HibernateProxyHelper.deproxy(featureTree.getRoot(), FeatureNode.class));
- viewer.setInput(featureTree);
+ public void paste(IStructuredSelection selection) {
+ if (StoreUtil.promptCheckIsDirty(this)) {
+ return;
+ }
- text_title.removeModifyListener(this);
- text_title.setText(featureTree.getTitleCache());
- text_title.addModifyListener(this);
- }
+ 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.getFeature(), parentNode, this, this);
+ AbstractUtility.executeOperation(operation, sync);
+ }
- public FeatureTree getSelectedFeatureTree(){
- return this.featureTree;
}
- /** {@inheritDoc} */
+ 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 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();
-
- button_add.setEnabled(selection.size() <= 1);
- button_remove.setEnabled(selection.size() > 0);
//propagate selection
- IStructuredSelection isel = (IStructuredSelection) event.getSelection();
- selService.setSelection((isel.size() == 1 ? isel.getFirstElement() : isel.toArray()));
+ selService.setSelection(event.getSelection());
}
@Focus
public void focus(){
- viewer.getControl().setFocus();
+ if(viewer!=null){
+ viewer.getControl().setFocus();
+ }
if(conversation!=null && !conversation.isBound()){
conversation.bind();
}
+ if(cdmEntitySession != null) {
+ cdmEntitySession.bind();
+ }
+ }
+
+ @Override
+ public void refresh(){
+ viewer.refresh();
}
- @Persist
- public void save(){
- CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(featureTree);
- setDirty(false);
+ public TreeViewer getViewer(){
+ return viewer;
}
- /**
- * @param additionalFeatures
- */
- public void addFeatures(Collection<Feature> additionalFeatures) {
- IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
- FeatureNode parent = (FeatureNode) (selection.getFirstElement() != null ? selection.getFirstElement()
- : ((FeatureTree) viewer.getInput()).getRoot());
- for (Feature feature : additionalFeatures) {
- FeatureNode child = FeatureNode.NewInstance(feature);
- //TODO session handling
-// CdmStore.getService(IFeatureNodeService.class).merge(child, true);
-
- parent.addChild(child);
- }
- //TODO session handling
-// setDirty(true);
- viewer.refresh();
- }
+ @Override
+ public IStructuredSelection getSelection() {
+ return (IStructuredSelection) viewer.getSelection();
+ }
- private class AddButtonListener extends SelectionAdapter {
- @Override
- public void widgetSelected(SelectionEvent e) {
- AvailableFeaturesWizard wizard = new AvailableFeaturesWizard(
- featureTree);
- WizardDialog dialog = new WizardDialog(shell, wizard);
+ @Override
+ public ConversationHolder getConversationHolder() {
+ return conversation;
+ }
- if (dialog.open() == IStatus.OK) {
- Collection<Feature> additionalFeatures = wizard.getAdditionalFeatures();
- addFeatures(additionalFeatures);
- }
+ @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());
- private class RemoveSelectionListener extends SelectionAdapter {
- @Override
- public void widgetSelected(SelectionEvent e) {
- IStructuredSelection selection = (IStructuredSelection) viewer
- .getSelection();
+ initializeTrees();
- for (Object selectedObject : selection.toArray()) {
- FeatureNode featureNode = (FeatureNode) selectedObject;
- FeatureNode parent = featureNode.getParent();
- parent.removeChild(featureNode);
+ this.setDirty(false);
+ }
- CdmStore.getService(IFeatureNodeService.class).deleteFeatureNode(featureNode.getUuid(), new FeatureNodeDeletionConfigurator());
+ 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);
+ }
- }
- setDirty(true);
- viewer.refresh();
- }
+ @PreDestroy
+ public void dispose(){
+ selService.setSelection(null);
+ clearSession();
}
- private class FeatureNodeDragListener extends DragSourceAdapter {
+ @Override
+ public ICdmEntitySession getCdmEntitySession() {
+ return cdmEntitySession;
+ }
- private final TreeViewer viewer;
+ @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;
+ }
- public FeatureNodeDragListener(TreeViewer viewer) {
- this.viewer = viewer;
- }
+ @Override
+ public List<FeatureTree> getRootEntities() {
+ return (List<FeatureTree>) viewer.getInput();
+ }
- /**
- * 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.remove(selection);
- 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();
- }
+ @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
+ }
+ }
- 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);
- }
- setDirty(true);
- return true;
- }
-
- @Override
- public boolean validateDrop(Object target, int operation,
- TransferData transferData) {
- return FeatureNodeTransfer.getInstance().isSupportedType(
- transferData);
- }
+ @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);
+ }
+ if(objectAffectedByOperation!=null){
+ StructuredSelection selection = new StructuredSelection(objectAffectedByOperation);
+ viewer.setSelection(selection);
+ }
+ return true;
+ }
+
+ @Override
+ public boolean onComplete() {
+ return false;
+ }
- }
}