Do not store virtual file pointers in library order entry, use library for that
[fedora-idea.git] / platform / lang-impl / src / com / intellij / openapi / roots / impl / ModuleLibraryTable.java
blob9c7965509bc4c0b663b2631298e061ab3489324a
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.openapi.roots.impl;
19 import com.intellij.openapi.Disposable;
20 import com.intellij.openapi.module.Module;
21 import com.intellij.openapi.project.ProjectBundle;
22 import com.intellij.openapi.roots.LibraryOrderEntry;
23 import com.intellij.openapi.roots.OrderEntry;
24 import com.intellij.openapi.roots.impl.libraries.LibraryTableImplUtil;
25 import com.intellij.openapi.roots.libraries.Library;
26 import com.intellij.openapi.roots.libraries.LibraryTable;
27 import com.intellij.openapi.roots.libraries.LibraryTablePresentation;
28 import com.intellij.openapi.util.Condition;
29 import com.intellij.util.containers.ContainerUtil;
30 import com.intellij.util.containers.ConvertingIterator;
31 import com.intellij.util.containers.Convertor;
32 import com.intellij.util.containers.FilteringIterator;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
36 import java.util.ArrayList;
37 import java.util.Iterator;
39 /**
40 * @author dsl
42 public class ModuleLibraryTable implements LibraryTable, LibraryTable.ModifiableModel {
43 private static final ModuleLibraryOrderEntryCondition MODULE_LIBRARY_ORDER_ENTRY_FILTER = new ModuleLibraryOrderEntryCondition();
44 private static final OrderEntryToLibraryConvertor ORDER_ENTRY_TO_LIBRARY_CONVERTOR = new OrderEntryToLibraryConvertor();
45 private final RootModelImpl myRootModel;
46 private final ProjectRootManagerImpl myProjectRootManager;
47 public static final LibraryTablePresentation MODULE_LIBRARY_TABLE_PRESENTATION = new LibraryTablePresentation() {
48 public String getDisplayName(boolean plural) {
49 return ProjectBundle.message("module.library.display.name", plural ? 2 : 1);
52 public String getDescription() {
53 return ProjectBundle.message("libraries.node.text.module");
56 public String getLibraryTableEditorTitle() {
57 return ProjectBundle.message("library.configure.module.title");
61 ModuleLibraryTable(RootModelImpl rootModel, ProjectRootManagerImpl projectRootManager) {
62 myRootModel = rootModel;
63 myProjectRootManager = projectRootManager;
66 @NotNull
67 public Library[] getLibraries() {
68 final ArrayList<Library> result = new ArrayList<Library>();
69 final Iterator<Library> libraryIterator = getLibraryIterator();
70 ContainerUtil.addAll(result, libraryIterator);
71 return result.toArray(new Library[result.size()]);
74 public Library createLibrary() {
75 final ModuleLibraryOrderEntryImpl orderEntry = new ModuleLibraryOrderEntryImpl(myRootModel, myProjectRootManager);
76 myRootModel.addOrderEntry(orderEntry);
77 return orderEntry.getLibrary();
80 public Library createLibrary(String name) {
81 final ModuleLibraryOrderEntryImpl orderEntry = new ModuleLibraryOrderEntryImpl(name, myRootModel, myProjectRootManager);
82 myRootModel.addOrderEntry(orderEntry);
83 return orderEntry.getLibrary();
86 public void removeLibrary(@NotNull Library library) {
87 final Iterator<OrderEntry> orderIterator = myRootModel.getOrderIterator();
88 while (orderIterator.hasNext()) {
89 OrderEntry orderEntry = orderIterator.next();
90 if (orderEntry instanceof LibraryOrderEntry) {
91 final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry;
92 if (libraryOrderEntry.isModuleLevel()) {
93 if (library.equals(libraryOrderEntry.getLibrary())) {
94 myRootModel.removeOrderEntry(orderEntry);
95 //Disposer.dispose(library);
96 return;
103 @NotNull
104 public Iterator<Library> getLibraryIterator() {
105 FilteringIterator<OrderEntry, LibraryOrderEntry> filteringIterator =
106 new FilteringIterator<OrderEntry, LibraryOrderEntry>(myRootModel.getOrderIterator(), MODULE_LIBRARY_ORDER_ENTRY_FILTER);
107 return new ConvertingIterator<LibraryOrderEntry, Library>(filteringIterator, ORDER_ENTRY_TO_LIBRARY_CONVERTOR);
110 public String getTableLevel() {
111 return LibraryTableImplUtil.MODULE_LEVEL;
114 public LibraryTablePresentation getPresentation() {
115 return MODULE_LIBRARY_TABLE_PRESENTATION;
118 public boolean isEditable() {
119 return true;
122 @Nullable
123 public Library getLibraryByName(@NotNull String name) {
124 final Iterator<Library> libraryIterator = getLibraryIterator();
125 while (libraryIterator.hasNext()) {
126 Library library = libraryIterator.next();
127 if (name.equals(library.getName())) return library;
129 return null;
132 public void addListener(Listener listener) {
133 throw new UnsupportedOperationException();
136 public void addListener(Listener listener, Disposable parentDisposable) {
137 throw new UnsupportedOperationException("Method addListener is not yet implemented in " + getClass().getName());
140 public void removeListener(Listener listener) {
141 throw new UnsupportedOperationException();
144 public Module getModule() {
145 return myRootModel.getModule();
149 private static class ModuleLibraryOrderEntryCondition implements Condition<OrderEntry> {
150 public boolean value(OrderEntry entry) {
151 return entry instanceof LibraryOrderEntry && ((LibraryOrderEntry)entry).isModuleLevel() && ((LibraryOrderEntry)entry).getLibrary() != null;
155 private static class OrderEntryToLibraryConvertor implements Convertor<LibraryOrderEntry, Library> {
156 public Library convert(LibraryOrderEntry o) {
157 return o.getLibrary();
161 public void commit() {
164 public boolean isChanged() {
165 return myRootModel.isChanged();
168 public ModifiableModel getModifiableModel() {
169 return this;