2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.termtree
;
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
;
22 import org
.eclipse
.swt
.dnd
.ByteArrayTransfer
;
23 import org
.eclipse
.swt
.dnd
.TransferData
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ITermNodeService
;
26 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
27 import eu
.etaxonomy
.cdm
.model
.description
.Character
;
28 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTerm
;
29 import eu
.etaxonomy
.cdm
.model
.term
.Representation
;
30 import eu
.etaxonomy
.cdm
.model
.term
.TermNode
;
31 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
32 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
39 public class CharacterTransfer
extends ByteArrayTransfer
{
41 private static CharacterTransfer instance
= new CharacterTransfer();
42 private static final String TYPE_NAME
= "characters-transfer-format";
43 private static final int TYPEID
= registerType(TYPE_NAME
);
46 public static synchronized CharacterTransfer
getInstance() {
50 private CharacterTransfer() {
54 protected int[] getTypeIds() {
55 return new int[] { TYPEID
};
59 protected String
[] getTypeNames() {
60 return new String
[] { TYPE_NAME
};
64 protected void javaToNative(Object object
, TransferData transferData
) {
66 Collection
<Character
> characters
= (Collection
<Character
>)object
;
67 ByteArrayOutputStream byteOut
= new ByteArrayOutputStream();
68 DataOutputStream out
= new DataOutputStream(byteOut
);
74 for(Character character
: characters
){
75 out
.writeUTF(character
.getStructure().getUuid().toString()
77 +character
.getProperty().getUuid().toString());
80 bytes
= byteOut
.toByteArray();
81 } catch (IOException e
) {
82 // when in doubt send nothing
85 super.javaToNative(bytes
, transferData
);
91 protected Object
nativeToJava(TransferData transferData
) {
92 byte[] bytes
= (byte[]) super.nativeToJava(transferData
);
94 DataInputStream in
= new DataInputStream(
95 new ByteArrayInputStream(bytes
));
98 List
<Character
> characters
= new ArrayList
<>();
102 String string
= in
.readUTF();
103 String
[] split
= string
.split(",");
104 ITermNodeService termNodeService
= CdmStore
.getService(ITermNodeService
.class);
105 TermNode
<DefinedTerm
> structure
= termNodeService
.load(UUID
.fromString(split
[0]));
106 TermNode
<DefinedTerm
> property
= termNodeService
.load(UUID
.fromString(split
[1]));
107 characters
.add(createCharacter(structure
, property
));
109 }catch(EOFException e
){
110 return characters
.toArray();
112 } catch (IOException e
) {
119 public static Character
createCharacter(TermNode
<DefinedTerm
> structureNode
,
120 TermNode
<DefinedTerm
> propertyNode
) {
121 Representation structureRepresentation
= structureNode
.getTerm().getRepresentation(PreferencesUtil
.getGlobalLanguage());
122 if(structureRepresentation
==null){
123 structureRepresentation
= structureNode
.getTerm().getRepresentation(Language
.DEFAULT());
125 Representation propertyRepresentation
= propertyNode
.getTerm().getRepresentation(PreferencesUtil
.getGlobalLanguage());
126 if(propertyRepresentation
==null){
127 propertyRepresentation
= propertyNode
.getTerm().getRepresentation(Language
.DEFAULT());
130 String abbrevLabel
= null;
131 if(structureRepresentation
!=null && propertyRepresentation
!=null){
132 if(structureRepresentation
.getLabel()!=null && propertyRepresentation
.getLabel()!=null){
133 label
= structureRepresentation
.getLabel()+" "+propertyRepresentation
.getLabel();
135 if(structureRepresentation
.getAbbreviatedLabel()!=null && propertyRepresentation
.getAbbreviatedLabel()!=null){
136 abbrevLabel
= structureRepresentation
.getAbbreviatedLabel()+" "+propertyRepresentation
.getAbbreviatedLabel();
141 label
= structureNode
.getTerm().getLabel()+" "+propertyNode
.getTerm().getLabel();
143 //create new Character
144 Character character
= Character
.NewInstance(structureNode
, propertyNode
, null, label
, abbrevLabel
);