/*
 * 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.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlFile;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.javaFX.fxml.JavaFxPsiUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * User: anna
 * Date: 1/17/13
 */
public class JavaFxFieldIdReferenceProvider extends JavaFxControllerBasedReferenceProvider {
  @Override
  protected PsiReference[] getReferencesByElement(@NotNull final PsiClass aClass,
                                                  final XmlAttributeValue xmlAttributeValue,
                                                  ProcessingContext context) {
    final PsiField field = aClass.findFieldByName(xmlAttributeValue.getValue(), true);
    return new PsiReference[]{new JavaFxControllerFieldRef(xmlAttributeValue, field, aClass)};
  }

  public static class JavaFxControllerFieldRef extends PsiReferenceBase<XmlAttributeValue> {
    private final XmlAttributeValue myXmlAttributeValue;
    private final PsiField myField;
    private final PsiClass myAClass;

    public JavaFxControllerFieldRef(XmlAttributeValue xmlAttributeValue, PsiField field, PsiClass aClass) {
      super(xmlAttributeValue, true);
      myXmlAttributeValue = xmlAttributeValue;
      myField = field;
      myAClass = aClass;
    }

    public XmlAttributeValue getXmlAttributeValue() {
      return myXmlAttributeValue;
    }

    public PsiClass getAClass() {
      return myAClass;
    }

    @Nullable
    @Override
    public PsiElement resolve() {
      return myField != null ? myField : myXmlAttributeValue;
    }

    public boolean isUnresolved() {
      if (myField == null && myAClass != null) {
        final XmlFile xmlFile = (XmlFile)myXmlAttributeValue.getContainingFile();
        if (xmlFile.getRootTag() != null && !JavaFxPsiUtil.isOutOfHierarchy(myXmlAttributeValue)) {
          return true;
        }
      }
      return false;
    }

    @NotNull
    @Override
    public Object[] getVariants() {
      final List<Object> fieldsToSuggest = new ArrayList<Object>();
      final PsiField[] fields = myAClass.getFields();
      for (PsiField psiField : fields) {
        if (!psiField.hasModifierProperty(PsiModifier.STATIC)) {
          if (JavaFxPsiUtil.isVisibleInFxml(psiField)) {
            fieldsToSuggest.add(psiField);
          }
        }
      }
      return ArrayUtil.toObjectArray(fieldsToSuggest);
    }
  }
}

@@ -1440,84 +1440,425 @@
 nal 
-PsiField field = aClass.findFieldByName(xmlAttributeValue.getValue(), true);
+String name = xmlAttributeValue.getValue();%0A    PsiMember fieldOrGetterMethod = aClass.findFieldByName(name, true);%0A    if (fieldOrGetterMethod == null) %7B%0A      final PsiMethod%5B%5D methods = aClass.findMethodsByName(name, true);%0A      for (PsiMethod method : methods) %7B%0A        if (method.getParameterList().getParameters().length == 0) %7B%0A          fieldOrGetterMethod = method;%0A          break;%0A        %7D%0A      %7D%0A    %7D
 %0A   
@@ -1937,16 +1937,30 @@
 e, field
+OrGetterMethod
 , aClass
@@ -2143,21 +2143,22 @@
  Psi
-Field
+Member
  myField
 ;%0A  
@@ -2149,24 +2149,32 @@
 mber myField
+OrMethod
 ;%0A    privat
@@ -2281,18 +2281,27 @@
  Psi
-Field fiel
+Member fieldOrMetho
 d, P
@@ -2420,15 +2420,31 @@
 ield
- = fiel
+OrMethod = fieldOrMetho
 d;%0A 
@@ -2715,16 +2715,24 @@
  myField
+OrMethod
  != null
@@ -2741,16 +2741,24 @@
  myField
+OrMethod
  : myXml
@@ -2833,16 +2833,24 @@
 (myField
+OrMethod
  == null

allow to link methods in fx:id (IDEA-149759)