tmysik commented on code in PR #8142: URL: https://github.com/apache/netbeans/pull/8142#discussion_r1927195529
########## php/php.project/src/org/netbeans/modules/php/project/ComputeTestMethodAnnotations.java: ########## @@ -0,0 +1,189 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.php.project; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; +import javax.swing.text.JTextComponent; +import org.netbeans.api.editor.EditorRegistry; +import org.netbeans.modules.editor.NbEditorUtilities; +import org.netbeans.modules.gsf.testrunner.ui.api.TestMethodController; +import org.netbeans.modules.gsf.testrunner.ui.api.TestMethodController.TestMethod; +import org.netbeans.modules.php.api.editor.EditorSupport; +import org.netbeans.modules.php.api.editor.PhpClass; +import org.netbeans.modules.php.api.editor.PhpType; +import org.netbeans.modules.php.api.phpmodule.PhpModule; +import org.netbeans.modules.php.api.util.FileUtils; +import org.netbeans.modules.php.project.ui.actions.support.CommandUtils; +import org.netbeans.modules.php.project.util.PhpProjectUtils; +import org.netbeans.modules.php.spi.testing.PhpTestingProvider; +import org.netbeans.spi.project.SingleMethod; +import org.openide.filesystems.FileObject; +import org.openide.util.Lookup; +import org.openide.util.RequestProcessor; + + +public class ComputeTestMethodAnnotations implements DocumentListener, PropertyChangeListener, Runnable { + + private static final Logger LOGGER = Logger.getLogger(ComputeTestMethodAnnotations.class.getName()); + + private static final RequestProcessor RP = new RequestProcessor(ComputeTestMethodAnnotations.class); + private final RequestProcessor.Task task = RP.create(this); + + private static final ComputeTestMethodAnnotations INSTANCE = new ComputeTestMethodAnnotations(); + + private static final AtomicInteger usagesCount = new AtomicInteger(0); + + private volatile Document handledDocument; + + public static ComputeTestMethodAnnotations getInstance() { + return INSTANCE; + } + + private ComputeTestMethodAnnotations() { + } + + public void register() { + if (usagesCount.getAndIncrement() == 0) { + EditorRegistry.addPropertyChangeListener(this); + } + } + + public void unregister() { + if (usagesCount.decrementAndGet() == 0) { + EditorRegistry.removePropertyChangeListener(this); + } + } + + @Override + public void propertyChange(PropertyChangeEvent event) { + JTextComponent textComponent = EditorRegistry.lastFocusedComponent(); + if (textComponent != null) { + Document document = textComponent.getDocument(); + if (document != null) { + FileObject fileObject = NbEditorUtilities.getFileObject(document); + if (fileObject != null) { + if (FileUtils.isPhpFile(fileObject)) { + if (event.getPropertyName().equals(EditorRegistry.FOCUS_GAINED_PROPERTY)) { + handleFileChange(document); + document.addDocumentListener(ComputeTestMethodAnnotations.this); + } else if (event.getPropertyName().equals(EditorRegistry.FOCUS_LOST_PROPERTY)) { + document.removeDocumentListener(this); + } + } + } + } + } + } + + @Override + public void insertUpdate(DocumentEvent event) { + handleFileChange(event.getDocument()); + } + + @Override + public void removeUpdate(DocumentEvent event) { + handleFileChange(event.getDocument()); + } + + @Override + public void changedUpdate(DocumentEvent event) { + handleFileChange(event.getDocument()); + } + + private void handleFileChange(Document doc) { + handledDocument = doc; + task.schedule(500); + } + + @Override + public void run() { + List<TestMethod> testMethods = new ArrayList<>(); + + List<TestMethod> methods = getTestMethods(handledDocument); + testMethods.addAll(methods); Review Comment: Perhaps better as implemented - with a variable, we can check, whether it is empty (and avoid this call in such a case). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
