Project

General

Profile

Revision 86fcba21

ID86fcba2118241803d5550cf84c19d93396050cd3
Parent 3e157055
Child 8127ab5c

Added by Patrick Plitzner 9 months ago

ref #7923 Extend drag & drop action for creating characters

View differences:

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/character/CharacterDragListener.java
8 8
*/
9 9
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.character;
10 10

  
11
import org.eclipse.jface.viewers.IStructuredSelection;
11
import java.util.List;
12

  
13
import org.eclipse.jface.util.LocalSelectionTransfer;
14
import org.eclipse.jface.viewers.StructuredSelection;
15
import org.eclipse.jface.viewers.TreeSelection;
12 16
import org.eclipse.jface.viewers.TreeViewer;
13 17
import org.eclipse.swt.dnd.DragSourceEvent;
14 18

  
15
import eu.etaxonomy.cdm.model.description.FeatureNode;
16
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
19
import eu.etaxonomy.cdm.model.description.Character;
17 20
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureNodeDragListener;
18 21

  
19 22
/**
......
26 29
    private TreeViewer structureViewer;
27 30
    private TreeViewer propertyViewer;
28 31

  
29
    /**
30
     * @param propertyViewer
31
     */
32 32
    public CharacterDragListener(TreeViewer structureViewer, TreeViewer propertyViewer) {
33 33
        super(propertyViewer);
34 34
        this.structureViewer = structureViewer;
35 35
        this.propertyViewer = propertyViewer;
36 36
    }
37 37

  
38
    /**
39
     * {@inheritDoc}
40
     */
41 38
    @Override
42 39
    public void dragSetData(DragSourceEvent event) {
43
        FeatureNode structure = (FeatureNode) ((IStructuredSelection) structureViewer.getSelection()).getFirstElement();
44
        FeatureNode property = (FeatureNode) ((IStructuredSelection) propertyViewer.getSelection()).getFirstElement();
45
        FeatureNode[] character = new FeatureNode[2];
46
        character[0] = structure;
47
        character[1] = property;
48
        if (FeatureNodeTransfer.getInstance().isSupportedType(event.dataType)) {
49
            event.data = character;
40
        TreeSelection structureSelection = (TreeSelection) structureViewer.getSelection();
41
        TreeSelection propertySelection = (TreeSelection) propertyViewer.getSelection();
42
        if(structureSelection!=null && propertySelection!=null
43
                && !structureSelection.isEmpty() && !propertySelection.isEmpty()){
44
            //create all possible characters
45
            List<Character> characters = CharacterEditor.createCharacters(structureSelection, propertySelection);
46

  
47
            LocalSelectionTransfer.getTransfer().setSelection(new StructuredSelection(characters));
50 48
        }
51 49
    }
52 50

  
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/character/CharacterDropAdapter.java
8 8
*/
9 9
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.character;
10 10

  
11
import java.util.Iterator;
12

  
13
import org.eclipse.jface.util.LocalSelectionTransfer;
14
import org.eclipse.jface.viewers.ISelection;
15
import org.eclipse.jface.viewers.IStructuredSelection;
11 16
import org.eclipse.jface.viewers.Viewer;
12
import org.eclipse.jface.viewers.ViewerDropAdapter;
17
import org.eclipse.swt.dnd.DropTargetEvent;
13 18
import org.eclipse.swt.dnd.TransferData;
14 19

  
20
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
15 21
import eu.etaxonomy.cdm.model.description.Character;
16 22
import eu.etaxonomy.cdm.model.description.FeatureNode;
17 23
import eu.etaxonomy.cdm.model.description.FeatureTree;
18
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
24
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureNodeDropAdapter;
25
import eu.etaxonomy.taxeditor.store.CdmStore;
26
import eu.etaxonomy.taxeditor.store.StoreUtil;
27
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
19 28

  
20 29
/**
21 30
 *
......
23 32
 * @date Jun 1, 2017
24 33
 *
25 34
 */
26
public class CharacterDropAdapter extends ViewerDropAdapter {
35
public class CharacterDropAdapter extends FeatureNodeDropAdapter {
27 36

  
28
	protected CharacterDropAdapter(Viewer viewer) {
29
	    super(viewer);
37
	protected CharacterDropAdapter(IE4SavablePart savablePart, Viewer viewer) {
38
	    super(savablePart, viewer);
30 39
	}
31 40

  
32 41
	@Override
33
	public boolean performDrop(Object data) {
42
	protected Object determineTarget(DropTargetEvent event) {
43
        return event.item == null ? ((FeatureTree)getViewer().getInput()).getRoot() : event.item.getData();
44
	}
34 45

  
46
	@Override
47
	public boolean performDrop(Object data) {
48
	    if(StoreUtil.checkDirty(savablePart)){
49
	        return false;
50
	    }
35 51
	    FeatureNode target = (FeatureNode) getCurrentTarget();
36
		int currentLocation = getCurrentLocation();
37
		Object[] characterData = (Object[]) data;
38

  
39
		//create new Character
40
		if(characterData.length==2){
41
		    FeatureNode structureNode = (FeatureNode) characterData[0];
42
		    FeatureNode propertyNode = (FeatureNode) characterData[1];
43
		    String label = structureNode.getFeature().toString()+" "+propertyNode.getFeature().toString();
44
		    Character character = Character.NewInstance(structureNode, propertyNode, null, label, label);
45
		    FeatureNode characterNode = FeatureNode.NewInstance(character);
46

  
47
		    if(getViewer().getInput()==null){
48
		        //create new feature tree
49
		        FeatureTree featureTree = FeatureTree.NewInstance();
50
		        getViewer().setInput(featureTree);
51
		    }
52
            if(target==null){
53
                FeatureNode root = ((FeatureTree)getViewer().getInput()).getRoot();
54
                root.addChild(characterNode);
55
            }
56
            else{
57
                target.addChild(characterNode);
58
            }
59
		}
60
		getViewer().refresh();
61
		return true;
52
	    ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
53
	    if(selection!=null
54
	            &&selection instanceof IStructuredSelection
55
	            ){
56
	        IStructuredSelection structuredSelection = (IStructuredSelection)selection;
57
	        Iterator iterator = structuredSelection.iterator();
58
	        while(iterator.hasNext()){
59
	            Object next = iterator.next();
60
	            if(next instanceof Character){
61
	                Character character = (Character)next;
62
	                if(target==null){
63
	                    //drop on feature tree
64
	                    target = ((FeatureTree)getViewer().getInput()).getRoot();
65
	                }
66
	                CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), character.getUuid());
67
	            }
68
	        }
69
	        getViewer().refresh();
70
	        LocalSelectionTransfer.getTransfer().setSelection(null);
71
	        return true;
72
	    }
73
	    return super.performDrop(data);
62 74
	}
63 75

  
64 76
	@Override
65 77
	public boolean validateDrop(Object target, int operation,
66
			TransferData transferType) {
67
		boolean transferDataIsSupported = FeatureNodeTransfer.getInstance().isSupportedType(transferType);
68
		return transferDataIsSupported;
78
			TransferData transferData) {
79
		LocalSelectionTransfer transfer = LocalSelectionTransfer.getTransfer();
80
        boolean valid = true;
81
		valid &= transfer.getSelection()!=null
82
		        && !transfer.getSelection().isEmpty();
83
		if(!valid){
84
		    return super.validateDrop(target, operation, transferData);
85
		}
86
		return valid;
69 87
	}
70 88

  
71 89
}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/character/CharacterEditor.java
155 155
        formToolkit.adapt(btnAddCharacter, true, true);
156 156

  
157 157
        characterTreeEditorComposite = addFeatureTreeEditor(composite_3);
158
        initFeatureTreeComposite(characterTreeEditorComposite, new FeatureNodeDragListener(characterTreeEditorComposite.getViewer()), new FeatureNodeDropAdapter(dirty, this, characterTreeEditorComposite.getViewer()));
158
        initFeatureTreeComposite(characterTreeEditorComposite, new FeatureNodeDragListener(characterTreeEditorComposite.getViewer()), new CharacterDropAdapter(this, characterTreeEditorComposite.getViewer()));
159 159
        characterTreeEditorComposite.getLabel_title().setText("Characters");
160 160

  
161 161
        //add context menu to character viewer//create context menu
......
204 204
                            "You have to select a structure, a property and a feature tree to perform this action.");
205 205
                    return;
206 206
                }
