Skip to content

Commit

Permalink
Replace most uses of Toolkit.getDefaultToolkit().getImage(URL) with I…
Browse files Browse the repository at this point in the history
…mageUtilities.loadImage(URI), to make SVG icons work in these cases.
  • Loading branch information
eirikbakke committed Jan 24, 2025
1 parent 55d123f commit 3fa8212
Show file tree
Hide file tree
Showing 22 changed files with 79 additions and 114 deletions.
2 changes: 1 addition & 1 deletion apisupport/apisupport.project/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>9.3</specification-version>
<specification-version>9.36</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.netbeans.modules.apisupport.project.layers;

import java.awt.Image;
import java.awt.Toolkit;
import java.beans.BeanInfo;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -64,6 +63,7 @@
import org.openide.loaders.DataObject;
import org.openide.loaders.InstanceDataObject;
import org.openide.util.Exceptions;
import org.openide.util.ImageUtilities;
import org.openide.util.Mutex;
import org.openide.util.NbBundle;
import org.openide.util.NbCollections;
Expand Down Expand Up @@ -373,7 +373,7 @@ private Image annotateIconGeneral(Image icon, boolean big, Set<? extends FileObj
ufo.removeFileChangeListener(fileChangeListener);
ufo.addFileChangeListener(fileChangeListener);
}
return Toolkit.getDefaultToolkit().getImage(u[0]);
return ImageUtilities.loadImage(u[0].toURI());
} catch (Exception e) {
LOG.log(Level.INFO, "For " + value + " on " + fo.getPath(), e);
}
Expand Down
2 changes: 1 addition & 1 deletion ide/editor.lib/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>9.3</specification-version>
<specification-version>9.36</specification-version>
</run-dependency>
</dependency>
</module-dependencies>
Expand Down
9 changes: 8 additions & 1 deletion ide/editor.lib/src/org/netbeans/editor/AnnotationType.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openide.util.ImageUtilities;

