You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2018/06/23 09:28:35 UTC

[GitHub] sdedic closed pull request #597: [NETBEANS-975] Wizard properly reports items created on configuration FS

sdedic closed pull request #597: [NETBEANS-975] Wizard properly reports items created on configuration FS
URL: https://github.com/apache/incubator-netbeans/pull/597
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/openide.loaders/src/org/openide/loaders/TemplateWizard2.java b/openide.loaders/src/org/openide/loaders/TemplateWizard2.java
index 1c000e754..ec0b17201 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.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 @@
     
     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 @@ private void fireStateChanged () {
     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 @@ public void setLocationFolder(File fd) {
         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 d27050136..739ff48fc 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 @@
 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.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 TemplateWizardTest (String name) {
     }
     
     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 void run() {
         });
     }
     
+    /**
+     * 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 ();


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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