1/*
2 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.awt.X11;
27
28import java.util.ArrayList;
29import java.util.Collections;
30import java.util.Iterator;
31import java.util.List;
32
33/**
34 * This class is a registry for the supported drag and drop protocols.
35 *
36 * @since 1.5
37 */
38final class XDragAndDropProtocols {
39    private static final List<XDragSourceProtocol> dragProtocols;
40    private static final List<XDropTargetProtocol> dropProtocols;
41
42    public static final String XDnD = "XDnD";
43    public static final String MotifDnD = "MotifDnD";
44
45    static {
46        // Singleton listener for all drag source protocols.
47        XDragSourceProtocolListener dragSourceProtocolListener =
48            XDragSourceContextPeer.getXDragSourceProtocolListener();
49        // Singleton listener for all drop target protocols.
50        XDropTargetProtocolListener dropTargetProtocolListener =
51            XDropTargetContextPeer.getXDropTargetProtocolListener();
52
53        List<XDragSourceProtocol> tDragSourceProtocols = new ArrayList<>();
54        XDragSourceProtocol xdndDragSourceProtocol =
55            XDnDDragSourceProtocol.createInstance(dragSourceProtocolListener);
56        tDragSourceProtocols.add(xdndDragSourceProtocol);
57        XDragSourceProtocol motifdndDragSourceProtocol =
58            MotifDnDDragSourceProtocol.createInstance(dragSourceProtocolListener);
59        tDragSourceProtocols.add(motifdndDragSourceProtocol);
60
61        List<XDropTargetProtocol> tDropTargetProtocols = new ArrayList<>();
62        XDropTargetProtocol xdndDropTargetProtocol =
63            XDnDDropTargetProtocol.createInstance(dropTargetProtocolListener);
64        tDropTargetProtocols.add(xdndDropTargetProtocol);
65        XDropTargetProtocol motifdndDropTargetProtocol =
66            MotifDnDDropTargetProtocol.createInstance(dropTargetProtocolListener);
67        tDropTargetProtocols.add(motifdndDropTargetProtocol);
68
69        dragProtocols = Collections.unmodifiableList(tDragSourceProtocols);
70        dropProtocols = Collections.unmodifiableList(tDropTargetProtocols);
71    }
72
73    static Iterator<XDragSourceProtocol> getDragSourceProtocols() {
74        return dragProtocols.iterator();
75    }
76
77    static Iterator<XDropTargetProtocol> getDropTargetProtocols() {
78        return dropProtocols.iterator();
79    }
80
81    /*
82     * Returns a XDragSourceProtocol whose name equals to the specified string
83     * or null if no such protocol is registered.
84     */
85    public static XDragSourceProtocol getDragSourceProtocol(String name) {
86        // Protocol name cannot be null.
87        if (name == null) {
88            return null;
89        }
90
91        Iterator<XDragSourceProtocol> dragProtocols =
92            XDragAndDropProtocols.getDragSourceProtocols();
93        while (dragProtocols.hasNext()) {
94            XDragSourceProtocol dragProtocol = dragProtocols.next();
95            if (dragProtocol.getProtocolName().equals(name)) {
96                return dragProtocol;
97            }
98        }
99
100        return null;
101    }
102
103    /*
104     * Returns a XDropTargetProtocol which name equals to the specified string
105     * or null if no such protocol is registered.
106     */
107    public static XDropTargetProtocol getDropTargetProtocol(String name) {
108        // Protocol name cannot be null.
109        if (name == null) {
110            return null;
111        }
112
113        Iterator<XDropTargetProtocol> dropProtocols =
114            XDragAndDropProtocols.getDropTargetProtocols();
115        while (dropProtocols.hasNext()) {
116            XDropTargetProtocol dropProtocol = dropProtocols.next();
117            if (dropProtocol.getProtocolName().equals(name)) {
118                return dropProtocol;
119            }
120        }
121
122        return null;
123    }
124}
125