GtkFileDialogPeer.java 5.6 KB
Newer Older
1
/*
2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * 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 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 */
package sun.awt.X11;

import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.peer.FileDialogPeer;
import java.io.File;
import java.io.FilenameFilter;
import sun.awt.AWTAccessor;

/**
 * FileDialogPeer for the GtkFileChooser.
 *
 * @author Costantino Cerbo (c.cerbo@gmail.com)
 */
class GtkFileDialogPeer extends XDialogPeer implements FileDialogPeer {

    private FileDialog fd;

43 44 45
    // A pointer to the native GTK FileChooser widget
    private volatile long widget = 0L;

46 47 48 49 50
    public GtkFileDialogPeer(FileDialog fd) {
        super((Dialog) fd);
        this.fd = fd;
    }

51 52 53 54 55
    private static native void initIDs();
    static {
        initIDs();
    }

56
    private native void run(String title, int mode, String dir, String file,
57
                            FilenameFilter filter, boolean isMultipleMode, int x, int y);
58 59
    private native void quit();

60 61 62
    @Override
    public native void toFront();

63 64
    @Override
    public native void setBounds(int x, int y, int width, int height, int op);
65

66 67 68 69 70 71 72 73 74 75 76 77
    /**
     * Called exclusively by the native C code.
     */
    private void setFileInternal(String directory, String[] filenames) {
        AWTAccessor.FileDialogAccessor accessor = AWTAccessor
                .getFileDialogAccessor();

        if (filenames == null) {
            accessor.setDirectory(fd, null);
            accessor.setFile(fd, null);
            accessor.setFiles(fd, null, null);
        } else {
78 79 80 81
            // Fix 6987233: add the trailing slash if it's absent
            accessor.setDirectory(fd, directory +
                    (directory.endsWith(File.separator) ?
                     "" : File.separator));
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
            accessor.setFile(fd, filenames[0]);
            accessor.setFiles(fd, directory, filenames);
        }
    }

    /**
     * Called exclusively by the native C code.
     */
    private boolean filenameFilterCallback(String fullname) {
        if (fd.getFilenameFilter() == null) {
            // no filter, accept all.
            return true;
        }

        File filen = new File(fullname);
        return fd.getFilenameFilter().accept(new File(filen.getParent()),
                filen.getName());
    }

    @Override
    public void setVisible(boolean b) {
        XToolkit.awtLock();
        try {
            if (b) {
                Thread t = new Thread() {
                    public void run() {
108
                        showNativeDialog();
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
                        fd.setVisible(false);
                    }
                };
                t.start();
            } else {
                quit();
                fd.setVisible(false);
            }
        } finally {
            XToolkit.awtUnlock();
        }
    }

    @Override
    public void dispose() {
        quit();
        super.dispose();
    }

    @Override
    public void setDirectory(String dir) {
        // We do not implement this method because we
        // have delegated to FileDialog#setDirectory
    }

    @Override
    public void setFile(String file) {
        // We do not implement this method because we
        // have delegated to FileDialog#setFile
    }

    @Override
    public void setFilenameFilter(FilenameFilter filter) {
        // We do not implement this method because we
        // have delegated to FileDialog#setFilenameFilter
    }
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

    private void showNativeDialog() {
        String dirname = fd.getDirectory();
        // File path has a priority against directory path.
        String filename = fd.getFile();
        if (filename != null) {
            final File file = new File(filename);
            if (fd.getMode() == FileDialog.LOAD
                && dirname != null
                && file.getParent() == null) {
                // File path for gtk_file_chooser_set_filename.
                filename = dirname + (dirname.endsWith(File.separator) ? "" :
                                              File.separator) + filename;
            }
            if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) {
                // Filename for gtk_file_chooser_set_current_name.
                filename = file.getName();
                // Directory path for gtk_file_chooser_set_current_folder.
                dirname = file.getParent();
            }
        }
        GtkFileDialogPeer.this.run(fd.getTitle(), fd.getMode(), dirname,
                                   filename, fd.getFilenameFilter(),
                                   fd.isMultipleMode(), fd.getX(), fd.getY());
    }

171
}