summaryrefslogtreecommitdiff
path: root/platform/analysis-impl/src/com/intellij/profile/codeInspection/InspectionProfileManager.java
blob: 7d0f203e94df3c165f66c06b32434edbf188e6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright 2000-2009 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.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.profile.codeInspection;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.components.NamedComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.profile.ApplicationProfileManager;
import com.intellij.profile.Profile;
import com.intellij.profile.ProfileChangeAdapter;
import com.intellij.profile.ProfileEx;
import com.intellij.psi.search.scope.packageSet.NamedScope;
import com.intellij.util.containers.ContainerUtil;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * User: anna
 * Date: 29-Nov-2005
 */
public abstract class InspectionProfileManager extends ApplicationProfileManager implements SeverityProvider, NamedComponent {
  @NonNls protected static final String INSPECTION_DIR = "inspection";
  @NonNls protected static final String FILE_SPEC = "$ROOT_CONFIG$/" + INSPECTION_DIR;

  private final List<ProfileChangeAdapter> myProfileChangeAdapters = ContainerUtil.createLockFreeCopyOnWriteList();

  protected static final Logger LOG = Logger.getInstance("#com.intellij.profile.DefaultProfileManager");

  public static InspectionProfileManager getInstance() {
    return ServiceManager.getService(InspectionProfileManager.class);
  }

  public InspectionProfileManager() {
  }

  protected abstract void initProfiles();

  public abstract Profile loadProfile(@NotNull String path) throws IOException, JDOMException;

  @Override
  @NotNull
  public String getComponentName() {
    return "InspectionProfileManager";
  }

  @Override
  public void addProfileChangeListener(@NotNull final ProfileChangeAdapter listener) {
    myProfileChangeAdapters.add(listener);
  }

  @Override
  public void addProfileChangeListener(@NotNull ProfileChangeAdapter listener, @NotNull Disposable parentDisposable) {
    ContainerUtil.add(listener, myProfileChangeAdapters, parentDisposable);
  }

  @Override
  public void removeProfileChangeListener(@NotNull final ProfileChangeAdapter listener) {
    myProfileChangeAdapters.remove(listener);
  }

  @Override
  public void fireProfileChanged(final Profile profile) {
    if (profile instanceof ProfileEx) {
      ((ProfileEx)profile).profileChanged();
    }
    for (ProfileChangeAdapter adapter : myProfileChangeAdapters) {
      adapter.profileChanged(profile);
    }
  }

  @Override
  public void fireProfileChanged(final Profile oldProfile, final Profile profile, final NamedScope scope) {
    for (ProfileChangeAdapter adapter : myProfileChangeAdapters) {
      adapter.profileActivated(oldProfile, profile);
    }
  }

  @Nullable
  public static File getProfileDirectory() {
    String directoryPath = PathManager.getConfigPath() + File.separator + INSPECTION_DIR;
    File directory = new File(directoryPath);
    if (!directory.exists()) {
      if (!directory.mkdir()) {
        return null;
      }
    }
    return directory;
  }

  @Override
  public Profile getProfile(@NotNull final String name) {
    return getProfile(name, true);
  }
}