diff --git a/projectapi/apichanges.xml b/projectapi/apichanges.xml --- a/projectapi/apichanges.xml +++ b/projectapi/apichanges.xml @@ -107,8 +107,22 @@ - - + + + Added a LookupMerger for SharabilityQueryImplementation2 + + + + + +

+ Added a factory method into the LookupProviderSupport creating a + LookupMerger for SharabilityQueryImplementation2. +

+
+ + +
SPI interface for ProjectManager @@ -126,7 +140,6 @@ - More information in ProjectManager.Result diff --git a/projectapi/manifest.mf b/projectapi/manifest.mf --- a/projectapi/manifest.mf +++ b/projectapi/manifest.mf @@ -1,6 +1,6 @@ Manifest-Version: 1.0 OpenIDE-Module: org.netbeans.modules.projectapi/1 -OpenIDE-Module-Specification-Version: 1.63 +OpenIDE-Module-Specification-Version: 1.64 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/projectapi/Bundle.properties OpenIDE-Module-Layer: org/netbeans/modules/projectapi/layer.xml OpenIDE-Module-Recommends: cnb.org.netbeans.modules.projectapi.nb diff --git a/projectapi/src/org/netbeans/spi/project/support/LookupProviderSupport.java b/projectapi/src/org/netbeans/spi/project/support/LookupProviderSupport.java --- a/projectapi/src/org/netbeans/spi/project/support/LookupProviderSupport.java +++ b/projectapi/src/org/netbeans/spi/project/support/LookupProviderSupport.java @@ -44,6 +44,7 @@ package org.netbeans.spi.project.support; +import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -51,11 +52,14 @@ import java.util.Set; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; +import org.netbeans.api.annotations.common.NonNull; import org.netbeans.api.project.SourceGroup; import org.netbeans.api.project.Sources; +import org.netbeans.api.queries.SharabilityQuery; import org.netbeans.spi.project.ActionProvider; import org.netbeans.spi.project.LookupMerger; import org.netbeans.spi.project.LookupProvider; +import org.netbeans.spi.queries.SharabilityQueryImplementation2; import org.openide.util.ChangeSupport; import org.openide.util.Exceptions; import org.openide.util.Lookup; @@ -130,6 +134,18 @@ return new ActionProviderMerger(); } + /** + * Factory method for creating {@link org.netbeans.spi.project.LookupMerger} instance that merges + * {@link SharabilityQueryImplementation2} instances in the project lookup. + * The first non {@link SharabilityQuery.Sharability#UNKNOWN} result returned by the {@link SharabilityQueryImplementation2}s + * included in the project's {@link Lookup} is returned. + * @return instance to include in project lookup + * @since 1.64 + */ + public static LookupMerger createSharabilityQueryMerger() { + return new SharabilityQueryMerger(); + } + private static class SourcesMerger implements LookupMerger { public @Override Class getMergeableClass() { return Sources.class; @@ -281,4 +297,36 @@ } } + + private static final class SharabilityQueryMerger implements LookupMerger { + + @Override + public Class getMergeableClass() { + return SharabilityQueryImplementation2.class; + } + + @Override + public SharabilityQueryImplementation2 merge(Lookup lookup) { + return new MergedSharabilityQueryImplementation2(lookup); + } + } + + private static final class MergedSharabilityQueryImplementation2 implements SharabilityQueryImplementation2 { + private final Lookup.Result lkpResult; + + MergedSharabilityQueryImplementation2(@NonNull final Lookup lkp) { + this.lkpResult = lkp.lookupResult(SharabilityQueryImplementation2.class); + } + + @Override + public SharabilityQuery.Sharability getSharability(URI uri) { + for (SharabilityQueryImplementation2 impl : lkpResult.allInstances()) { + SharabilityQuery.Sharability res = impl.getSharability(uri); + if (res != SharabilityQuery.Sharability.UNKNOWN) { + return res; + } + } + return SharabilityQuery.Sharability.UNKNOWN; + } + } } diff --git a/projectapi/test/unit/src/org/netbeans/spi/project/support/LookupProviderSupportTest.java b/projectapi/test/unit/src/org/netbeans/spi/project/support/LookupProviderSupportTest.java --- a/projectapi/test/unit/src/org/netbeans/spi/project/support/LookupProviderSupportTest.java +++ b/projectapi/test/unit/src/org/netbeans/spi/project/support/LookupProviderSupportTest.java @@ -45,6 +45,9 @@ package org.netbeans.spi.project.support; import java.beans.PropertyChangeListener; +import java.io.File; +import java.io.IOException; +import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -55,12 +58,15 @@ import java.util.Map; import javax.swing.Icon; import javax.swing.event.ChangeListener; +import org.netbeans.api.annotations.common.NonNull; import org.netbeans.api.project.SourceGroup; import org.netbeans.api.project.Sources; +import org.netbeans.api.queries.SharabilityQuery; import org.netbeans.junit.NbTestCase; import org.netbeans.spi.project.ActionProvider; import org.netbeans.spi.project.LookupMerger; import org.netbeans.spi.project.LookupProvider; +import org.netbeans.spi.queries.SharabilityQueryImplementation2; import org.openide.filesystems.FileObject; import org.openide.util.Lookup; import org.openide.util.lookup.AbstractLookup; @@ -252,6 +258,47 @@ assertEquals("[group1, group2, group3]", names.toString()); } + + + public void testSharabilityQueryMerger() throws IOException { + final File wd = getWorkDir(); + final File f1 = new File (wd, "f1"); //NOI18N + final File f2 = new File (wd, "f2"); //NOI18N + final File f3 = new File (wd, "f3"); //NOI18N + final File f4 = new File (wd, "f4"); //NOI18N + final File f5 = new File (wd, "f5"); //NOI18N + + final SharabilityQueryImpl impl1 = new SharabilityQueryImpl(Collections.singletonMap(f1.toURI(), SharabilityQuery.Sharability.SHARABLE)); + final SharabilityQueryImpl impl2 = new SharabilityQueryImpl(Collections.singletonMap(f2.toURI(), SharabilityQuery.Sharability.NOT_SHARABLE)); + final SharabilityQueryImpl impl3 = new SharabilityQueryImpl(new HashMap(){{ + put(f3.toURI(),SharabilityQuery.Sharability.SHARABLE); + put(f4.toURI(),SharabilityQuery.Sharability.NOT_SHARABLE); + }}); + final SharabilityQueryImpl impl4 = new SharabilityQueryImpl(Collections.singletonMap(f2.toURI(), SharabilityQuery.Sharability.SHARABLE)); + Lookup base = Lookups.fixed(impl1, LookupProviderSupport.createSharabilityQueryMerger()); + LookupProviderImpl2 pro2 = new LookupProviderImpl2(); + LookupProviderImpl2 pro3 = new LookupProviderImpl2(); + LookupProviderImpl2 pro4 = new LookupProviderImpl2(); + + InstanceContent provInst = new InstanceContent(); + Lookup providers = new AbstractLookup(provInst); + provInst.add(pro2); + provInst.add(pro3); + provInst.add(pro4); + pro2.ic.add(impl2); + pro3.ic.add(impl3); + pro4.ic.add(impl4); + DelegatingLookupImpl del = new DelegatingLookupImpl(base, providers, ""); + + SharabilityQueryImplementation2 sharability = del.lookup(SharabilityQueryImplementation2.class); + assertNotNull(sharability); + assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability.getSharability(f1.toURI())); + assertEquals(SharabilityQuery.Sharability.NOT_SHARABLE, sharability.getSharability(f2.toURI())); + assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability.getSharability(f3.toURI())); + assertEquals(SharabilityQuery.Sharability.NOT_SHARABLE, sharability.getSharability(f4.toURI())); + assertEquals(SharabilityQuery.Sharability.UNKNOWN, sharability.getSharability(f5.toURI())); + } + private class LookupProviderImpl2 implements LookupProvider { InstanceContent ic = new InstanceContent(); AbstractLookup l; @@ -350,4 +397,23 @@ return res; } } + + private static final class SharabilityQueryImpl implements SharabilityQueryImplementation2 { + + private final Map sharability; + + SharabilityQueryImpl(@NonNull final Map sharability) { + this.sharability = sharability; + } + + @Override + @NonNull + public SharabilityQuery.Sharability getSharability(@NonNull final URI uri) { + SharabilityQuery.Sharability res = sharability.get(uri); + if (res == null) { + res = SharabilityQuery.Sharability.UNKNOWN; + } + return res; + } + } }