Search in sources :

Example 1 with RootProvider

use of com.intellij.openapi.roots.RootProvider in project intellij-community by JetBrains.

the class JavaSdkImpl method createMockJdk.

@NotNull
@TestOnly
public Sdk createMockJdk(@NotNull String jdkName, @NotNull String home, boolean isJre) {
    String homePath = home.replace(File.separatorChar, '/');
    File jdkHomeFile = new File(homePath);
    ProjectRootContainerImpl rootContainer = new ProjectRootContainerImpl(true);
    SdkModificator sdkModificator = new SdkModificator() {

        @Override
        public String getName() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void setName(String name) {
            throw new UnsupportedOperationException();
        }

        @Override
        public String getHomePath() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void setHomePath(String path) {
            throw new UnsupportedOperationException();
        }

        @Override
        public String getVersionString() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void setVersionString(String versionString) {
            throw new UnsupportedOperationException();
        }

        @Override
        public SdkAdditionalData getSdkAdditionalData() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void setSdkAdditionalData(SdkAdditionalData data) {
            throw new UnsupportedOperationException();
        }

        @Override
        public VirtualFile[] getRoots(OrderRootType rootType) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void removeRoot(VirtualFile root, OrderRootType rootType) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void removeRoots(OrderRootType rootType) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void removeAllRoots() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void commitChanges() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean isWritable() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void addRoot(VirtualFile root, OrderRootType rootType) {
            rootContainer.addRoot(root, rootType);
        }
    };
    rootContainer.startChange();
    addClasses(jdkHomeFile, sdkModificator, isJre);
    addSources(jdkHomeFile, sdkModificator);
    rootContainer.finishChange();
    ProjectJdkImpl jdk = new ProjectJdkImpl(jdkName, this, homePath, jdkName) {

        @Override
        public void setName(@NotNull String name) {
            throwReadOnly();
        }

        @Override
        public void readExternal(@NotNull Element element) {
            throwReadOnly();
        }

        @Override
        public void readExternal(@NotNull Element element, @Nullable ProjectJdkTable projectJdkTable) {
            throwReadOnly();
        }

        @NotNull
        @Override
        public SdkModificator getSdkModificator() {
            throwReadOnly();
            return null;
        }

        @Override
        public void setSdkAdditionalData(SdkAdditionalData data) {
            throwReadOnly();
        }

        @Override
        public void addRoot(VirtualFile root, OrderRootType rootType) {
            throwReadOnly();
        }

        @Override
        public void removeRoot(VirtualFile root, OrderRootType rootType) {
            throwReadOnly();
        }

        @Override
        public void removeRoots(OrderRootType rootType) {
            throwReadOnly();
        }

        @Override
        public void removeAllRoots() {
            throwReadOnly();
        }

        @Override
        public boolean isWritable() {
            return false;
        }

        @Override
        public void update() {
            throwReadOnly();
        }

        @Override
        public VirtualFile[] getRoots(OrderRootType rootType) {
            return rootContainer.getRootFiles(rootType);
        }

        @NotNull
        @Override
        public RootProvider getRootProvider() {
            return new RootProvider() {

                @NotNull
                @Override
                public String[] getUrls(@NotNull OrderRootType rootType) {
                    return ContainerUtil.map2Array(getFiles(rootType), String.class, VirtualFile::getUrl);
                }

                @NotNull
                @Override
                public VirtualFile[] getFiles(@NotNull OrderRootType rootType) {
                    return getRoots(rootType);
                }

                @Override
                public void addRootSetChangedListener(@NotNull RootSetChangedListener listener) {
                }

                @Override
                public void addRootSetChangedListener(@NotNull RootSetChangedListener listener, @NotNull Disposable parentDisposable) {
                }

                @Override
                public void removeRootSetChangedListener(@NotNull RootSetChangedListener listener) {
                }
            };
        }

        private void throwReadOnly() {
            throw new IncorrectOperationException("Can't modify, MockJDK is read-only, consider calling .clone() first");
        }
    };
    ProjectJdkImpl.copyRoots(rootContainer, jdk);
    return jdk;
}
Also used : Disposable(com.intellij.openapi.Disposable) Element(org.jdom.Element) NotNull(org.jetbrains.annotations.NotNull) JavadocOrderRootType(com.intellij.openapi.roots.JavadocOrderRootType) OrderRootType(com.intellij.openapi.roots.OrderRootType) AnnotationOrderRootType(com.intellij.openapi.roots.AnnotationOrderRootType) RootProvider(com.intellij.openapi.roots.RootProvider) IncorrectOperationException(com.intellij.util.IncorrectOperationException) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable) TestOnly(org.jetbrains.annotations.TestOnly) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with RootProvider

use of com.intellij.openapi.roots.RootProvider in project intellij-plugins by JetBrains.

the class FlexCompositeSdk method getRootProvider.

@NotNull
public RootProvider getRootProvider() {
    return new RootProvider() {

        @NotNull
        public String[] getUrls(@NotNull final OrderRootType rootType) {
            final Collection<String> result = new HashSet<>();
            forAllSdks(sdk -> {
                result.addAll(Arrays.asList(sdk.getRootProvider().getUrls(rootType)));
                return true;
            });
            return ArrayUtil.toStringArray(result);
        }

        @NotNull
        public VirtualFile[] getFiles(@NotNull final OrderRootType rootType) {
            final Collection<VirtualFile> result = new HashSet<>();
            forAllSdks(sdk -> {
                result.addAll(Arrays.asList(sdk.getRootProvider().getFiles(rootType)));
                return true;
            });
            return result.toArray(new VirtualFile[result.size()]);
        }

        public void addRootSetChangedListener(@NotNull final RootSetChangedListener listener) {
            forAllSdks(sdk -> {
                final RootProvider rootProvider = sdk.getRootProvider();
                rootProvider.removeRootSetChangedListener(listener);
                rootProvider.addRootSetChangedListener(listener);
                return true;
            });
        }

        public void addRootSetChangedListener(@NotNull final RootSetChangedListener listener, @NotNull final Disposable parentDisposable) {
            forAllSdks(sdk -> {
                sdk.getRootProvider().addRootSetChangedListener(listener, parentDisposable);
                return true;
            });
        }

        public void removeRootSetChangedListener(@NotNull final RootSetChangedListener listener) {
            forAllSdks(sdk -> {
                sdk.getRootProvider().removeRootSetChangedListener(listener);
                return true;
            });
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Disposable(com.intellij.openapi.Disposable) OrderRootType(com.intellij.openapi.roots.OrderRootType) RootProvider(com.intellij.openapi.roots.RootProvider) NotNull(org.jetbrains.annotations.NotNull) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Disposable (com.intellij.openapi.Disposable)2 OrderRootType (com.intellij.openapi.roots.OrderRootType)2 RootProvider (com.intellij.openapi.roots.RootProvider)2 NotNull (org.jetbrains.annotations.NotNull)2 AnnotationOrderRootType (com.intellij.openapi.roots.AnnotationOrderRootType)1 JavadocOrderRootType (com.intellij.openapi.roots.JavadocOrderRootType)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 File (java.io.File)1 HashSet (java.util.HashSet)1 Element (org.jdom.Element)1 Nullable (org.jetbrains.annotations.Nullable)1 TestOnly (org.jetbrains.annotations.TestOnly)1