color completion popup restyling
[fedora-idea.git] / xml / impl / src / com / intellij / xml / util / ColorIconCache.java
blob15b31716e5cade8fc5d9e27d52b4d7ae2a4f8f68
1 /*
2 * Copyright 2000-2010 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.xml.util;
18 import com.intellij.util.containers.HashMap;
19 import com.intellij.util.containers.SoftFactoryMap;
20 import com.intellij.util.containers.WeakFactoryMap;
21 import com.intellij.util.ui.EmptyIcon;
22 import org.jetbrains.annotations.NotNull;
24 import javax.swing.*;
25 import java.awt.*;
26 import java.util.Map;
28 /**
29 * @author spleaner
31 public class ColorIconCache {
32 private static final ColorIconCache INSTANCE = new ColorIconCache();
33 private static final SoftFactoryMap<Color, Map<Integer, Icon>> ourCache = new SoftFactoryMap<Color, Map<Integer, Icon>>() {
34 @Override
35 protected Map<Integer, Icon> create(Color key) {
36 return new HashMap<Integer, Icon>();
40 private ColorIconCache() {
43 public static ColorIconCache getIconCache() {
44 return INSTANCE;
47 @SuppressWarnings({"MethodMayBeStatic"})
48 public Icon getIcon(@NotNull final Color color, final int size) {
49 Icon icon = ourCache.get(color).get(size);
50 if (icon == null) {
51 icon = new ColorIcon(size, color);
52 ourCache.get(color).put(size, icon);
55 return icon;
58 public static class ColorIcon extends EmptyIcon {
59 private Color myColor;
60 private Color[] myColours;
62 public ColorIcon(final int size, final Color color) {
63 super(size);
64 myColor = color;
67 public ColorIcon(final int size, final Color[] colours) {
68 super(size);
69 myColours = colours;
72 @Override
73 public void paintIcon(final Component component, final Graphics g, final int i, final int j) {
74 final int iconWidth = getIconWidth();
75 final int iconHeight = getIconHeight();
76 if (myColor != null) {
77 g.setColor(myColor);
78 g.fillRect(i, j, iconWidth, iconHeight);
80 else if (myColours != null) {
81 final Color top = myColours[0];
82 g.setColor(top);
83 g.fillRect(i, j, iconWidth, 2);
85 final Color right = myColours[1];
86 g.setColor(right);
87 g.fillRect(i + iconWidth / 2, j + 2, iconWidth / 2, iconHeight / 2);
89 final Color bottom = myColours[2];
90 g.setColor(bottom);
91 g.fillRect(i, j + iconHeight - 2, iconWidth, 2);
93 final Color left = myColours[3];
94 g.setColor(left);
95 g.fillRect(i, j + 2, iconWidth / 2, iconHeight / 2);
98 final Composite old = ((Graphics2D)g).getComposite();
99 ((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
100 g.setColor(Color.BLACK);
101 g.drawRect(i, j, iconWidth-1, iconHeight-1);
102 ((Graphics2D)g).setComposite(old);