changeset 2362:a471d1c4e9bf

Fix for RT-16111: FileChooser: Need to be able to specify initial file name in save dialog
author Lubomir Nerad <lubomir.nerad@oracle.com>
date Wed, 23 Jan 2013 10:23:26 +0100
parents ea96f2c95dba
children dfbafd7a337a 3ef9bd5a6a64
files javafx-ui-common/src/com/sun/javafx/tk/DummyToolkit.java javafx-ui-common/src/com/sun/javafx/tk/Toolkit.java javafx-ui-common/src/javafx/stage/FileChooser.java javafx-ui-common/test/unit/javafx/stage/CommonDialogsTest.java test-stub-toolkit/src/com/sun/javafx/pgstub/StubToolkit.java
diffstat 5 files changed, 272 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-common/src/com/sun/javafx/tk/DummyToolkit.java	Tue Jan 22 07:24:01 2013 -0800
+++ b/javafx-ui-common/src/com/sun/javafx/tk/DummyToolkit.java	Wed Jan 23 10:23:26 2013 +0100
@@ -487,6 +487,7 @@
     public List<File> showFileChooser(TKStage ownerWindow,
                                       String title,
                                       File initialDirectory,
+                                      String initialFileName,
                                       FileChooserType fileChooserType,
                                       List<ExtensionFilter> extensionFilters) {
         throw new UnsupportedOperationException("Not supported yet.");
--- a/javafx-ui-common/src/com/sun/javafx/tk/Toolkit.java	Tue Jan 22 07:24:01 2013 -0800
+++ b/javafx-ui-common/src/com/sun/javafx/tk/Toolkit.java	Wed Jan 23 10:23:26 2013 +0100
@@ -796,6 +796,7 @@
             TKStage ownerWindow,
             String title,
             File initialDirectory,
+            String initialFileName,
             FileChooserType fileChooserType,
             List<ExtensionFilter> extensionFilters);
 
--- a/javafx-ui-common/src/javafx/stage/FileChooser.java	Tue Jan 22 07:24:01 2013 -0800
+++ b/javafx-ui-common/src/javafx/stage/FileChooser.java	Wed Jan 23 10:23:26 2013 +0100
@@ -240,6 +240,37 @@
     }
 
     /**
+     * The initial file name for the displayed dialog.
+     * <p>
+     * This property is used mostly in the displayed file save dialogs as the
+     * initial file name for the file being saved. If set for a file open
+     * dialog it will have any impact on the displayed dialog only if the
+     * corresponding platform provides support for such property in its
+     * file open dialogs.
+     * </p>
+     *
+     * @since JavaFX 8.0
+     */
+    private ObjectProperty<String> initialFileName;
+
+    public final void setInitialFileName(final String value) {
+        initialFileNameProperty().set(value);
+    }
+
+    public final String getInitialFileName() {
+        return (initialFileName != null) ? initialFileName.get() : null;
+    }
+
+    public final ObjectProperty<String> initialFileNameProperty() {
+        if (initialFileName == null) {
+            initialFileName =
+                    new SimpleObjectProperty<String>(this, "initialFileName");
+        }
+
+        return initialFileName;
+    }
+
+    /**
      * Specifies the extension filters used in the displayed file dialog.
      */
     private ObservableList<ExtensionFilter> extensionFilters =
@@ -327,6 +358,7 @@
                 (ownerWindow != null) ? ownerWindow.impl_getPeer() : null,
                 getTitle(),
                 getInitialDirectory(),
