Skip to content

Commit

Permalink
More work on centralizing icon loading to ImageUtilities.
Browse files Browse the repository at this point in the history
This is a follow-up on apache#8114 and apache#8109 . To render at full HiDPI resolution, Icon/Image instances must be created via the methods in ImageUtilities rather than, in particular, the constructors of ImageIcon.

This PR, combined with the previously mentioned PRs, handles most of the remaining cases.

Specifically:
* Search for 'new ImageIcon(' and rewrite each case to use ImageUtilities to load icons instead.
* Search for 'instanceof ImageIcon' and generalize to 'instanceof Icon' when appropriate.
* Search for 'getLookAndFeel*getDisabledIcon' and switch to ImageUtilities.createDisabledIcon.
  • Loading branch information
eirikbakke committed Jan 25, 2025
1 parent ab20d9a commit 29c5bd6
Show file tree
Hide file tree
Showing 64 changed files with 202 additions and 445 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.netbeans.modules.j2ee.sun.share.configbean.Utils;
import org.openide.awt.Mnemonics;
import org.openide.util.HelpCtx;
import org.openide.util.ImageUtilities;


/* A modal dialog object with Ok, Cancel, Help buttons and an optional required
Expand Down Expand Up @@ -287,7 +288,7 @@ public void showErrors() {

// Add warning message
JLabel label = new JLabel();
label.setIcon(Util.warningMessageIcon);
label.setIcon(ImageUtilities.loadIcon("org/netbeans/modules/j2ee/sun/share/configbean/customizers/common/resources/warningIcon.png"));
label.setText("<html>" + message + "</html>"); // NOI18N
label.getAccessibleContext().setAccessibleName(bundle.getString("ASCN_WarningMessage")); // NOI18N
label.getAccessibleContext().setAccessibleDescription(message);
Expand All @@ -308,7 +309,7 @@ public void showErrors() {

// Add error message
JLabel label = new JLabel();
label.setIcon(Util.errorMessageIcon);
label.setIcon(ImageUtilities.loadIcon("org/netbeans/modules/j2ee/sun/share/configbean/customizers/common/resources/errorIcon.png"));
label.setText("<html>" + message + "</html>"); // NOI18N
label.getAccessibleContext().setAccessibleName(bundle.getString("ASCN_ErrorMessage")); // NOI18N
label.getAccessibleContext().setAccessibleDescription(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common;

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
import org.netbeans.modules.j2ee.sun.share.configbean.Utils;
import org.openide.ErrorManager;


/**
Expand All @@ -35,34 +32,6 @@ public class Util {
private Util() {
}

/** Error/warning message icons.
*/
private static final String errorIconPath =
"org/netbeans/modules/j2ee/sun/share/configbean/customizers/common/resources/errorIcon.png"; // NOI18N
private static final String warningIconPath =
"org/netbeans/modules/j2ee/sun/share/configbean/customizers/common/resources/warningIcon.png"; // NOI18N

public static ImageIcon errorMessageIcon;
public static ImageIcon warningMessageIcon;

static {
// Diagnostic test code to try to get more information about a suspicious intermittant exception
// (This is circa NB 4.1, so it may not be necessary anymore).
try {
errorMessageIcon = new ImageIcon(Utils.getResourceURL(errorIconPath, InputDialog.class));
} catch(NullPointerException ex) {
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
errorMessageIcon = null;
}
try {
warningMessageIcon = new ImageIcon(Utils.getResourceURL(warningIconPath, InputDialog.class));
} catch(NullPointerException ex) {
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
warningMessageIcon = null;
}
}


/** !PW Foreground color for error message text when in the NetBeans IDE.
* See http://ui.netbeans.org/docs/inline_errors/index.html, about
* halfway down, for specification. (Was light blue: [89, 79, 191])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@
*/
package org.netbeans.modules.web.beans.actions;

import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

import org.openide.filesystems.FileObject;
import org.openide.util.ImageUtilities;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;

