This is an automated email from the ASF dual-hosted git repository.

sdedic pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 8db91d3  [NETBEANS-975] Wizard properly reports items created on 
configuration FS (#597)
8db91d3 is described below

commit 8db91d34cdf1d629ec59492147f96701fff4ca63
Author: Svatopluk Dedic <[email protected]>
AuthorDate: Sat Jun 23 11:28:33 2018 +0200

    [NETBEANS-975] Wizard properly reports items created on configuration FS 
(#597)
---
 .../src/org/openide/loaders/TemplateWizard2.java   | 41 +++++++++++++++-
 .../org/openide/loaders/TemplateWizardTest.java    | 54 +++++++++++++++++++++-
 2 files changed, 92 insertions(+), 3 deletions(-)

diff --git a/openide.loaders/src/org/openide/loaders/TemplateWizard2.java 
b/openide.loaders/src/org/openide/loaders/TemplateWizard2.java
index 1c000e7..ec0b172 100644
--- a/openide.loaders/src/org/openide/loaders/TemplateWizard2.java
+++ b/openide.loaders/src/org/openide/loaders/TemplateWizard2.java
@@ -26,6 +26,7 @@ import java.beans.PropertyDescriptor;
 import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Method;
+import java.nio.file.Path;
 import javax.swing.*;
 import javax.swing.event.*;
 import org.openide.WizardDescriptor;
@@ -44,6 +45,7 @@ final class TemplateWizard2 extends javax.swing.JPanel 
implements DocumentListen
     
     private static final String PROP_LOCATION_FOLDER = "locationFolder"; // 
NOI18N
     private File locationFolder;
+    private DataFolder locationDataFolder;
     private DefaultPropertyModel locationFolderModel;
 
     /** File extension of the template and of the created file -
@@ -392,7 +394,39 @@ final class TemplateWizard2 extends javax.swing.JPanel 
implements DocumentListen
     private javax.swing.JTextField newObjectName;
     // End of variables declaration//GEN-END:variables
 
+    private DataFolder findRelativeFolder(File fd) {
+        if (locationDataFolder == null) {
+            return null;
+        }
+        File f = FileUtil.toFile(locationDataFolder.getPrimaryFile());
+        if (f == null) {
+            return null;
+        } else if (f.equals(fd)) {
+            return locationDataFolder;
+        }
+        // attempt to locate neighbouring file on the DF's filesystem
+        FileObject folderFO = locationDataFolder.getPrimaryFile();
+        File lf = FileUtil.toFile(folderFO);
+        if (lf != null) {
+            try {
+                Path p = lf.toPath().relativize(fd.toPath());
+                FileObject newDF = folderFO.getFileObject(p.toString());
+                if (newDF != null) {
+                    return DataFolder.findFolder(newDF);
+                }
+            } catch (IllegalArgumentException ex) {
+                // cannot relativize
+            }
+        }
+        return null;
+    }
+    
     public void setLocationFolder(File fd) {
+        locationDataFolder = findRelativeFolder(fd);
+        setLocationFolder0(fd);
+    }
+    
+    private void setLocationFolder0(File fd) {
         if (locationFolder == fd)
             return ;
         if (locationFolder != null && locationFolder.equals (fd))
@@ -405,14 +439,19 @@ final class TemplateWizard2 extends javax.swing.JPanel 
implements DocumentListen
         firePropertyChange (PROP_LOCATION_FOLDER, oldLocation, locationFolder);
         fireStateChanged ();
     }
+    
     private void setLocationDataFolder(DataFolder fd) {
-        setLocationFolder(fd != null ? FileUtil.toFile(fd.getPrimaryFile()) : 
null);
+        locationDataFolder = fd;
+        setLocationFolder0(fd != null ? FileUtil.toFile(fd.getPrimaryFile()) : 
null);
     }
     
     public File getLocationFolder() {
         return locationFolder;
     }
     private DataFolder getLocationDataFolder() {
+        if (locationDataFolder != null) {
+            return locationDataFolder;
+        }
         if (locationFolder != null) {
             FileObject f = 
FileUtil.toFileObject(FileUtil.normalizeFile(locationFolder));
             if (f != null && f.isFolder()) {
diff --git 
a/openide.loaders/test/unit/src/org/openide/loaders/TemplateWizardTest.java 
b/openide.loaders/test/unit/src/org/openide/loaders/TemplateWizardTest.java
index d270501..739ff48 100644
--- a/openide.loaders/test/unit/src/org/openide/loaders/TemplateWizardTest.java
+++ b/openide.loaders/test/unit/src/org/openide/loaders/TemplateWizardTest.java
@@ -22,6 +22,7 @@ package org.openide.loaders;
 import java.awt.Component;
 import java.awt.Dialog;
 import java.awt.EventQueue;
+import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -29,6 +30,7 @@ import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.Set;
 import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
 import javax.swing.event.ChangeListener;
 import org.netbeans.junit.MockServices;
 import org.netbeans.junit.NbTestCase;
@@ -51,8 +53,17 @@ public class TemplateWizardTest extends NbTestCase {
     }
     
     protected void setUp() throws Exception {
-         FileObject fo = FileUtil.getConfigRoot ();
-         FileUtil.createFolder (fo, "Templates");
+        // set up user directory
+        clearWorkDir();
+        File wd = new File(getWorkDir(), "config");
+        wd.mkdirs();
+        System.setProperty("netbeans.user", wd.toString());
+        FileObject fo = FileUtil.getConfigRoot ();
+        FileUtil.createFolder (fo, "Templates");
+    }
+    
+    protected void tearDown() {
+        System.getProperties().remove("netbeans.user");
     }
 
     /** Does getIterator honours DataObject's cookies?
@@ -199,6 +210,45 @@ public class TemplateWizardTest extends NbTestCase {
         });
     }
     
+    /**
+     * When creating file on Config FS, the resulting object must be created
+     * on the config FS and not on the underlying OS filesystem visible through
+     * FileSystem APIs.
+     */
+    public void testWizardConfigLocation() throws Exception {
+        final TemplateWizard tw = new TemplateWizard();
+        FileObject cf = FileUtil.getConfigRoot().createFolder("target"); // 
NOI18N
+        DataFolder target = DataFolder.findFolder(cf);
+        tw.setTargetFolder(target);
+        
+        // folder does not have a default action -> instantiate does not open 
an editor
+        FileObject f = 
FileUtil.getConfigFile("Templates").createFolder("bubu"); // NOI18N
+        tw.setTemplate(DataObject.find(f));
+
+        tw.initialize();
+        final TemplateWizardIterImpl iter = tw.getIterImpl ();
+        iter.first();
+        // initialize the target
+        iter.nextPanel();
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                // this is what is called by UI on Finish
+                tw.updateState();
+                iter.getIterator().current().storeSettings(tw);
+            }
+        });
+        tw.setValue(TemplateWizard.FINISH_OPTION);
+        Set<DataObject> result = iter.instantiate();
+        assertNotNull(result);
+        assertFalse(result.isEmpty());
+        
+        FileObject tf = result.iterator().next().getPrimaryFile();
+        
+        // check that the object is really created on config fs
+        assertTrue(FileUtil.isParentOf(cf, tf));
+    }
+    
     private void doNextOnIterImpl (boolean notify) {
         TemplateWizard wizard = new TemplateWizard ();
         wizard.initialize ();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to