Project

General

Profile

Revision a4063890

IDa40638905e06ece4dc7fd019c454f90d96bbc8a1
Parent fd79a16f
Child c2849ea1

Added by Patrick Plitzner 5 months ago

ref #7923 Use character transfer instead of LocalSelectionTransfer

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 java.util.List;
11
import java.util.Collection;
12 12

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

  
19 18
import eu.etaxonomy.cdm.model.description.Character;
19
import eu.etaxonomy.cdm.model.description.FeatureNode;
20
import eu.etaxonomy.taxeditor.featuretree.CharacterTransfer;
20 21
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureNodeDragListener;
21 22

  
22 23
/**
......
42 43
        if(structureSelection!=null && propertySelection!=null
43 44
                && !structureSelection.isEmpty() && !propertySelection.isEmpty()){
44 45
            //create all possible characters
45
            List<Character> characters = CharacterEditor.createCharacters(structureSelection, propertySelection);
46
            Collection<Character> characters = CharacterEditor.createCharacters(structureSelection, propertySelection);
46 47

  
47
            LocalSelectionTransfer.getTransfer().setSelection(new StructuredSelection(characters));
48
            if (CharacterTransfer.getInstance().isSupportedType(event.dataType)) {
49
                event.data = characters;
50
            }
48 51
        }
49 52
    }
50 53

  
54
    /**
55
     * Method declared on DragSourceListener
56
     */
57
    @Override
58
    public void dragStart(DragSourceEvent event) {
59
        event.doit =
60
                !structureViewer.getSelection().isEmpty()
61
                && !propertyViewer.getSelection().isEmpty()
62
                && ((IStructuredSelection) structureViewer.getSelection()).toList()
63
                .stream().allMatch(element -> element instanceof FeatureNode)
64
                && ((IStructuredSelection) propertyViewer.getSelection()).toList()
65
                .stream().allMatch(element -> element instanceof FeatureNode);
66
    }
67

  
51 68
}
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 11
import org.eclipse.e4.ui.di.UISynchronize;
14
import org.eclipse.jface.util.LocalSelectionTransfer;
15
import org.eclipse.jface.viewers.ISelection;
16
import org.eclipse.jface.viewers.IStructuredSelection;
17 12
import org.eclipse.jface.viewers.Viewer;
18 13
import org.eclipse.swt.dnd.DropTargetEvent;
19 14
import org.eclipse.swt.dnd.TransferData;
......
22 17
import eu.etaxonomy.cdm.model.description.Character;
23 18
import eu.etaxonomy.cdm.model.description.FeatureNode;
24 19
import eu.etaxonomy.cdm.model.description.FeatureTree;
20
import eu.etaxonomy.taxeditor.featuretree.CharacterTransfer;
25 21
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeDropAdapter;
26 22
import eu.etaxonomy.taxeditor.featuretree.e4.IFeatureTreeEditor;
27 23
import eu.etaxonomy.taxeditor.store.CdmStore;
......
46 42

  
47 43
	@Override
