Fix user scope component libraries so that each one has a Library java_tasklist_89600_t2b_i
authoredwingo@netbeans.org
Mon, 16 Apr 2007 04:26:41 +0000
changeset 58592b46c0cc0e2
parent 584 2d82e77b4cda
child 586 da6a7493d189
Fix user scope component libraries so that each one has a Library
Def. Each project that contains an embedded component library will
have a corresponding user scope component library installed. It will
also have a separate Library Ref that points to the user scope Library
Def. TODO: make shared component libraries work in the same way.
visualweb.complib/src/org/netbeans/modules/visualweb/complib/ComplibServiceProvider.java
     1.1 --- a/visualweb.complib/src/org/netbeans/modules/visualweb/complib/ComplibServiceProvider.java	Mon Apr 16 02:36:17 2007 +0000
     1.2 +++ b/visualweb.complib/src/org/netbeans/modules/visualweb/complib/ComplibServiceProvider.java	Mon Apr 16 04:26:41 2007 +0000
     1.3 @@ -576,7 +576,7 @@
     1.4                          ExtensionComplib userComplib = userScope
     1.5                                  .getExistingComplib(projectComplib);
     1.6                          if (userComplib != null) {
     1.7 -                            addLibraryDefsAndRefs(project, userComplib);
     1.8 +                            addLibraryRef(project, userComplib);
     1.9                          }
    1.10                      }
    1.11                  } catch (IOException e) {
    1.12 @@ -661,55 +661,37 @@
    1.13      }
    1.14  
    1.15      /**
    1.16 -     * Add any needed library definitions and references for a project scoped
    1.17 -     * complib.
    1.18 +     * Add a Library Ref to a user scoped complib for a project.
    1.19       * 
    1.20       * @param project
    1.21 -     * @param complib
    1.22 +     * @param userComplib
    1.23       * @throws IOException
    1.24       */
    1.25 -    private void addLibraryDefsAndRefs(Project project, ExtensionComplib complib)
    1.26 +    private void addLibraryRef(Project project, ExtensionComplib userComplib)
    1.27              throws IOException {
    1.28 -        String localizingBundle = LibraryLocalizationBundle.class.getName();
    1.29 -
    1.30 -        // Derive unique name and description for global NB Library Defs.
    1.31 -        String libName = complib.getDirectoryBaseName();
    1.32 -        String description = complib.getVersionedTitle();
    1.33 -
    1.34 +        String libName = deriveUniqueLibraryName(userComplib);
    1.35          Library libDef = LibraryManager.getDefault().getLibrary(libName);
    1.36          if (libDef == null) {
    1.37 -            /*
    1.38 -             * If we don't find a lib def create one, else assume the lib def is
    1.39 -             * correct. If it is not the user can manually remove it and it will
    1.40 -             * be recreated when the project is re-opened.
    1.41 -             */
    1.42 -
    1.43 -            // Use the name of the library as a key for the description
    1.44 -            LibraryLocalizationBundle.add(libName, description);
    1.45 -
    1.46 -            List<URL> rtPath = fileListToUrlList(complib.getRuntimePath());
    1.47 -            List<URL> dtPath = fileListToUrlList(complib.getDesignTimePath());
    1.48 -            List<URL> javadocPath = fileListToUrlList(complib.getJavadocPath());
    1.49 -            List<URL> sourcePath = fileListToUrlList(complib.getSourcePath());
    1.50 -            libDef = JsfProjectUtils.createComponentLibrary(libName, libName,
    1.51 -                    localizingBundle, rtPath, sourcePath, javadocPath, dtPath);
    1.52 +            // assert false;
    1.53 +            IdeUtil
    1.54 +                    .logError("Cannot add Library Ref, unable to find Library Definition: "
    1.55 +                            + libName);
    1.56 +            return;
    1.57          }
    1.58  
    1.59 -        // If needed, create new compile-time and deploy Library Ref
    1.60 +        // If needed, create new Library Ref
    1.61          if (!JsfProjectUtils.hasLibraryReference(project, libDef)) {
    1.62              if (!JsfProjectUtils.addLibraryReferences(project,
    1.63                      new Library[] { libDef })) {
    1.64 -                IdeUtil
    1.65 -                        .logError("Failed to add compile-time library reference to project: "
    1.66 -                                + libDef.getName());
    1.67 +                IdeUtil.logError("Failed to add library reference to project: "
    1.68 +                        + libDef.getName());
    1.69              }
    1.70          }
    1.71      }
    1.72  
    1.73      /**
    1.74       * Remove an existing NB Library Ref corresponding to a project embedded
    1.75 -     * complib and possibly also its corresponding global Library Def if it is
    1.76 -     * no longer used by any other open project.
    1.77 +     * complib.
    1.78       * 
    1.79       * @param project
    1.80       * @param prjCompLib
    1.81 @@ -717,40 +699,33 @@
    1.82       */
    1.83      private void removeLibraryDefsAndRefs(Project project,
    1.84          ExtensionComplib prjCompLib) throws IOException {
    1.85 -        String libName = deriveUniqueLibraryName(project, prjCompLib);
    1.86 +        ExtensionComplib userComplib = userScope.getExistingComplib(prjCompLib);
    1.87 +        if (userComplib == null) {
    1.88 +            // assert false;
    1.89 +            IdeUtil
    1.90 +                    .logWarning("Unable to find installed component library in userdir: "
    1.91 +                            + prjCompLib.getVersionedTitle());
    1.92 +            return;
    1.93 +        }
    1.94 +        String libName = deriveUniqueLibraryName(userComplib);
    1.95          Library libDef = LibraryManager.getDefault().getLibrary(libName);
    1.96          if (libDef != null) {
    1.97 -            // Existing definition so first remove any existing reference
    1.98              if (JsfProjectUtils.hasLibraryReference(project, libDef)) {
    1.99                  JsfProjectUtils.removeLibraryReferences(project,
   1.100                          new Library[] { libDef });
   1.101              }
   1.102 -
   1.103 -            /**
   1.104 -             * Check to see if the lib def is used by any other project and
   1.105 -             * remove it if it is not
   1.106 -             */
   1.107 -            boolean inUse = false;
   1.108 -            Project[] projectsArray = OpenProjects.getDefault()
   1.109 -                    .getOpenProjects();
   1.110 -            for (Project iProject : projectsArray) {
   1.111 -                if (JsfProjectUtils.hasLibraryReference(iProject, libDef)) {
   1.112 -                    inUse = true;
   1.113 -                    break;
   1.114 -                }
   1.115 -            }
   1.116 -
   1.117 -            if (!inUse) {
   1.118 -                JsfProjectUtils.removeLibrary(libName);
   1.119 -
   1.120 -                // Cleanup bundle
   1.121 -                LibraryLocalizationBundle.remove(libName);
   1.122 -            }
   1.123          }
   1.124      }
   1.125  
   1.126 -    private String deriveUniqueLibraryName(Project project,
   1.127 -        ExtensionComplib complib) {
   1.128 +    /**
   1.129 +     * Derive unique name for global NB Library Defs.
   1.130 +     * 
   1.131 +     * @param complib
   1.132 +     *            User scope complib
   1.133 +     * @return
   1.134 +     */
   1.135 +    private String deriveUniqueLibraryName(ExtensionComplib complib) {
   1.136 +        // assert: complib is in user scope
   1.137          return complib.getDirectoryBaseName();
   1.138      }
   1.139  
   1.140 @@ -873,27 +848,10 @@
   1.141       */
   1.142      public ExtensionComplib installComplibPackage(ComplibPackage pkg)
   1.143              throws ComplibException, IOException {
   1.144 -        // Install pkg to user scope
   1.145 -        Scope scope = userScope;
   1.146          Identifier identifer = pkg.getIdentifer();
   1.147 -
   1.148          removeExistingInstalledComplib(identifer);
   1.149 -
   1.150 -        /*
   1.151 -         * Temporarily install the complib into the system so that UI Item-s can
   1.152 -         * be created and then rollback if a problem occurs
   1.153 -         */
   1.154 -        ExtensionComplib complib = scope.installComplibPackage(pkg);
   1.155 -        try {
   1.156 -            addToPalette(complib);
   1.157 -        } catch (ComplibException e1) {
   1.158 -            // Rollback
   1.159 -            remove(complib);
   1.160 -            throw e1;
   1.161 -        }
   1.162 -        JavaHelpStorage.installComplibHelp(complib);
   1.163 -
   1.164 -        fireAddableComplibsChanged(scope);
   1.165 +        ExtensionComplib complib = userScope.installComplibPackage(pkg);
   1.166 +        finishInstallToUserScope(complib);
   1.167          return complib;
   1.168      }
   1.169  
   1.170 @@ -909,20 +867,54 @@
   1.171       */
   1.172      private void installProjectComplib(ExtensionComplib projectComplib)
   1.173              throws ComplibException, IOException {
   1.174 -        // Install to user scope
   1.175 -        Scope scope = userScope;
   1.176          Identifier identifer = projectComplib.getIdentifier();
   1.177 +        removeExistingInstalledComplib(identifer);
   1.178 +        ExtensionComplib newComplib = userScope.installComplib(projectComplib);
   1.179 +        finishInstallToUserScope(newComplib);
   1.180 +    }
   1.181  
   1.182 -        removeExistingInstalledComplib(identifer);
   1.183 -
   1.184 +    private void finishInstallToUserScope(ExtensionComplib complib)
   1.185 +            throws ComplibException, IOException, MalformedURLException {
   1.186          /*
   1.187           * Temporarily install the complib into the system so that UI Item-s can
   1.188           * be created and then rollback if a problem occurs
   1.189           */
   1.190 -        ExtensionComplib newComplib = scope.installComplib(projectComplib);
   1.191 -        addToPalette(newComplib);
   1.192 -        JavaHelpStorage.installComplibHelp(newComplib);
   1.193 -        fireAddableComplibsChanged(scope);
   1.194 +        try {
   1.195 +            addToPalette(complib);
   1.196 +        } catch (ComplibException e1) {
   1.197 +            // Rollback
   1.198 +            remove(complib);
   1.199 +            throw e1;
   1.200 +        }
   1.201 +        JavaHelpStorage.installComplibHelp(complib);
   1.202 +
   1.203 +        // Create Lib Def
   1.204 +        createUserScopeLibDef(complib);
   1.205 +
   1.206 +        fireAddableComplibsChanged(userScope);
   1.207 +    }
   1.208 +
   1.209 +    private Library createUserScopeLibDef(ExtensionComplib complib)
   1.210 +            throws IOException, MalformedURLException {
   1.211 +        String libName = deriveUniqueLibraryName(complib);
   1.212 +
   1.213 +        Library libDef = LibraryManager.getDefault().getLibrary(libName);
   1.214 +        if (libDef != null) {
   1.215 +            // assert false;
   1.216 +            JsfProjectUtils.removeLibrary(libName);
   1.217 +        }
   1.218 +
   1.219 +        // Use unique libName as a key for the description.
   1.220 +        String description = complib.getVersionedTitle();
   1.221 +        LibraryLocalizationBundle.add(libName, description);
   1.222 +
   1.223 +        String localizingBundle = LibraryLocalizationBundle.class.getName();
   1.224 +        List<URL> rtPath = fileListToUrlList(complib.getRuntimePath());
   1.225 +        List<URL> dtPath = fileListToUrlList(complib.getDesignTimePath());
   1.226 +        List<URL> javadocPath = fileListToUrlList(complib.getJavadocPath());
   1.227 +        List<URL> sourcePath = fileListToUrlList(complib.getSourcePath());
   1.228 +        return JsfProjectUtils.createComponentLibrary(libName, libName,
   1.229 +                localizingBundle, rtPath, sourcePath, javadocPath, dtPath);
   1.230      }
   1.231  
   1.232      /**
   1.233 @@ -1031,6 +1023,19 @@
   1.234  
   1.235          // Remove from user scope
   1.236          userScope.remove(complib);
   1.237 +
   1.238 +        // Remove the corresponding Library Definition
   1.239 +        String libName = deriveUniqueLibraryName(complib);
   1.240 +        Library libDef = LibraryManager.getDefault().getLibrary(libName);
   1.241 +        if (libDef != null) {
   1.242 +            try {
   1.243 +                JsfProjectUtils.removeLibrary(libName);
   1.244 +            } catch (IOException e) {
   1.245 +                IdeUtil.logWarning("Unable to remove Library Definition: "
   1.246 +                        + libName, e);
   1.247 +            }
   1.248 +        }
   1.249 +
   1.250          fireAddableComplibsChanged(userScope);
   1.251      }
   1.252  
   1.253 @@ -1240,7 +1245,7 @@
   1.254          ExtensionComplib projectComplib = scope
   1.255                  .installComplib(userDirExtComplib);
   1.256          fireAddableComplibsChanged(scope);
   1.257 -        addLibraryDefsAndRefs(project, userDirExtComplib);
   1.258 +        addLibraryRef(project, userDirExtComplib);
   1.259          installProjectResources(userDirExtComplib, project);
   1.260          return projectComplib;
   1.261      }