Path: blob/master/src/java.desktop/unix/classes/sun/awt/X11/XDesktopPeer.java
41159 views
/*1* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt.X11;262728import sun.awt.UNIXToolkit;2930import java.io.File;31import java.io.IOException;32import java.net.MalformedURLException;33import java.net.URI;3435import java.awt.Desktop.Action;36import java.awt.peer.DesktopPeer;37import java.util.ArrayList;38import java.util.Arrays;39import java.util.List;404142/**43* Concrete implementation of the interface {@code DesktopPeer} for44* the Gnome desktop on Linux and Unix platforms.45*46* @see DesktopPeer47*/48public class XDesktopPeer implements DesktopPeer {4950// supportedActions may be changed from native within an init() call51private static final List<Action> supportedActions52= new ArrayList<>(Arrays.asList(Action.OPEN, Action.MAIL, Action.BROWSE));5354private static boolean nativeLibraryLoaded = false;55private static boolean initExecuted = false;5657private static void initWithLock(){58XToolkit.awtLock();59try {60if (!initExecuted) {61nativeLibraryLoaded = init(UNIXToolkit.getEnabledGtkVersion()62.getNumber(), UNIXToolkit.isGtkVerbose());63}64} finally {65initExecuted = true;66XToolkit.awtUnlock();67}68}6970//package-private71XDesktopPeer(){72initWithLock();73}7475static boolean isDesktopSupported() {76initWithLock();77return nativeLibraryLoaded && !supportedActions.isEmpty();78}7980public boolean isSupported(Action type) {81return supportedActions.contains(type);82}8384public void open(File file) throws IOException {85try {86launch(file.toURI());87} catch (MalformedURLException e) {88throw new IOException(file.toString());89}90}9192public void edit(File file) throws IOException {93throw new UnsupportedOperationException("The current platform " +94"doesn't support the EDIT action.");95}9697public void print(File file) throws IOException {98throw new UnsupportedOperationException("The current platform " +99"doesn't support the PRINT action.");100}101102public void mail(URI uri) throws IOException {103launch(uri);104}105106public void browse(URI uri) throws IOException {107launch(uri);108}109110private void launch(URI uri) throws IOException {111byte[] uriByteArray = ( uri.toString() + '\0' ).getBytes();112boolean result = false;113XToolkit.awtLock();114try {115if (!nativeLibraryLoaded) {116throw new IOException("Failed to load native libraries.");117}118result = gnome_url_show(uriByteArray);119} finally {120XToolkit.awtUnlock();121}122if (!result) {123throw new IOException("Failed to show URI:" + uri);124}125}126127private native boolean gnome_url_show(byte[] url);128private static native boolean init(int gtkVersion, boolean verbose);129}130131132