207
                //get selected structures and properties
208
                Iterator<FeatureNode> structureIterator = ((TreeSelection)structureTreeSelection).iterator();
209

  
210
                //create all combinations of structures and properties
211
                while(structureIterator.hasNext()){
212
                    FeatureNode structureNode = structureIterator.next();
213
                    Iterator<FeatureNode> propertiesIterator = ((TreeSelection)propertyTreeSelection).iterator();
214
                    while(propertiesIterator.hasNext()){
215
                        FeatureNode propertyNode = propertiesIterator.next();
216
                        //create new Character
217
                        String label = structureNode.getFeature().generateTitle()+" "+propertyNode.getFeature().generateTitle();
218
                        Character character = Character.NewInstance(structureNode, propertyNode, "", label, label);
219
                        character = HibernateProxyHelper.deproxy(CdmStore.getService(ITermService.class).merge(character), Character.class);
220

  
221
                        addCharacterSorted(character);
222
                    }
207
                List<Character> characters = createCharacters((TreeSelection)structureTreeSelection, (TreeSelection)propertyTreeSelection);
208
                for (Character character : characters) {
209
                    //add new Character to feature tree
210
                    FeatureNode parent = ((FeatureTree) characterTreeEditorComposite.getViewer().getInput()).getRoot();
211
                    CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(parent.getUuid(), character.getUuid());
223 212
                }
224 213

  
225 214
                setDirty();
......
228 217
        });
229 218
    }
