use of org.talend.core.model.components.IComponentsHandler in project tdi-studio-se by Talend.
the class TalendEditorPaletteFactory method getRelatedComponents.
protected static List<IComponent> getRelatedComponents(final IComponentsFactory compFac, String keyword, boolean needCheckVisible) {
Set<IComponent> componentSet = null;
IComponentsHandler componentsHandler = compFac.getComponentsHandler();
String lowerCasedKeyword = null;
if (keyword != null) {
lowerCasedKeyword = keyword.toLowerCase().trim();
}
/**
* No need to sort result from help, since it may confuse user
*/
boolean needSort = true;
if (compFac != null && lowerCasedKeyword != null && 0 < lowerCasedKeyword.length()) {
componentSet = new LinkedHashSet<IComponent>();
// 1. match the full name component
Map<String, Map<String, Set<IComponent>>> componentNameMap = compFac.getComponentNameMap();
if (componentNameMap != null) {
Map<String, Set<IComponent>> map = componentNameMap.get(lowerCasedKeyword);
if (map != null) {
Collection<Set<IComponent>> componentSets = map.values();
Iterator<Set<IComponent>> componentSetIter = componentSets.iterator();
List<IComponent> filteredComponent = new ArrayList<IComponent>();
while (componentSetIter.hasNext()) {
filteredComponent.addAll(componentSetIter.next());
}
if (componentsHandler != null) {
filteredComponent = componentsHandler.filterComponents(filteredComponent);
}
Iterator<IComponent> componentIter = filteredComponent.iterator();
while (componentIter.hasNext()) {
IComponent iComponent = componentIter.next();
if (iComponent == null || !ComponentUtilities.isComponentVisible(iComponent) || iComponent.isTechnical()) {
componentIter.remove();
}
}
if (!filteredComponent.isEmpty()) {
componentSet.addAll(filteredComponent);
}
}
}
// 2. do usual search
if (componentNameMap != null) {
// 2.1 search from local palette
addComponentsByNameFilter(compFac, componentSet, lowerCasedKeyword);
if (!componentSet.isEmpty()) {
componentSet = new LinkedHashSet<IComponent>(sortResultsBasedOnRecentlyUsed(new ArrayList<IComponent>(componentSet)));
needSort = false;
}
// 2.2 search from help document
boolean shouldSearchFromHelpAPI = PaletteSettingsPreferencePage.isPaletteSearchFromHelp();
if (shouldSearchFromHelpAPI) {
String helpKeyword = keyword;
if (helpKeyword != null) {
helpKeyword = helpKeyword.trim();
}
Set<String> componentNames = getRelatedComponentNamesFromHelp(helpKeyword);
if (componentNames != null && 0 < componentNames.size()) {
int limit = PaletteSettingsPreferencePage.getPaletteSearchResultLimitFromHelp();
int i = 0;
Iterator<String> nameIter = componentNames.iterator();
while (nameIter.hasNext()) {
if (limit <= i) {
break;
}
String componentName = nameIter.next();
Map<String, Set<IComponent>> map = componentNameMap.get(componentName.toLowerCase());
if (map == null) {
continue;
}
Set<IComponent> findedComponents = map.get(componentName);
if (findedComponents != null && !findedComponents.isEmpty()) {
for (IComponent iComponent : findedComponents) {
if (limit <= i) {
break;
}
if (ComponentUtilities.isComponentVisible(iComponent) && !iComponent.isTechnical() && filterComponent(iComponent, componentsHandler)) {
componentSet.add(iComponent);
i++;
}
}
// componentSet.addAll(findedComponents);
}
}
if (0 < i) {
needSort = false;
}
}
}
}
} else if (compFac != null) {
componentSet = compFac.getComponents();
}
List<IComponent> relatedComponents = null;
if (componentSet == null || componentSet.isEmpty()) {
relatedComponents = new LinkedList<IComponent>();
} else {
relatedComponents = new LinkedList<IComponent>(componentSet);
}
if (compFac != null && componentsHandler != null && !relatedComponents.isEmpty()) {
relatedComponents = componentsHandler.filterComponents(relatedComponents);
}
if (needCheckVisible && relatedComponents != null && !relatedComponents.isEmpty()) {
Iterator<IComponent> iter = relatedComponents.iterator();
while (iter.hasNext()) {
IComponent component = iter.next();
if (component == null || !ComponentUtilities.isComponentVisible(component) || component.isTechnical()) {
iter.remove();
}
}
}
if (needSort) {
relatedComponents = sortResultsBasedOnRecentlyUsed(relatedComponents);
}
return relatedComponents;
}
Aggregations