sandbox/java.hints/spi.java.hints/src/org/netbeans/modules/java/hints/spiimpl/JavaHintsPositionRefresher.java
branchdonation_review
changeset 1043 57843026e60b
parent 1027 205b7632914c
parent 1040 f7b6892fd754
child 1044 7feb751ba76b
     1.1 --- a/sandbox/java.hints/spi.java.hints/src/org/netbeans/modules/java/hints/spiimpl/JavaHintsPositionRefresher.java	Mon Dec 19 11:37:36 2016 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,161 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2009-2010 Oracle and/or its affiliates. All rights reserved.
     1.8 - *
     1.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 - * Other names may be trademarks of their respective owners.
    1.11 - *
    1.12 - * The contents of this file are subject to the terms of either the GNU
    1.13 - * General Public License Version 2 only ("GPL") or the Common
    1.14 - * Development and Distribution License("CDDL") (collectively, the
    1.15 - * "License"). You may not use this file except in compliance with the
    1.16 - * License. You can obtain a copy of the License at
    1.17 - * http://www.netbeans.org/cddl-gplv2.html
    1.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 - * specific language governing permissions and limitations under the
    1.20 - * License.  When distributing the software, include this License Header
    1.21 - * Notice in each file and include the License file at
    1.22 - * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 - * particular file as subject to the "Classpath" exception as provided
    1.24 - * by Oracle in the GPL Version 2 section of the License file that
    1.25 - * accompanied this code. If applicable, add the following below the
    1.26 - * License Header, with the fields enclosed by brackets [] replaced by
    1.27 - * your own identifying information:
    1.28 - * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 - *
    1.30 - * If you wish your version of this file to be governed by only the CDDL
    1.31 - * or only the GPL Version 2, indicate your decision by adding
    1.32 - * "[Contributor] elects to include this software in this distribution
    1.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.34 - * single choice of license, a recipient has the option to distribute
    1.35 - * your version of this file under either the CDDL, the GPL Version 2 or
    1.36 - * to extend the choice of license to its licensees as provided above.
    1.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.38 - * Version 2 license, then the option applies only if the new code is
    1.39 - * made subject to such option by the copyright holder.
    1.40 - *
    1.41 - * Contributor(s):
    1.42 - *
    1.43 - * Portions Copyrighted 2009-2010 Sun Microsystems, Inc.
    1.44 - */
    1.45 -
    1.46 -package org.netbeans.modules.java.hints.spiimpl;
    1.47 -
    1.48 -import java.io.IOException;
    1.49 -import java.util.ArrayList;
    1.50 -import java.util.Collections;
    1.51 -import java.util.HashMap;
    1.52 -import java.util.Iterator;
    1.53 -import java.util.List;
    1.54 -import java.util.Map;
    1.55 -import java.util.logging.Level;
    1.56 -import java.util.logging.Logger;
    1.57 -import javax.swing.text.Document;
    1.58 -import org.netbeans.api.editor.mimelookup.MimeLookup;
    1.59 -import org.netbeans.api.editor.mimelookup.MimeRegistration;
    1.60 -import org.netbeans.api.java.source.CompilationController;
    1.61 -import org.netbeans.api.java.source.JavaSource;
    1.62 -import org.netbeans.api.java.source.JavaSource.Phase;
    1.63 -import org.netbeans.api.java.source.Task;
    1.64 -import org.netbeans.api.progress.ProgressUtils;
    1.65 -import org.netbeans.spi.editor.hints.Context;
    1.66 -import org.netbeans.spi.editor.hints.ErrorDescription;
    1.67 -import org.netbeans.spi.editor.hints.PositionRefresher;
    1.68 -import org.netbeans.modules.java.hints.providers.spi.PositionRefresherHelper;
    1.69 -import org.openide.util.Exceptions;
    1.70 -import org.openide.util.NbBundle;
    1.71 -
    1.72 -/**
    1.73 - * Refreshes all Java Hints on current line upon Alt-Enter or mouseclick
    1.74 - * @author Max Sauer
    1.75 - */
    1.76 -@MimeRegistration(mimeType="text/x-java", service=PositionRefresher.class)
    1.77 -public class JavaHintsPositionRefresher implements PositionRefresher {
    1.78 -
    1.79 -    private static final Logger LOG = Logger.getLogger(JavaHintsPositionRefresher.class.getName());
    1.80 -
    1.81 -    @Override
    1.82 -    public Map<String, List<ErrorDescription>> getErrorDescriptionsAt(final Context context, final Document doc) {
    1.83 -        final List<PositionRefresherHelper> refreshers = new ArrayList<PositionRefresherHelper>(MimeLookup.getLookup("text/x-java").lookupAll(PositionRefresherHelper.class));
    1.84 -
    1.85 -        for (Iterator<PositionRefresherHelper> it = refreshers.iterator(); it.hasNext();) {
    1.86 -            PositionRefresherHelper h = it.next();
    1.87 -
    1.88 -            if (h.upToDateCheck(context, doc)) {
    1.89 -                LOG.log(Level.FINE, "Not computing warnings for {0}, results are up-to-date.", h.getKey());
    1.90 -                it.remove();
    1.91 -            } else {
    1.92 -                LOG.log(Level.FINE, "Will compute warnings for {0}, results not up-to-date.", h.getKey());
    1.93 -            }
    1.94 -        }
    1.95 -
    1.96 -        if (refreshers.isEmpty()) return Collections.emptyMap();
    1.97 -        
    1.98 -        final JavaSource js = JavaSource.forDocument(doc);
    1.99 -
   1.100 -        if (js == null) {
   1.101 -            LOG.log(Level.FINE, "No JavaSource associated to: {0}", new Object[] {doc, doc.getProperty(Document.StreamDescriptionProperty)});
   1.102 -            return Collections.emptyMap();
   1.103 -        }
   1.104 -
   1.105 -        final Map<String, List<ErrorDescription>> eds = new HashMap<String, List<ErrorDescription>>();
   1.106 -
   1.107 -        Runnable r = new Runnable() {
   1.108 -
   1.109 -            public void run() {
   1.110 -                try {
   1.111 -                    js.runUserActionTask(new RefreshTask(eds, refreshers, context, doc), true);
   1.112 -                } catch (IOException ex) {
   1.113 -                    Exceptions.printStackTrace(ex);
   1.114 -                }
   1.115 -            }
   1.116 -        };
   1.117 -        
   1.118 -        ProgressUtils.runOffEventDispatchThread(r, NbBundle.getMessage(JavaHintsPositionRefresher.class, "Refresh_hints"), context.getCancel(), false); // NOI18N
   1.119 -
   1.120 -        return eds;
   1.121 -    }
   1.122 -
   1.123 -
   1.124 -    private class RefreshTask implements Task<CompilationController> {
   1.125 -
   1.126 -        private final Map<String, List<ErrorDescription>> eds;
   1.127 -        private final List<PositionRefresherHelper> refreshers;
   1.128 -        private final Context ctx;
   1.129 -        private final Document doc;
   1.130 -
   1.131 -        public RefreshTask(Map<String, List<ErrorDescription>> eds, List<PositionRefresherHelper> refreshers, Context ctx, Document doc) {
   1.132 -            this.eds = eds;
   1.133 -            this.refreshers = refreshers;
   1.134 -            this.ctx = ctx;
   1.135 -            this.doc = doc;
   1.136 -        }
   1.137 -
   1.138 -        public void run(CompilationController controller) throws Exception {
   1.139 -            if (controller.toPhase(JavaSource.Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) {
   1.140 -                return ;
   1.141 -            }
   1.142 -
   1.143 -            Document doc = controller.getDocument();
   1.144 -
   1.145 -            if (doc == null) {
   1.146 -                return;
   1.147 -            }
   1.148 -
   1.149 -            for (PositionRefresherHelper h : refreshers) {
   1.150 -                if (ctx.isCanceled()) {
   1.151 -                    return;
   1.152 -                }
   1.153 -                
   1.154 -                List errors = h.getErrorDescriptionsAt(controller, ctx, doc);
   1.155 -                
   1.156 -                if (errors == null) continue;
   1.157 -                
   1.158 -                eds.put(h.getKey(), errors);
   1.159 -            }
   1.160 -        }
   1.161 -        
   1.162 -    }
   1.163 -
   1.164 -}