1 /*
2 * Copyright (c) 2010, 2013, 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 package sun.awt.X11;
26
27 import java.awt.FileDialog;
28 import java.awt.peer.FileDialogPeer;
29 import java.io.File;
30 import java.io.FilenameFilter;
31 import sun.awt.AWTAccessor;
32
33 /**
34 * FileDialogPeer for the GtkFileChooser.
35 *
36 * @author Costantino Cerbo (c.cerbo@gmail.com)
37 */
38 final class GtkFileDialogPeer extends XDialogPeer implements FileDialogPeer {
39
40 private final FileDialog fd;
41
42 // A pointer to the native GTK FileChooser widget
43 private volatile long widget = 0L;
44
45 GtkFileDialogPeer(FileDialog fd) {
46 super(fd);
47 this.fd = fd;
48 }
49
50 private static native void initIDs();
51 static {
52 initIDs();
53 }
54
55 private native void run(String title, int mode, String dir, String file,
56 FilenameFilter filter, boolean isMultipleMode, int x, int y);
57 private native void quit();
58
59 @Override
60 public native void toFront();
61
62 @Override
63 public native void setBounds(int x, int y, int width, int height, int op);
64
65 /**
66 * Called exclusively by the native C code.
67 */
68 private void setFileInternal(String directory, String[] filenames) {
69 AWTAccessor.FileDialogAccessor accessor = AWTAccessor
70 .getFileDialogAccessor();
71
72 if (filenames == null) {
73 accessor.setDirectory(fd, null);
74 accessor.setFile(fd, null);
75 accessor.setFiles(fd, null);
76 } else {
78 String with_separator = directory;
79 if (directory != null) {
80 with_separator = directory.endsWith(File.separator) ?
81 directory : (directory + File.separator);
82 }
83 accessor.setDirectory(fd, with_separator);
84 accessor.setFile(fd, filenames[0]);
85
86 int filesNumber = (filenames != null) ? filenames.length : 0;
87 File[] files = new File[filesNumber];
88 for (int i = 0; i < filesNumber; i++) {
89 files[i] = new File(directory, filenames[i]);
90 }
91 accessor.setFiles(fd, files);
92 }
93 }
94
95 /**
96 * Called exclusively by the native C code.
97 */
98 private boolean filenameFilterCallback(String fullname) {
99 if (fd.getFilenameFilter() == null) {
100 // no filter, accept all.
101 return true;
102 }
103
104 File filen = new File(fullname);
105 return fd.getFilenameFilter().accept(new File(filen.getParent()),
106 filen.getName());
107 }
108
109 @Override
110 public void setVisible(boolean b) {
111 XToolkit.awtLock();
112 try {
113 if (b) {
114 Thread t = new Thread() {
115 public void run() {
116 showNativeDialog();
117 fd.setVisible(false);
135
136 @Override
137 public void setDirectory(String dir) {
138 // We do not implement this method because we
139 // have delegated to FileDialog#setDirectory
140 }
141
142 @Override
143 public void setFile(String file) {
144 // We do not implement this method because we
145 // have delegated to FileDialog#setFile
146 }
147
148 @Override
149 public void setFilenameFilter(FilenameFilter filter) {
150 // We do not implement this method because we
151 // have delegated to FileDialog#setFilenameFilter
152 }
153
154 private void showNativeDialog() {
155 String dirname = fd.getDirectory();
156 // File path has a priority against directory path.
157 String filename = fd.getFile();
158 if (filename != null) {
159 final File file = new File(filename);
160 if (fd.getMode() == FileDialog.LOAD
161 && dirname != null
162 && file.getParent() == null) {
163 // File path for gtk_file_chooser_set_filename.
164 filename = dirname + (dirname.endsWith(File.separator) ? "" :
165 File.separator) + filename;
166 }
167 if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) {
168 // Filename for gtk_file_chooser_set_current_name.
169 filename = file.getName();
170 // Directory path for gtk_file_chooser_set_current_folder.
171 dirname = file.getParent();
172 }
173 }
174 run(fd.getTitle(), fd.getMode(), dirname, filename,
175 fd.getFilenameFilter(), fd.isMultipleMode(), fd.getX(), fd.getY());
176 }
177 }
|
1 /*
2 * Copyright (c) 2010, 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 package sun.awt.X11;
26
27 import java.awt.*;
28 import java.awt.event.ComponentEvent;
29 import java.awt.peer.FileDialogPeer;
30 import java.io.File;
31 import java.io.FilenameFilter;
32 import sun.awt.AWTAccessor;
33
34 /**
35 * FileDialogPeer for the GtkFileChooser.
36 *
37 * @author Costantino Cerbo (c.cerbo@gmail.com)
38 */
39 final class GtkFileDialogPeer extends XDialogPeer implements FileDialogPeer {
40
41 private final FileDialog fd;
42
43 // A pointer to the native GTK FileChooser widget
44 private volatile long widget = 0L;
45
46 GtkFileDialogPeer(FileDialog fd) {
47 super(fd);
48 this.fd = fd;
49 }
50
51 private static native void initIDs();
52 static {
53 initIDs();
54 }
55
56 private native void run(long ownerWindow, 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 @Override
62 public native void toFront();
63
64 @Override
65 public native void setBounds(int x, int y, int width, int height, int op);
66
67 /**
68 * Called exclusively by the native C code.
69 */
70 private void setFileInternal(String directory, String[] filenames) {
71 AWTAccessor.FileDialogAccessor accessor = AWTAccessor
72 .getFileDialogAccessor();
73
74 if (filenames == null) {
75 accessor.setDirectory(fd, null);
76 accessor.setFile(fd, null);
77 accessor.setFiles(fd, null);
78 } else {
80 String with_separator = directory;
81 if (directory != null) {
82 with_separator = directory.endsWith(File.separator) ?
83 directory : (directory + File.separator);
84 }
85 accessor.setDirectory(fd, with_separator);
86 accessor.setFile(fd, filenames[0]);
87
88 int filesNumber = (filenames != null) ? filenames.length : 0;
89 File[] files = new File[filesNumber];
90 for (int i = 0; i < filesNumber; i++) {
91 files[i] = new File(directory, filenames[i]);
92 }
93 accessor.setFiles(fd, files);
94 }
95 }
96
97 /**
98 * Called exclusively by the native C code.
99 */
100 private void notifyConfigure(long window, int x, int y, int width, int height) {
101 XToolkit.awtLock();
102 try {
103 Insets insets = XWM.getInsetsFromExtents(window);
104 if (insets == null) {
105 insets = new Insets(0,0,0,0);
106 }
107
108 int resX = x - insets.left;
109 int resY = y - insets.top;
110 int resWidth = width + insets.left + insets.right;
111 int resHeight = height + insets.top + insets.bottom;
112
113 Rectangle bounds = target.getBounds();
114
115 if (bounds.x != resX || bounds.y != resY) {
116 AWTAccessor.getComponentAccessor().setLocation(target, resX, resY);
117 postEvent(new ComponentEvent(target, ComponentEvent.COMPONENT_MOVED));
118 }
119
120 if (bounds.width != resWidth || bounds.height != resHeight) {
121 AWTAccessor.getComponentAccessor().setSize(target, resWidth, resHeight);
122 postEvent(new ComponentEvent(target, ComponentEvent.COMPONENT_RESIZED));
123 }
124 } finally {
125 XToolkit.awtUnlock();
126 }
127 }
128
129 /**
130 * Called exclusively by the native C code.
131 */
132 private boolean filenameFilterCallback(String fullname) {
133 if (fd.getFilenameFilter() == null) {
134 // no filter, accept all.
135 return true;
136 }
137
138 File filen = new File(fullname);
139 return fd.getFilenameFilter().accept(new File(filen.getParent()),
140 filen.getName());
141 }
142
143 @Override
144 public void setVisible(boolean b) {
145 XToolkit.awtLock();
146 try {
147 if (b) {
148 Thread t = new Thread() {
149 public void run() {
150 showNativeDialog();
151 fd.setVisible(false);
169
170 @Override
171 public void setDirectory(String dir) {
172 // We do not implement this method because we
173 // have delegated to FileDialog#setDirectory
174 }
175
176 @Override
177 public void setFile(String file) {
178 // We do not implement this method because we
179 // have delegated to FileDialog#setFile
180 }
181
182 @Override
183 public void setFilenameFilter(FilenameFilter filter) {
184 // We do not implement this method because we
185 // have delegated to FileDialog#setFilenameFilter
186 }
187
188 private void showNativeDialog() {
189 long ownerWindow = 0L;
190
191 XWindowPeer ownerPeer = getOwnerPeer();
192 if (ownerPeer != null) {
193 ownerWindow = ownerPeer.getWindow();
194 }
195
196 String dirname = fd.getDirectory();
197 // File path has a priority against directory path.
198 String filename = fd.getFile();
199 if (filename != null) {
200 final File file = new File(filename);
201 if (fd.getMode() == FileDialog.LOAD
202 && dirname != null
203 && file.getParent() == null) {
204 // File path for gtk_file_chooser_set_filename.
205 filename = dirname + (dirname.endsWith(File.separator) ? "" :
206 File.separator) + filename;
207 }
208 if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) {
209 // Filename for gtk_file_chooser_set_current_name.
210 filename = file.getName();
211 // Directory path for gtk_file_chooser_set_current_folder.
212 dirname = file.getParent();
213 }
214 }
215
216 run(ownerWindow, fd.getTitle(), fd.getMode(), dirname, filename,
217 fd.getFilenameFilter(), fd.isMultipleMode(), fd.getX(), fd.getY());
218 }
219 }
|