Skip to content

Commit

Permalink
Add 'Attach Jar SOURCE Directories' feature to module setup.
Browse files Browse the repository at this point in the history
Similar to 'Attach Jar Directories', this feature allows you to directories (e.g. lib/src) that contain source jars/zips. This directory will be watched, and any new source jars added will automatically be associated with class jars.

This is convenient if you already have many class and source jars already organized on your file system (perhaps by an external build tool) and don't want to change your IDE configuration everytime a file is added/removed/renamed.

----

Implementation notes:

* I changed the signatures of some methods, but in every case I've ensured there old signature is still supported for API backwards compatibility.

* Supports both .iml and .idea directory project configs.

* The <jarDirectory> element now has an additional type="SOURCES" attribute to differentiate the OrderRootType.

* It also supports a directory being both a Jar Dir and Source Jar Dir.

* Although there is no UI to support it, the API now allows directories of any OrderRootTypes (e.g. Annotations, JavaDoc). It should just be a case of adding a button to support these.
  • Loading branch information
Joe Walnes committed Jan 26, 2011
1 parent 9838ed0 commit 1fa2c45
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static void genLibraryContent(final ProjectEx project,
libraryPath.add(new PathElement(path));
}
else if (url.startsWith(LocalFileSystem.PROTOCOL_PREFIX)) {
if (library.isJarDirectory(url)) {
if (library.isJarDirectory(url, OrderRootType.CLASSES)) {
final FileSet fileSet = new FileSet(path);
fileSet.add(new PatternSetRef(BuildProperties.PROPERTY_LIBRARIES_PATTERNS));
libraryPath.add(fileSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public OrderRootTypePresentation getRootTypePresentation(@NotNull OrderRootType
@NotNull
@Override
public List<? extends AttachRootButtonDescriptor> createAttachButtons() {
return Arrays.asList(new AttachClassesDescriptor(), new AttachJarDirectoriesDescriptor(), new AttachSourcesDescriptor(),
new AttachAnnotationsDescriptor(), new AttachJavadocDescriptor(), new AttachUrlJavadocDescriptor());
return Arrays.asList(new AttachClassesDescriptor(), new AttachJarDirectoriesDescriptor(),
new AttachSourcesDescriptor(), new AttachJarSourcesDirectoriesDescriptor(),
new AttachAnnotationsDescriptor(), new AttachJavadocDescriptor(),
new AttachUrlJavadocDescriptor());
}

public static OrderRootTypePresentation getDefaultPresentation(OrderRootType type) {
Expand Down Expand Up @@ -105,6 +107,33 @@ public String getChooserDescription() {
}
}

private static class AttachJarSourcesDirectoriesDescriptor extends ChooserBasedAttachRootButtonDescriptor {
private AttachJarSourcesDirectoriesDescriptor() {
super(OrderRootType.SOURCES, ProjectBundle.message("module.libraries.attach.jar.sources.directories.button"));
}

public FileChooserDescriptor createChooserDescriptor() {
return new FileChooserDescriptor(false, true, false, false, false, true);
}

public boolean addAsJarDirectories() {
return true;
}

public String getChooserTitle(final String libraryName) {
if (StringUtil.isEmpty(libraryName)) {
return ProjectBundle.message("library.attach.jar.sources.directory.action");
}
else {
return ProjectBundle.message("library.attach.jar.sources.directory.to.library.action", libraryName);
}
}

public String getChooserDescription() {
return ProjectBundle.message("library.attach.jar.sources.directory.description");
}
}

private static class AttachSourcesDescriptor extends ChooserBasedAttachRootButtonDescriptor {
private AttachSourcesDescriptor() {
super(OrderRootType.SOURCES, ProjectBundle.message("module.libraries.attach.sources.button"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ public void addJarDirectory(String url, boolean recursive) {
getModel().addJarDirectory(url, recursive);
}

@Override
public void addJarDirectory(VirtualFile file, boolean recursive, OrderRootType rootType) {
getModel().addJarDirectory(file, recursive, rootType);
}

@Override
public void addJarDirectory(String url, boolean recursive, OrderRootType rootType) {
getModel().addJarDirectory(url, recursive, rootType);
}

@Override
public void removeRoot(String url, OrderRootType rootType) {
while (getModel().removeRoot(url, rootType)) ;
Expand Down Expand Up @@ -156,20 +166,25 @@ public boolean hasChanges() {
}
return myLibraryProperties != null && !myLibraryProperties.equals(getOriginalProperties());
}

@Override
public boolean isJarDirectory(String url) {
return isJarDirectory(url, OrderRootType.CLASSES);
}

@Override
public boolean isJarDirectory(String url, OrderRootType rootType) {
if (myModel != null) {
return myModel.isJarDirectory(url);
return myModel.isJarDirectory(url, rootType);
}
return myLibrary.isJarDirectory(url);
return myLibrary.isJarDirectory(url, rootType);
}

@Override
public boolean isValid(final String url, final OrderRootType orderRootType) {
if (myModel != null) {
return myModel.isValid(url, orderRootType);
}
return myLibrary.isValid(url, orderRootType);
return myLibrary.isValid(url, orderRootType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public void run() {
final LibraryEditor libraryEditor = getLibraryEditor();
for (VirtualFile file : filesToAttach) {
if (isJarDirectories) {
libraryEditor.addJarDirectory(file, false);
libraryEditor.addJarDirectory(file, false, rootType);
}
else {
libraryEditor.addRoot(file, rootType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Object[] getChildElements(Object element) {
final String[] urls = libraryEditor.getUrls(orderRootType).clone();
Arrays.sort(urls, LibraryRootsComponent.ourUrlComparator);
for (String url : urls) {
items.add(new ItemElement(rootTypeElement, url, orderRootType, libraryEditor.isJarDirectory(url), libraryEditor.isValid(url, orderRootType)));
items.add(new ItemElement(rootTypeElement, url, orderRootType, libraryEditor.isJarDirectory(url, orderRootType), libraryEditor.isValid(url, orderRootType)));
}
return items.toArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.intellij.openapi.roots.libraries.LibraryProperties;
import com.intellij.openapi.roots.libraries.LibraryType;
import com.intellij.openapi.roots.ui.LightFilePointer;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ArrayUtil;
Expand All @@ -36,7 +37,7 @@
public class NewLibraryEditor implements LibraryEditor {
private String myLibraryName;
private final MultiMap<OrderRootType, LightFilePointer> myRoots;
private final Map<String, Boolean> myJarDirectories = new HashMap<String, Boolean>();
private final Map<Pair<String, OrderRootType>, Boolean> myJarDirectories = new HashMap<Pair<String, OrderRootType>, Boolean>();
private final LibraryType myType;
private LibraryProperties myProperties;

Expand Down Expand Up @@ -82,7 +83,7 @@ public VirtualFile[] getFiles(OrderRootType rootType) {
for (LightFilePointer pointer : myRoots.get(rootType)) {
final VirtualFile file = pointer.getFile();
if (file.isDirectory()) {
final Boolean recursively = myJarDirectories.get(file.getUrl());
final Boolean recursively = myJarDirectories.get(Pair.create(file.getUrl(), rootType));
if (recursively != null) {
LibraryImpl.collectJarFiles(file, result, recursively);
continue;
Expand Down Expand Up @@ -110,19 +111,29 @@ public void addRoot(String url, OrderRootType rootType) {

@Override
public void addJarDirectory(VirtualFile file, boolean recursive) {
addJarDirectory(file.getUrl(), recursive);
addJarDirectory(file.getUrl(), recursive, OrderRootType.CLASSES);
}

@Override
public void addJarDirectory(final String url, boolean recursive) {
addRoot(url, OrderRootType.CLASSES);
myJarDirectories.put(url, recursive);
addJarDirectory(url, recursive, OrderRootType.CLASSES);
}

@Override
public void addJarDirectory(VirtualFile file, boolean recursive, OrderRootType rootType) {
addJarDirectory(file.getUrl(), recursive, rootType);
}

@Override
public void addJarDirectory(final String url, boolean recursive, OrderRootType rootType) {
addRoot(url, rootType);
myJarDirectories.put(Pair.create(url, rootType), recursive);
}

@Override
public void removeRoot(String url, OrderRootType rootType) {
myRoots.removeValue(rootType, new LightFilePointer(url));
myJarDirectories.remove(url);
myJarDirectories.remove(Pair.create(url, rootType));
}

@Override
Expand All @@ -132,7 +143,12 @@ public boolean hasChanges() {

@Override
public boolean isJarDirectory(String url) {
return myJarDirectories.containsKey(url);
return isJarDirectory(url, OrderRootType.CLASSES);
}

@Override
public boolean isJarDirectory(String url, OrderRootType rootType) {
return myJarDirectories.containsKey(Pair.create(url, rootType));
}

@Override
Expand All @@ -157,8 +173,11 @@ public void applyRoots(Library.ModifiableModel model) {
model.addRoot(pointer.getUrl(), type);
}
}
for (Map.Entry<String, Boolean> entry : myJarDirectories.entrySet()) {
model.addJarDirectory(entry.getKey(), entry.getValue());
for (Map.Entry<Pair<String, OrderRootType>, Boolean> entry : myJarDirectories.entrySet()) {
final String url = entry.getKey().getFirst();
final OrderRootType rootType = entry.getKey().getSecond();
final Boolean recursive = entry.getValue();
model.addJarDirectory(url, recursive, rootType);
}
}

Expand All @@ -168,8 +187,11 @@ public void copyRoots(ExistingLibraryEditor editor) {
editor.addRoot(pointer.getUrl(), type);
}
}
for (Map.Entry<String, Boolean> entry : myJarDirectories.entrySet()) {
editor.addJarDirectory(entry.getKey(), entry.getValue());
for (Map.Entry<Pair<String, OrderRootType>, Boolean> entry : myJarDirectories.entrySet()) {
final String url = entry.getKey().getFirst();
final OrderRootType rootType = entry.getKey().getSecond();
final Boolean recursive = entry.getValue();
editor.addJarDirectory(url, recursive, rootType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface Library extends JDOMExternalizable, Disposable {
@NotNull VirtualFile[] getFiles(@NotNull OrderRootType rootType);

/**
* As soon as you obtaining modifiable model you will have to commit it or call Disposer.dispose(model)!
* As soon as you obtaining modifiable model you will have to commit it or call Disposer.dispose(model)!
*/
@NotNull ModifiableModel getModifiableModel();

Expand All @@ -45,9 +45,11 @@ public interface Library extends JDOMExternalizable, Disposable {
@NotNull RootProvider getRootProvider();

boolean isJarDirectory(@NotNull String url);


boolean isJarDirectory(@NotNull String url, @NotNull OrderRootType rootType);

boolean isValid(@NotNull String url, @NotNull OrderRootType rootType);

interface ModifiableModel extends Disposable {
@NotNull String[] getUrls(@NotNull OrderRootType rootType);

Expand All @@ -56,13 +58,17 @@ interface ModifiableModel extends Disposable {
String getName();

void addRoot(@NonNls @NotNull String url, @NotNull OrderRootType rootType);

void addJarDirectory(@NotNull String url, boolean recursive);

void addJarDirectory(@NotNull String url, boolean recursive, @NotNull OrderRootType rootType);

void addRoot(@NotNull VirtualFile file, @NotNull OrderRootType rootType);

void addJarDirectory(@NotNull VirtualFile file, boolean recursive);

void addJarDirectory(@NotNull VirtualFile file, boolean recursive, @NotNull OrderRootType rootType);

void moveRootUp(@NotNull String url, @NotNull OrderRootType rootType);

void moveRootDown(@NotNull String url, @NotNull OrderRootType rootType);
Expand All @@ -74,9 +80,11 @@ interface ModifiableModel extends Disposable {
@NotNull VirtualFile[] getFiles(@NotNull OrderRootType rootType);

boolean isChanged();

boolean isJarDirectory(@NotNull String url);


boolean isJarDirectory(@NotNull String url, @NotNull OrderRootType rootType);

boolean isValid(@NotNull String url, @NotNull OrderRootType rootType);
}
}
Loading

0 comments on commit 1fa2c45

Please sign in to comment.