Expand All @@ -58,14 +55,7 @@ public InterceptorPanel( JButton approveButton, String bindingName,
myInterceptorName.getDocument().addDocumentListener(
createValidationListener( approveButton ) );
myInterceptorName.setText( getProposedName() );
URL errorUrl;
try {
errorUrl = new URL("nbresloc:/org/netbeans/modules/dialogs/error.gif");
myStatusLbl.setIcon( new ImageIcon( errorUrl ));
}
catch (MalformedURLException e) {
assert false;
}
myStatusLbl.setIcon(ImageUtilities.loadIcon("org/netbeans/modules/dialogs/error.gif"));

myStatusLbl.setVisible( false );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.awt.event.ActionListener;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.openide.util.ImageUtilities;
import javax.swing.JButton;

import org.openide.util.NbBundle;
Expand All @@ -41,12 +41,9 @@ class SortButton extends JButton {

public SortButton(final DisplayTable dt) {
super();
icon[0] = new ImageIcon(TransactionView.class.getResource
("/org/netbeans/modules/web/monitor/client/icons/unsorted.gif")); // NOI18N)
icon[1] = new ImageIcon(TransactionView.class.getResource
("/org/netbeans/modules/web/monitor/client/icons/a2z.gif")); // NOI18N
icon[2] = new ImageIcon(TransactionView.class.getResource
("/org/netbeans/modules/web/monitor/client/icons/z2a.gif")); // NOI18N
icon[0] = ImageUtilities.loadIcon("org/netbeans/modules/web/monitor/client/icons/unsorted.gif"); // NOI18N)
icon[1] = ImageUtilities.loadIcon("org/netbeans/modules/web/monitor/client/icons/a2z.gif"); // NOI18N
icon[2] = ImageUtilities.loadIcon("org/netbeans/modules/web/monitor/client/icons/z2a.gif"); // NOI18N
setIcon(icon[state]);
setBorder(null);
setBorderPainted(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.netbeans.modules.web.monitor.data.DataRecord;
import org.openide.util.ImageUtilities;

/**
* Update title does not work like it should. Maybe there is a getName
Expand Down Expand Up @@ -127,8 +128,7 @@ public HelpCtx getHelpCtx() {
* retrieve any data until the Monitor is opened.
*/
private TransactionView() {
setIcon(new ImageIcon(TransactionView.class.getResource
("/org/netbeans/modules/web/monitor/client/icons/menuitem.gif")).getImage());
setIcon(ImageUtilities.loadImage("org/netbeans/modules/web/monitor/client/icons/menuitem.gif"));
setToolTipText(NbBundle.getMessage(TransactionView.class, "MON_Window_Tooltip"));
controller = Controller.getInstance();
initialize();
Expand Down Expand Up @@ -285,16 +285,16 @@ private void createLogPanel() {
));
buttonPanel.setFloatable (false);

JButton updateButton = new JButton(new ImageIcon(TransactionView.class.getResource
("/org/netbeans/modules/web/monitor/client/icons/update.gif"))); // NOI18N
JButton updateButton = new JButton(ImageUtilities.loadIcon(
"org/netbeans/modules/web/monitor/client/icons/update.gif")); // NOI18N
updateButton.setToolTipText(NbBundle.getBundle(TransactionView.class).getString("MON_Reload_all_17"));
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
controller.getTransactions();
}});

timeAButton = new JToggleButton(new ImageIcon(
TransactionView.class.getResource("/org/netbeans/modules/web/monitor/client/icons/timesortA.gif")), false);
timeAButton = new JToggleButton(ImageUtilities.loadIcon(
"org/netbeans/modules/web/monitor/client/icons/timesortA.gif"), false);
timeAButton.setToolTipText(NbBundle.getBundle(TransactionView.class).getString("MON_Order_transactions_15"));

timeAButton.addActionListener(new ActionListener() {
Expand All @@ -310,8 +310,8 @@ public void actionPerformed(ActionEvent e) {
}
}});

