From 2f9b7cda57d2f4a5921e522219afec3fca269462 Mon Sep 17 00:00:00 2001 From: anna Date: Thu, 17 Jan 2013 21:54:46 +0100 Subject: [PATCH] javafx: completion of static properties --- .../JavaFxClassBackedElementDescriptor.java | 18 ++- .../fxml/JavaFxSetterAttributeDescriptor.java | 111 ++++++++++++++++++ .../testData/completion/staticProperties.fxml | 9 ++ .../completion/staticProperties_after.fxml | 9 ++ .../javaFX/fxml/JavaFxCompletionTest.java | 24 +++- 5 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxSetterAttributeDescriptor.java create mode 100644 plugins/javaFX/testData/completion/staticProperties.fxml create mode 100644 plugins/javaFX/testData/completion/staticProperties_after.fxml diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxClassBackedElementDescriptor.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxClassBackedElementDescriptor.java index a1cdf0be2a43..df871f09708b 100644 --- a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxClassBackedElementDescriptor.java +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxClassBackedElementDescriptor.java @@ -110,6 +110,7 @@ public class JavaFxClassBackedElementDescriptor implements XmlElementDescriptor, @Override public XmlAttributeDescriptor[] getAttributesDescriptors(@Nullable XmlTag context) { + //todo filter if (context != null) { final String name = context.getName(); if (Comparing.equal(name, getName()) && myPsiClass != null) { @@ -120,11 +121,26 @@ public class JavaFxClassBackedElementDescriptor implements XmlElementDescriptor, if (field.hasModifierProperty(PsiModifier.STATIC)) continue; final PsiType fieldType = field.getType(); if (PropertyUtil.findPropertyGetter(myPsiClass, field.getName(), false, true) != null && - InheritanceUtil.isInheritor(fieldType, JavaFxCommonClassNames.JAVAFX_BEANS_PROPERTY_PROPERTY)) {//todo filter + InheritanceUtil.isInheritor(fieldType, JavaFxCommonClassNames.JAVAFX_BEANS_PROPERTY_PROPERTY)) { simpleAttrs.add(new JavaFxPropertyAttributeDescriptor(field.getName(), myPsiClass)); } } } + XmlTag tag = context.getParentTag(); + while (tag != null) { + final XmlElementDescriptor descriptor = tag.getDescriptor(); + if (descriptor instanceof JavaFxClassBackedElementDescriptor) { + final PsiElement element = descriptor.getDeclaration(); + if (element instanceof PsiClass) { + for (PsiMethod method : ((PsiClass)element).getMethods()) { + if (method.hasModifierProperty(PsiModifier.STATIC) && method.getName().startsWith("set")) { + simpleAttrs.add(new JavaFxSetterAttributeDescriptor(method.getName(), (PsiClass)element)); + } + } + } + } + tag = tag.getParentTag(); + } return simpleAttrs.isEmpty() ? XmlAttributeDescriptor.EMPTY : simpleAttrs.toArray(new XmlAttributeDescriptor[simpleAttrs.size()]); } } diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxSetterAttributeDescriptor.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxSetterAttributeDescriptor.java new file mode 100644 index 000000000000..02f85017626a --- /dev/null +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/JavaFxSetterAttributeDescriptor.java @@ -0,0 +1,111 @@ +/* + * 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; + +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.xml.XmlElement; +import com.intellij.util.ArrayUtil; +import com.intellij.xml.XmlAttributeDescriptor; +import org.jetbrains.annotations.Nullable; + +/** + * User: anna + * Date: 1/10/13 + */ +public class JavaFxSetterAttributeDescriptor implements XmlAttributeDescriptor { + private final String myName; + private final PsiClass myPsiClass; + + public JavaFxSetterAttributeDescriptor(String name, PsiClass psiClass) { + myName = name; + myPsiClass = psiClass; + } + + @Override + public boolean isRequired() { + return false; + } + + @Override + public boolean isFixed() { + return false; + } + + @Override + public boolean hasIdType() { + return false; + } + + @Override + public boolean hasIdRefType() { + return false; + } + + @Nullable + @Override + public String getDefaultValue() { + return null; + } + + @Override + public boolean isEnumerated() { + return false; + } + + @Nullable + @Override + public String[] getEnumeratedValues() { + return null; + } + + @Nullable + @Override + public String validateValue(XmlElement context, String value) { + return null; + } + + @Override + public PsiElement getDeclaration() { + if (myPsiClass != null) { + final PsiMethod[] methods = myPsiClass.findMethodsByName(myName, false); + if (methods.length == 1) { //todo + return methods[0]; + } + } + return null; + } + + @Override + public String getName(PsiElement context) { + return getName(); + } + + @Override + public String getName() { + return myPsiClass.getName() + "." + StringUtil.decapitalize(myName.substring("set".length())); + } + + @Override + public void init(PsiElement element) {} + + @Override + public Object[] getDependences() { + return ArrayUtil.EMPTY_OBJECT_ARRAY; + } +} diff --git a/plugins/javaFX/testData/completion/staticProperties.fxml b/plugins/javaFX/testData/completion/staticProperties.fxml new file mode 100644 index 000000000000..44cbdab0cd76 --- /dev/null +++ b/plugins/javaFX/testData/completion/staticProperties.fxml @@ -0,0 +1,9 @@ + + + + + + + + GridPane.rowIndex="4"/> + \ No newline at end of file diff --git a/plugins/javaFX/testData/completion/staticProperties_after.fxml b/plugins/javaFX/testData/completion/staticProperties_after.fxml new file mode 100644 index 000000000000..055b7631cb8c --- /dev/null +++ b/plugins/javaFX/testData/completion/staticProperties_after.fxml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/plugins/javaFX/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxCompletionTest.java b/plugins/javaFX/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxCompletionTest.java index fbc45669314e..992ff51448a4 100644 --- a/plugins/javaFX/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxCompletionTest.java +++ b/plugins/javaFX/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxCompletionTest.java @@ -16,6 +16,7 @@ package org.jetbrains.plugins.javaFX.fxml; import com.intellij.codeInsight.completion.CompletionTestCase; +import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.openapi.application.PluginPathManager; import com.intellij.testFramework.PsiTestUtil; import org.jetbrains.annotations.NotNull; @@ -36,10 +37,31 @@ public class JavaFxCompletionTest extends CompletionTestCase { doTest(); } + public void testStaticProperties() throws Exception { + doTest("GridPane.columnIndex"); + } + private void doTest() throws Exception { + doTest(null); + } + + private void doTest(final String selection) throws Exception { configureByFile(getTestName(true) + ".fxml"); assertTrue(myItems.length > 0); - selectItem(myItems[0]); + LookupElement selectionElement = null; + for (LookupElement item : myItems) { + if (item.getLookupString().equals(selection)) { + selectionElement = item; + break; + } + } + if (selection != null && selectionElement == null) { + fail(selection + " is not suggested"); + } + if (selectionElement == null) { + selectionElement = myItems[0]; + } + selectItem(selectionElement); checkResultByFile(getTestName(true) + "_after.fxml"); }