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
;
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
>>() {
35 protected Map
<Integer
, Icon
> create(Color key
) {
36 return new HashMap
<Integer
, Icon
>();
40 private ColorIconCache() {
43 public static ColorIconCache
getIconCache() {
47 @SuppressWarnings({"MethodMayBeStatic"})
48 public Icon
getIcon(@NotNull final Color color
, final int size
) {
49 Icon icon
= ourCache
.get(color
).get(size
);
51 icon
= new ColorIcon(size
, color
);
52 ourCache
.get(color
).put(size
, 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
) {
67 public ColorIcon(final int size
, final Color
[] colours
) {
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) {
78 g
.fillRect(i
, j
, iconWidth
, iconHeight
);
80 else if (myColours
!= null) {
81 final Color top
= myColours
[0];
83 g
.fillRect(i
, j
, iconWidth
, 2);
85 final Color right
= myColours
[1];
87 g
.fillRect(i
+ iconWidth
/ 2, j
+ 2, iconWidth
/ 2, iconHeight
/ 2);
89 final Color bottom
= myColours
[2];
91 g
.fillRect(i
, j
+ iconHeight
- 2, iconWidth
, 2);
93 final Color left
= myColours
[3];
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
);