[hg] main-silver: #200833 package modification implementation fo...

  • From: Milos Kleint < >
  • To: , ,
  • Subject: [hg] main-silver: #200833 package modification implementation fo...
  • Date: Tue, 07 May 2013 18:45:39 -0700

changeset 53b7b150f138 in main-silver ((none))
details: http://hg.netbeans.org/main-silver/rev/53b7b150f138
description:
        #200833 package modification implementation for maven nbm projects, 
simplified api, base export action enablement/title on AccessibilityQuery 
result.

diffstat:

 apisupport.ant/manifest.mf                                                   
                                       |    2 +-
 apisupport.ant/nbproject/project.xml                                         
                                       |    2 +-
 
apisupport.ant/src/org/netbeans/modules/apisupport/project/ProjectXMLManager.java
                                   |   16 +-
 
apisupport.ant/src/org/netbeans/modules/apisupport/project/ui/customizer/NbModulePackageModifierImplementation.java
 |   65 +-
 java.api.common/apichanges.xml                                               
                                       |   13 +-
 java.api.common/manifest.mf                                                  
                                       |    2 +-
 java.api.common/nbproject/project.xml                                        
                                       |    1 +
 
java.api.common/src/org/netbeans/modules/java/api/common/ant/PackageModifierImplementation.java
                     |   16 +-
 
java.api.common/src/org/netbeans/modules/java/api/common/impl/ExportPackageAction.java
                              |   32 +-
 maven.apisupport/manifest.mf                                                 
                                       |    2 +-
 maven.apisupport/nbproject/project.xml                                       
                                       |   25 +
 
maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenPackageModifierImpl.java
                            |  284 ++++++++++
 12 files changed, 382 insertions(+), 78 deletions(-)

diffs (658 lines):

diff --git a/apisupport.ant/manifest.mf b/apisupport.ant/manifest.mf
--- a/apisupport.ant/manifest.mf
+++ b/apisupport.ant/manifest.mf
@@ -1,6 +1,6 @@
 Manifest-Version: 1.0
 OpenIDE-Module: org.netbeans.modules.apisupport.ant
 OpenIDE-Module-Localizing-Bundle: 
org/netbeans/modules/apisupport/project/Bundle.properties
-OpenIDE-Module-Specification-Version: 2.58
+OpenIDE-Module-Specification-Version: 2.59
 AutoUpdate-Show-In-Client: false
 OpenIDE-Module-Layer: 
org/netbeans/modules/apisupport/project/resources/layer.xml
diff --git a/apisupport.ant/nbproject/project.xml 
b/apisupport.ant/nbproject/project.xml
--- a/apisupport.ant/nbproject/project.xml
+++ b/apisupport.ant/nbproject/project.xml
@@ -151,7 +151,7 @@
                     <compile-dependency/>
                     <run-dependency>
                         <release-version>0</release-version>
-                        <specification-version>1.31</specification-version>
+                        <specification-version>1.49</specification-version>
                     </run-dependency>
                 </dependency>
                 <dependency>
diff --git 
a/apisupport.ant/src/org/netbeans/modules/apisupport/project/ProjectXMLManager.java
 
b/apisupport.ant/src/org/netbeans/modules/apisupport/project/ProjectXMLManager.java
--- 
a/apisupport.ant/src/org/netbeans/modules/apisupport/project/ProjectXMLManager.java
+++ 
b/apisupport.ant/src/org/netbeans/modules/apisupport/project/ProjectXMLManager.java
@@ -749,7 +749,7 @@
     /**
      * Replaces all original public packages with the given
      * <code>newPackages</code>. Also removes friend packages if there are 
any
-     * since those two mutually exclusive.
+     * since those two mutually exclusive. packages ending with .* will be 
translated to <subpackages> element
      */
     public void replacePublicPackages(Set<String> newPackages) {
         removePublicAndFriends();
@@ -760,9 +760,14 @@
         insertPublicOrFriend(publicPackagesEl);
 
             for (String pkg : newPackages) {
+            if (pkg.endsWith(".*")) {
+                publicPackagesEl.appendChild(
+                    createModuleElement(doc, ProjectXMLManager.SUBPACKAGES, 
pkg.substring(0, pkg.length() - ".*".length())));
+            } else {
             publicPackagesEl.appendChild(
                     createModuleElement(doc, ProjectXMLManager.PACKAGE, 
pkg));
         }
+        }
         project.putPrimaryConfigurationData(_confData);
         publicPackages = null; // XXX cleaner would be to listen on changes 
in helper
     }