+                getInitialFileName(),
                 fileChooserType,
                 extensionFilters);
         
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/javafx-ui-common/test/unit/javafx/stage/CommonDialogsTest.java	Wed Jan 23 10:23:26 2013 +0100
@@ -0,0 +1,207 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * 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
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+package javafx.stage;
+
+import com.sun.javafx.pgstub.StubToolkit;
+import com.sun.javafx.pgstub.StubToolkit.CommonDialogsSupport;
+import com.sun.javafx.tk.FileChooserType;
+import com.sun.javafx.tk.TKStage;
+import com.sun.javafx.tk.Toolkit;
+import java.io.File;
+import java.util.List;
+import javafx.stage.FileChooser.ExtensionFilter;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public final class CommonDialogsTest {
+    private StubToolkit toolkit;
+    private StubCommonDialogs stubDialogs;
+
+    @Before
+    public void setUp() {
+        toolkit = (StubToolkit) Toolkit.getToolkit();
+        stubDialogs = new StubCommonDialogs();
+
+        toolkit.setCommonDialogsSupport(stubDialogs);
+    }
+
+    @After
+    public void tearDown() {
+        toolkit.setCommonDialogsSupport(null);
+    }
+
+    @Test
+    public void testFileChooser_showOpenDialog() {
+        final FileChooser fileChooser = new FileChooser();
+        final ExtensionFilter txtFiles =
+                new ExtensionFilter("Text Files", "*.txt");
+        final ExtensionFilter jpgFiles =
+                new ExtensionFilter("JPEG Files", "*.jpg");
+        final File initialDirectory = new File(".");
+
+        fileChooser.setTitle("Open Single");
+        fileChooser.setInitialDirectory(initialDirectory);
+        fileChooser.setInitialFileName("open.txt");
+        fileChooser.getExtensionFilters().addAll(txtFiles, jpgFiles);
+
+        Assert.assertEquals(
+                null, fileChooser.showOpenDialog(null));
+        Assert.assertEquals(FileChooserType.OPEN,
+                            stubDialogs.getFileChooserType());
+        Assert.assertEquals(null, stubDialogs.getOwnerWindow());
+        Assert.assertEquals("Open Single", stubDialogs.getTitle());
+        Assert.assertEquals(initialDirectory,
+                            stubDialogs.getInitialDirectory());
+        Assert.assertEquals("open.txt", stubDialogs.getInitialFileName());
+        Assert.assertArrayEquals(
+                new Object[] { txtFiles, jpgFiles },
+                stubDialogs.getExtensionFilters().toArray());
+    }
+
+    @Test
+    public void testFileChooser_showOpenMultipleDialog() {
+        final FileChooser fileChooser = new FileChooser();
+        final ExtensionFilter allFiles =
+                new ExtensionFilter("All Files", "*.*");
+
+        fileChooser.setTitle("Open Multiple");
+        fileChooser.getExtensionFilters().addAll(allFiles);
+
+        Assert.assertEquals(
+                null, fileChooser.showOpenMultipleDialog(null));
+        Assert.assertEquals(FileChooserType.OPEN_MULTIPLE,
+                            stubDialogs.getFileChooserType());
+        Assert.assertEquals(null, stubDialogs.getOwnerWindow());
+        Assert.assertEquals("Open Multiple", stubDialogs.getTitle());
+        Assert.assertEquals(null, stubDialogs.getInitialDirectory());
+        Assert.assertEquals(null, stubDialogs.getInitialFileName());
+        Assert.assertArrayEquals(
+                new Object[] { allFiles },
+                stubDialogs.getExtensionFilters().toArray());
+    }
+
+    @Test
+    public void testFileChooser_showSaveDialog() {
+        final FileChooser fileChooser = new FileChooser();
+        final File initialDirectory = new File(".");
+
+        fileChooser.setTitle("Save");
+        fileChooser.setInitialDirectory(initialDirectory);
+        fileChooser.setInitialFileName("save.txt");
+
+        Assert.assertEquals(
+                null, fileChooser.showSaveDialog(null));
+        Assert.assertEquals(FileChooserType.SAVE,
+                            stubDialogs.getFileChooserType());
+        Assert.assertEquals(null, stubDialogs.getOwnerWindow());
+        Assert.assertEquals("Save", stubDialogs.getTitle());
+        Assert.assertEquals(initialDirectory,
+                            stubDialogs.getInitialDirectory());
+        Assert.assertEquals("save.txt", stubDialogs.getInitialFileName());
+        Assert.assertEquals(0, stubDialogs.getExtensionFilters().size());
+    }
+
+    @Test
+    public void testDirectoryChooser_showDialog() {
+        final DirectoryChooser directoryChooser = new DirectoryChooser();
+        final File initialDirectory = new File(".");
+
+        directoryChooser.setTitle("Open");
+        directoryChooser.setInitialDirectory(initialDirectory);
+
+        Assert.assertEquals(
+                null, directoryChooser.showDialog(null));
+        Assert.assertEquals(null, stubDialogs.getOwnerWindow());
+        Assert.assertEquals("Open", stubDialogs.getTitle());
+        Assert.assertEquals(initialDirectory,
+                            stubDialogs.getInitialDirectory());
+    }
+
+    private static final class StubCommonDialogs
+            implements CommonDialogsSupport {
+        private FileChooserType fileChooserType;
+        private TKStage ownerWindow;
+        private String title;
+        private File initialDirectory;
+        private String initialFileName;
+        private List<ExtensionFilter> extensionFilters;
+
+        public FileChooserType getFileChooserType() {
+            return fileChooserType;
+        }
+
+        public TKStage getOwnerWindow() {
+            return ownerWindow;
+        }
+
+        public String getTitle() {
+            return title;
+        }
+
+        public File getInitialDirectory() {
+            return initialDirectory;
+        }
+
+        public String getInitialFileName() {
+            return initialFileName;
+        }
+
+        public List<ExtensionFilter> getExtensionFilters() {
+            return extensionFilters;
+        }
+
+        @Override
+        public List<File> showFileChooser(
+                              final TKStage ownerWindow,
+                              final String title,
+                              final File initialDirectory,
+                              final String initialFileName,
+                              final FileChooserType fileChooserType,
+                              final List<ExtensionFilter> extensionFilters) {
+            this.ownerWindow = ownerWindow;
+            this.title = title;
+            this.initialDirectory = initialDirectory;
+            this.initialFileName = initialFileName;
+            this.fileChooserType = fileChooserType;
+            this.extensionFilters = extensionFilters;
+
+            return null;
+        }
+
+        @Override
+        public File showDirectoryChooser(final TKStage ownerWindow,
+                                         final String title,
+                                         final File initialDirectory) {
+            this.ownerWindow = ownerWindow;
+            this.title = title;
+            this.initialDirectory = initialDirectory;
+
+            return null;
+        }
+    }
+}
--- a/test-stub-toolkit/src/com/sun/javafx/pgstub/StubToolkit.java	Tue Jan 22 07:24:01 2013 -0800
+++ b/test-stub-toolkit/src/com/sun/javafx/pgstub/StubToolkit.java	Wed Jan 23 10:23:26 2013 +0100
@@ -827,13 +827,39 @@
         void enableDrop(TKDropTargetListener l);
     }
 
