Search in sources :

Example 1 with ISearchHitCollector

use of org.eclipse.help.internal.search.ISearchHitCollector in project tdi-studio-se by Talend.

the class TalendEditorPaletteFactory method getRelatedComponentNamesFromHelp.

protected static Set<String> getRelatedComponentNamesFromHelp(final String keyword) {
    if (keyword == null || keyword.isEmpty()) {
        return null;
    }
    // This method will cost lots of time to complete when it is called the first time
    final List<SearchHit> querySearchResult = new ArrayList<SearchHit>();
    if (searchInHelpJob != null && searchInHelpJob.getState() != Job.NONE) {
        searchInHelpJob.cancel();
    }
    searchInHelpJob = new Job(SEARCHING_FROM_HELP) {

        @SuppressWarnings("restriction")
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            String helpKeyword = keyword;
            try {
                TalendPaletteSearchIndex searchIndex = TalendPaletteSearchIndex.getInstance();
                LocalSearchManager localSearchManager = BaseHelpSystem.getLocalSearchManager();
                // if not work or null, maybe should throw a warn to inform user and help us trace
                // if (searchIndex != null && localSearchManager != null) {
                localSearchManager.ensureIndexUpdated(monitor, searchIndex);
                //$NON-NLS-1$
                final String WHITESPACE = " ";
                //$NON-NLS-1$//$NON-NLS-2$
                helpKeyword = helpKeyword + WHITESPACE + "OR" + WHITESPACE + helpKeyword + "*";
                ISearchQuery searchQuery = new SearchQuery(helpKeyword, false, new ArrayList<String>(), Platform.getNL());
                searchIndex.search(searchQuery, new ISearchHitCollector() {

                    @Override
                    public void addQTCException(QueryTooComplexException exception) throws QueryTooComplexException {
                    // nothing need to do
                    }

                    @Override
                    public void addHits(List<SearchHit> hits, String wordsSearched) {
                        querySearchResult.addAll(hits);
                    }
                });
            // }
            } catch (Throwable e) {
                CommonExceptionHandler.process(e, Priority.WARN);
            }
            return Status.OK_STATUS;
        }

        @Override
        public boolean belongsTo(Object family) {
            return family.equals(FederatedSearchJob.FAMILY);
        }
    };
    try {
        searchInHelpJob.setPriority(Job.INTERACTIVE);
        searchInHelpJob.schedule();
        searchInHelpJob.join();
    } catch (Throwable e) {
        CommonExceptionHandler.process(e, Priority.WARN);
    }
    Set<String> componentNames = new LinkedHashSet<String>();
    // the limitation has been moved to it's caller
    // int limit = PaletteSettingsPreferencePage.getPaletteSearchResultLimitFromHelp();
    // int i = 1;
    Iterator<SearchHit> iter = querySearchResult.iterator();
    while (iter.hasNext()) {
        // if (limit < i) {
        // break;
        // }
        SearchHit result = iter.next();
        String label = result.getLabel();
        if (label == null || label.trim().length() == 0) {
            continue;
        }
        componentNames.add(label);
    // i++;
    }
    return componentNames;
}
Also used : ISearchQuery(org.eclipse.help.internal.search.ISearchQuery) SearchQuery(org.eclipse.help.internal.search.SearchQuery) LinkedHashSet(java.util.LinkedHashSet) IStatus(org.eclipse.core.runtime.IStatus) SearchHit(org.eclipse.help.internal.search.SearchHit) ArrayList(java.util.ArrayList) LocalSearchManager(org.eclipse.help.internal.search.LocalSearchManager) ISearchHitCollector(org.eclipse.help.internal.search.ISearchHitCollector) QueryTooComplexException(org.eclipse.help.internal.search.QueryTooComplexException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) FederatedSearchJob(org.eclipse.help.internal.search.federated.FederatedSearchJob) Job(org.eclipse.core.runtime.jobs.Job) ISearchQuery(org.eclipse.help.internal.search.ISearchQuery)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 Job (org.eclipse.core.runtime.jobs.Job)1 ISearchHitCollector (org.eclipse.help.internal.search.ISearchHitCollector)1 ISearchQuery (org.eclipse.help.internal.search.ISearchQuery)1 LocalSearchManager (org.eclipse.help.internal.search.LocalSearchManager)1 QueryTooComplexException (org.eclipse.help.internal.search.QueryTooComplexException)1 SearchHit (org.eclipse.help.internal.search.SearchHit)1 SearchQuery (org.eclipse.help.internal.search.SearchQuery)1 FederatedSearchJob (org.eclipse.help.internal.search.federated.FederatedSearchJob)1