eirikbakke commented on a change in pull request #3414:
URL: https://github.com/apache/netbeans/pull/3414#discussion_r784963212



##########
File path: 
java/java.lsp.server/nbcode/integration/src/org/netbeans/modules/nbcode/integration/UIDefaultsIconMetadata.java
##########
@@ -0,0 +1,357 @@
+/*
+ * 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.nbcode.integration;
+
+import java.awt.Component;
+import java.awt.EventQueue;
+import java.awt.Graphics;
+import java.awt.HeadlessException;
+import java.awt.Image;
+import java.awt.MediaTracker;
+import java.awt.Toolkit;
+import java.awt.Transparency;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
+import java.awt.image.ImageObserver;
+import java.awt.image.WritableRaster;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.Icon;
+import javax.swing.ImageIcon;
+import javax.swing.UIDefaults;
+import javax.swing.UIDefaults.ActiveValue;
+import javax.swing.UIDefaults.LazyValue;
+import javax.swing.UIManager;
+import org.openide.modules.OnStart;
+import org.openide.util.ImageUtilities;
+
+/**
+ * Goes through UIDefaults and replaces Icons and Images with variants that
+ * return image's URL from the 'url' property.
+ * @author sdedic
+ */
+@OnStart
+public class UIDefaultsIconMetadata implements Runnable {
+    private static final Logger LOG = 
Logger.getLogger(UIDefaultsIconMetadata.class.getName());
+    
+    // for diagnostic purposes only
+    public static final String PROP_ORIGINAL_ICON = "originalIcon"; // NOI18N
+    // for diagnostic purposes only
+    public static final String PROP_ORIGINAL_IMAGE = "originalImage"; // NOI18N
+    
+    private static final String URN_DEFAULTS_PREFIX = "uidefaults:"; // NOI18N
+    
+    private static Object wrapImage(String key, Image image, Icon icon, 
Object... meta) {
+        Object o = image.getProperty(ImageUtilities.PROPERTY_ID, null); // 
NOI18N
+        if (o instanceof URL) {
+            return image;
+        }
+        
+        o = image.getProperty(ImageUtilities.PROPERTY_ID, null);
+        if (o instanceof String || o instanceof URI) {
+            return image;
+        } else {
+            
+            Hashtable props = new Hashtable();
+            for (int i = 0; i < meta.length; i+= 2) {
+                props.put(meta[i], meta[i+1]);
+            }
+            props.put(ImageUtilities.PROPERTY_ID, URN_DEFAULTS_PREFIX + key);
+            return createNew(image, icon, props);
+        }
+    }
+    
+    @Override
+    public void run() {
+        EventQueue.invokeLater(() -> {
+            replaceIconsAndImages(UIManager.getDefaults());
+        });
+    }
+    
+    static void replaceIconsAndImages(UIDefaults defs) {

Review comment:
       > IMO we need an API whereby icon loading can use a logical ID and 
request an icon for X. And an SPI that provides the icons for X. In the LSP 
client you'd implement the same. Any such API/SPI could use the existing URLs 
(file locations) as a backup ID to aid transition. So both needs would benefit 
from migrating to a logical ID, as well as being able to query that ID, without 
any icon being loaded.
   
   Yes, something like this would be exactly what I'd have in mind for this.
   
   > that would require to change UIManager.getIcon|Image to "something 
different" that would involve that loading SPI (an implementation could do 
UIManager.get* and return the image/icon with 'logical ID' ) - the change would 
need to be done throughout the codebase, right ?
   
   I think the existing APIs could be just fine initially. Just keep loading 
icons by URL; after all, there needs to be a default icon file in the NetBeans 
codebase for each logical icon ID in any case. But there could indeed be an SPI 
that ImageUtilities will check to see if a given URL should be resolved to a 
different Icon instance.
   
   > Good; and what should we do with Swing calling UIManager.get() from its 
own code ?
   
   For this, just modify the icon in UIDefaults, like Swing intended. We 
already do this e.g. from Windows8LFCustoms.
   




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