From e68f79a45bcb80bf1a96b99ee600164954682487 Mon Sep 17 00:00:00 2001 From: anna Date: Thu, 7 Mar 2013 15:41:06 +0100 Subject: [PATCH] javafx: color completion/navigation/gutter (IDEA-101338) --- .../javaFX/fxml/JavaFXHighlightingTest.java | 4 ++ .../javaFX/fxml/JavaFxCompletionTest.java | 4 ++ .../javaFX/fxml/JavaFxCommonClassNames.java | 1 + .../fxml/refs/FxmlReferencesContributor.java | 3 + .../javaFX/fxml/refs/JavaFxAnnotator.java | 46 +++++-------- .../fxml/refs/JavaFxColorReference.java | 69 +++++++++++++++++++ .../refs/JavaFxColorReferenceProvider.java | 55 +++++++++++++++ .../testData/completion/namedColors.fxml | 5 ++ .../completion/namedColors_after.fxml | 5 ++ .../testData/highlighting/namedColor.fxml | 5 ++ 10 files changed, 169 insertions(+), 28 deletions(-) create mode 100644 plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxColorReference.java create mode 100644 plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxColorReferenceProvider.java create mode 100644 plugins/javaFX/testData/completion/namedColors.fxml create mode 100644 plugins/javaFX/testData/completion/namedColors_after.fxml create mode 100644 plugins/javaFX/testData/highlighting/namedColor.fxml diff --git a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXHighlightingTest.java b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXHighlightingTest.java index 3ec2a6385bb2..ce2b38cd128b 100644 --- a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXHighlightingTest.java +++ b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXHighlightingTest.java @@ -136,6 +136,10 @@ public class JavaFXHighlightingTest extends DaemonAnalyzerTestCase { doTestNavigation("MyController", "label", "injected/" + getTestName(true) + ".fxml", "injected/FooVBox.java", "injected/MyController.java"); } + public void testNamedColor() throws Exception { + doTestNavigation(JavaFxCommonClassNames.JAVAFX_SCENE_COLOR, "ORANGE"); + } + private void doTestNavigation(String resultClassName, String resultFieldName) throws Exception { doTestNavigation(resultClassName, resultFieldName, ArrayUtil.EMPTY_STRING_ARRAY); } diff --git a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxCompletionTest.java b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxCompletionTest.java index 296b93dd2ee7..199c3d2fa739 100644 --- a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxCompletionTest.java +++ b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxCompletionTest.java @@ -109,6 +109,10 @@ public class JavaFxCompletionTest extends CompletionTestCase { doTest("stylesheets"); } + public void testNamedColors() throws Exception { + doTest("blue"); + } + public void testReadOnly() throws Exception { configureByFile(getTestName(true) + ".fxml"); assertTrue(myItems.length > 0); diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxCommonClassNames.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxCommonClassNames.java index 51f104370393..5e03303fb3eb 100644 --- a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxCommonClassNames.java +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxCommonClassNames.java @@ -35,6 +35,7 @@ public class JavaFxCommonClassNames { @NonNls public static final String JAVAFX_EVENT_EVENT_HANDLER = "javafx.event.EventHandler"; @NonNls public static final String JAVAFX_SCENE_NODE = "javafx.scene.Node"; @NonNls public static final String JAVAFX_SCENE_PAINT = "javafx.scene.paint.Paint"; + @NonNls public static final String JAVAFX_SCENE_COLOR = "javafx.scene.paint.Color"; @NonNls public static final String JAVAFX_FXML_BUILDER = "javafx.util.Builder"; @NonNls public static final String JAVAFX_BEANS_OBSERVABLE = "javafx.beans.Observable"; @NonNls public static final String VALUE_OF = "valueOf"; diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/FxmlReferencesContributor.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/FxmlReferencesContributor.java index febd08f873ab..787a274c9230 100644 --- a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/FxmlReferencesContributor.java +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/FxmlReferencesContributor.java @@ -100,6 +100,9 @@ public class FxmlReferencesContributor extends PsiReferenceContributor { registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue().and(attributeValueInFxml), new EnumeratedAttributeReferenceProvider()); + registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue().and(attributeValueInFxml), + new JavaFxColorReferenceProvider()); + registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue() .withParent(XmlPatterns.xmlAttribute().withName(FxmlConstants.FX_VALUE) .withParent(XmlPatterns.xmlTag().withParent(XmlPatterns.xmlTag().withName(FxmlConstants.STYLESHEETS)))) diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxAnnotator.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxAnnotator.java index 9da83e9eee7a..2670e4d928e4 100644 --- a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxAnnotator.java +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxAnnotator.java @@ -28,19 +28,17 @@ import com.intellij.openapi.editor.markup.GutterIconRenderer; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; import com.intellij.psi.presentation.java.SymbolPresentationUtil; -import com.intellij.psi.util.InheritanceUtil; import com.intellij.psi.xml.XmlAttribute; import com.intellij.psi.xml.XmlAttributeValue; import com.intellij.ui.ColorUtil; import com.intellij.util.ArrayUtil; import com.intellij.util.ui.ColorIcon; -import com.intellij.xml.XmlAttributeDescriptor; +import com.intellij.xml.util.ColorSampleLookupValue; import org.jetbrains.annotations.NotNull; import org.jetbrains.plugins.javaFX.fxml.FxmlConstants; import org.jetbrains.plugins.javaFX.fxml.JavaFxCommonClassNames; import org.jetbrains.plugins.javaFX.fxml.JavaFxFileTypeFactory; import org.jetbrains.plugins.javaFX.fxml.JavaFxPsiUtil; -import org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyAttributeDescriptor; import javax.swing.*; import java.awt.*; @@ -70,11 +68,8 @@ public class JavaFxAnnotator implements Annotator { } } } - if (references.length == 0) { - final String attributeValueText = StringUtil.stripQuotesAroundValue(element.getText()); - if (attributeValueText.startsWith("#")) { - attachColorIcon(element, holder, attributeValueText); - } + if (references.length == 1 && references[0] instanceof JavaFxColorReference) { + attachColorIcon(element, holder, StringUtil.stripQuotesAroundValue(element.getText())); } } else if (element instanceof XmlAttribute) { final String attributeName = ((XmlAttribute)element).getName(); @@ -87,28 +82,23 @@ public class JavaFxAnnotator implements Annotator { } private static void attachColorIcon(final PsiElement element, AnnotationHolder holder, String attributeValueText) { - final PsiElement parent = element.getParent(); - if (parent instanceof XmlAttribute) { - final XmlAttributeDescriptor descriptor = ((XmlAttribute)parent).getDescriptor(); - if (descriptor instanceof JavaFxPropertyAttributeDescriptor) { - final PsiElement declaration = descriptor.getDeclaration(); - if (declaration instanceof PsiField) { - final PsiField field = (PsiField)declaration; - final PsiClassType propertyClassType = JavaFxPsiUtil.getPropertyClassType(field); - if (propertyClassType != null && InheritanceUtil.isInheritor(propertyClassType, JavaFxCommonClassNames.JAVAFX_SCENE_PAINT)) { - try { - final Color color = ColorUtil.fromHex(attributeValueText.substring(1)); - if (color != null) { - final ColorIcon icon = new ColorIcon(8, color); - final Annotation annotation = holder.createInfoAnnotation(element, null); - annotation.setGutterIconRenderer(new ColorIconRenderer(icon, element)); - } - } - catch (Exception ignored) { - } - } + try { + Color color = null; + if (attributeValueText.startsWith("#")) { + color = ColorUtil.fromHex(attributeValueText.substring(1)); + } else { + final String hexCode = ColorSampleLookupValue.getHexCodeForColorName(StringUtil.toLowerCase(attributeValueText)); + if (hexCode != null) { + color = ColorUtil.fromHex(hexCode); } } + if (color != null) { + final ColorIcon icon = new ColorIcon(8, color); + final Annotation annotation = holder.createInfoAnnotation(element, null); + annotation.setGutterIconRenderer(new ColorIconRenderer(icon, element)); + } + } + catch (Exception ignored) { } } diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxColorReference.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxColorReference.java new file mode 100644 index 000000000000..e3ac22476a13 --- /dev/null +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxColorReference.java @@ -0,0 +1,69 @@ +/* + * Copyright 2000-2013 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 org.jetbrains.plugins.javaFX.fxml.refs; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.JavaPsiFacade; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiReferenceBase; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.psi.xml.XmlAttributeValue; +import com.intellij.xml.util.ColorSampleLookupValue; +import com.intellij.xml.util.UserColorLookup; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.plugins.javaFX.fxml.JavaFxCommonClassNames; + +/** + * User: anna + * Date: 3/7/13 + */ +public class JavaFxColorReference extends PsiReferenceBase { + public JavaFxColorReference(XmlAttributeValue value) { + super(value); + } + + @Nullable + @Override + public PsiElement resolve() { + final Project project = getElement().getProject(); + final PsiClass psiClass = + JavaPsiFacade.getInstance(project).findClass(JavaFxCommonClassNames.JAVAFX_SCENE_COLOR, GlobalSearchScope.allScope(project)); + if (psiClass != null) { + return psiClass.findFieldByName(getCanonicalText().toUpperCase(), false); + } + return null; + } + + @NotNull + @Override + public Object[] getVariants() { + final ColorSampleLookupValue[] lookupValues = ColorSampleLookupValue.getColors(); + final Object[] vars = new Object[lookupValues.length + 1]; + for (int i = 0; i < lookupValues.length; i++) { + final ColorSampleLookupValue value = lookupValues[i]; + vars[i] = new ColorSampleLookupValue(value.getName(), value.getValue(), true); + } + vars[lookupValues.length] = new UserColorLookup(); + return vars; + } + + @Override + public boolean isSoft() { + return true; + } +} diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxColorReferenceProvider.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxColorReferenceProvider.java new file mode 100644 index 000000000000..d53f8752ec90 --- /dev/null +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxColorReferenceProvider.java @@ -0,0 +1,55 @@ +/* + * Copyright 2000-2013 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 org.jetbrains.plugins.javaFX.fxml.refs; + +import com.intellij.psi.*; +import com.intellij.psi.util.InheritanceUtil; +import com.intellij.psi.xml.XmlAttribute; +import com.intellij.psi.xml.XmlAttributeValue; +import com.intellij.util.ProcessingContext; +import com.intellij.xml.XmlAttributeDescriptor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.javaFX.fxml.JavaFxCommonClassNames; +import org.jetbrains.plugins.javaFX.fxml.JavaFxPsiUtil; +import org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyAttributeDescriptor; + +/** +* User: anna +* Date: 3/7/13 +*/ +class JavaFxColorReferenceProvider extends PsiReferenceProvider { + @NotNull + @Override + public PsiReference[] getReferencesByElement(@NotNull PsiElement element, + @NotNull ProcessingContext context) { + final XmlAttributeValue attributeValue = (XmlAttributeValue)element; + final PsiElement parent = attributeValue.getParent(); + if (parent instanceof XmlAttribute) { + final XmlAttributeDescriptor descriptor = ((XmlAttribute)parent).getDescriptor(); + if (descriptor instanceof JavaFxPropertyAttributeDescriptor) { + final PsiElement declaration = descriptor.getDeclaration(); + if (declaration instanceof PsiField) { + final PsiField field = (PsiField)declaration; + final PsiClassType propertyClassType = JavaFxPsiUtil.getPropertyClassType(field); + if (propertyClassType != null && InheritanceUtil.isInheritor(propertyClassType, JavaFxCommonClassNames.JAVAFX_SCENE_PAINT)) { + return new PsiReference[] {new JavaFxColorReference(attributeValue)}; + } + } + } + } + return PsiReference.EMPTY_ARRAY; + } +} diff --git a/plugins/javaFX/testData/completion/namedColors.fxml b/plugins/javaFX/testData/completion/namedColors.fxml new file mode 100644 index 000000000000..dda432697fcb --- /dev/null +++ b/plugins/javaFX/testData/completion/namedColors.fxml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/plugins/javaFX/testData/completion/namedColors_after.fxml b/plugins/javaFX/testData/completion/namedColors_after.fxml new file mode 100644 index 000000000000..e1b752eca61d --- /dev/null +++ b/plugins/javaFX/testData/completion/namedColors_after.fxml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/plugins/javaFX/testData/highlighting/namedColor.fxml b/plugins/javaFX/testData/highlighting/namedColor.fxml new file mode 100644 index 000000000000..7322af9e0fb9 --- /dev/null +++ b/plugins/javaFX/testData/highlighting/namedColor.fxml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file