1 package com
.intellij
.openapi
.roots
.ui
.configuration
.packaging
;
3 import com
.intellij
.ide
.IdeBundle
;
4 import com
.intellij
.openapi
.deployment
.*;
5 import com
.intellij
.openapi
.diagnostic
.Logger
;
6 import com
.intellij
.openapi
.module
.Module
;
7 import com
.intellij
.openapi
.module
.StdModuleTypes
;
8 import com
.intellij
.openapi
.project
.ProjectBundle
;
9 import com
.intellij
.openapi
.roots
.libraries
.Library
;
10 import com
.intellij
.openapi
.roots
.ui
.configuration
.libraryEditor
.ChooseModulesDialog
;
11 import com
.intellij
.util
.Icons
;
12 import com
.intellij
.util
.containers
.ContainerUtil
;
13 import org
.jetbrains
.annotations
.NotNull
;
14 import org
.jetbrains
.annotations
.Nullable
;
22 public abstract class PackagingEditorPolicy
{
23 private static final Logger LOG
= Logger
.getInstance("#com.intellij.openapi.roots.ui.configuration.packaging.PackagingEditorPolicy");
24 public static final String NOT_APPLICABLE
= IdeBundle
.message("text.not.applicable");
25 private final Module myModule
;
27 protected PackagingEditorPolicy(final @NotNull Module module
) {
31 protected abstract PackagingMethod
[] getAllowedPackagingMethodsForLibrary(LibraryLink libraryLink
);
33 protected abstract PackagingMethod
[] getAllowedPackagingMethodsForModule(@NotNull Module module
);
35 public abstract void setDefaultAttributes(ContainerElement element
);
37 public abstract String
suggestDefaultRelativePath(ContainerElement element
);
39 protected abstract List
<Module
> getSuitableModules(final PackagingEditor packagingEditor
);
41 protected abstract List
<Library
> getSuitableLibraries(final PackagingEditor packagingEditor
);
43 protected abstract ContainerElement
[] getModifiedElements(PackagingEditor packagingEditor
);
45 public boolean isRelativePathCellEditable(final ContainerElement element
) {
46 return element
instanceof LibraryLink
47 || element
instanceof ModuleLink
&& ((ModuleLink
)element
).getModule() != null;
51 public AdditionalPropertiesConfigurable
getAdditionalPropertiesConfigurable(@NotNull ContainerElement element
) {
55 public PackagingMethod
[] getAllowedPackagingMethods(ContainerElement element
) {
56 if (element
instanceof LibraryLink
) {
57 final LibraryLink libraryLink
= (LibraryLink
)element
;
58 if (libraryLink
.getLibrary() == null) {
59 return getPackagingMethodForUnresolvedElement(libraryLink
);
61 return getAllowedPackagingMethodsForLibrary(libraryLink
);
63 if (element
instanceof ModuleLink
) {
64 final ModuleLink moduleLink
= (ModuleLink
)element
;
65 final Module module
= moduleLink
.getModule();
67 return getAllowedPackagingMethodsForModule(module
);
69 return getPackagingMethodForUnresolvedElement(moduleLink
);
71 LOG
.assertTrue(false, "unexpected element: " + element
.getClass());
75 protected PackagingMethod
[] getPackagingMethodForUnresolvedElement(final ContainerElement element
) {
76 final PackagingMethod method
= element
.getPackagingMethod();
77 if (method
== PackagingMethod
.DO_NOT_PACKAGE
) {
78 return PackagingMethod
.EMPTY_ARRAY
;
80 return new PackagingMethod
[]{method
};
83 public boolean isAllowedToPackage(ContainerElement element
) {
84 return getAllowedPackagingMethods(element
).length
> 0;
88 public Module
getModule() {
92 protected List
<AddPackagingElementAction
> getAddActions() {
93 List
<AddPackagingElementAction
> actions
= new ArrayList
<AddPackagingElementAction
>();
94 actions
.add(new AddPackagingElementAction(ProjectBundle
.message("action.name.packaging.add.library"), Icons
.LIBRARY_ICON
) {
95 public boolean isEnabled(@NotNull final PackagingEditor editor
) {
96 return !getLibrariesToAdd(editor
).isEmpty();
99 public void perform(final PackagingEditor packagingEditor
) {
100 List
<Library
> libraries
= getLibrariesToAdd(packagingEditor
);
101 ContainerUtil
.removeDuplicates(libraries
);
102 ChooseLibrariesDialog dialog
= new ChooseLibrariesDialog(packagingEditor
.getMainPanel(),
103 ProjectBundle
.message("dialog.title.packaging.choose.library"), libraries
);
106 packagingEditor
.addLibraries(dialog
.getChosenElements());
110 actions
.add(new AddPackagingElementAction(ProjectBundle
.message("action.name.packaging.add.module"), StdModuleTypes
.JAVA
.getNodeIcon(false)) {
111 public boolean isEnabled(@NotNull final PackagingEditor editor
) {
112 return !getModulesToAdd(editor
).isEmpty();
115 public void perform(final PackagingEditor packagingEditor
) {
116 List
<Module
> moduleList
= getModulesToAdd(packagingEditor
);
117 ContainerUtil
.removeDuplicates(moduleList
);
118 ChooseModulesDialog dialog
= new ChooseModulesDialog(packagingEditor
.getMainPanel(), moduleList
,
119 ProjectBundle
.message("dialog.title.packaging.choose.module"));
121 List
<Module
> modules
= new ArrayList
<Module
>(dialog
.getChosenElements());
123 packagingEditor
.addModules(modules
);
130 private List
<Library
> getLibrariesToAdd(final PackagingEditor editor
) {
131 List
<Library
> libraries
= new ArrayList
<Library
>(getSuitableLibraries(editor
));
132 Set
<Library
> addedLibraries
= new HashSet
<Library
>();
133 for (ContainerElement element
: getModifiedElements(editor
)) {
134 if (element
instanceof LibraryLink
) {
135 addedLibraries
.add(((LibraryLink
)element
).getLibrary());
138 libraries
.removeAll(addedLibraries
);
142 private List
<Module
> getModulesToAdd(final PackagingEditor editor
) {
143 List
<Module
> moduleList
= new ArrayList
<Module
>(getSuitableModules(editor
));
144 Set
<Module
> addedModules
= new HashSet
<Module
>();
145 for (ContainerElement element
: getModifiedElements(editor
)) {
146 if (element
instanceof ModuleLink
) {
147 addedModules
.add(((ModuleLink
)element
).getModule());
150 moduleList
.removeAll(addedModules
);
154 public boolean removeObsoleteElements(final PackagingEditor packagingEditor
) {
155 boolean modelChanged
= false;
156 Set
<Library
> libraries
= new HashSet
<Library
>(getSuitableLibraries(packagingEditor
));
157 Set
<Module
> modules
= new HashSet
<Module
>(getSuitableModules(packagingEditor
));
158 List
<ContainerElement
> elements
= new ArrayList
<ContainerElement
>(Arrays
.asList(getModifiedElements(packagingEditor
)));
159 PackagingConfiguration configuration
= packagingEditor
.getModifiedConfiguration();
160 for (ContainerElement element
: elements
) {
161 boolean remove
= false;
162 if (element
instanceof LibraryLink
) {
163 Library library
= ((LibraryLink
)element
).getLibrary();
164 remove
= library
!= null && !libraries
.contains(library
);
166 else if (element
instanceof ModuleLink
) {
167 Module module
= ((ModuleLink
)element
).getModule();
168 remove
= module
!= null && !modules
.contains(module
);
171 configuration
.removeContainerElement(element
);
178 public String
getElementText(final ContainerElement element
) {
179 if (element
instanceof LibraryLink
) {
180 final LibraryLink libraryLink
= (LibraryLink
)element
;
181 Library library
= libraryLink
.getLibrary();
182 if (library
!= null) {
183 return PackagingEditorUtil
.getLibraryDescription(library
);
186 if (element
instanceof ModuleLink
) {
187 final ModuleLink moduleLink
= (ModuleLink
)element
;
188 return ProjectBundle
.message("text.module.link.description.0.module", moduleLink
.getName());
190 return element
.getPresentableName();
193 public void processNewLibraries(final PackagingEditor editor
, final List
<Library
> libraries
) {
194 List
<Library
> toAdd
= new ArrayList
<Library
>();
195 Set
<Library
> suitable
= new HashSet
<Library
>(getLibrariesToAdd(editor
));
196 for (Library library
: libraries
) {
197 if (suitable
.contains(library
) && shouldAddSilently(library
)) {
201 editor
.addLibraries(toAdd
);
204 protected boolean shouldAddSilently(final Library library
) {
208 protected static abstract class AdditionalPropertiesConfigurable
{
210 public abstract JComponent
getLabelsComponent();
211 public abstract JComponent
getFieldsComponent();
213 public abstract void resetFrom(ContainerElement element
);
214 public abstract void applyTo(ContainerElement element
);