aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/SimpleXmlTransfer.java
blob: 20ac2033e400e4705d3b2cd208e4b217fad4dc1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.editors.layout.gle2;

import com.android.ide.eclipse.adt.internal.editors.descriptors.ElementDescriptor;
import com.android.ide.eclipse.adt.internal.editors.layout.descriptors.ViewElementDescriptor;

import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.TransferData;

import java.io.UnsupportedEncodingException;

/**
 * A d'n'd {@link Transfer} class that can transfer a <em>simplified</em> XML fragment
 * to transfer elements and their attributes between {@link LayoutCanvas}.
 * <p/>
 * The implementation is based on the {@link ByteArrayTransfer} and what we transfer
 * is text with the following fixed format:
 * <p/>
 * <pre>
 * {element-name element-property ...
 *  attrib_name="attrib_value"
 *  attrib2="..."
 *  {...inner elements...
 *  }
 * }
 * {...next element...
 * }
 *
 * </pre>
 * The format has nothing to do with XML per se, except for the fact that the
 * transfered content represents XML elements and XML attributes.
 *
 * <p/>
 * The detailed syntax is:
 * <pre>
 * - ELEMENT     := {NAME PROPERTY*\nATTRIB_LINE*ELEMENT*}\n
 * - PROPERTY    := $[A-Z]=[^ ]*
 * - NAME        := [^\n=]+
 * - ATTRIB_LINE := @URI:NAME=[^\n]*\n
 * </pre>
 *
 * Elements are represented by {@link SimpleElement}s and their attributes by
 * {@link SimpleAttribute}s, all of which have very specific properties that are
 * specifically limited to our needs for drag'n'drop.
 */
final class SimpleXmlTransfer extends ByteArrayTransfer {

    // Reference: http://www.eclipse.org/articles/Article-SWT-DND/DND-in-SWT.html

    private static final String TYPE_NAME = "android.ADT.simple.xml.transfer.1";    //$NON-NLS-1$
    private static final int TYPE_ID = registerType(TYPE_NAME);
    private static final SimpleXmlTransfer sInstance = new SimpleXmlTransfer();

    /** Private constructor. Use {@link #getInstance()} to retrieve the singleton instance. */
    private SimpleXmlTransfer() {
        // pass
    }

    /** Returns the singleton instance. */
    public static SimpleXmlTransfer getInstance() {
        return sInstance;
    }

    /**
     * Helper method that returns the FQCN transfered for the given {@link ElementDescriptor}.
     * <p/>
     * If the descriptor is a {@link ViewElementDescriptor}, the transfered data is the FQCN
     * of the Android View class represented (e.g. "android.widget.Button").
     * For any other non-null descriptor, the XML name is used.
     * Otherwise it is null.
     *
     * @param desc The {@link ElementDescriptor} to transfer.
     * @return The FQCN, XML name or null.
     */
    public static String getFqcn(ElementDescriptor desc) {
        if (desc instanceof ViewElementDescriptor) {
            return ((ViewElementDescriptor) desc).getFullClassName();
        } else if (desc != null) {
            return desc.getXmlName();
        }

        return null;
    }

    @Override
    protected int[] getTypeIds() {
        return new int[] { TYPE_ID };
    }

    @Override
    protected String[] getTypeNames() {
        return new String[] { TYPE_NAME };
    }

    /** Transforms a array of {@link SimpleElement} into a native data transfer. */
    @Override
    protected void javaToNative(Object object, TransferData transferData) {
        if (object == null || !(object instanceof SimpleElement[])) {
            return;
        }

        if (isSupportedType(transferData)) {
            StringBuilder sb = new StringBuilder();
            for (SimpleElement e : (SimpleElement[]) object) {
                sb.append(e.toString());
            }
            String data = sb.toString();

            try {
                byte[] buf = data.getBytes("UTF-8");  //$NON-NLS-1$
                super.javaToNative(buf, transferData);
            } catch (UnsupportedEncodingException e) {
                // unlikely; ignore
            }
        }
    }

    /**
     * Recreates an array of {@link SimpleElement} from a native data transfer.
     *
     * @return An array of {@link SimpleElement} or null. The array may be empty.
     */
    @Override
    protected Object nativeToJava(TransferData transferData) {
        if (isSupportedType(transferData)) {
            byte[] buf = (byte[]) super.nativeToJava(transferData);
            if (buf != null && buf.length > 0) {
                try {
                    String s = new String(buf, "UTF-8"); //$NON-NLS-1$
                    return SimpleElement.parseString(s);
                } catch (UnsupportedEncodingException e) {
                    // unlikely to happen, but still possible
                }
            }
        }

        return null;
    }
}