diff --git a/platform/lang-api/src/com/intellij/openapi/roots/libraries/Library.java b/platform/lang-api/src/com/intellij/openapi/roots/libraries/Library.java index 4a2f6ae8310f..8a2bda7ade0f 100644 --- a/platform/lang-api/src/com/intellij/openapi/roots/libraries/Library.java +++ b/platform/lang-api/src/com/intellij/openapi/roots/libraries/Library.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import com.intellij.openapi.util.JDOMExternalizable; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author dsl @@ -29,7 +30,7 @@ import org.jetbrains.annotations.NotNull; public interface Library extends JDOMExternalizable, Disposable { Library[] EMPTY_ARRAY = new Library[0]; - String getName(); + @Nullable String getName(); @NotNull String[] getUrls(@NotNull OrderRootType rootType); diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/diff/GradleDiffUtil.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/diff/GradleDiffUtil.java index 7c53b3df3148..0b9c29ee0897 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/diff/GradleDiffUtil.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/diff/GradleDiffUtil.java @@ -113,10 +113,7 @@ public class GradleDiffUtil { @Override public void visit(@NotNull LibraryOrderEntry libraryDependency) { - final String libraryName = libraryDependency.getLibraryName(); - if (libraryName != null) { - context.register(new GradleLibraryDependencyPresenceChange(null, libraryDependency)); - } + context.register(new GradleLibraryDependencyPresenceChange(null, libraryDependency)); } @Override diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/diff/library/GradleLibraryStructureChangesCalculator.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/diff/library/GradleLibraryStructureChangesCalculator.java index 22d88a5ea65a..77f54b8e7eb9 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/diff/library/GradleLibraryStructureChangesCalculator.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/diff/library/GradleLibraryStructureChangesCalculator.java @@ -9,6 +9,7 @@ import org.jetbrains.plugins.gradle.diff.GradleChangesCalculationContext; import org.jetbrains.plugins.gradle.diff.GradleStructureChangesCalculator; import org.jetbrains.plugins.gradle.model.gradle.GradleLibrary; import org.jetbrains.plugins.gradle.model.gradle.LibraryPathType; +import org.jetbrains.plugins.gradle.util.GradleUtil; import java.util.HashSet; import java.util.Set; @@ -47,7 +48,7 @@ public class GradleLibraryStructureChangesCalculator implements GradleStructureC @NotNull @Override public Object getIntellijKey(@NotNull Library entity) { - return entity.getName(); + return GradleUtil.getLibraryName(entity); } @NotNull diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/diff/library/GradleMismatchedLibraryPathChange.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/diff/library/GradleMismatchedLibraryPathChange.java index 579a929a599b..e1c97223d174 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/diff/library/GradleMismatchedLibraryPathChange.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/diff/library/GradleMismatchedLibraryPathChange.java @@ -6,6 +6,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.gradle.diff.GradleAbstractConflictingPropertyChange; import org.jetbrains.plugins.gradle.diff.GradleProjectStructureChangeVisitor; import org.jetbrains.plugins.gradle.util.GradleBundle; +import org.jetbrains.plugins.gradle.util.GradleUtil; import java.util.Set; @@ -22,12 +23,8 @@ public class GradleMismatchedLibraryPathChange extends GradleAbstractConflicting @Nullable Set intellijValue) throws IllegalArgumentException { - super(GradleBundle.message("gradle.sync.change.library.path", entity.getName()), gradleValue, intellijValue); - myLibraryName = entity.getName(); - if (myLibraryName == null) { - throw new IllegalArgumentException(String.format("Can't create %s instance. Reason: given library has no name (%s)", - getClass().getName(), entity)); - } + super(GradleBundle.message("gradle.sync.change.library.path", GradleUtil.getLibraryName(entity)), gradleValue, intellijValue); + myLibraryName = GradleUtil.getLibraryName(entity); } @NotNull diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/model/id/GradleEntityIdMapper.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/model/id/GradleEntityIdMapper.java index 2293a21a16ea..fdd4709971c1 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/model/id/GradleEntityIdMapper.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/model/id/GradleEntityIdMapper.java @@ -4,8 +4,8 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.LibraryOrderEntry; import com.intellij.openapi.roots.ModuleOrderEntry; +import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.util.Ref; -import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.gradle.model.GradleEntityOwner; @@ -92,20 +92,23 @@ public class GradleEntityIdMapper { @Override public void visit(@NotNull ModuleAwareContentRoot contentRoot) { - final VirtualFile file = contentRoot.getFile(); - if (file == null) { - return; - } - result.set(new GradleContentRootId(GradleEntityOwner.INTELLIJ, contentRoot.getModule().getName(), file.getPath())); + final String path = contentRoot.getFile().getPath(); + result.set(new GradleContentRootId(GradleEntityOwner.INTELLIJ, contentRoot.getModule().getName(), path)); } @Override public void visit(@NotNull LibraryOrderEntry libraryDependency) { - final String libraryName = libraryDependency.getLibraryName(); - if (libraryName != null) { - result - .set(new GradleLibraryDependencyId(GradleEntityOwner.INTELLIJ, libraryDependency.getOwnerModule().getName(), libraryName)); + String libraryName = libraryDependency.getLibraryName(); + if (libraryName == null) { + final Library library = libraryDependency.getLibrary(); + if (library != null) { + libraryName = GradleUtil.getLibraryName(library); + } } + if (libraryName == null) { + return; + } + result.set(new GradleLibraryDependencyId(GradleEntityOwner.INTELLIJ, libraryDependency.getOwnerModule().getName(), libraryName)); } @Override diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/sync/GradleProjectStructureChangesPanel.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/sync/GradleProjectStructureChangesPanel.java index 52b1b1980d62..e647066e7d01 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/sync/GradleProjectStructureChangesPanel.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/sync/GradleProjectStructureChangesPanel.java @@ -10,7 +10,6 @@ import com.intellij.openapi.ui.popup.Balloon; import com.intellij.openapi.ui.popup.JBPopupFactory; import com.intellij.openapi.util.Disposer; import com.intellij.ui.TreeSpeedSearch; -import com.intellij.ui.TreeUIHelper; import com.intellij.ui.awt.RelativePoint; import com.intellij.ui.treeStructure.Tree; import com.intellij.util.Alarm; @@ -50,8 +49,7 @@ public class GradleProjectStructureChangesPanel extends GradleToolWindowPanel { private static final int TOOLTIP_DELAY_MILLIS = 500; private static final int COLLAPSE_STATE_PROCESSING_DELAY_MILLIS = 200; - private static final String PATH_SEPARATOR = "/"; - + private static final Comparator PATH_COMPARATOR = new Comparator() { @Override public int compare(TreePath o1, TreePath o2) { @@ -385,7 +383,7 @@ public class GradleProjectStructureChangesPanel extends GradleToolWindowPanel { private static String getPath(@NotNull TreePath path) { StringBuilder buffer = new StringBuilder(); for (TreePath current = path; current != null; current = current.getParentPath()) { - buffer.append(current.getLastPathComponent().toString()).append(PATH_SEPARATOR); + buffer.append(current.getLastPathComponent().toString()).append(GradleUtil.PATH_SEPARATOR); } buffer.setLength(buffer.length() - 1); return buffer.toString(); diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/sync/GradleProjectStructureHelper.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/sync/GradleProjectStructureHelper.java index b62d3fa6bd5e..df0e73be81c5 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/sync/GradleProjectStructureHelper.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/sync/GradleProjectStructureHelper.java @@ -19,6 +19,7 @@ import org.jetbrains.plugins.gradle.model.id.GradleContentRootId; import org.jetbrains.plugins.gradle.model.id.GradleLibraryDependencyId; import org.jetbrains.plugins.gradle.model.id.GradleModuleDependencyId; import org.jetbrains.plugins.gradle.model.intellij.ModuleAwareContentRoot; +import org.jetbrains.plugins.gradle.util.GradleUtil; /** * Thread-safe. @@ -127,7 +128,7 @@ public class GradleProjectStructureHelper extends AbstractProjectComponent { } for (ModuleAwareContentRoot contentRoot : myFacade.getContentRoots(module)) { final VirtualFile file = contentRoot.getFile(); - if (file != null && id.getRootPath().equals(file.getPath())) { + if (id.getRootPath().equals(file.getPath())) { return contentRoot; } } @@ -144,7 +145,7 @@ public class GradleProjectStructureHelper extends AbstractProjectComponent { final LibraryTable libraryTable = myFacade.getProjectLibraryTable(myProject); for (Library intellijLibrary : libraryTable.getLibraries()) { // TODO den consider 'merged libraries' at the registered project structure changes here. - if (libraryName.equals(intellijLibrary.getName())) { + if (libraryName.equals(GradleUtil.getLibraryName(intellijLibrary))) { return intellijLibrary; } } diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/sync/conflict/GradleLibraryConflictControlFactory.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/sync/conflict/GradleLibraryConflictControlFactory.java index 577ce61a5591..fc751bff8857 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/sync/conflict/GradleLibraryConflictControlFactory.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/sync/conflict/GradleLibraryConflictControlFactory.java @@ -30,7 +30,7 @@ public class GradleLibraryConflictControlFactory { @NotNull public JComponent getControl(@NotNull Library library, @NotNull GradleMismatchedLibraryPathChange change) { GradleProjectStructureNode root = new GradleProjectStructureNode( - GradleUtil.buildSyntheticDescriptor(library.getName(), GradleIcons.LIB_ICON) + GradleUtil.buildSyntheticDescriptor(GradleUtil.getLibraryName(library), GradleIcons.LIB_ICON) ); final Set gradleLocalBinaryPaths = change.getGradleValue(); if (gradleLocalBinaryPaths != null) { diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/util/GradleUtil.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/util/GradleUtil.java index 505bff71191b..80f467e60117 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/util/GradleUtil.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/util/GradleUtil.java @@ -12,6 +12,8 @@ import com.intellij.openapi.progress.Task; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.LibraryOrderEntry; import com.intellij.openapi.roots.ModuleOrderEntry; +import com.intellij.openapi.roots.OrderRootType; +import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.ui.MessageType; import com.intellij.openapi.ui.popup.Balloon; import com.intellij.openapi.ui.popup.BalloonBuilder; @@ -54,6 +56,8 @@ import java.util.concurrent.TimeUnit; */ public class GradleUtil { + public static final String PATH_SEPARATOR = "/"; + private GradleUtil() { } @@ -275,6 +279,37 @@ public class GradleUtil { } return new Point(bounds.x + xAdjustment, bounds.y + bounds.height); } + + @NotNull + public static String getLibraryName(@NotNull Library library) { + final String result = library.getName(); + if (result != null) { + return result; + } + String[] endingsToStrip = { "/", "!", ".jar" }; + StringBuilder buffer = new StringBuilder(); + for (OrderRootType type : OrderRootType.getAllTypes()) { + for (String url : library.getUrls(type)) { + buffer.setLength(0); + buffer.append(url); + for (String ending : endingsToStrip) { + if (buffer.lastIndexOf(ending) == buffer.length() - ending.length()) { + buffer.setLength(buffer.length() - ending.length()); + } + } + final int i = buffer.lastIndexOf(PATH_SEPARATOR); + if (i < 0 || i >= buffer.length() - 1) { + continue; + } + String candidate = buffer.substring(i + 1); + if (!StringUtil.isEmpty(candidate)) { + return candidate; + } + } + } + assert false; + return "unknown-lib"; + } private interface TaskUnderProgress { void execute(@NotNull ProgressIndicator indicator);