/** Definition of the annotation type. Annotation type is defined by attributes like
* highlight color, foreground color, glyph icon, etc. Each annotation added to document
Expand Down Expand Up @@ -156,7 +158,12 @@ public void setGlyph(java.net.URL glyph) {
*/
public Image getGlyphImage() {
if (img == null) {
img = Toolkit.getDefaultToolkit().createImage(getGlyph());
try {
img = ImageUtilities.loadImage(getGlyph().toURI());
} catch (URISyntaxException e) {
LOG.log(Level.WARNING, "getGlyph() returned invalid URI", e);
return null;
}
final boolean waiting[] = new boolean [1];
waiting[0] = true;
if (!Toolkit.getDefaultToolkit().prepareImage(img, -1, -1, new ImageObserver() {
Expand Down
2 changes: 1 addition & 1 deletion ide/editor/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>9.31</specification-version>
<specification-version>9.36</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
package org.netbeans.modules.editor.options;

import java.awt.Image;
import java.awt.Toolkit;
import org.openide.util.Exceptions;
import org.openide.util.HelpCtx;
import org.openide.util.ImageUtilities;
import org.openide.util.actions.SystemAction;
import org.openide.actions.PropertiesAction;
import org.openide.nodes.Children;
import org.netbeans.modules.editor.options.AnnotationTypesFolder;
import org.netbeans.editor.AnnotationType;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.BeanNode;
Expand All @@ -40,15 +38,18 @@
import java.lang.Boolean;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/** Node representing the Annotation Types in Options window.
*
* @author David Konecny
* @since 07/2001
*/
public class AnnotationTypesNode extends AbstractNode {

private static final Logger LOGGER = Logger.getLogger(AnnotationTypesNode.class.getName());
private static final String HELP_ID = "editing.configuring.annotations"; // !!! NOI18N
private static final String ICON_BASE = "org/netbeans/modules/editor/resources/annotationtypes"; // NOI18N

Expand Down Expand Up @@ -196,12 +197,11 @@ public AnnotationTypesSubnode(AnnotationType type) throws IntrospectionException
}

public Image getIcon(int type) {
// Utilities.loadImage does not handle URLs.
// Toolkit.getImage would work, but U.lI does nicer caching.
if (iconURL.getProtocol().equals("nbresloc")) { // NOI18N
return ImageUtilities.loadImage(iconURL.getPath().substring(1));
} else {
return Toolkit.getDefaultToolkit().getImage(iconURL);
try {
return ImageUtilities.loadImage(iconURL.toURI());
} catch (URISyntaxException e) {
LOGGER.log(Level.WARNING, "AnnotationType.getGlyph() returned invalid URI", e);
return super.getIcon(type);
}
}

Expand Down
2 changes: 1 addition & 1 deletion ide/options.editor/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>9.3</specification-version>
<specification-version>9.36</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import java.awt.Cursor;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -38,7 +38,6 @@
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
Expand Down Expand Up @@ -134,14 +133,13 @@ private List<AttributeSet> processAnnotations(Map<String, AttributeSet> annos, b
category.addAttribute(StyleConstants.NameAttribute, annotationType.getName());

URL iconURL = annotationType.getGlyph ();
Image image = null;
if (iconURL.getProtocol ().equals ("nbresloc")) { // NOI18N
image = ImageUtilities.loadImage(iconURL.getPath().substring(1));
} else {
image = Toolkit.getDefaultToolkit ().getImage (iconURL);
}
if (image != null) {
category.addAttribute("icon", new ImageIcon(image)); //NOI18N
try {
Image image = ImageUtilities.loadImage(iconURL.toURI());
if (image != null) {
category.addAttribute("icon", ImageUtilities.image2Icon(image)); //NOI18N
}
} catch (URISyntaxException e) {
LOG.log(Level.WARNING, "AnnotationType.getGlyph() returned invalid URI", e);
}

Color bgColor = annotationType.getHighlight();
Expand Down
2 changes: 1 addition & 1 deletion java/form/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>9.3</specification-version>
<specification-version>9.36</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.*;
import java.io.*;
import java.beans.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;

import org.openide.util.ImageUtilities;
Expand Down Expand Up @@ -423,21 +425,19 @@ private java.awt.Image getExplicitIcon(int type) {
{
if (icon32URL != null) { // explicit icon specified in file
try {
return java.awt.Toolkit.getDefaultToolkit().getImage(
new java.net.URL(icon32URL));
return ImageUtilities.loadImage(new URI(icon32URL));
}
catch (java.net.MalformedURLException ex) {} // ignore
catch (URISyntaxException ex) {} // ignore
}
else if (getPrimaryFile().getAttribute("SystemFileSystem.icon32") != null) // NOI18N
return super.getIcon(type);
}
else { // get small icon in other cases
if (icon16URL != null) { // explicit icon specified in file
try {
return java.awt.Toolkit.getDefaultToolkit().getImage(
new java.net.URL(icon16URL));
return ImageUtilities.loadImage(new URI(icon16URL));
}
catch (java.net.MalformedURLException ex) {} // ignore
catch (URISyntaxException ex) {} // ignore
}
else if (getPrimaryFile().getAttribute("SystemFileSystem.icon") != null) // NOI18N
return super.getIcon(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

package org.netbeans.actions.simple;

import jakarta.websocket.ContainerProvider;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -31,13 +34,12 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;
import org.netbeans.actions.spi.ActionProvider;
import org.netbeans.actions.spi.ContainerProvider;
import org.netbeans.spi.project.ActionProvider;
import org.openide.util.ImageUtilities;
import org.openide.xml.XMLUtil;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderAdapter;
Expand Down Expand Up @@ -253,9 +255,9 @@ public Icon getIconForAction (String action) {
int idx = s.lastIndexOf("/");
String urlString = s.substring(0, idx) + "/" + partialPath;
try {
URL url = new URL (urlString);
return new ImageIcon(Toolkit.getDefaultToolkit().getImage(url));
} catch (Exception e) {
Image image = ImageUtilities.loadImage(new URI(urlString));
return image == null ? new ImageIcon() : ImageUtilities.image2Icon(image);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
Expand Down
2 changes: 1 addition & 1 deletion java/performance/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>9.3</specification-version>
<specification-version>9.36</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,6 @@ static final class ActionItemNode extends FilterNode {
super(filter, Children.LEAF);
}

/*
public Image getIcon (int type) {
if ((type == java.beans.BeanInfo.ICON_COLOR_16x16) ||
(type == java.beans.BeanInfo.ICON_MONO_16x16)) {
if (itemIcon == null)
itemIcon = Toolkit.getDefaultToolkit ().getImage (
getClass ().getResource ("/org/netbeans/core/resources/action.gif")); // NOI18N
return itemIcon;
} else {
if (itemIcon32 == null)
itemIcon32 = Toolkit.getDefaultToolkit ().getImage (
getClass ().getResource ("/org/netbeans/core/resources/action32.gif")); // NOI18N
return itemIcon32;
}
}
public Image getOpenedIcon (int type) {
return getIcon (type);
}
*/

/** Actions.
* @return array of actions for this node
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
public abstract class Util {
private Util() {}

// Called by reflection via registration in platform/o.n.core/test/unit/data/projects/sfs-attr-test/sfs_attr_test/layer.xml
private static Image mergeIcons(FileObject fo) throws IOException {
int count = ((Integer)fo.getAttribute("iconCount")).intValue();
if (count < 2) throw new IOException();
Expand Down
2 changes: 1 addition & 1 deletion platform/openide.awt/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>9.12</specification-version>
<specification-version>9.36</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collection;
import java.util.Map;
Expand Down Expand Up @@ -227,7 +228,11 @@ static final Object extractCommonAttribute(Map fo, String name) {
return (Icon) icon;
}
if (icon instanceof URL) {
icon = Toolkit.getDefaultToolkit().getImage((URL)icon);
try {
icon = ImageUtilities.loadImage(((URL)icon).toURI());
} catch (URISyntaxException e) {
LOG.log(Level.WARNING, "SMALL_ICON attribute had invalid URI", e);
}
}
if (icon instanceof Image) {
return ImageUtilities.image2Icon((Image)icon);
Expand Down
2 changes: 1 addition & 1 deletion platform/openide.filesystems.compat8/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>9.3</specification-version>
<specification-version>9.36</specification-version>
</run-dependency>
</dependency>
</module-dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@
package org.openide.filesystems;

import java.awt.Image;
import java.awt.Toolkit;
import java.beans.BeanInfo;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Set;
import java.util.logging.Level;
import static org.openide.filesystems.FileSystem.LOG;
import org.openide.modules.PatchFor;
import org.openide.util.Exceptions;
Expand Down Expand Up @@ -284,7 +285,11 @@ private Image annotateIcon(FileObject fo, int type) {
Object value = fo.getAttribute(attr);
if (value != null) {
if (value instanceof URL) {
return Toolkit.getDefaultToolkit().getImage((URL) value);
try {
return ImageUtilities.loadImage(((URL) value).toURI());
} catch (URISyntaxException e) {
LOG.log(Level.WARNING, "Annotation has invalid icon URI", e);
}
} else if (value instanceof Image) {
// #18832
return (Image) value;
Expand Down
2 changes: 1 addition & 1 deletion platform/openide.filesystems.nb/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>9.3</specification-version>
<specification-version>9.36</specification-version>
</run-dependency>
</dependency>
</module-dependencies>
Expand Down
Loading

0 comments on commit 3fa8212

Please sign in to comment.