From 0c7b25a8d7301f9c2c59618bdab399ffee6826e2 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Wed, 15 Aug 2012 13:17:24 +0400 Subject: [PATCH] WI-10396 Duplicate color values in css editor --- .../lookup/LookupValueWithUIHint.java | 4 ++ .../xml/util/ColorSampleLookupValue.java | 54 ++++++++++++++++--- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/LookupValueWithUIHint.java b/platform/lang-impl/src/com/intellij/codeInsight/lookup/LookupValueWithUIHint.java index 883067a81c4b..1961926272b2 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/LookupValueWithUIHint.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/LookupValueWithUIHint.java @@ -16,6 +16,8 @@ package com.intellij.codeInsight.lookup; +import org.jetbrains.annotations.Nullable; + import java.awt.*; /** @@ -23,8 +25,10 @@ import java.awt.*; * @deprecated use InsertHandler */ public interface LookupValueWithUIHint extends PresentableLookupValue { + @Nullable String getTypeHint(); + @Nullable Color getColorHint(); boolean isBold(); diff --git a/xml/impl/src/com/intellij/xml/util/ColorSampleLookupValue.java b/xml/impl/src/com/intellij/xml/util/ColorSampleLookupValue.java index 837ad67b2e84..2b129714da06 100644 --- a/xml/impl/src/com/intellij/xml/util/ColorSampleLookupValue.java +++ b/xml/impl/src/com/intellij/xml/util/ColorSampleLookupValue.java @@ -26,6 +26,7 @@ import com.intellij.psi.PsiElement; import com.intellij.xml.XmlBundle; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; @@ -36,7 +37,7 @@ import java.util.List; * @author maxim */ public class ColorSampleLookupValue implements LookupValueWithUIHint, DeferredUserLookupValue, Iconable, LookupValueWithPriority { - private static ColorSampleLookupValue[] ourColors; + private static volatile ColorSampleLookupValue[] ourColors; private static Map ourColorNameToHexCodeMap; private static Map ourHexCodeToColorNameMap; @@ -378,11 +379,12 @@ public class ColorSampleLookupValue implements LookupValueWithUIHint, DeferredUs return ourColors; } + @Nullable public String getTypeHint() { - return myName != null && myValue.charAt(0) == '#' ? myValue : null; + return myValue != null && myValue.charAt(0) == '#' ? myValue : null; } - @SuppressWarnings({"HardCodedStringLiteral"}) + @Nullable public Color getColorHint() { return null; } @@ -413,12 +415,12 @@ public class ColorSampleLookupValue implements LookupValueWithUIHint, DeferredUs final Color colorFromElement = UserColorLookup.getColorFromElement(currentElement); if (colorFromElement != null) { - addColorPreviewAndCodeToLookup(colorFromElement, currentElement.getText(), buf); + addColorPreviewAndCodeToLookup(colorFromElement, buf); } } private static String toHex(@NotNull final Color color) { - final StringBuffer sb = new StringBuffer(); + final StringBuilder sb = new StringBuilder(); for (int i = 0; i < 3; i++) { String s = Integer.toHexString(i == 0 ? color.getRed() : i == 1 ? color.getGreen() : color.getBlue()); if (s.length() < 2) { @@ -431,7 +433,7 @@ public class ColorSampleLookupValue implements LookupValueWithUIHint, DeferredUs return sb.toString(); } - public static void addColorPreviewAndCodeToLookup(final Color color, final String value, final StringBuilder buf) { + public static void addColorPreviewAndCodeToLookup(final Color color, final StringBuilder buf) { if (color == null) return; final String code = '#' + toHex(color); final String colorName = getColorNameForHexCode(code); @@ -462,7 +464,9 @@ public class ColorSampleLookupValue implements LookupValueWithUIHint, DeferredUs try { return Color.decode("0x" + hexValue2); } - catch (NumberFormatException e) {} + catch (NumberFormatException e) { + //ignore + } } return null; } @@ -471,4 +475,40 @@ public class ColorSampleLookupValue implements LookupValueWithUIHint, DeferredUs public String toString() { return myName == null ? myValue : myValue + " " + myName; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof ColorSampleLookupValue)) { + return false; + } + + ColorSampleLookupValue value = (ColorSampleLookupValue)o; + + if (myIsStandard != value.myIsStandard) { + return false; + } + if (myColor != null ? !myColor.equals(value.myColor) : value.myColor != null) { + return false; + } + if (myName != null ? !myName.equals(value.myName) : value.myName != null) { + return false; + } + if (myValue != null ? !myValue.equals(value.myValue) : value.myValue != null) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + int result = (myIsStandard ? 1 : 0); + result = 31 * result + (myName != null ? myName.hashCode() : 0); + result = 31 * result + (myValue != null ? myValue.hashCode() : 0); + result = 31 * result + (myColor != null ? myColor.hashCode() : 0); + return result; + } }