48 44
	public boolean performDrop(Object data) {
45
	    if(!CharacterTransfer.getInstance().isSupportedType(getCurrentEvent().currentDataType)){
46
	        return super.performDrop(data);
47
	    }
49 48
	    if(StoreUtil.checkDirty(editor)){
50 49
	        return false;
51 50
	    }
52 51
	    FeatureNode target = (FeatureNode) getCurrentTarget();
53
	    ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
54
	    if(selection!=null
55
	            &&selection instanceof IStructuredSelection
56
	            ){
57
	        IStructuredSelection structuredSelection = (IStructuredSelection)selection;
58
	        Iterator iterator = structuredSelection.iterator();
59
	        while(iterator.hasNext()){
60
	            Object next = iterator.next();
61
	            if(next instanceof Character){
62
	                Character character = (Character)next;
52
	    if(data instanceof Object[]){
53
	        Object[] transferObjects = (Object[])data;
54
	        for(int i=0;i<transferObjects.length;i++){
55
	            Object object = transferObjects[i];
56
	            if(object instanceof Character){
57
	                Character character = (Character)object;
63 58
	                if(target==null){
64
	                    //drop on feature tree
65
	                    target = ((FeatureTree)getViewer().getInput()).getRoot();
66
	                }
67
	                CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(target.getUuid(), character.getUuid());
59
                        //drop on feature tree
60
                        target = ((FeatureTree)getViewer().getInput()).getRoot();
61
                    }
62
                    CdmStore.getService(IFeatureNodeService.class).createChildFeatureNode(target, character);
68 63
	            }
69 64
	        }
70 65
	        getViewer().refresh();
71
	        LocalSelectionTransfer.getTransfer().setSelection(null);
72 66
	        return true;
73 67
	    }
74
	    return super.performDrop(data);
68
	    return false;
75 69
	}
76 70

  
77 71
	@Override
78 72
	public boolean validateDrop(Object target, int operation,
79 73
			TransferData transferData) {
80
		LocalSelectionTransfer transfer = LocalSelectionTransfer.getTransfer();
81
        boolean valid = true;
82
		valid &= transfer.getSelection()!=null
83
		        && !transfer.getSelection().isEmpty();
84
		if(!valid){
85
		    return super.validateDrop(target, operation, transferData);
74
		if(CharacterTransfer.getInstance().isSupportedType(transferData)){
75
		    return true;
86 76
		}
87
		return valid;
77
		return super.validateDrop(target, operation, transferData);
88 78
	}
89 79

  
90 80
}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/character/CharacterEditor.java
11 11

  
12 12
import java.util.ArrayList;
13 13
import java.util.Arrays;
14
import java.util.Collection;
14 15
import java.util.HashMap;
16
import java.util.HashSet;
15 17
import java.util.Iterator;
16 18
import java.util.List;
17 19
import java.util.Map;
20
import java.util.Set;
18 21

  
19 22
import javax.annotation.PostConstruct;
20 23
import javax.annotation.PreDestroy;
......
35 38
import org.eclipse.jface.viewers.TreeSelection;
36 39
import org.eclipse.swt.SWT;
37 40
import org.eclipse.swt.custom.SashForm;
41
import org.eclipse.swt.dnd.Transfer;
38 42
import org.eclipse.swt.events.ModifyEvent;
39 43
import org.eclipse.swt.events.ModifyListener;
40 44
import org.eclipse.swt.events.SelectionAdapter;
......
52 56
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
53 57
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
54 58
import eu.etaxonomy.cdm.api.service.ITermService;
55
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
56 59
import eu.etaxonomy.cdm.model.description.Character;
57 60
import eu.etaxonomy.cdm.model.description.FeatureNode;
58 61
import eu.etaxonomy.cdm.model.description.FeatureTree;
59 62
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
60 63
import eu.etaxonomy.taxeditor.editor.AppModelId;
61 64
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
65
import eu.etaxonomy.taxeditor.featuretree.CharacterTransfer;
66
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
62 67
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureNodeDragListener;
63 68
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeDropAdapter;
64 69
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditorComposite;
......
135 140
        composite.setLayout(gl_composite);
136 141

  
137 142
        structuresTreeEditorComposite = addFeatureTreeEditor(composite);
138
        initFeatureTreeComposite(structuresTreeEditorComposite, null, null);
143
        initFeatureTreeComposite(structuresTreeEditorComposite, null, null, null);
139 144
        structuresTreeEditorComposite.getLabel_title().setText(Messages.CharacterEditor_STRUCTURES);
140 145

  
141 146
        Composite composite_1 = new Composite(sashForm, SWT.NONE);
......
146 151
        composite_1.setLayout(gl_composite_1);
147 152

  
148 153
        propertiesTreeEditorComposite = addFeatureTreeEditor(composite_1);
149
        initFeatureTreeComposite(propertiesTreeEditorComposite, new CharacterDragListener(structuresTreeEditorComposite.getViewer(), propertiesTreeEditorComposite.getViewer()), null);
154
        initFeatureTreeComposite(propertiesTreeEditorComposite,
155
                new CharacterDragListener(structuresTreeEditorComposite.getViewer(),
156
                        propertiesTreeEditorComposite.getViewer()),
157
                null,
158
                new Transfer[] {CharacterTransfer.getInstance()}
159
        );
150 160
        propertiesTreeEditorComposite.getLabel_title().setText(Messages.CharacterEditor_PROPERTIES);
151 161

  
152 162
        Composite composite_3 = new Composite(sashForm, SWT.NONE);
......
161 171
        formToolkit.adapt(btnAddCharacter, true, true);
162 172

  
163 173
        characterTreeEditorComposite = addFeatureTreeEditor(composite_3);
164
        initFeatureTreeComposite(characterTreeEditorComposite, new FeatureNodeDragListener(characterTreeEditorComposite.getViewer()), new CharacterDropAdapter(this, characterTreeEditorComposite.getViewer(), sync));
174
        initFeatureTreeComposite(characterTreeEditorComposite, new FeatureNodeDragListener(characterTreeEditorComposite.getViewer()),
175
                new CharacterDropAdapter(this, characterTreeEditorComposite.getViewer(), sync),
176
                new Transfer[] {FeatureNodeTransfer.getInstance(), CharacterTransfer.getInstance()});
165 177
        characterTreeEditorComposite.getLabel_title().setText(Messages.CharacterEditor_CHARACTERS);
166 178

  
167 179
        //add context menu to character viewer//create context menu
......
177 189
        return featureTreeEditorComposite;
178 190
    }
179 191

  
180
    private void initFeatureTreeComposite(FeatureTreeEditorComposite featureTreeEditorComposite,
181
            FeatureNodeDragListener featureNodeDragListener, FeatureTreeDropAdapter featureNodeDropAdapter) {
182
        featureTreeEditorComposite.init(featureNodeDragListener, featureNodeDropAdapter, this,
183
                new FeatureTreeChooserListener(featureTreeEditorComposite), this);
192
    private void initFeatureTreeComposite(
193
            FeatureTreeEditorComposite featureTreeEditorComposite,
194
            FeatureNodeDragListener featureNodeDragListener,
195
            FeatureTreeDropAdapter featureNodeDropAdapter
196
            , Transfer[] transfers) {
197
        featureTreeEditorComposite.init(
198
                featureNodeDragListener,
199
                featureNodeDropAdapter,
200
                transfers,
201
                this,
202
                new FeatureTreeChooserListener(featureTreeEditorComposite),
203
                this);
184 204
    }
185 205

  
186 206
    @Focus
......
210 230
                            Messages.CharacterEditor_CANNOT_PERFORM_MESSAGE);
211 231
                    return;
212 232
                }
