Search in sources :

Example 1 with IGroupByKeyComputer

use of org.eclipse.search.ui.IGroupByKeyComputer in project eclipse.platform.text by eclipse.

the class SearchManager method internalSetCurrentSearch.

void internalSetCurrentSearch(final Search search, IProgressMonitor monitor) {
    if (fCurrentSearch != null)
        fCurrentSearch.backupMarkers();
    final Search previousSearch = fCurrentSearch;
    fCurrentSearch = search;
    monitor.beginTask(SearchMessages.SearchManager_updating, getCurrentResults().size() + 20);
    // remove current search markers
    try {
        SearchPlugin.getWorkspace().getRoot().deleteMarkers(SearchUI.SEARCH_MARKER, true, IResource.DEPTH_INFINITE);
    } catch (CoreException ex) {
        ExceptionHandler.handle(ex, SearchMessages.Search_Error_deleteMarkers_title, SearchMessages.Search_Error_deleteMarkers_message);
    }
    monitor.worked(10);
    // add search markers
    Iterator<SearchResultViewEntry> iter = getCurrentResults().iterator();
    ArrayList<SearchResultViewEntry> emptyEntries = new ArrayList<>(10);
    boolean filesChanged = false;
    boolean filesDeleted = false;
    IGroupByKeyComputer groupByKeyComputer = getCurrentSearch().getGroupByKeyComputer();
    while (iter.hasNext()) {
        monitor.worked(1);
        SearchResultViewEntry entry = iter.next();
        Iterator<Map<String, Object>> attrPerMarkerIter = entry.getAttributesPerMarker().iterator();
        entry.clearMarkerList();
        if (entry.getResource() == null || !entry.getResource().exists()) {
            emptyEntries.add(entry);
            filesDeleted = true;
            continue;
        }
        while (attrPerMarkerIter.hasNext()) {
            IMarker newMarker = null;
            try {
                newMarker = entry.getResource().createMarker(entry.getMarkerType());
            } catch (CoreException ex) {
                ExceptionHandler.handle(ex, SearchMessages.Search_Error_createMarker_title, SearchMessages.Search_Error_createMarker_message);
                continue;
            }
            try {
                newMarker.setAttributes(attrPerMarkerIter.next());
                if (groupByKeyComputer != null && groupByKeyComputer.computeGroupByKey(newMarker) == null) {
                    filesDeleted = true;
                    newMarker.delete();
                    continue;
                }
            } catch (CoreException ex) {
                ExceptionHandler.handle(ex, SearchMessages.Search_Error_markerAttributeAccess_title, SearchMessages.Search_Error_markerAttributeAccess_message);
            }
            entry.add(newMarker);
        }
        if (entry.getMatchCount() == 0)
            emptyEntries.add(entry);
        else if (!filesChanged && entry.getResource().getModificationStamp() != entry.getModificationStamp())
            filesChanged = true;
    }
    getCurrentResults().removeAll(emptyEntries);
    monitor.worked(10);
    String warningMessage = null;
    Display display = getDisplay();
    if (filesChanged)
        warningMessage = SearchMessages.SearchManager_resourceChanged;
    if (filesDeleted) {
        if (warningMessage == null)
            // $NON-NLS-1$
            warningMessage = "";
        else
            // $NON-NLS-1$
            warningMessage += "\n";
        warningMessage += SearchMessages.SearchManager_resourceDeleted;
    }
    if (warningMessage != null) {
        if (display != null && !display.isDisposed()) {
            final String warningTitle = SearchMessages.SearchManager_resourceChangedWarning;
            final String warningMsg = warningMessage;
            display.syncExec(() -> MessageDialog.openWarning(getShell(), warningTitle, warningMsg));
        }
    }
    // update viewers
    Iterator<SearchResultViewer> iter2 = fListeners.iterator();
    if (display != null && !display.isDisposed()) {
        final Viewer visibleViewer = ((SearchResultView) SearchUI.getSearchResultView()).getViewer();
        while (iter2.hasNext()) {
            final SearchResultViewer viewer = iter2.next();
            display.syncExec(() -> {
                if (previousSearch != null && viewer == visibleViewer)
                    previousSearch.setSelection(viewer.getSelection());
                viewer.setInput(null);
                viewer.setPageId(search.getPageId());
                viewer.setGotoMarkerAction(search.getGotoMarkerAction());
                viewer.setContextMenuTarget(search.getContextMenuContributor());
                viewer.setActionGroupFactory(null);
                viewer.setInput(getCurrentResults());
                viewer.setActionGroupFactory(search.getActionGroupFactory());
                viewer.setSelection(fCurrentSearch.getSelection(), true);
            });
        }
    }
    monitor.done();
}
Also used : ArrayList(java.util.ArrayList) Viewer(org.eclipse.jface.viewers.Viewer) CoreException(org.eclipse.core.runtime.CoreException) IGroupByKeyComputer(org.eclipse.search.ui.IGroupByKeyComputer) IMarker(org.eclipse.core.resources.IMarker) Map(java.util.Map) Display(org.eclipse.swt.widgets.Display)

Aggregations

ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 IMarker (org.eclipse.core.resources.IMarker)1 CoreException (org.eclipse.core.runtime.CoreException)1 Viewer (org.eclipse.jface.viewers.Viewer)1 IGroupByKeyComputer (org.eclipse.search.ui.IGroupByKeyComputer)1 Display (org.eclipse.swt.widgets.Display)1