artifacts processing refactored
[fedora-idea.git] / java / compiler / impl / src / com / intellij / packaging / impl / artifacts / ArtifactVirtualFileListener.java
bloba0e22f88bfc0562a234ce8e05623ad9956d43537
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.
16 package com.intellij.packaging.impl.artifacts;
18 import com.intellij.openapi.project.Project;
19 import com.intellij.openapi.util.MultiValuesMap;
20 import com.intellij.openapi.util.io.FileUtil;
21 import com.intellij.openapi.vfs.VirtualFile;
22 import com.intellij.openapi.vfs.VirtualFileAdapter;
23 import com.intellij.openapi.vfs.VirtualFileMoveEvent;
24 import com.intellij.openapi.vfs.VirtualFilePropertyEvent;
25 import com.intellij.packaging.artifacts.Artifact;
26 import com.intellij.packaging.artifacts.ModifiableArtifactModel;
27 import com.intellij.packaging.impl.elements.FileOrDirectoryCopyPackagingElement;
28 import com.intellij.psi.util.CachedValue;
29 import com.intellij.psi.util.CachedValueProvider;
30 import com.intellij.psi.util.CachedValuesManager;
31 import com.intellij.util.PathUtil;
32 import org.jetbrains.annotations.NotNull;
34 import java.util.Collection;
36 /**
37 * @author nik
39 public class ArtifactVirtualFileListener extends VirtualFileAdapter {
40 private CachedValue<MultiValuesMap<String, Artifact>> myParentPathsToArtifacts;
41 private final ArtifactManagerImpl myArtifactManager;
43 public ArtifactVirtualFileListener(Project project, final ArtifactManagerImpl artifactManager) {
44 myArtifactManager = artifactManager;
45 myParentPathsToArtifacts =
46 CachedValuesManager.getManager(project).createCachedValue(new CachedValueProvider<MultiValuesMap<String, Artifact>>() {
47 public Result<MultiValuesMap<String, Artifact>> compute() {
48 MultiValuesMap<String, Artifact> result = computeParentPathToArtifactMap();
49 return Result.createSingleDependency(result, artifactManager.getModificationTracker());
51 }, false);
54 private MultiValuesMap<String, Artifact> computeParentPathToArtifactMap() {
55 final MultiValuesMap<String, Artifact> result = new MultiValuesMap<String, Artifact>();
56 for (final Artifact artifact : myArtifactManager.getArtifacts()) {
57 ArtifactUtil.processFileOrDirectoryCopyElements(artifact, new PackagingElementProcessor<FileOrDirectoryCopyPackagingElement<?>>() {
58 @Override
59 public boolean process(@NotNull FileOrDirectoryCopyPackagingElement<?> element, @NotNull PackagingElementPath pathToElement) {
60 String path = element.getFilePath();
61 while (path.length() > 0) {
62 result.put(path, artifact);
63 path = PathUtil.getParentPath(path);
65 return true;
67 }, myArtifactManager.getResolvingContext(), false);
69 return result;
73 @Override
74 public void fileMoved(VirtualFileMoveEvent event) {
75 final String oldPath = event.getOldParent().getPath() + "/" + event.getFileName();
76 filePathChanged(oldPath, event.getNewParent().getPath() + "/" + event.getFileName());
79 private void filePathChanged(@NotNull final String oldPath, @NotNull final String newPath) {
80 final Collection<Artifact> artifacts = myParentPathsToArtifacts.getValue().get(oldPath);
81 if (artifacts != null) {
82 final ModifiableArtifactModel model = myArtifactManager.createModifiableModel();
83 for (Artifact artifact : artifacts) {
84 final Artifact copy = model.getOrCreateModifiableArtifact(artifact);
85 ArtifactUtil.processFileOrDirectoryCopyElements(copy, new PackagingElementProcessor<FileOrDirectoryCopyPackagingElement<?>>() {
86 @Override
87 public boolean process(@NotNull FileOrDirectoryCopyPackagingElement<?> element, @NotNull PackagingElementPath pathToElement) {
88 final String path = element.getFilePath();
89 if (FileUtil.startsWith(path, oldPath)) {
90 element.setFilePath(newPath + path.substring(oldPath.length()));
92 return true;
94 }, myArtifactManager.getResolvingContext(), false);
96 model.commit();
100 @Override
101 public void propertyChanged(VirtualFilePropertyEvent event) {
102 if (VirtualFile.PROP_NAME.equals(event.getPropertyName())) {
103 final VirtualFile parent = event.getParent();
104 if (parent != null) {
105 filePathChanged(parent.getPath() + "/" + event.getOldValue(), parent.getPath() + "/" + event.getNewValue());