230 219

  
231
    private void addCharacterSorted(Character character){
232
        //add new Character to feature tree
233
        FeatureNode parent = ((FeatureTree) characterTreeEditorComposite.getViewer().getInput()).getRoot();
234
        CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(parent.getUuid(), character.getUuid());
220
    public static List<Character> createCharacters(TreeSelection structureTreeSelection, TreeSelection propertyTreeSelection){
221
        List<Character> characters = new ArrayList<>();
222
        //get selected structures and properties
223
        Iterator<FeatureNode> structureIterator = structureTreeSelection.iterator();
224

  
225
        //create all combinations of structures and properties
226
        while(structureIterator.hasNext()){
227
            FeatureNode structureNode = structureIterator.next();
228
            Iterator<FeatureNode> propertiesIterator = propertyTreeSelection.iterator();
229
            while(propertiesIterator.hasNext()){
230
                FeatureNode propertyNode = propertiesIterator.next();
231
                //create new Character
232
                String label = structureNode.getFeature().generateTitle()+" "+propertyNode.getFeature().generateTitle();
233
                Character character = Character.NewInstance(structureNode, propertyNode, "", label, label);
234
                characters.add(HibernateProxyHelper.deproxy(CdmStore.getService(ITermService.class).save(character), Character.class));
235
            }
236
        }
237
        return characters;
235 238
    }
236 239

  
237 240
    @Override
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/featuretree/e4/FeatureNodeDropAdapter.java
13 13
import java.util.Collection;
14 14
import java.util.Collections;
15 15

  
16
import org.eclipse.core.runtime.NullProgressMonitor;
17
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
18 16
import org.eclipse.jface.util.LocalSelectionTransfer;
19 17
import org.eclipse.jface.viewers.IStructuredSelection;
20 18
import org.eclipse.jface.viewers.TreeViewer;
......
34 32
import eu.etaxonomy.taxeditor.l10n.Messages;
35 33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
36 34
import eu.etaxonomy.taxeditor.store.CdmStore;
35
import eu.etaxonomy.taxeditor.store.StoreUtil;
37 36
import eu.etaxonomy.taxeditor.ui.dialog.selection.TermVocabularySelectionDialog;
38 37
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
39 38
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
......
43 42
    public static final String SAVE_CHANGES_TITLE = "Unsaved changes";
44 43
    public static final String SAVE_CHANGES_MESSAGE = "The editor has unsaved changes. You need to save before performind this operation. Save now?";
45 44

  
46
    private MDirtyable dirtyable;
47
    private IE4SavablePart savablePart;
45
    protected IE4SavablePart savablePart;
48 46

  
49
    public FeatureNodeDropAdapter(MDirtyable dirtyable, IE4SavablePart savablePart, Viewer viewer) {
47
    public FeatureNodeDropAdapter(IE4SavablePart savablePart, Viewer viewer) {
50 48
		super(viewer);
51
		this.dirtyable = dirtyable;
52 49
		this.savablePart = savablePart;
53 50
	}
54 51

  
55 52
	@Override
56 53
	public boolean performDrop(Object data) {
57
        if(dirtyable.isDirty()){
58
            if(MessagingUtils.confirmDialog(SAVE_CHANGES_TITLE, SAVE_CHANGES_MESSAGE)){
59
                savablePart.save(new NullProgressMonitor());
60
            }
61
            else{
62
                return false;
63
            }
64
        }
54
	    if(StoreUtil.checkDirty(savablePart)){
55
	        return false;
56
	    }
65 57
		Object currentTarget = getCurrentTarget();
66 58
		FeatureNode target = null;
67 59
		if(currentTarget instanceof FeatureTree){
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/featuretree/e4/FeatureTreeEditor.java
105 105
                TermTransfer.getInstance(),
106 106
                LocalSelectionTransfer.getTransfer()};
107 107
        viewer.addDragSupport(ops, transfers, new FeatureNodeDragListener(viewer));
108
        viewer.addDropSupport(ops, transfers, new FeatureNodeDropAdapter(dirty, this, viewer));
108
        viewer.addDropSupport(ops, transfers, new FeatureNodeDropAdapter(this, viewer));
109 109
        viewer.addSelectionChangedListener(this);
110 110

  
111 111
        List<FeatureTree> trees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/featuretree/e4/FeatureTreeEditorComposite.java
8 8
*/
9 9
package eu.etaxonomy.taxeditor.featuretree.e4;
10 10

  
11
import org.eclipse.jface.util.LocalSelectionTransfer;
11 12
import org.eclipse.jface.viewers.ISelectionChangedListener;
12 13
import org.eclipse.jface.viewers.TreeViewer;
13 14
import org.eclipse.swt.SWT;
......
109 110
        viewer.setLabelProvider(new FeatureTreeLabelProvider());
110 111

  
111 112
        int ops = DND.DROP_COPY | DND.DROP_MOVE;
112
        Transfer[] transfers = new Transfer[] { FeatureNodeTransfer
113
                .getInstance(), TermTransfer.getInstance() };
113
        Transfer[] transfers = new Transfer[] {
114
                FeatureNodeTransfer.getInstance(),
115
                TermTransfer.getInstance(),
116
                LocalSelectionTransfer.getTransfer()
117
                };
114 118
        if(dragSourceListener!=null){
115 119
            viewer.addDragSupport(ops, transfers, dragSourceListener);
116 120
        }

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)