/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2014-2014 AS3Boyan
* Copyright 2014-2014 Elias Ku
*
* 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.plugins.haxe.ide;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.intellij.navigation.ItemPresentation;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.plugins.haxe.lang.psi.HaxeComponentName;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @author: Fedor.Korotkov
*/
public class HaxeLookupElement extends LookupElement {
private final HaxeComponentName myComponentName;
public static Collection<HaxeLookupElement> convert(@NotNull Collection<HaxeComponentName> componentNames) {
final List<HaxeLookupElement> result = new ArrayList<HaxeLookupElement>(componentNames.size());
for (HaxeComponentName componentName : componentNames) {
result.add(new HaxeLookupElement(componentName));
}
return result;
}
public HaxeLookupElement(HaxeComponentName name) {
myComponentName = name;
}
@NotNull
@Override
public String getLookupString() {
final String result = myComponentName.getName();
if (result != null) {
return result;
}
return myComponentName.getIdentifier().getText();
}
@Override
public void renderElement(LookupElementPresentation presentation) {
final ItemPresentation myComponentNamePresentation = myComponentName.getPresentation();
if (myComponentNamePresentation == null) {
presentation.setItemText(getLookupString());
return;
}
presentation.setItemText(myComponentNamePresentation.getPresentableText());
presentation.setIcon(myComponentNamePresentation.getIcon(true));
final String pkg = myComponentNamePresentation.getLocationString();
if (StringUtil.isNotEmpty(pkg)) {
presentation.setTailText(" " + pkg, true);
}
}
@NotNull
@Override
public Object getObject() {
return myComponentName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof HaxeLookupElement)) return false;
return myComponentName.equals(((HaxeLookupElement)o).myComponentName);
}
@Override
public int hashCode() {
return myComponentName.hashCode();
}
}