diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaColorProvider.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaColorProvider.java new file mode 100644 index 000000000000..84c1d1445993 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaColorProvider.java @@ -0,0 +1,69 @@ +/* + * Copyright 2000-2012 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.codeInsight.daemon.impl; + +import com.intellij.lang.java.JavaLanguage; +import com.intellij.openapi.editor.ElementColorProvider; +import com.intellij.psi.*; +import com.intellij.psi.impl.JavaConstantExpressionEvaluator; +import com.intellij.psi.util.PsiTypesUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.awt.*; + +/** + * @author Konstantin Bulenkov + */ +public class JavaColorProvider implements ElementColorProvider { + @Override + public Color getColorFrom(@NotNull PsiElement element) { + if (element.getLanguage() == JavaLanguage.INSTANCE) { + if (element instanceof PsiNewExpression) { + final PsiNewExpression expr = (PsiNewExpression)element; + final PsiType type = expr.getType(); + if (type != null) { + final PsiClass aClass = PsiTypesUtil.getPsiClass(type); + if (aClass != null) { + if ("java.awt.Color".equals(aClass.getQualifiedName())) { + return getColor(expr.getArgumentList()); + } + } + } + } + } + return null; + } + + @Nullable + private static Color getColor(PsiExpressionList list) { + try { + final PsiExpression[] args = list.getExpressions(); + final PsiType[] types = list.getExpressionTypes(); + if (types.length == 1 && types[0].equals(PsiType.INT)) { + final Object num = JavaConstantExpressionEvaluator.computeConstantExpression(args[0], true); + if (num instanceof Integer) { + return new Color(((Integer)num).intValue()); + } + } + } catch (Exception ignore) {} + return null; + } + + @Override + public void setColorTo(@NotNull PsiElement element, @NotNull Color color) { + } +} diff --git a/platform/lang-api/src/com/intellij/openapi/editor/ElementColorProvider.java b/platform/lang-api/src/com/intellij/openapi/editor/ElementColorProvider.java new file mode 100644 index 000000000000..5221df9b6e53 --- /dev/null +++ b/platform/lang-api/src/com/intellij/openapi/editor/ElementColorProvider.java @@ -0,0 +1,35 @@ +/* + * Copyright 2000-2012 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.openapi.editor; + +import com.intellij.openapi.extensions.ExtensionPointName; +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.awt.*; + +/** + * @author Konstantin Bulenkov + */ +public interface ElementColorProvider { + ExtensionPointName EP_NAME = new ExtensionPointName("com.intellij.colorProvider"); + + @Nullable + Color getColorFrom(@NotNull PsiElement element); + + void setColorTo(@NotNull PsiElement element, @NotNull Color color); +} diff --git a/platform/lang-impl/src/com/intellij/ui/ColorLineMarkerProvider.java b/platform/lang-impl/src/com/intellij/ui/ColorLineMarkerProvider.java new file mode 100644 index 000000000000..2cdb11a0f9a1 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/ui/ColorLineMarkerProvider.java @@ -0,0 +1,77 @@ +/* + * Copyright 2000-2012 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.ui; + +import com.intellij.codeHighlighting.Pass; +import com.intellij.codeInsight.daemon.GutterIconNavigationHandler; +import com.intellij.codeInsight.daemon.LineMarkerInfo; +import com.intellij.codeInsight.daemon.LineMarkerProvider; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.ElementColorProvider; +import com.intellij.openapi.editor.markup.GutterIconRenderer; +import com.intellij.psi.PsiElement; +import com.intellij.psi.util.PsiUtilBase; +import com.intellij.util.FunctionUtil; +import com.intellij.util.ui.ColorIcon; +import org.jetbrains.annotations.NotNull; + +import java.awt.*; +import java.awt.event.MouseEvent; +import java.util.Collection; +import java.util.List; + +/** + * @author Konstantin Bulenkov + */ +public class ColorLineMarkerProvider implements LineMarkerProvider { + @Override + public LineMarkerInfo getLineMarkerInfo(PsiElement element) { + for (ElementColorProvider colorProvider : ElementColorProvider.EP_NAME.getExtensions()) { + final Color color = colorProvider.getColorFrom(element); + if (color != null) { + return new MyInfo(element, color, colorProvider); + } + } + return null; + } + + @Override + public void collectSlowLineMarkers(List elements, Collection result) { + } + + private static class MyInfo extends LineMarkerInfo { + + public MyInfo(@NotNull final PsiElement element, final Color color, final ElementColorProvider colorProvider) { + super(element, + element.getTextRange(), + new ColorIcon(12, color), + Pass.UPDATE_ALL, + FunctionUtil.nullConstant(), + new GutterIconNavigationHandler() { + @Override + public void navigate(MouseEvent e, PsiElement elt) { + final Editor editor = PsiUtilBase.findEditor(element); + assert editor != null; + final Color c = ColorChooser.chooseColor(editor.getComponent(), "Choose color", color, true); + if (c != null) { + colorProvider.setColorTo(element, c); + } + } + }, + GutterIconRenderer.Alignment.RIGHT); + } + } +} diff --git a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml index f6b862a34698..9f001e7a2620 100644 --- a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml +++ b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml @@ -211,6 +211,8 @@ interface="com.intellij.ide.fileTemplates.FileTemplateGroupDescriptorFactory"/> + + +