1/*
2 * Copyright (c) 2014, 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.datatransfer;
27
28import sun.awt.AppContext;
29import sun.datatransfer.DesktopDatatransferService;
30
31import java.awt.EventQueue;
32import java.awt.datatransfer.DataFlavor;
33import java.awt.datatransfer.FlavorMap;
34import java.util.LinkedHashSet;
35import java.util.function.Supplier;
36
37/**
38 * Provides desktop services to the datatransfer module according to
39 * {@code DesktopDatatransferService} interface.
40 *
41 * @author Petr Pchelko
42 * @since 9
43 */
44public class DesktopDatatransferServiceImpl implements DesktopDatatransferService {
45
46    private static final Object FLAVOR_MAP_KEY = new Object();
47
48    @Override
49    public void invokeOnEventThread(Runnable r) {
50        EventQueue.invokeLater(r);
51    }
52
53    @Override
54    public String getDefaultUnicodeEncoding() {
55        DataTransferer dataTransferer = DataTransferer.getInstance();
56        if (dataTransferer != null) {
57            return dataTransferer.getDefaultUnicodeEncoding();
58        }
59        return null;
60    }
61
62    @Override
63    public FlavorMap getFlavorMap(Supplier<FlavorMap> supplier) {
64        AppContext context = AppContext.getAppContext();
65        FlavorMap fm = (FlavorMap) context.get(FLAVOR_MAP_KEY);
66        if (fm == null) {
67            fm = supplier.get();
68            context.put(FLAVOR_MAP_KEY, fm);
69        }
70        return fm;
71    }
72
73    @Override
74    public boolean isDesktopPresent() {
75        return true;
76    }
77
78    @Override
79    public LinkedHashSet<DataFlavor> getPlatformMappingsForNative(String nat) {
80        DataTransferer instance = DataTransferer.getInstance();
81        return instance != null ? instance.getPlatformMappingsForNative(nat) : new LinkedHashSet<>();
82    }
83
84    @Override
85    public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
86        DataTransferer instance = DataTransferer.getInstance();
87        return instance != null ? instance.getPlatformMappingsForFlavor(df) : new LinkedHashSet<>();
88    }
89
90    @Override
91    public void registerTextFlavorProperties(String nat, String charset, String eoln, String terminators) {
92        DataTransferer instance = DataTransferer.getInstance();
93        if (instance != null) {
94            instance.registerTextFlavorProperties(nat, charset, eoln, terminators);
95        }
96    }
97}
98