This is an automated email from the ASF dual-hosted git repository.

neilcsmith pushed a commit to branch delivery
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/delivery by this push:
     new 354850432d Add lookup listening to SystemOpenAction so that it works 
dynamically in global menu.
     new 84b94c9e95 Merge pull request #5870 from 
neilcsmith-net/global-system-open
354850432d is described below

commit 354850432d53ac6d2d09408ba64b0cba5a50699a
Author: Neil C Smith <[email protected]>
AuthorDate: Fri Apr 21 17:54:00 2023 +0100

    Add lookup listening to SystemOpenAction so that it works dynamically in 
global menu.
---
 .../netbeans/core/ui/sysopen/SystemOpenAction.java | 95 +++++++++++-----------
 1 file changed, 49 insertions(+), 46 deletions(-)

diff --git 
a/platform/core.ui/src/org/netbeans/core/ui/sysopen/SystemOpenAction.java 
b/platform/core.ui/src/org/netbeans/core/ui/sysopen/SystemOpenAction.java
index 406235407c..93d087c38f 100644
--- a/platform/core.ui/src/org/netbeans/core/ui/sysopen/SystemOpenAction.java
+++ b/platform/core.ui/src/org/netbeans/core/ui/sysopen/SystemOpenAction.java
@@ -16,7 +16,6 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 package org.netbeans.core.ui.sysopen;
 
 import java.awt.Desktop;
@@ -38,9 +37,11 @@ import org.openide.filesystems.FileUtil;
 import org.openide.loaders.DataObject;
 import org.openide.util.ContextAwareAction;
 import org.openide.util.Lookup;
+import org.openide.util.LookupListener;
 import org.openide.util.NbBundle;
 import org.openide.util.RequestProcessor;
 import org.openide.util.Utilities;
+import org.openide.util.WeakListeners;
 
 /**
  * Open the selected file(s) with the system default tool.
@@ -50,63 +51,65 @@ import org.openide.util.Utilities;
 @ActionRegistration(displayName = "#CTL_SystemOpenAction", lazy=false)
 @ActionReferences ({
     @ActionReference(path = "UI/ToolActions/Files", position = 2045),
-    @ActionReference(path = "Projects/Actions", position = 101) 
+    @ActionReference(path = "Projects/Actions", position = 101)
 })
 public final class SystemOpenAction extends AbstractAction implements 
ContextAwareAction {
-    
+
     private static final RequestProcessor PROC = new 
RequestProcessor(SystemOpenAction.class);
-    
+
+    private final Lookup.Result<DataObject> result;
+    private final LookupListener resultListener;
+    private final Set<File> files;
+
     public SystemOpenAction() {
-        super(NbBundle.getMessage(SystemOpenAction.class, 
"CTL_SystemOpenAction"));
+        this(Utilities.actionsGlobalContext());
     }
 
-    public @Override void actionPerformed(ActionEvent e) {
-        new ContextAction(Utilities.actionsGlobalContext()).actionPerformed(e);
+    private SystemOpenAction(Lookup context) {
+        super(NbBundle.getMessage(SystemOpenAction.class, 
"CTL_SystemOpenAction"));
+        putValue(DynamicMenuContent.HIDE_WHEN_DISABLED, true);
+        result = context.lookupResult(DataObject.class);
+        resultListener = e -> updateFileSet();
+        result.addLookupListener(WeakListeners.create(LookupListener.class, 
resultListener, result));
+        files = new HashSet<>();
+        updateFileSet();
     }
 
-    public @Override Action createContextAwareInstance(Lookup context) {
-        return new ContextAction(context);
-    }
-    
-    private static final class ContextAction extends AbstractAction {
-        
-        private final Set<File> files;
-        
-        public ContextAction(Lookup context) {
-            super(NbBundle.getMessage(SystemOpenAction.class, 
"CTL_SystemOpenAction"));
-            putValue(DynamicMenuContent.HIDE_WHEN_DISABLED, true);
-            files = new HashSet<>();
-            for (DataObject d : context.lookupAll(DataObject.class)) {
-                File f = FileUtil.toFile(d.getPrimaryFile());
-                if (f == null || /* #144575 */Utilities.isWindows() && 
f.isFile() && !f.getName().contains(".")) {
-                    files.clear();
-                    break;
+    @Override
+    public void actionPerformed(ActionEvent e) {
+        PROC.post(() -> { // #176879: asynch
+            Desktop desktop = Desktop.getDesktop();
+            for (File f : files) {
+                try {
+                    desktop.open(f);
+                } catch (IOException x) {
+                    
Logger.getLogger(SystemOpenAction.class.getName()).log(Level.INFO, null, x);
+                    // XXX or perhaps notify user of problem; but not very 
useful on Unix at least (#6940853)
                 }
-                files.add(f);
             }
-        }
+        });
+    }
 
-        public @Override boolean isEnabled() {
-            return !files.isEmpty() && Desktop.isDesktopSupported() && 
Desktop.getDesktop().isSupported(Desktop.Action.OPEN);
-        }
+    @Override
+    public Action createContextAwareInstance(Lookup context) {
+        return new SystemOpenAction(context);
+    }
 
-        public @Override void actionPerformed(ActionEvent e) {
-            PROC.post(new Runnable() { // #176879: asynch
-                public @Override void run() {
-                    Desktop desktop = Desktop.getDesktop();
-                    for (File f : files) {
-                        try {
-                            desktop.open(f);
-                        } catch (IOException x) {
-                            
Logger.getLogger(SystemOpenAction.class.getName()).log(Level.INFO, null, x);
-                            // XXX or perhaps notify user of problem; but not 
very useful on Unix at least (#6940853)
-                        }
-                    }
-                }
-            });
-        }
+    @Override
+    public boolean isEnabled() {
+        return !files.isEmpty() && Desktop.isDesktopSupported() && 
Desktop.getDesktop().isSupported(Desktop.Action.OPEN);
+    }
 
+    private void updateFileSet() {
+        files.clear();
+        for (DataObject d : result.allInstances()) {
+            File f = FileUtil.toFile(d.getPrimaryFile());
+            if (f == null || /* #144575 */ Utilities.isWindows() && f.isFile() 
&& !f.getName().contains(".")) {
+                files.clear();
+                break;
+            }
+            files.add(f);
+        }
     }
-    
-}
 
+}


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