timeDButton = new JToggleButton(new ImageIcon(
TransactionView.class.getResource("/org/netbeans/modules/web/monitor/client/icons/timesortB.gif")), true);
timeDButton = new JToggleButton(ImageUtilities.loadIcon(
"org/netbeans/modules/web/monitor/client/icons/timesortB.gif"), true);
timeDButton.setToolTipText(NbBundle.getBundle(TransactionView.class).getString("MON_Order_transactions_16"));
timeDButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Expand All @@ -327,8 +327,8 @@ public void actionPerformed(ActionEvent e) {

}});

alphaButton = new JToggleButton(new ImageIcon(
TransactionView.class.getResource("/org/netbeans/modules/web/monitor/client/icons/a2z.gif")), false);
alphaButton = new JToggleButton(ImageUtilities.loadIcon(
"/org/netbeans/modules/web/monitor/client/icons/a2z.gif"), false);
alphaButton.setToolTipText(NbBundle.getBundle(TransactionView.class).getString("MON_Order_transactions_14"));
alphaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Expand All @@ -345,10 +345,9 @@ public void actionPerformed(ActionEvent e) {
}});


timestampButton = new
JToggleButton(new ImageIcon(
TransactionView.class.getResource("/org/netbeans/modules/web/monitor/client/icons/timestamp.gif")),
TransactionNode.showTimeStamp());
timestampButton = new JToggleButton(
ImageUtilities.loadIcon("org/netbeans/modules/web/monitor/client/icons/timestamp.gif"),
TransactionNode.showTimeStamp());
timestampButton.setToolTipText(NbBundle.getBundle(TransactionView.class).getString("MON_Show_time_25"));
timestampButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.util.HashMap;
import java.util.Map;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.Icon;
import javax.swing.JButton;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.action.SelectProvider;
Expand All @@ -47,6 +47,7 @@
import org.netbeans.api.visual.model.ObjectState;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;
import org.openide.util.ImageUtilities;

