Search in sources :

Example 21 with ISafeRunnable

use of org.eclipse.core.runtime.ISafeRunnable in project che by eclipse.

the class JavaSearchQuery method run.

public IStatus run(IProgressMonitor monitor) {
    final JavaSearchResult textResult = (JavaSearchResult) getSearchResult();
    textResult.removeAll();
    // Don't need to pass in working copies in 3.0 here
    SearchEngine engine = new SearchEngine();
    try {
        int totalTicks = 1000;
        IProject[] projects = JavaSearchScopeFactory.getInstance().getProjects(fPatternData.getScope());
        final SearchParticipantRecord[] participantDescriptors = SearchParticipantsExtensionPoint.getInstance().getSearchParticipants(projects);
        final int[] ticks = new int[participantDescriptors.length];
        for (int i = 0; i < participantDescriptors.length; i++) {
            final int iPrime = i;
            ISafeRunnable runnable = new ISafeRunnable() {

                public void handleException(Throwable exception) {
                    ticks[iPrime] = 0;
                    String message = SearchMessages.JavaSearchQuery_error_participant_estimate;
                    JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, message, exception));
                }

                public void run() throws Exception {
                    ticks[iPrime] = participantDescriptors[iPrime].getParticipant().estimateTicks(fPatternData);
                }
            };
            SafeRunner.run(runnable);
            totalTicks += ticks[i];
        }
        SearchPattern pattern;
        String stringPattern;
        if (fPatternData instanceof ElementQuerySpecification) {
            IJavaElement element = ((ElementQuerySpecification) fPatternData).getElement();
            stringPattern = JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT);
            if (!element.exists()) {
                return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(SearchMessages.JavaSearchQuery_error_element_does_not_exist, stringPattern), null);
            }
            pattern = SearchPattern.createPattern(element, fPatternData.getLimitTo(), SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
        } else {
            PatternQuerySpecification patternSpec = (PatternQuerySpecification) fPatternData;
            stringPattern = patternSpec.getPattern();
            int matchMode = getMatchMode(stringPattern) | SearchPattern.R_ERASURE_MATCH;
            if (patternSpec.isCaseSensitive())
                matchMode |= SearchPattern.R_CASE_SENSITIVE;
            pattern = SearchPattern.createPattern(patternSpec.getPattern(), patternSpec.getSearchFor(), patternSpec.getLimitTo(), matchMode);
        }
        if (pattern == null) {
            return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(SearchMessages.JavaSearchQuery_error_unsupported_pattern, stringPattern), null);
        }
        monitor.beginTask(Messages.format(SearchMessages.JavaSearchQuery_task_label, stringPattern), totalTicks);
        IProgressMonitor mainSearchPM = new SubProgressMonitor(monitor, 1000);
        boolean ignorePotentials = NewSearchUI.arePotentialMatchesIgnored();
        NewSearchResultCollector collector = new NewSearchResultCollector(textResult, ignorePotentials);
        engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, fPatternData.getScope(), collector, mainSearchPM);
        for (int i = 0; i < participantDescriptors.length; i++) {
            final ISearchRequestor requestor = new SearchRequestor(participantDescriptors[i].getParticipant(), textResult);
            final IProgressMonitor participantPM = new SubProgressMonitor(monitor, ticks[i]);
            final int iPrime = i;
            ISafeRunnable runnable = new ISafeRunnable() {

                public void handleException(Throwable exception) {
                    participantDescriptors[iPrime].getDescriptor().disable();
                    String message = SearchMessages.JavaSearchQuery_error_participant_search;
                    JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, message, exception));
                }

                public void run() throws Exception {
                    final IQueryParticipant participant = participantDescriptors[iPrime].getParticipant();
                    final PerformanceStats stats = PerformanceStats.getStats(PERF_SEARCH_PARTICIPANT, participant);
                    stats.startRun();
                    participant.search(requestor, fPatternData, participantPM);
                    stats.endRun();
                }
            };
            SafeRunner.run(runnable);
        }
    } catch (CoreException e) {
        return e.getStatus();
    }
    String message = Messages.format(SearchMessages.JavaSearchQuery_status_ok_message, String.valueOf(textResult.getMatchCount()));
    return new Status(IStatus.OK, JavaPlugin.getPluginId(), 0, message, null);
}
Also used : SearchEngine(org.eclipse.jdt.core.search.SearchEngine) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IJavaElement(org.eclipse.jdt.core.IJavaElement) IQueryParticipant(org.eclipse.jdt.ui.search.IQueryParticipant) ElementQuerySpecification(org.eclipse.jdt.ui.search.ElementQuerySpecification) PatternQuerySpecification(org.eclipse.jdt.ui.search.PatternQuerySpecification) IProject(org.eclipse.core.resources.IProject) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) ISearchRequestor(org.eclipse.jdt.ui.search.ISearchRequestor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ISearchRequestor(org.eclipse.jdt.ui.search.ISearchRequestor) PerformanceStats(org.eclipse.core.runtime.PerformanceStats) CoreException(org.eclipse.core.runtime.CoreException)

Example 22 with ISafeRunnable

use of org.eclipse.core.runtime.ISafeRunnable in project che by eclipse.

the class UndoManager2 method fireAboutToPerformChange.

//---- event firing methods -------------------------------------------------
private void fireAboutToPerformChange(final Change change) {
    if (fListeners == null)
        return;
    Object[] listeners = fListeners.getListeners();
    for (int i = 0; i < listeners.length; i++) {
        final IUndoManagerListener listener = (IUndoManagerListener) listeners[i];
        SafeRunner.run(new ISafeRunnable() {

            public void run() throws Exception {
                listener.aboutToPerformChange(UndoManager2.this, change);
            }

            public void handleException(Throwable exception) {
                RefactoringCorePlugin.log(exception);
            }
        });
    }
}
Also used : IUndoManagerListener(org.eclipse.ltk.core.refactoring.IUndoManagerListener) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) CoreException(org.eclipse.core.runtime.CoreException) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 23 with ISafeRunnable

