update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInsight / daemon / impl / SeverityRegistrar.java
blob3122731089864ba59c098fbd6f85db180c0c1b46
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.codeInsight.daemon.impl;
19 import com.intellij.codeHighlighting.HighlightDisplayLevel;
20 import com.intellij.lang.annotation.HighlightSeverity;
21 import com.intellij.openapi.editor.colors.EditorColorsManager;
22 import com.intellij.openapi.editor.colors.EditorColorsScheme;
23 import com.intellij.openapi.editor.colors.TextAttributesKey;
24 import com.intellij.openapi.editor.markup.TextAttributes;
25 import com.intellij.openapi.extensions.Extensions;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.util.InvalidDataException;
28 import com.intellij.openapi.util.JDOMExternalizable;
29 import com.intellij.openapi.util.JDOMExternalizableStringList;
30 import com.intellij.openapi.util.WriteExternalException;
31 import com.intellij.profile.codeInspection.InspectionProfileManager;
32 import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
33 import gnu.trove.THashMap;
34 import org.jdom.Element;
35 import org.jetbrains.annotations.NonNls;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
39 import javax.swing.*;
40 import java.awt.*;
41 import java.util.*;
42 import java.util.List;
44 /**
45 * User: anna
46 * Date: 24-Feb-2006
48 public class SeverityRegistrar implements JDOMExternalizable, Comparator<HighlightSeverity> {
49 @NonNls private static final String INFO = "info";
50 private final Map<String, SeverityBasedTextAttributes> ourMap = new THashMap<String, SeverityBasedTextAttributes>();
51 private final Map<String, Color> ourRendererColors = new THashMap<String, Color>();
52 @NonNls private static final String COLOR = "color";
54 private final JDOMExternalizableStringList myOrder = new JDOMExternalizableStringList();
55 private JDOMExternalizableStringList myReadOrder;
57 private static final Map<String, HighlightInfoType> STANDART_SEVERITIES = new HashMap<String, HighlightInfoType>();
59 static {
60 STANDART_SEVERITIES.put(HighlightSeverity.ERROR.toString(), HighlightInfoType.ERROR);
61 STANDART_SEVERITIES.put(HighlightSeverity.WARNING.toString(), HighlightInfoType.WARNING);
62 STANDART_SEVERITIES.put(HighlightSeverity.INFO.toString(), HighlightInfoType.INFO);
63 STANDART_SEVERITIES.put(HighlightSeverity.GENERIC_SERVER_ERROR_OR_WARNING.toString(), HighlightInfoType.GENERIC_WARNINGS_OR_ERRORS_FROM_SERVER);
64 final EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
65 for (SeveritiesProvider provider : Extensions.getExtensions(SeveritiesProvider.EP_NAME)) {
66 for (HighlightInfoType highlightInfoType : provider.getSeveritiesHighlightInfoTypes()) {
67 final HighlightSeverity highlightSeverity = highlightInfoType.getSeverity(null);
68 STANDART_SEVERITIES.put(highlightSeverity.toString(), highlightInfoType);
69 final TextAttributesKey attributesKey = highlightInfoType.getAttributesKey();
70 TextAttributes textAttributes = scheme.getAttributes(attributesKey);
71 if (textAttributes == null) {
72 textAttributes = attributesKey.getDefaultAttributes();
74 HighlightDisplayLevel.registerSeverity(highlightSeverity, provider.getTrafficRendererColor(textAttributes));
79 public static SeverityRegistrar getInstance() {
80 return InspectionProfileManager.getInstance().getSeverityRegistrar();
83 public static SeverityRegistrar getInstance(@Nullable Project project) {
84 return project != null ? InspectionProjectProfileManager.getInstance(project).getSeverityRegistrar() : getInstance();
87 public void registerSeverity(SeverityBasedTextAttributes info, Color renderColor){
88 final HighlightSeverity severity = info.getType().getSeverity(null);
89 ourMap.put(severity.toString(), info);
90 ourRendererColors.put(severity.toString(), renderColor);
91 myOrder.clear();
92 HighlightDisplayLevel.registerSeverity(severity, renderColor);
95 public Collection<SeverityBasedTextAttributes> getRegisteredHighlightingInfoTypes() {
96 final Collection<SeverityBasedTextAttributes> collection = new ArrayList<SeverityBasedTextAttributes>(ourMap.values());
97 for (HighlightInfoType type : STANDART_SEVERITIES.values()) {
98 collection.add(getSeverityBasedTextAttributes(type));
100 return collection;
103 private SeverityBasedTextAttributes getSeverityBasedTextAttributes(HighlightInfoType type) {
104 final EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
105 final TextAttributes textAttributes = scheme.getAttributes(type.getAttributesKey());
106 if (textAttributes != null) {
107 return new SeverityBasedTextAttributes(textAttributes, (HighlightInfoType.HighlightInfoTypeImpl)type);
109 return new SeverityBasedTextAttributes(getTextAttributesBySeverity(type.getSeverity(null)), (HighlightInfoType.HighlightInfoTypeImpl)type);
113 public SeverityBasedTextAttributes unregisterSeverity(HighlightSeverity severity){
114 return ourMap.remove(severity.toString());
117 public HighlightInfoType.HighlightInfoTypeImpl getHighlightInfoTypeBySeverity(HighlightSeverity severity) {
118 HighlightInfoType infoType = STANDART_SEVERITIES.get(severity.toString());
119 if (infoType != null) {
120 return (HighlightInfoType.HighlightInfoTypeImpl)infoType;
123 if (severity == HighlightSeverity.INFORMATION){
124 return (HighlightInfoType.HighlightInfoTypeImpl)HighlightInfoType.INFORMATION;
127 final SeverityBasedTextAttributes type = ourMap.get(severity.toString());
128 return (HighlightInfoType.HighlightInfoTypeImpl)(type != null ? type.getType() : HighlightInfoType.WARNING);
131 @Nullable
132 public TextAttributes getTextAttributesBySeverity(HighlightSeverity severity) {
133 final SeverityBasedTextAttributes infoType = ourMap.get(severity.toString());
134 if (infoType != null) {
135 return infoType.getAttributes();
137 return null;
141 public void readExternal(Element element) throws InvalidDataException {
142 ourMap.clear();
143 ourRendererColors.clear();
144 final List children = element.getChildren(INFO);
145 if (children != null){
146 for (Object child : children) {
147 final Element infoElement = (Element)child;
149 final SeverityBasedTextAttributes highlightInfo = new SeverityBasedTextAttributes();
150 highlightInfo.readExternal(infoElement);
152 Color color = null;
153 final String colorStr = infoElement.getAttributeValue(COLOR);
154 if (colorStr != null){
155 color = new Color(Integer.parseInt(colorStr, 16));
157 registerSeverity(highlightInfo, color);
160 myOrder.clear();
161 myOrder.readExternal(element);
163 myReadOrder = new JDOMExternalizableStringList();
164 myReadOrder.addAll(myOrder);
166 final List<String> knownSeverities = createCurrentSeverities();
167 myOrder.retainAll(knownSeverities);
169 if (myOrder.isEmpty()) {
170 myOrder.addAll(getDefaultOrder());
172 //enforce include all known
173 for (int i = 0; i < knownSeverities.size(); i++) {
174 String stdSeverity = knownSeverities.get(i);
175 if (!myOrder.contains(stdSeverity)) {
176 for (int oIdx = 0; oIdx < myOrder.size(); oIdx++) {
177 final String orderSeverity = myOrder.get(oIdx);
178 final HighlightInfoType type = STANDART_SEVERITIES.get(orderSeverity);
179 if (type != null && knownSeverities.indexOf(type.getSeverity(null).toString()) > i) {
180 myOrder.add(oIdx, stdSeverity);
181 myReadOrder = null;
182 break;
189 public void writeExternal(Element element) throws WriteExternalException {
190 for (String severity : getOrder()) {
191 Element info = new Element(INFO);
192 final SeverityBasedTextAttributes infoType = ourMap.get(severity);
193 if (infoType != null) {
194 infoType.writeExternal(info);
195 final Color color = ourRendererColors.get(severity);
196 if (color != null) {
197 info.setAttribute(COLOR, Integer.toString(color.getRGB() & 0xFFFFFF, 16));
199 element.addContent(info);
202 if (myReadOrder != null && !myReadOrder.isEmpty()) {
203 myReadOrder.writeExternal(element);
204 } else {
205 if (!getDefaultOrder().equals(getOrder())) {
206 getOrder().writeExternal(element);
211 public int getSeveritiesCount() {
212 return createCurrentSeverities().size();
215 public HighlightSeverity getSeverityByIndex(final int i) {
216 return getSeverity(getOrder().get(i));
219 public HighlightSeverity getSeverity(final String name) {
220 final HighlightInfoType type = STANDART_SEVERITIES.get(name);
221 if (type != null) return type.getSeverity(null);
222 final SeverityBasedTextAttributes attributes = ourMap.get(name);
223 if (attributes != null) return attributes.getSeverity();
224 return null;
227 private List<String> createCurrentSeverities() {
228 List<String> list = new ArrayList<String>();
229 list.addAll(STANDART_SEVERITIES.keySet());
230 list.addAll(ourMap.keySet());
231 Collections.sort(list);
232 return list;
235 public Icon getRendererIconByIndex(final int i) {
236 final HighlightSeverity severity = getSeverityByIndex(i);
237 HighlightDisplayLevel level = HighlightDisplayLevel.find(severity);
238 if (level != null) {
239 return level.getIcon();
242 return HighlightDisplayLevel.createIconByMask(ourRendererColors.get(severity.toString()));
245 public boolean isSeverityValid(final String severity) {
246 return createCurrentSeverities().contains(severity);
249 public int compare(final HighlightSeverity s1, final HighlightSeverity s2) {
250 return getOrder().indexOf(s1.myName) - getOrder().indexOf(s2.myName);
253 private JDOMExternalizableStringList getOrder() {
254 if (myOrder.isEmpty()) {
255 myOrder.addAll(getDefaultOrder());
257 return myOrder;
260 private List<String> getDefaultOrder() {
261 final List<HighlightSeverity> order = new ArrayList<HighlightSeverity>();
262 for (HighlightInfoType type : STANDART_SEVERITIES.values()) {
263 order.add(type.getSeverity(null));
265 for (SeverityBasedTextAttributes attributes : ourMap.values()) {
266 order.add(attributes.getSeverity());
268 Collections.sort(order);
269 final List<String> result = new ArrayList<String>();
270 for (HighlightSeverity severity : order) {
271 result.add(severity.toString());
273 return result;
276 public void setOrder(List<String> order) {
277 myOrder.clear();
278 myOrder.addAll(order);
280 myReadOrder = null;
283 public int getSeverityIdx(@NotNull HighlightSeverity severity) {
284 return getOrder().indexOf(severity.toString());
287 public boolean isDefaultSeverity(HighlightSeverity severity) {
288 return STANDART_SEVERITIES.containsKey(severity.myName);
291 public static boolean skipSeverity(HighlightSeverity minSeverity) {
292 for (SeveritiesProvider provider : Extensions.getExtensions(SeveritiesProvider.EP_NAME)) {
293 if (!provider.isGotoBySeverityEnabled(minSeverity)) return true;
295 return minSeverity == HighlightSeverity.INFORMATION;
298 public static class SeverityBasedTextAttributes implements JDOMExternalizable {
299 private final TextAttributes myAttributes;
300 private final HighlightInfoType.HighlightInfoTypeImpl myType;
302 //readexternal
303 public SeverityBasedTextAttributes() {
304 myAttributes = new TextAttributes();
305 myType = new HighlightInfoType.HighlightInfoTypeImpl();
308 public SeverityBasedTextAttributes(final TextAttributes attributes, final HighlightInfoType.HighlightInfoTypeImpl type) {
309 myAttributes = attributes;
310 myType = type;
313 public TextAttributes getAttributes() {
314 return myAttributes;
317 public HighlightInfoType.HighlightInfoTypeImpl getType() {
318 return myType;
321 public void readExternal(Element element) throws InvalidDataException {
322 myAttributes.readExternal(element);
323 myType.readExternal(element);
326 public void writeExternal(Element element) throws WriteExternalException {
327 myAttributes.writeExternal(element);
328 myType.writeExternal(element);
331 public HighlightSeverity getSeverity() {
332 return myType.getSeverity(null);
335 public boolean equals(final Object o) {
336 if (this == o) return true;
337 if (o == null || getClass() != o.getClass()) return false;
339 final SeverityBasedTextAttributes that = (SeverityBasedTextAttributes)o;
341 if (myAttributes != null ? !myAttributes.equals(that.myAttributes) : that.myAttributes != null) return false;
342 if (myType != null ? !myType.equals(that.myType) : that.myType != null) return false;
344 return true;
347 public int hashCode() {
348 int result;
349 result = (myAttributes != null ? myAttributes.hashCode() : 0);
350 result = 31 * result + (myType != null ? myType.hashCode() : 0);
351 return result;