/**
* @author Ajit Bhate
Expand Down Expand Up @@ -261,7 +262,7 @@ private static String getActionName(Action action) {

private static Image getActionIcon(Action action) {
Object icon = action.getValue(Action.SMALL_ICON);
return (icon instanceof ImageIcon ? ((ImageIcon)icon).getImage(): null);
return (icon instanceof Icon ? ImageUtilities.icon2Image((Icon)icon): null);
}

private static String getActionTooltip(Action action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
Expand All @@ -49,6 +46,7 @@
import org.openide.cookies.InstanceCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.ImageUtilities;
import org.openide.util.actions.Presenter;
import org.openide.util.RequestProcessor;
import org.openide.util.WeakListeners;
Expand Down Expand Up @@ -163,11 +161,7 @@ public void run() {
return Actions.cutAmpersand(pname);
}
} else if (Action.SMALL_ICON.equals (key)) {
try {
return new ImageIcon(new URL("nbresloc:/org/apache/tools/ant/module/resources/AntIcon.gif"));
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
return ImageUtilities.loadImageIcon("org/apache/tools/ant/module/resources/AntIcon.gif", false);
} else if (Action.MNEMONIC_KEY.equals (key)) {
Element el = proj.getProjectElement ();
if (el != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.openide.filesystems.FileRenameEvent;
import org.openide.filesystems.FileUtil;
import org.openide.util.Cancellable;
import org.openide.util.ImageUtilities;
import org.openide.util.Mutex;
import org.openide.util.NbBundle;
import org.openide.util.Pair;
Expand Down Expand Up @@ -203,7 +204,7 @@ public StopAction() {
@Override
public Object getValue(String key) {
if (key.equals(Action.SMALL_ICON)) {
return new ImageIcon(TargetExecutor.class.getResource("/org/apache/tools/ant/module/resources/stop.png"));
return ImageUtilities.loadImageIcon("org/apache/tools/ant/module/resources/stop.png", false);
} else if (key.equals(Action.SHORT_DESCRIPTION)) {
return NbBundle.getMessage(TargetExecutor.class, "TargetExecutor.StopAction.stop");
} else {
Expand Down Expand Up @@ -259,9 +260,9 @@ private void reinit(TargetExecutor prototype) {
public Object getValue(String key) {
if (key.equals(Action.SMALL_ICON)) {
if (withModifications) {
return new ImageIcon(TargetExecutor.class.getResource("/org/apache/tools/ant/module/resources/rerun-mod.png"));
return ImageUtilities.loadImageIcon("org/apache/tools/ant/module/resources/rerun-mod.png", false);
} else {
return new ImageIcon(TargetExecutor.class.getResource("/org/apache/tools/ant/module/resources/rerun.png"));
return ImageUtilities.loadImageIcon("org/apache/tools/ant/module/resources/rerun.png", false);
}
} else if (key.equals(Action.SHORT_DESCRIPTION)) {
if (withModifications) {
Expand Down Expand Up @@ -332,7 +333,7 @@ private static final class OptionsAction extends AbstractAction { // #59396
@Override
public Object getValue(String key) {
if (key.equals(Action.SMALL_ICON)) {
return new ImageIcon(TargetExecutor.class.getResource("/org/apache/tools/ant/module/resources/options.png"));
return ImageUtilities.loadImageIcon("org/apache/tools/ant/module/resources/options.png", false);
} else if (key.equals(Action.SHORT_DESCRIPTION)) {
return NbBundle.getMessage(TargetExecutor.class, "TargetExecutor.OptionsAction");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,88 +238,8 @@ protected ImageIcon getIcon() {
ImageIcon imageIcon = org.netbeans.modules.csl.navigation.Icons.getElementIcon(item.getKind(), item.getModifiers());
// TODO - cache!
return imageIcon;
//
//
// ElementKind kind = item.getKind();
// switch (kind) {
// case CONSTRUCTOR:
// case METHOD:
// return getMethodIcon();
// case ATTRIBUTE:
// case FIELD:
// return getFieldIcon();
// case CLASS:
// case INTERFACE:
// return getClassIcon();
// case MODULE:
// return getModuleIcon();
// case CONSTANT:
// return getConstantIcon();
// case VARIABLE:
// return getVariableIcon();
// case KEYWORD:
// return getKeywordIcon();
// case OTHER:
// }
//
// return null;
}

// protected ImageIcon getMethodIcon() {
// Set<Modifier> modifiers = item.getModifiers();
//
// boolean isStatic = modifiers.contains(Modifier.STATIC);
//// int level = getProtectionLevel(elem.getModifiers());
//
// ImageIcon cachedIcon = icon[isStatic?1:0][level];
// if (cachedIcon != null) {
// return cachedIcon;
// }
//
// String iconPath = METHOD_PUBLIC;
// if (isStatic) {
// switch (level) {
// case PRIVATE_LEVEL:
// iconPath = METHOD_ST_PRIVATE;
// break;
//
// case PACKAGE_LEVEL:
// iconPath = METHOD_ST_PACKAGE;
// break;
//
// case PROTECTED_LEVEL:
// iconPath = METHOD_ST_PROTECTED;
// break;
//
// case PUBLIC_LEVEL:
// iconPath = METHOD_ST_PUBLIC;
// break;
// }
// }else{
// switch (level) {
// case PRIVATE_LEVEL:
// iconPath = METHOD_PRIVATE;
// break;
//
// case PACKAGE_LEVEL:
// iconPath = METHOD_PACKAGE;
// break;
//
// case PROTECTED_LEVEL:
// iconPath = METHOD_PROTECTED;
// break;
//
// case PUBLIC_LEVEL:
// iconPath = METHOD_PUBLIC;
// break;
// }
// }
// ImageIcon newIcon = new ImageIcon(org.openide.util.Utilities.loadImage(iconPath));
// icon[isStatic?1:0][level] = newIcon;
// return newIcon;
// }


@Override
protected void substituteText(final JTextComponent c, int offset, int len, String toAdd) {
if (completionResult != null) {
Expand Down
Loading

0 comments on commit 29c5bd6

Please sign in to comment.