use of org.eclipse.core.runtime.ISafeRunnable in project che by eclipse.

the class DeltaProcessor method notifyListeners.

private void notifyListeners(IJavaElementDelta deltaToNotify, int eventType, IElementChangedListener[] listeners, int[] listenerMask, int listenerCount) {
    final ElementChangedEvent extraEvent = new ElementChangedEvent(deltaToNotify, eventType);
    for (int i = 0; i < listenerCount; i++) {
        if ((listenerMask[i] & eventType) != 0) {
            final IElementChangedListener listener = listeners[i];
            long start = -1;
            if (VERBOSE) {
                //$NON-NLS-1$//$NON-NLS-2$
                System.out.print("Listener #" + (i + 1) + "=" + listener.toString());
                start = System.currentTimeMillis();
            }
            // wrap callbacks with Safe runnable for subsequent listeners to be called when some are causing grief
            SafeRunner.run(new ISafeRunnable() {

                public void handleException(Throwable exception) {
                    //$NON-NLS-1$
                    Util.log(exception, "Exception occurred in listener of Java element change notification");
                }

                public void run() throws Exception {
                    PerformanceStats stats = null;
                    if (PERF) {
                    //							stats = PerformanceStats.getStats(JavaModelManager.DELTA_LISTENER_PERF, listener);
                    //							stats.startRun();
                    }
                    listener.elementChanged(extraEvent);
                    if (PERF) {
                        stats.endRun();
                    }
                }
            });
            if (VERBOSE) {
                //$NON-NLS-1$ //$NON-NLS-2$
                System.out.println(" -> " + (System.currentTimeMillis() - start) + "ms");
            }
        }
    }
}
Also used : PerformanceStats(org.eclipse.core.runtime.PerformanceStats) IElementChangedListener(org.eclipse.jdt.core.IElementChangedListener) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) JavaModelException(org.eclipse.jdt.core.JavaModelException) CoreException(org.eclipse.core.runtime.CoreException) ElementChangedEvent(org.eclipse.jdt.core.ElementChangedEvent)

Example 24 with ISafeRunnable

use of org.eclipse.core.runtime.ISafeRunnable in project translationstudio8 by heartsome.

