javafx: completion of static properties

This commit is contained in:
anna
2013-01-17 22:11:25 +01:00
parent 76a5b55624
commit 2f9b7cda57
5 changed files with 169 additions and 2 deletions
@@ -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()]);
}
}
@@ -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;
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<GridPane fx:controller="pp.ExampleController" xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<HBox spacing="10" alignment="BOTTOM_RIGHT" <caret> GridPane.rowIndex="4"/>
</GridPane>
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<GridPane fx:controller="pp.ExampleController" xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<HBox spacing="10" alignment="BOTTOM_RIGHT" GridPane.columnIndex="" GridPane.rowIndex="4"/>
</GridPane>
@@ -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");
}