+    public interface CommonDialogsSupport {
+        List<File> showFileChooser(TKStage ownerWindow,
+                                   String title,
+                                   File initialDirectory,
+                                   String initialFileName,
+                                   FileChooserType fileChooserType,
+                                   List<ExtensionFilter> extensionFilters);
+
+        File showDirectoryChooser(TKStage ownerWindow,
+                                  String title,
+                                  File initialDirectory);
+    }
+
+    private CommonDialogsSupport commonDialogsSupport;
+    public void setCommonDialogsSupport(
+            final CommonDialogsSupport commonDialogsSupport) {
+        this.commonDialogsSupport = commonDialogsSupport;
+    }
+
     @Override
     public List<File> showFileChooser(TKStage ownerWindow,
                                       String title,
                                       File initialDirectory,
+                                      String initialFileName,
                                       FileChooserType fileChooserType,
                                       List<ExtensionFilter> extensionFilters) {
-        throw new UnsupportedOperationException("Not supported yet.");
+        return commonDialogsSupport.showFileChooser(
+                                        ownerWindow,
+                                        title,
+                                        initialDirectory,
+                                        initialFileName,
+                                        fileChooserType,
+                                        extensionFilters);
     }
 
 
@@ -841,7 +867,10 @@
     public File showDirectoryChooser(TKStage ownerWindow,
                                      String title,
                                      File initialDirectory) {
-        throw new UnsupportedOperationException("Not supported yet.");
+        return commonDialogsSupport.showDirectoryChooser(
+                                        ownerWindow,
+                                        title,
+                                        initialDirectory);
     }
 
     @Override