Path: blob/master/src/java.desktop/unix/classes/sun/awt/X11/GtkFileDialogPeer.java
41159 views
/*1* Copyright (c) 2010, 2015, 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*/24package sun.awt.X11;2526import java.awt.FileDialog;27import java.awt.peer.FileDialogPeer;28import java.io.File;29import java.io.FilenameFilter;30import sun.awt.AWTAccessor;3132/**33* FileDialogPeer for the GtkFileChooser.34*35* @author Costantino Cerbo ([email protected])36*/37final class GtkFileDialogPeer extends XDialogPeer implements FileDialogPeer {3839private final FileDialog fd;4041// A pointer to the native GTK FileChooser widget42private volatile long widget = 0L;43private long standaloneWindow;44private volatile boolean quit;4546GtkFileDialogPeer(FileDialog fd) {47super(fd);48this.fd = fd;49}5051private static native void initIDs();52static {53initIDs();54}5556private native void run(String title, int mode, String dir, String file,57FilenameFilter filter, boolean isMultipleMode, int x, int y);58private native void quit();5960@Override61public native void toFront();6263@Override64public native void setBounds(int x, int y, int width, int height, int op);6566/**67* Called exclusively by the native C code.68*/69private void setFileInternal(String directory, String[] filenames) {70AWTAccessor.FileDialogAccessor accessor = AWTAccessor71.getFileDialogAccessor();7273if (filenames == null) {74accessor.setDirectory(fd, null);75accessor.setFile(fd, null);76accessor.setFiles(fd, null);77} else {78// Fix 6987233: add the trailing slash if it's absent79String with_separator = directory;80if (directory != null) {81with_separator = directory.endsWith(File.separator) ?82directory : (directory + File.separator);83}84accessor.setDirectory(fd, with_separator);85accessor.setFile(fd, filenames[0]);8687int filesNumber = (filenames != null) ? filenames.length : 0;88File[] files = new File[filesNumber];89for (int i = 0; i < filesNumber; i++) {90files[i] = new File(directory, filenames[i]);91}92accessor.setFiles(fd, files);93}94}9596/**97* Called exclusively by the native C code.98*/99private boolean filenameFilterCallback(String fullname) {100if (fd.getFilenameFilter() == null) {101// no filter, accept all.102return true;103}104105File filen = new File(fullname);106return fd.getFilenameFilter().accept(new File(filen.getParent()),107filen.getName());108}109110@Override111public void setVisible(boolean b) {112XToolkit.awtLock();113try {114quit = !b;115if (b) {116Runnable task = () -> {117showNativeDialog();118standaloneWindow = 0;119fd.setVisible(false);120};121new Thread(null, task, "ShowDialog", 0, false).start();122} else {123quit();124fd.setVisible(false);125}126} finally {127XToolkit.awtUnlock();128}129}130131@Override132public void dispose() {133XToolkit.awtLock();134try {135quit = true;136quit();137}138finally {139XToolkit.awtUnlock();140}141super.dispose();142}143144@Override145public void setDirectory(String dir) {146// We do not implement this method because we147// have delegated to FileDialog#setDirectory148}149150@Override151public void setFile(String file) {152// We do not implement this method because we153// have delegated to FileDialog#setFile154}155156protected void requestXFocus(long time, boolean timeProvided) {157if(standaloneWindow == 0) {158super.requestXFocus(time, timeProvided);159return;160}161XNETProtocol net_protocol = XWM.getWM().getNETProtocol();162if (net_protocol != null) {163net_protocol.setActiveWindow(standaloneWindow);164}165}166167@Override168public void setFilenameFilter(FilenameFilter filter) {169// We do not implement this method because we170// have delegated to FileDialog#setFilenameFilter171}172173private void showNativeDialog() {174String dirname = fd.getDirectory();175// File path has a priority against directory path.176String filename = fd.getFile();177if (filename != null) {178final File file = new File(filename);179if (fd.getMode() == FileDialog.LOAD180&& dirname != null181&& file.getParent() == null) {182// File path for gtk_file_chooser_set_filename.183filename = dirname + (dirname.endsWith(File.separator) ? "" :184File.separator) + filename;185}186if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) {187// Filename for gtk_file_chooser_set_current_name.188filename = file.getName();189// Directory path for gtk_file_chooser_set_current_folder.190dirname = file.getParent();191}192}193if (!quit) {194run(fd.getTitle(), fd.getMode(), dirname, filename,195fd.getFilenameFilter(), fd.isMultipleMode(), fd.getX(), fd.getY());196}197}198199/**200* Called by native code when GTK dialog is created.201*/202boolean setWindow(long xid) {203if (!quit && widget != 0) {204standaloneWindow = xid;205requestXFocus();206return true;207}208return false;209}210}211212213