@@ -776,7 +781,7 @@
      * Replaces all original friends with the given <code>friends</code> with
      * <code>packagesToExpose</code> as exposed packages to those friends. 
Also
      * removes public packages if there are any since those two are mutually
-     * exclusive.
+     * exclusive. packages ending with .* will be translated to 
<subpackages> element
      */
     public void replaceFriends(Set<String> friends, Set<String> 
packagesToExpose) {
         removePublicAndFriends();
@@ -786,8 +791,13 @@
         insertPublicOrFriend(friendPackages);
 
         for (String friend : friends) {
+            if (friend.endsWith(".*")) {
             friendPackages.appendChild(
-                    createModuleElement(doc, ProjectXMLManager.FRIEND, 
friend));
+                    createModuleElement(doc, ProjectXMLManager.SUBPACKAGES, 
friend.substring(0, friend.length() - ".*".length())));
+            } else {
+                friendPackages.appendChild(
+                    createModuleElement(doc, ProjectXMLManager.PACKAGE, 
friend));
+            }
         }
         for (String pkg : packagesToExpose) {
             friendPackages.appendChild(
diff --git 
a/apisupport.ant/src/org/netbeans/modules/apisupport/project/ui/customizer/NbModulePackageModifierImplementation.java
 
b/apisupport.ant/src/org/netbeans/modules/apisupport/project/ui/customizer/NbModulePackageModifierImplementation.java
--- 
a/apisupport.ant/src/org/netbeans/modules/apisupport/project/ui/customizer/NbModulePackageModifierImplementation.java
+++ 
b/apisupport.ant/src/org/netbeans/modules/apisupport/project/ui/customizer/NbModulePackageModifierImplementation.java
@@ -42,13 +42,16 @@
 package org.netbeans.modules.apisupport.project.ui.customizer;
 
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.Collection;
+import java.util.Set;
+import java.util.TreeSet;
 import org.netbeans.api.project.ProjectManager;
-import org.netbeans.modules.apisupport.project.ApisupportAntUtils;
 import org.netbeans.modules.apisupport.project.NbModuleProject;
+import org.netbeans.modules.apisupport.project.ProjectXMLManager;
+import org.netbeans.modules.apisupport.project.api.ManifestManager;
 import 
org.netbeans.modules.java.api.common.ant.PackageModifierImplementation;
 import org.openide.ErrorManager;
-import org.openide.filesystems.FileUtil;
 import org.openide.util.Mutex;
 import org.openide.util.MutexException;
 
@@ -71,40 +74,34 @@
     }
     
     @Override
-    public Collection<String> getAllPackages() {
-        if(this.project != null) {
-            return 
ApisupportAntUtils.scanProjectForPackageNames(FileUtil.toFile(this.project.getProjectDirectory()),
 false);
-        }
-        return null;
-    }
-    
-    @Override
-    public Collection<String> getPublicPackages() {
-        if(this.project != null) {
-            final SingleModuleProperties properties = 
SingleModuleProperties.getInstance(this.project);
-            return properties.getPublicPackagesModel().getSelectedPackages();
-        }
-        return null;
-    }
-
-    @Override
-    public void exportPackageAction(Collection<String> packagesToExport, 
boolean export) {
-        final SingleModuleProperties properties = 
SingleModuleProperties.getInstance(this.project);
-        CustomizerComponentFactory.PublicPackagesTableModel tableModel = 
properties.getPublicPackagesModel();
-            for(String packageIter:packagesToExport) {
-                for(int i = 0; i < tableModel.getRowCount(); i++) {
-                    if(tableModel.getValueAt(i, 1).equals(packageIter)) {
-                        tableModel.setValueAt(export, i, 0);
-                        break;
-                    }
-                }
-            }
-            final NbModuleProject nbmProject = this.project;
+    public void exportPackageAction(final Collection<String> 
packagesToExport, final boolean export) {
             try {
             ProjectManager.mutex().writeAccess(new 
Mutex.ExceptionAction<Void>() {
-                @Override public Void run() throws IOException {
-                    properties.storeProperties();
-                    ProjectManager.getDefault().saveProject(nbmProject);
+                @Override
+                public Void run() throws IOException {
+                    ProjectXMLManager projectXMLManager = 
ProjectXMLManager.getInstance(project.getProjectDirectoryFile());
+                    ManifestManager.PackageExport[] previousPubs = 
projectXMLManager.getPublicPackages();
+
+                    String[] friends = projectXMLManager.getFriends();
+                    Set<String> newPacks = new TreeSet<String>();
+                    for (ManifestManager.PackageExport exp : previousPubs) {
+                        newPacks.add(exp.getPackage() + (exp.isRecursive() ? 
".*" : ""));
+                    }
+                    for (String newP : packagesToExport) {
+                        if (export) {
+                            newPacks.add(newP);
+                        } else {
+                            boolean removed = newPacks.remove(newP);
+                            //TODO if not removed we have a problem, the 
package falls under "subpackages" element.
+                            //but I suppose subpackages is rare these days.
+                        }
+                    }
+                    if (friends == null) {
+                        projectXMLManager.replacePublicPackages(newPacks);
+                    } else {
+                        projectXMLManager.replaceFriends(new 
TreeSet<String>(Arrays.asList(friends)), newPacks);
+                    }
+                    ProjectManager.getDefault().saveProject(project);
                     return null;
                 }
             });
diff --git a/java.api.common/apichanges.xml b/java.api.common/apichanges.xml
--- a/java.api.common/apichanges.xml
+++ b/java.api.common/apichanges.xml
@@ -108,21 +108,18 @@
         <change id="api-package-modification">
             <api name="java-api-common"/>
             <summary>New API 
<code>PackageModificationImplementation</code></summary>
-            <version major="1" minor="47"/>
+            <version major="1" minor="49"/>
             <date day="10" month="4" year="2013"/>
             <author login="mkozeny"/>
             <compatibility semantic="compatible" source="compatible" 
binary="compatible"/>
             <description>
                 <p>
-                    Added new API 
<code>PackageModificationImplementation</code>, which provides methods
-                    for getting all available packages 
(<code>getPackagesToExport</code>)
-                    and all public packages (<code>getPublicPackages</code>),
-                    (un)exporting them (<code>exportPackageAction</code>).
-                    This API is used by <code>ExportPackageAction</code> 
located in non-public package 
<code>org.netbeans.modules.java.api.common.impl</code>
-                    which is action implemented for exporting package(s) 
from NetBeans Module Project.
+                    Added new API 
<code>PackageModificationImplementation</code> which serves as backend for 
action to set given package as public or private.
+                    Only applies on packages/folders that are handled by 
<code>AccessibilityQuery</code> (a non null result is returned from the 
query) that are in projects
+                    providing this interface's implementation in project 
lookup.
                 </p>
             </description>
-            <issue number="202329"/>
+            <issue number="228409"/>
         </change>
         <change id="jre-profiles">
             <api name="java-api-common"/>
diff --git a/java.api.common/manifest.mf b/java.api.common/manifest.mf
--- a/java.api.common/manifest.mf
+++ b/java.api.common/manifest.mf
@@ -1,4 +1,4 @@
 Manifest-Version: 1.0
 OpenIDE-Module: org.netbeans.modules.java.api.common/0
 OpenIDE-Module-Localizing-Bundle: 
org/netbeans/modules/java/api/common/resources/Bundle.properties
-OpenIDE-Module-Specification-Version: 1.47
+OpenIDE-Module-Specification-Version: 1.49
diff --git a/java.api.common/nbproject/project.xml 
b/java.api.common/nbproject/project.xml
--- a/java.api.common/nbproject/project.xml
+++ b/java.api.common/nbproject/project.xml
@@ -298,6 +298,7 @@
                 <friend>org.netbeans.modules.javafx2.project</friend>
                 <friend>org.netbeans.modules.javame.bdj.bdjo.project</friend>
                 <friend>org.netbeans.modules.maven</friend>
+                <friend>org.netbeans.modules.maven.apisupport</friend>
                 <friend>org.netbeans.modules.maven.groovy</friend>
                 <friend>org.netbeans.modules.maven.j2ee</friend>
                 <friend>org.netbeans.modules.projectimport</friend>
diff --git 
a/java.api.common/src/org/netbeans/modules/java/api/common/ant/PackageModifierImplementation.java
 
b/java.api.common/src/org/netbeans/modules/java/api/common/ant/PackageModifierImplementation.java
--- 
a/java.api.common/src/org/netbeans/modules/java/api/common/ant/PackageModifierImplementation.java
+++ 
b/java.api.common/src/org/netbeans/modules/java/api/common/ant/PackageModifierImplementation.java
@@ -45,23 +45,13 @@
 import org.netbeans.api.annotations.common.NonNull;
 
 /**
- * Provide API for manipulating with project packages.
+ * Provide API for manipulating with project packages. This API is 
associated with <code>org.netbeans.api.java.queries.AccessibilityQuery</code>.
+ * Packages passed to exportPackageAction method were marked by 
AccessibilityQuery to be either public or private.
  * @author mkozeny
- * @since 1.47
+ * @since 1.49
  */
 public interface PackageModifierImplementation {
     
-    /**
-     * Returns collection of all available packages to export or to unexport
-     * @return collection of all packages which are possible to export or to 
unexport
-     */
-    public @NonNull Collection<String> getAllPackages();
-    
-    /**
-     * Returns collection of public packages
-     * @return collection of public packages
-     */
-    public @NonNull Collection<String> getPublicPackages();
     
     /**
      * Do the export or unexport of passed set of packages
diff --git 
a/java.api.common/src/org/netbeans/modules/java/api/common/impl/ExportPackageAction.java
 
b/java.api.common/src/org/netbeans/modules/java/api/common/impl/ExportPackageAction.java
--- 
a/java.api.common/src/org/netbeans/modules/java/api/common/impl/ExportPackageAction.java
+++ 
b/java.api.common/src/org/netbeans/modules/java/api/common/impl/ExportPackageAction.java
@@ -47,6 +47,8 @@
 import java.util.Iterator;
 import javax.swing.AbstractAction;
 import javax.swing.Action;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.queries.AccessibilityQuery;
 import org.netbeans.api.project.FileOwnerQuery;
 import org.netbeans.api.project.Project;
 import 
org.netbeans.modules.java.api.common.ant.PackageModifierImplementation;
@@ -68,7 +70,7 @@
         category = "Project",
         id = "org.netbeans.modules.apisupport.project.ExportPackageAction")
 @ActionRegistration(
-        displayName = "#CTL_ExportPackageAction", lazy = false)
+        displayName = "#CTL_ExportPackageAction", lazy = false, asynchronous 
= true)
 @ActionReferences({
     @ActionReference(path = "Projects/package/Actions", position = 100),
 })
@@ -104,24 +106,23 @@
                     return new ExportPackageAction.ContextAction();
                 }
             }
-            PackageModifierImplementation pmi = null;
-            if((pmi = 
project.getLookup().lookup(PackageModifierImplementation.class)) != null) {
-                Collection<String> allPackages = pmi.getAllPackages();
+            PackageModifierImplementation pmi = 
project.getLookup().lookup(PackageModifierImplementation.class);
+            if(pmi != null) {
                 Collection<String> packagesToExport = new ArrayList<>();
-                String selectedPackageNameIter = "";
+                boolean export = false;
                 for(FileObject selectedPkgIter:selectedPackages) {
-                    if(allPackages.contains(
-                            selectedPackageNameIter = 
selectedPkgIter.getPath()
-                            
.substring(project.getProjectDirectory().getPath().length()+5).replace('/', 
'.'))) {
-                        packagesToExport.add(selectedPackageNameIter);
+                    Boolean isPublic = 
AccessibilityQuery.isPubliclyAccessible(selectedPkgIter);
+                    if (isPublic != null) {
+                        ClassPath cp = 
ClassPath.getClassPath(selectedPkgIter, ClassPath.SOURCE);
+                        assert cp != null;
+                        String packageName = 
cp.getResourceName(selectedPkgIter, '.', false);
+                        if (!isPublic) {
+                            export = true;
                     }
+                        packagesToExport.add(packageName);
                 }
-                Collection<String> publicPackages = pmi.getPublicPackages();
-                boolean export = false;
-                for(String exportPkgIter:packagesToExport) {
-                    if(!publicPackages.contains(exportPkgIter)) {
-                        export = true;
-                        break;
+                    else {
+                        return new ExportPackageAction.ContextAction();
                     }
                 }
                 return new ExportPackageAction.ContextAction(pmi, 
packagesToExport, export);
@@ -134,7 +135,6 @@
      * The particular instance of this action for a given package(s).
      */
     private static final class ContextAction extends AbstractAction {
-
         private Collection<String> packagesToExport; 
         private boolean export;
         private PackageModifierImplementation pmi;
diff --git a/maven.apisupport/manifest.mf b/maven.apisupport/manifest.mf
--- a/maven.apisupport/manifest.mf
+++ b/maven.apisupport/manifest.mf
@@ -2,5 +2,5 @@
 OpenIDE-Module: org.netbeans.modules.maven.apisupport/1
 OpenIDE-Module-Localizing-Bundle: 
org/netbeans/modules/maven/apisupport/Bundle.properties
 AutoUpdate-Show-In-Client: false
-OpenIDE-Module-Specification-Version: 1.42
+OpenIDE-Module-Specification-Version: 1.44
 
diff --git a/maven.apisupport/nbproject/project.xml 
b/maven.apisupport/nbproject/project.xml
--- a/maven.apisupport/nbproject/project.xml
+++ b/maven.apisupport/nbproject/project.xml
@@ -121,6 +121,15 @@
                     </run-dependency>
                 </dependency>
                 <dependency>
+                    
<code-name-base>org.netbeans.modules.java.api.common</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <release-version>0</release-version>
+                        <specification-version>1.49</specification-version>
+                    </run-dependency>
+                </dependency>
+                <dependency>
                     
<code-name-base>org.netbeans.modules.java.platform</code-name-base>
                     <build-prerequisite/>
                     <compile-dependency/>
@@ -269,6 +278,14 @@
                     </run-dependency>
                 </dependency>
                 <dependency>
+                    <code-name-base>org.openide.explorer</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <specification-version>6.52</specification-version>
+                    </run-dependency>
+                </dependency>
+                <dependency>
                     <code-name-base>org.openide.filesystems</code-name-base>
                     <build-prerequisite/>
                     <compile-dependency/>
@@ -324,6 +341,14 @@
                         <specification-version>8.0</specification-version>
                     </run-dependency>
                 </dependency>
+                <dependency>
+                    <code-name-base>org.openide.windows</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <specification-version>6.63</specification-version>
+                    </run-dependency>
+                </dependency>
             </module-dependencies>
             <test-dependencies>
                 <test-type>
diff --git 
a/maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenPackageModifierImpl.java
 
b/maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenPackageModifierImpl.java
new file mode 100644
--- /dev/null
+++ 
b/maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenPackageModifierImpl.java
@@ -0,0 +1,284 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
+ *
+ * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
+ * Other names may be trademarks of their respective owners.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common
+ * Development and Distribution License("CDDL") (collectively, the
+ * "License"). You may not use this file except in compliance with the
+ * License. You can obtain a copy of the License at
+ * http://www.netbeans.org/cddl-gplv2.html
+ * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
+ * specific language governing permissions and limitations under the
+ * License.  When distributing the software, include this License Header
+ * Notice in each file and include the License file at
+ * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the GPL Version 2 section of the License file that
+ * accompanied this code. If applicable, add the following below the
+ * License Header, with the fields enclosed by brackets [] replaced by
+ * your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * If you wish your version of this file to be governed by only the CDDL
+ * or only the GPL Version 2, indicate your decision by adding
+ * "[Contributor] elects to include this software in this distribution
+ * under the [CDDL or GPL Version 2] license." If you do not indicate a
+ * single choice of license, a recipient has the option to distribute
+ * your version of this file under either the CDDL, the GPL Version 2 or
+ * to extend the choice of license to its licensees as provided above.
+ * However, if you add GPL Version 2 code and therefore, elected the GPL
+ * Version 2 license, then the option applies only if the new code is
+ * made subject to such option by the copyright holder.
+ *
+ * Contributor(s):
+ *
+ * Portions Copyrighted 2013 Sun Microsystems, Inc.
+ */
+
+package org.netbeans.modules.maven.apisupport;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import javax.swing.SwingUtilities;
+import org.netbeans.api.project.Project;
+import 
org.netbeans.modules.java.api.common.ant.PackageModifierImplementation;
+import org.netbeans.modules.maven.api.NbMavenProject;
+import org.netbeans.modules.maven.model.ModelOperation;
+import org.netbeans.modules.maven.model.Utilities;
+import org.netbeans.modules.maven.model.pom.Build;
+import org.netbeans.modules.maven.model.pom.Configuration;
+import org.netbeans.modules.maven.model.pom.POMExtensibilityElement;
+import org.netbeans.modules.maven.model.pom.POMModel;
+import org.netbeans.modules.maven.model.pom.POMQName;
+import org.netbeans.modules.maven.model.pom.Plugin;
+import org.netbeans.modules.maven.model.pom.PluginContainer;
+import org.netbeans.modules.maven.model.pom.PluginManagement;
+import org.netbeans.spi.project.ProjectServiceProvider;
+import org.netbeans.spi.project.ui.LogicalViewProvider;
+import org.openide.explorer.ExplorerManager;
+import org.openide.filesystems.FileObject;
+import org.openide.nodes.Node;
+import org.openide.util.Exceptions;
+import org.openide.windows.TopComponent;
+import org.openide.windows.WindowManager;
+
+/**
+ *
+ * @author mkleint
+ */
+@ProjectServiceProvider(service = PackageModifierImplementation.class, 
projectType="org-netbeans-modules-maven/" + NbMavenProject.TYPE_NBM)
+public class MavenPackageModifierImpl implements 
PackageModifierImplementation {
+    private final Project project;
+
+    public MavenPackageModifierImpl(Project p) {
+        this.project = p;
+    }
+
+    static final String ID_LOGICAL = "projectTabLogical_tc"; // NOI18N    
+    @Override
+    public void exportPackageAction(final Collection<String> 
packagesToExport, final boolean export) {
+        
+        final PropertyChangeListener prop = new PropertyChangeListener() {
+
+            @Override
+            public void propertyChange(PropertyChangeEvent evt) {
+                try {
+                    FileObject srcdir = 
org.netbeans.modules.maven.api.FileUtilities.convertStringToFileObject(project.getLookup().lookup(NbMavenProject.class).getMavenProject().getBuild().getSourceDirectory());
+                    if (srcdir != null) {
+                        LogicalViewProvider lvp = 
project.getLookup().lookup(LogicalViewProvider.class);
+                        assert lvp != null;
+                        Node root = findProjectTCRootNode();
+                        if (root != null) {
+                            for (Node prjNode : 
root.getChildren().getNodes(true)) {
+                                Project p = 
prjNode.getLookup().lookup(Project.class);
+                                if (project.equals(p)) {
+                                    Node found = lvp.findPath(prjNode, 
srcdir);
+                                    if (found != null) {
+                                        iterateNodesAndFireIconChange(found);
+                                    }
+                                    break;
+                                }
+                            }
+                        }
+                    }
+                } finally {
+                    
project.getLookup().lookup(NbMavenProject.class).removePropertyChangeListener(this);
+                }
+            }
+
+            //ugly reflection to force a change of icon badge
+            private void iterateNodesAndFireIconChange(Node found) {
+                for (Node nd : found.getChildren().getNodes(true)) {
+                    FileObject fo = nd.getLookup().lookup(FileObject.class);
+                    if (fo != null && fo.isFolder() && fo.isFolder()) {
+                        try {
+                            Method m1 = 
Node.class.getDeclaredMethod("fireIconChange");
+                            m1.setAccessible(true);
+                            Method m2 = 
Node.class.getDeclaredMethod("fireOpenedIconChange");
+                            m2.setAccessible(true);
+                            m2.invoke(nd);
+                            m1.invoke(nd);
+                        } catch (NoSuchMethodException ex) {
+                            Exceptions.printStackTrace(ex);
+                        } catch (SecurityException ex) {
+                            Exceptions.printStackTrace(ex);
+                        } catch (IllegalAccessException ex) {
+                            Exceptions.printStackTrace(ex);
+                        } catch (IllegalArgumentException ex) {
+                            Exceptions.printStackTrace(ex);
+                        } catch (InvocationTargetException ex) {
+                            Exceptions.printStackTrace(ex);
+                        }
+                        iterateNodesAndFireIconChange(nd);
+                    }
+                }
+            }
+
+            //ugly way of finding the shown root package node to fire 
iconChange events
+            private Node findProjectTCRootNode() {
+                final Node[] toRet = new Node[1];
+                try {
+                    SwingUtilities.invokeAndWait(new Runnable() {
+
+                        @Override
+                        public void run() {
+                            TopComponent projecttc = 
WindowManager.getDefault().findTopComponent(ID_LOGICAL);
+                            if (projecttc != null && projecttc instanceof 
ExplorerManager.Provider) {
+                                ExplorerManager.Provider em = 
(ExplorerManager.Provider) projecttc;
+                                Node root = 
em.getExplorerManager().getRootContext();
+                                toRet[0] = root;
+                            }
+                        }
+                    });
+                } catch (InterruptedException ex) {
+                    Exceptions.printStackTrace(ex);
+                } catch (InvocationTargetException ex) {
+                    Exceptions.printStackTrace(ex);
+                }
+                return toRet[0];
+            }
+        };
+        
Utilities.performPOMModelOperations(project.getProjectDirectory().getFileObject("pom.xml"),
+                Collections.singletonList(new ModelOperation<POMModel>() {
+
+                    @Override
+                    public void performOperation(POMModel model) {
+                        org.netbeans.modules.maven.model.pom.Project p = 
model.getProject();
+                        Build bld = p.getBuild();
+                        POMExtensibilityElement publicPackages = null;
+                        if (bld != null) {
+                            publicPackages = findPublicPackagesElement(bld);
+                            if (publicPackages != null) {
+                                //find in pluginmanagement
+                                PluginManagement pm = 
bld.getPluginManagement();
+                                if (pm != null) {
+                                    publicPackages = 
findPublicPackagesElement(pm);
+                                }
+                            }
+                        }
+
+                        if (publicPackages == null && export) {
+                            publicPackages = createPublicPackagesElement(p);
+                            assert publicPackages != null;
+                        }
+                        if (export) {
+                            for (String exp : packagesToExport) {
+                                POMExtensibilityElement pack = 
model.getFactory().createPOMExtensibilityElement(POMQName.createQName("publicPackage"));
+                                publicPackages.addExtensibilityElement(pack);
+                                pack.setElementText(exp);
+                            }
+                        } else {
+                            //find the right publicPackage element and 
remove it.
+                            //TODO what to do with subpackages?
+                            Set<POMExtensibilityElement> toRemove = new 
HashSet<POMExtensibilityElement>();
+                            LBL : for (String exp : packagesToExport) {
+                                for (POMExtensibilityElement el : 
publicPackages.getExtensibilityElements()) {
+                                    if 
("publicPackage".equals(el.getQName().getLocalPart()) && el.getElementText() 
!= null && el.getElementText().equals(exp)) {
+                                        toRemove.add(el);
+                                        continue LBL;
+                                    }
+                                }
+                                //TODO not in the current file's list or is 
covered by subpackage?
+                                
+                            }
+                            if (!toRemove.isEmpty()) {
+                                for (POMExtensibilityElement el : toRemove) {
+                                    
publicPackages.removeExtensibilityElement(el);
+                                }
+                                if (publicPackages.getChildren() == null || 
publicPackages.getChildren().isEmpty()) {
+                                    
publicPackages.getParent().removeExtensibilityElement(publicPackages);
+                                }
+                            }
+                        }
+                        
project.getLookup().lookup(NbMavenProject.class).addPropertyChangeListener(prop);
+                    }
+
+                    private POMExtensibilityElement 
createPublicPackagesElement(org.netbeans.modules.maven.model.pom.Project p) {
+                        Build bld = p.getBuild();
+                        if (bld == null) {
+                            bld = p.getModel().getFactory().createBuild();
+                            p.setBuild(bld);
+                        }
+                        Plugin plug = 
bld.findPluginById("org.codehaus.mojo", "nbm-maven-plugin");
+                        if (plug == null) {
+                            //mostly should not happen with nb modules..
+                            plug = 
bld.getModel().getFactory().createPlugin();
+                            plug.setGroupId("org.codehaus.mojo");
+                            plug.setArtifactId("nbm-maven-plugin");
+                            plug.setExtensions(Boolean.TRUE);
+                            //set version? let's hope it's managed..
+                            bld.addPlugin(plug);
+                        }
+                        Configuration conf = plug.getConfiguration();
+                        if (conf == null) {
+                            conf = 
plug.getModel().getFactory().createConfiguration();
+                            plug.setConfiguration(conf);
+                        }
+                        List<POMExtensibilityElement> elems = 
conf.getConfigurationElements();
+                        if (elems != null) {
+                            for (POMExtensibilityElement el : elems) {
+                                if 
("publicPackages".equals(el.getQName().getLocalPart())) {
+                                    return el;
+                                }
+                            }
+                        }
+                        POMExtensibilityElement toRet = 
conf.getModel().getFactory().createPOMExtensibilityElement(POMQName.createQName("publicPackages"));
+                        conf.addExtensibilityElement(toRet);
+                        return toRet;
+                    }
+
+                    private POMExtensibilityElement 
findPublicPackagesElement(PluginContainer bld) {
+                        POMExtensibilityElement publicPackages = null;
+                        Plugin plug = 
bld.findPluginById("org.codehaus.mojo", "nbm-maven-plugin");
+                        if (plug != null) {
+                            Configuration conf = plug.getConfiguration();
+                            if (conf != null) {
+                                List<POMExtensibilityElement> elems = 
conf.getConfigurationElements();
+                                if (elems != null) {
+                                    for (POMExtensibilityElement el : elems) 
{
+                                        if 
("publicPackages".equals(el.getQName().getLocalPart())) {
+                                            publicPackages = el;
+                                            break;
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                        return publicPackages;
+                    }
+                }));
+    }
+    
+}

[hg] main-silver: #200833 package modification implementation fo...

Milos Kleint 05/08/2013

Project Features

About this Project

APIsupport was started in November 2009, is owned by Antonin Nebuzelsky, and has 64 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20150626.29986a4). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close