tmysik commented on code in PR #8142:
URL: https://github.com/apache/netbeans/pull/8142#discussion_r1912384362


##########
php/php.project/src/org/netbeans/modules/php/project/ComputeTestMethodAnnotations.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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 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.Exceptions;
+import org.openide.util.Lookup;
+import org.openide.util.RequestProcessor;
+
+
+public class ComputeTestMethodAnnotations implements DocumentListener, 
PropertyChangeListener, Runnable {
+
+    private static final RequestProcessor rp = new 
RequestProcessor(ComputeTestMethodAnnotations.class);
+    private final RequestProcessor.Task task = rp.create(this);
+
+    private static ComputeTestMethodAnnotations instance;
+    private static boolean isRegistered = false;
+    private static int usagesCount = 0;
+
+    private Document handledDocument;
+
+    public static ComputeTestMethodAnnotations getInstance() {
+        if (instance == null) {
+            instance = new ComputeTestMethodAnnotations();
+        }
+
+        return instance;
+    }
+
+    private ComputeTestMethodAnnotations() {
+    }
+
+    public void register() {
+        if (!isRegistered) {
+            EditorRegistry.addPropertyChangeListener(this);
+            isRegistered = true;
+        }
+        usagesCount++;
+    }
+
+    public void unregister() {
+        usagesCount--;
+        if (usagesCount == 0) {
+            EditorRegistry.removePropertyChangeListener(this);
+            isRegistered = false;
+        }
+    }
+
+    @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);
+                        }
+
+                        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() {
+        if (handledDocument != null) {

Review Comment:
   When can handledDocument be null?
   



-- 
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

Reply via email to