213
                List<Character> characters = createCharacters((TreeSelection)structureTreeSelection, (TreeSelection)propertyTreeSelection);
233
                Collection<Character> characters = createCharacters((TreeSelection)structureTreeSelection, (TreeSelection)propertyTreeSelection);
214 234
                for (Character character : characters) {
215 235
                    //add new Character to feature tree
216 236
                    FeatureNode parent = ((FeatureTree) characterTreeEditorComposite.getViewer().getInput()).getRoot();
......
223 243
        });
224 244
    }
225 245

  
226
    public static List<Character> createCharacters(TreeSelection structureTreeSelection, TreeSelection propertyTreeSelection){
227
        List<Character> characters = new ArrayList<>();
246
    public static Collection<Character> createCharacters(TreeSelection structureTreeSelection, TreeSelection propertyTreeSelection){
247
        Set<Character> characters = new HashSet<>();
228 248
        //get selected structures and properties
229 249
        Iterator<FeatureNode> structureIterator = structureTreeSelection.iterator();
230 250

  
......
235 255
            while(propertiesIterator.hasNext()){
236 256
                FeatureNode propertyNode = propertiesIterator.next();
237 257
                //create new Character
238
                String label = structureNode.getFeature().generateTitle()+" "+propertyNode.getFeature().generateTitle(); //$NON-NLS-1$
239
                Character character = Character.NewInstance(structureNode, propertyNode, "", label, label); //$NON-NLS-1$
240
                characters.add(HibernateProxyHelper.deproxy(CdmStore.getService(ITermService.class).save(character), Character.class));
258
                Character character = Character.NewInstance(structureNode, propertyNode);
259
                characters.add(character);
241 260
            }
242 261
        }
243 262
        return characters;
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/featuretree/CharacterTransfer.java
1
/**
2
* Copyright (C) 2018 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.taxeditor.featuretree;
10

  
11
import java.io.ByteArrayInputStream;
12
import java.io.ByteArrayOutputStream;
13
import java.io.DataInputStream;
14
import java.io.DataOutputStream;
15
import java.io.EOFException;
16
import java.io.IOException;
17
import java.util.ArrayList;
18
import java.util.Collection;
19
import java.util.List;
20
import java.util.UUID;
21

  
22
import org.eclipse.swt.dnd.ByteArrayTransfer;
23
import org.eclipse.swt.dnd.TransferData;
24

  
25
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
26
import eu.etaxonomy.cdm.model.description.Character;
27
import eu.etaxonomy.cdm.model.description.FeatureNode;
28
import eu.etaxonomy.taxeditor.store.CdmStore;
29

  
30
/**
31
 * @author pplitzner
32
 * @since Nov 30, 2018
33
 *
34
 */
35
public class CharacterTransfer extends ByteArrayTransfer {
36

  
37
    private static CharacterTransfer instance = new CharacterTransfer();
38
    private static final String TYPE_NAME = "characters-transfer-format";
39
    private static final int TYPEID = registerType(TYPE_NAME);
40

  
41

  
42
    public static synchronized CharacterTransfer getInstance() {
43
        return instance;
44
    }
45

  
46
    private CharacterTransfer() {
47
    }
48

  
49
    @Override
50
    protected int[] getTypeIds() {
51
        return new int[] { TYPEID };
52
    }
53

  
54
    @Override
55
    protected String[] getTypeNames() {
56
        return new String[] { TYPE_NAME };
57
    }
58

  
59
    @Override
60
    protected void javaToNative(Object object, TransferData transferData) {
61
        if (object != null){
62
            Collection<Character> characters = (Collection<Character>)object;
63
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
64
            DataOutputStream out = new DataOutputStream(byteOut);
65

  
66
            byte[] bytes = null;
67

  
68
            try {
69

  
70
                for(Character character : characters){
71
                    out.writeUTF(character.getStructure().getUuid().toString()
72
                            +","
73
                            +character.getProperty().getUuid().toString());
74
                }
75
                out.close();
76
                bytes = byteOut.toByteArray();
77
            } catch (IOException e) {
78
                // when in doubt send nothing
79
            }
80
            if (bytes != null) {
81
                super.javaToNative(bytes, transferData);
82
            }
83
        }
84
    }
85

  
86
    @Override
87
    protected Object nativeToJava(TransferData transferData) {
88
        byte[] bytes = (byte[]) super.nativeToJava(transferData);
89
        if (bytes != null){
90
            DataInputStream in = new DataInputStream(
91
                    new ByteArrayInputStream(bytes));
92

  
93
            try {
94
                List<Character> characters = new ArrayList<>();
95

  
96
                try{
97
                    while(true){
98
                        String string = in.readUTF();
99
                        String[] split = string.split(",");
100
                        IFeatureNodeService featureNodeService = CdmStore.getService(IFeatureNodeService.class);
101
                        FeatureNode structure = featureNodeService.load(UUID.fromString(split[0]));
102
                        FeatureNode property = featureNodeService.load(UUID.fromString(split[1]));
103
                        characters.add(Character.NewInstance(structure, property));
104
                    }
105
                }catch(EOFException e){
106
                    return characters.toArray();
107
                }
108
            } catch (IOException e) {
109
                return null;
110
            }
111
        }
112
        return null;
113
    }
114

  
115
}
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;
12 11
import org.eclipse.jface.viewers.ISelectionChangedListener;
13 12
import org.eclipse.jface.viewers.TreeViewer;
14 13
import org.eclipse.swt.SWT;
......
84 83
                new GridData(SWT.FILL, SWT.FILL, true, true));