the class ProjectSettingHandler method runWizardPageExtension.

/**
	 * 加载扩展向导页 ;
	 */
private void runWizardPageExtension() {
    IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor("net.heartsome.cat.ts.ui.extensionpoint.projectsetting");
    try {
        // 修改术语库重复
        extensionPages.clear();
        for (IConfigurationElement e : config) {
            final Object o = e.createExecutableExtension("class");
            if (o instanceof AbstractProjectSettingPage) {
                ISafeRunnable runnable = new ISafeRunnable() {

                    public void handleException(Throwable exception) {
                        logger.error(Messages.getString("handlers.ProjectSettingHandler.logger1"), exception);
                    }

                    public void run() throws Exception {
                        extensionPages.add((AbstractProjectSettingPage) o);
                    }
                };
                SafeRunner.run(runnable);
            }
        }
    } catch (CoreException ex) {
        logger.error(Messages.getString("handlers.ProjectSettingHandler.logger1"), ex);
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) AbstractProjectSettingPage(net.heartsome.cat.ts.ui.extensionpoint.AbstractProjectSettingPage) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement)

Example 25 with ISafeRunnable

use of org.eclipse.core.runtime.ISafeRunnable in project che by eclipse.

the class ASTProvider method createAST.

/**
     * Creates a new compilation unit AST.
     *
     * @param input the Java element for which to create the AST
     * @param progressMonitor the progress monitor
     * @return AST
     */
public static CompilationUnit createAST(final ITypeRoot input, final IProgressMonitor progressMonitor) {
    if (!hasSource(input))
        return null;
    if (progressMonitor != null && progressMonitor.isCanceled())
        return null;
    final ASTParser parser = ASTParser.newParser(SHARED_AST_LEVEL);
    parser.setResolveBindings(true);
    parser.setStatementsRecovery(SHARED_AST_STATEMENT_RECOVERY);
    parser.setBindingsRecovery(SHARED_BINDING_RECOVERY);
    parser.setSource(input);
    if (progressMonitor != null && progressMonitor.isCanceled())
        return null;
    final CompilationUnit[] root = new CompilationUnit[1];
    SafeRunner.run(new ISafeRunnable() {

        public void run() {
            try {
                if (progressMonitor != null && progressMonitor.isCanceled())
                    return;
                if (DEBUG)
                    System.err.println(getThreadName() + " - " + DEBUG_PREFIX + "creating AST for: " + //$NON-NLS-1$ //$NON-NLS-2$
                    input.getElementName());
                root[0] = (CompilationUnit) parser.createAST(progressMonitor);
                //mark as unmodifiable
                ASTNodes.setFlagsToAST(root[0], ASTNode.PROTECT);
            } catch (OperationCanceledException ex) {
                return;
            }
        }

        public void handleException(Throwable ex) {
            LOG.error(ex.getMessage(), ex);
        }
    });
    return root[0];
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Aggregations

ISafeRunnable (org.eclipse.core.runtime.ISafeRunnable)44 CoreException (org.eclipse.core.runtime.CoreException)28 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)11 IStatus (org.eclipse.core.runtime.IStatus)11 Status (org.eclipse.core.runtime.Status)11 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)6 ISpellingPreferenceBlock (org.eclipse.ui.texteditor.spelling.ISpellingPreferenceBlock)5 ExecutionException (org.eclipse.core.commands.ExecutionException)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 SpellingEngineDescriptor (org.eclipse.ui.texteditor.spelling.SpellingEngineDescriptor)4 ArrayList (java.util.ArrayList)3 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 ASTParser (org.eclipse.jdt.core.dom.ASTParser)3 IDocument (org.eclipse.jface.text.IDocument)3 IUndoManagerListener (org.eclipse.ltk.core.refactoring.IUndoManagerListener)3 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 IDocumentSetupParticipant (org.eclipse.core.filebuffers.IDocumentSetupParticipant)2 IDocumentSetupParticipantExtension (org.eclipse.core.filebuffers.IDocumentSetupParticipantExtension)2 IFile (org.eclipse.core.resources.IFile)2