Project

General

Profile

Download (3.61 KB) Statistics
| Branch: | Tag: | Revision:
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
}
(3-3/6)