85 84
    }
86 85

  
87
    public void init(String label, DragSourceListener dragSourceListener, DropTargetListener dropTargetListener,
88
            ISelectionChangedListener viewerSelectionChangedListener, IDirtyMarkable dirtyMarkable,
86
    public void init(String label,
87
            DragSourceListener dragSourceListener,
88
            DropTargetListener dropTargetListener,
89
            ISelectionChangedListener viewerSelectionChangedListener,
90
            IDirtyMarkable dirtyMarkable,
89 91
            ModifyListener textModifyListener) {
90
        this.init(label, dragSourceListener, dropTargetListener, viewerSelectionChangedListener, null, dirtyMarkable,
92
        this.init(label,
93
                dragSourceListener,
94
                dropTargetListener,
95
                null,
96
                viewerSelectionChangedListener,
97
                null,
98
                dirtyMarkable,
91 99
                textModifyListener);
92 100
    }
93
    public void init(DragSourceListener dragSourceListener, DropTargetListener dropTargetListener,
101
    public void init(DragSourceListener dragSourceListener,
102
            DropTargetListener dropTargetListener,
103
            Transfer[] transfers,
94 104
            ISelectionChangedListener viewerSelectionChangedListener,
95 105
            SelectionListener openFeatureTreeSelectionListener,
96 106
            ModifyListener textModifyListener) {
97
        init(null, dragSourceListener, dropTargetListener, viewerSelectionChangedListener, openFeatureTreeSelectionListener,
98
                null, textModifyListener);
107
        init(null, dragSourceListener,
108
                dropTargetListener,
109
                transfers,
110
                viewerSelectionChangedListener,
111
                openFeatureTreeSelectionListener,
112
                null,
113
                textModifyListener);
99 114
    }
100 115
    private void init(String label, DragSourceListener dragSourceListener,
101 116
            DropTargetListener dropTargetListener,
117
            Transfer[] transfers,
102 118
            ISelectionChangedListener viewerSelectionChangedListener,
103
            SelectionListener openFeatureTreeSelectionListener, IDirtyMarkable dirtyMarkable,
119
            SelectionListener openFeatureTreeSelectionListener,
120
            IDirtyMarkable dirtyMarkable,
104 121
            ModifyListener textModifyListener) {
105 122
        if(label!=null){
106 123
            label_title.setText(label);
......
110 127
        viewer.setLabelProvider(new FeatureTreeLabelProvider());
111 128

  
112 129
        int ops = DND.DROP_COPY | DND.DROP_MOVE;
113
        Transfer[] transfers = new Transfer[] {
130
        if(transfers==null){
131
        transfers = new Transfer[] {
114 132
                FeatureNodeTransfer.getInstance(),
115 133
                TermTransfer.getInstance(),
116
                LocalSelectionTransfer.getTransfer()
117 134
                };
135
        }
118 136
        if(dragSourceListener!=null){
119 137
            viewer.addDragSupport(ops, transfers, dragSourceListener);
120 138
        }
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/newWizard/NewPolytomousKeyWizard.java
4 4
package eu.etaxonomy.taxeditor.newWizard;
5 5

  
6 6
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
7
import eu.etaxonomy.cdm.common.CdmUtils;
7 8
import eu.etaxonomy.cdm.model.description.PolytomousKey;
8 9
import eu.etaxonomy.taxeditor.store.CdmStore;
9 10

  
......
30 31
	 */
31 32
	@Override
32 33
	protected void saveEntity() {
33
	    if(!polytomousKeyPage.getPolytomousKeyName().equals("")) {
34
	    if(CdmUtils.isNotBlank(polytomousKeyPage.getPolytomousKeyName())) {
34 35
	        CdmStore.getService(IPolytomousKeyService.class).merge(getEntity(), true);
35 36
	    }
36 37
	}
......
50 51
		return "Polytomous Key";
51 52
	}
52 53

  
54
	@Override
55
	public boolean canFinish() {
56
        if(CdmUtils.isBlank(polytomousKeyPage.getPolytomousKeyName())) {
57
            return false;
58
        }
59
	    return super.canFinish();
60
	}
61

  
53 62
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)