Skip to content

Commit

Permalink
Avoid remaining trivial uses of new ImageIcon(Image), for HiDPI icons…
Browse files Browse the repository at this point in the history
…. (Commit 2: Slightly more complex cases, and mergeImages cases, separated out for ease of review.)
  • Loading branch information
eirikbakke committed Jan 24, 2025
1 parent 55cd58f commit 8f8b150
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 137 deletions.
91 changes: 18 additions & 73 deletions ide/csl.api/src/org/netbeans/modules/csl/navigation/Icons.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
package org.netbeans.modules.csl.navigation;

import java.awt.Image;
import java.util.Collection;
import java.util.Collections;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.netbeans.modules.csl.api.ElementKind;
import org.netbeans.modules.csl.api.Modifier;
Expand All @@ -47,121 +47,66 @@ public final class Icons {
private Icons() {
}

// public static Icon getBusyIcon() {
// Image img = Utilities.loadImage(WAIT);
//
// if (img == null) {
// return null;
// } else {
// return new ImageIcon(img);
// }
// }
//
// public static Icon getMethodIcon() {
// // TODO - consider modifiers
// Image img =
// Utilities.loadImage(ICON_BASE + "method" + "Public" + PNG_EXTENSION);
//
// if (img == null) {
// return null;
// } else {
// return new ImageIcon(img);
// }
// }
//
// public static Icon getFieldIcon() {
// // TODO - consider modifiers
// Image img =
// Utilities.loadImage(ICON_BASE + "field" + "Public" + PNG_EXTENSION);
//
// if (img == null) {
// return null;
// } else {
// return new ImageIcon(img);
// }
// }
//
// public static Icon getClassIcon() {
// Image img = Utilities.loadImage(ICON_BASE + "class" + PNG_EXTENSION);
//
// if (img == null) {
// return null;
// } else {
// return new ImageIcon(img);
// }
// }
//
// public static Icon getModuleIcon() {
// Image img =
// Utilities.loadImage(ICON_BASE + "package" + GIF_EXTENSION);
//
// if (img == null) {
// return null;
// } else {
// return new ImageIcon(img);
// }
// }

public static ImageIcon getElementIcon( ElementKind elementKind, Collection<Modifier> modifiers ) {

if ( modifiers == null ) {
modifiers = Collections.<Modifier>emptyList();
}

Image img = null;
Icon icon = null;

switch( elementKind ) {
case FILE:
img = ImageUtilities.loadImage( ICON_BASE + "emptyfile-icon" + PNG_EXTENSION );
icon = ImageUtilities.loadIcon( ICON_BASE + "emptyfile-icon" + PNG_EXTENSION );
break;
case ERROR:
img = ImageUtilities.loadImage( ICON_BASE + "error-glyph" + GIF_EXTENSION );
icon = ImageUtilities.loadIcon( ICON_BASE + "error-glyph" + GIF_EXTENSION );
break;
case PACKAGE:
case MODULE:
img = ImageUtilities.loadImage( ICON_BASE + "package" + GIF_EXTENSION );
icon = ImageUtilities.loadIcon( ICON_BASE + "package" + GIF_EXTENSION );
break;
case TEST:
img = ImageUtilities.loadImage( ICON_BASE + "test" + PNG_EXTENSION );
icon = ImageUtilities.loadIcon( ICON_BASE + "test" + PNG_EXTENSION );
break;
case CLASS:
case INTERFACE:
img = ImageUtilities.loadImage( ICON_BASE + "class" + PNG_EXTENSION );
icon = ImageUtilities.loadIcon( ICON_BASE + "class" + PNG_EXTENSION );
break;
case TAG:
img = ImageUtilities.loadImage( ICON_BASE + "html_element" + PNG_EXTENSION );
icon = ImageUtilities.loadIcon( ICON_BASE + "html_element" + PNG_EXTENSION );
break;
case RULE:
img = ImageUtilities.loadImage( ICON_BASE + "rule" + PNG_EXTENSION );
icon = ImageUtilities.loadIcon( ICON_BASE + "rule" + PNG_EXTENSION );
break;
case VARIABLE:
case PROPERTY:
case GLOBAL:
case ATTRIBUTE:
case FIELD:
img = ImageUtilities.loadImage( getIconName( ICON_BASE + "field", PNG_EXTENSION, modifiers ) );
icon = ImageUtilities.loadIcon( getIconName( ICON_BASE + "field", PNG_EXTENSION, modifiers ) );
break;
case PARAMETER:
case CONSTANT:
img = ImageUtilities.loadImage(getIconName(ICON_BASE + "constant", PNG_EXTENSION, modifiers)); // NOI18N
if (img == null) {
img = ImageUtilities.loadImage(ICON_BASE + "constantPublic" + PNG_EXTENSION); // NOI18N
icon = ImageUtilities.loadIcon(getIconName(ICON_BASE + "constant", PNG_EXTENSION, modifiers)); // NOI18N
if (icon == null) {
icon = ImageUtilities.loadIcon(ICON_BASE + "constantPublic" + PNG_EXTENSION); // NOI18N
}
break;
case CONSTRUCTOR:
img = ImageUtilities.loadImage( getIconName( ICON_BASE + "constructor", PNG_EXTENSION, modifiers ) );
icon = ImageUtilities.loadIcon( getIconName( ICON_BASE + "constructor", PNG_EXTENSION, modifiers ) );
break;
case METHOD:
img = ImageUtilities.loadImage( getIconName( ICON_BASE + "method", PNG_EXTENSION, modifiers ) );
icon = ImageUtilities.loadIcon( getIconName( ICON_BASE + "method", PNG_EXTENSION, modifiers ) );
break;
case DB:
img = ImageUtilities.loadImage(ICON_BASE + "database" + GIF_EXTENSION);
icon = ImageUtilities.loadIcon(ICON_BASE + "database" + GIF_EXTENSION);
break;
default:
img = null;
icon = null;
}

return img == null ? null : new ImageIcon (img);
return icon == null ? null : ImageUtilities.icon2ImageIcon (icon);
}

// Private Methods ---------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,14 @@
package org.netbeans.modules.db.sql.visualeditor.ui;

import org.netbeans.modules.db.sql.visualeditor.Log;
import java.awt.Image;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import org.openide.NotifyDescriptor;
import org.openide.DialogDisplayer;
import org.openide.DialogDescriptor;
import org.openide.util.ImageUtilities;
import org.openide.util.NbBundle;
import org.openide.util.HelpCtx;
import org.openide.util.Utilities;
/**
* Shows the status of a connction in a dialog.
* This just presentss the results of a testConnection(), it
Expand All @@ -52,11 +47,9 @@ public class ConnectionStatusPanel extends javax.swing.JPanel {

private JButton okButton = new JButton(NbBundle.getMessage(ConnectionStatusPanel.class, "OK")); // NOI18N

// private Image conGood = Utilities.loadImage("org/netbeans/modules/db/sql/visualeditor/resources/started.png"); // NOI18N
// private Image conFailed = Utilities.loadImage("org/netbeans/modules/db/sql/visualeditor/resources/disconnected.png"); // NOI18N
private Image conGood = ImageUtilities.loadImage("org/netbeans/modules/db/sql/visualeditor/resources/ok.gif"); // NOI18N
private Image conFailed = ImageUtilities.loadImage("org/netbeans/modules/db/sql/visualeditor/resources/error.gif"); // NOI18N
private Image conWarning = ImageUtilities.loadImage("org/netbeans/modules/db/sql/visualeditor/resources/warning.gif"); // NOI18N
private Icon conGood = ImageUtilities.loadIcon("org/netbeans/modules/db/sql/visualeditor/resources/ok.gif"); // NOI18N
private Icon conFailed = ImageUtilities.loadIcon("org/netbeans/modules/db/sql/visualeditor/resources/error.gif"); // NOI18N
private Icon conWarning = ImageUtilities.loadIcon("org/netbeans/modules/db/sql/visualeditor/resources/warning.gif"); // NOI18N
/** Creates new form ConnectionStatusDialog */
public ConnectionStatusPanel() {
initComponents();
Expand Down Expand Up @@ -127,28 +120,28 @@ public void configureDisplay( String dsName, boolean connected, String sqlExcept
/* calculate the displayd values based on this method's input parameters.
*/
if ( connected ) {
connectionStatusIcon.setIcon( new ImageIcon(conGood) ) ;
connectionStatusIcon.setIcon( conGood ) ;
connectionStatusText.setText(getMsg("ConStat_succeeded_msg")) ; // NOI18N
connectionStatusMessage.setVisible(false) ;
validationInfo.setVisible(true) ;
if ( sqlException == null ) {
valStatusText.setText(getMsg("ConStat_rows_selected_msg", tableName, Integer.valueOf(rows)) ) ; // NOI18N
valStatusIcon.setIcon( new ImageIcon(conGood) ) ;
valStatusIcon.setIcon( conGood ) ;
String valMsg ;
if (rows > 1 ) {

valMsg = getMsg("ConStat_valtable_bad_msg") ;// NOI18N
valStatusMessageIcon.setIcon( new ImageIcon(conWarning) ) ;
valStatusMessageIcon.setIcon( conWarning ) ;
} else {
valMsg = getMsg("ConStat_valtable_good_msg") ;// NOI18N
valStatusMessageIcon.setIcon( new ImageIcon(conGood) ) ;
valStatusMessageIcon.setIcon( conGood ) ;
}
valMsg = valMsg + "\n" + getMsg("ConStat_validationTableInfo") ; // NOI18N
valStatusMessage.setText(valMsg) ;

} else if (tableName != null) {
// validation table test failed.
valStatusIcon.setIcon( new ImageIcon(conFailed) ) ;
valStatusIcon.setIcon( conFailed ) ;

// validation failed.
valStatusText.setText(getMsg("ConStat_failed_msg")) ;// NOI18N
Expand All @@ -168,7 +161,7 @@ public void configureDisplay( String dsName, boolean connected, String sqlExcept
}
} else {
// connection failed.
connectionStatusIcon.setIcon( new ImageIcon(conFailed) ) ;
connectionStatusIcon.setIcon( conFailed ) ;
connectionStatusText.setText(getMsg("ConStat_failed_msg")) ;// NOI18N
connectionStatusMessage.setText(sqlException) ;
validationInfo.setVisible(false) ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

package org.netbeans.modules.xml.text.navigator;
import java.awt.Component;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
Expand All @@ -45,12 +43,12 @@ public class NavigatorTreeCellRenderer extends DefaultTreeCellRenderer {

private static final String ERROR_16 = "org/netbeans/modules/xml/text/navigator/resources/badge_error.png";

private final Image ERROR_IMAGE = ImageUtilities.loadImage(ERROR_16, true);
private final Icon ERROR_ICON = ImageUtilities.loadIcon(ERROR_16, true);

private final Icon[] TAG_ICON = new Icon[]{getImageIcon(TAG_16, false), getImageIcon(TAG_16, true)};
private final Icon[] PI_ICON = new Icon[]{getImageIcon(PI_16, false), getImageIcon(PI_16, true)};
private final Icon[] DOCTYPE_ICON = new Icon[]{getImageIcon(DOCTYPE_16, false), getImageIcon(DOCTYPE_16, true)};
private final Icon[] CDATA_ICON = new Icon[]{getImageIcon(CDATA_16, false), getImageIcon(CDATA_16, true)};
private final Icon[] TAG_ICON = new Icon[]{getIcon(TAG_16, false), getIcon(TAG_16, true)};
private final Icon[] PI_ICON = new Icon[]{getIcon(PI_16, false), getIcon(PI_16, true)};
private final Icon[] DOCTYPE_ICON = new Icon[]{getIcon(DOCTYPE_16, false), getIcon(DOCTYPE_16, true)};
private final Icon[] CDATA_ICON = new Icon[]{getIcon(CDATA_16, false), getIcon(CDATA_16, true)};

private HtmlRenderer.Renderer renderer;

Expand Down Expand Up @@ -91,12 +89,13 @@ public void setIcon(Icon[] icons, boolean containsError) {
renderer.setIcon(icons[containsError ? 1 : 0]);
}

private ImageIcon getImageIcon(String name, boolean error){
ImageIcon icon = ImageUtilities.loadImageIcon(name, false);
if(error)
return new ImageIcon(ImageUtilities.mergeImages( icon.getImage(), ERROR_IMAGE, 15, 7 ));
else
private Icon getIcon(String name, boolean error){
Icon icon = ImageUtilities.loadIcon(name);
if (error) {
return ImageUtilities.mergeIcons(icon, ERROR_ICON, 15, 7);
} else {
return icon;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.regex.Pattern;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.JTree;
Expand Down Expand Up @@ -66,11 +65,10 @@ public class ClassPathListCellRenderer extends DefaultListCellRenderer {
private static final Pattern FOREIGN_PLAIN_FILE_REFERENCE = Pattern.compile("\\$\\{file\\.reference\\.([^${}]+)\\}"); // NOI18N
private static final Pattern UNKNOWN_FILE_REFERENCE = Pattern.compile("\\$\\{([^${}]+)\\}"); // NOI18N

private static ImageIcon ICON_FOLDER = null;

private static ImageIcon ICON_BROKEN_JAR;
private static ImageIcon ICON_BROKEN_LIBRARY;
private static ImageIcon ICON_BROKEN_ARTIFACT;
private static Icon ICON_FOLDER = null;
private static Icon ICON_BROKEN_JAR;
private static Icon ICON_BROKEN_LIBRARY;
private static Icon ICON_BROKEN_ARTIFACT;

private PropertyEvaluator evaluator;
private FileObject projectFolder;
Expand Down Expand Up @@ -199,7 +197,7 @@ static Icon getIcon( ClassPathSupport.Item item ) {
case ClassPathSupport.Item.TYPE_LIBRARY:
if ( item.isBroken() ) {
if ( ICON_BROKEN_LIBRARY == null ) {
ICON_BROKEN_LIBRARY = new ImageIcon( ImageUtilities.mergeImages( ProjectProperties.ICON_LIBRARY.getImage(), ProjectProperties.ICON_BROKEN_BADGE.getImage(), 7, 7 ) );
ICON_BROKEN_LIBRARY = ImageUtilities.mergeIcons( ProjectProperties.ICON_LIBRARY, ProjectProperties.ICON_BROKEN_BADGE, 7, 7 );
}
return ICON_BROKEN_LIBRARY;
}
Expand All @@ -209,7 +207,7 @@ static Icon getIcon( ClassPathSupport.Item item ) {
case ClassPathSupport.Item.TYPE_ARTIFACT:
if ( item.isBroken() ) {
if ( ICON_BROKEN_ARTIFACT == null ) {
ICON_BROKEN_ARTIFACT = new ImageIcon( ImageUtilities.mergeImages( ProjectProperties.ICON_ARTIFACT.getImage(), ProjectProperties.ICON_BROKEN_BADGE.getImage(), 7, 7 ) );
ICON_BROKEN_ARTIFACT = ImageUtilities.mergeIcons(ProjectProperties.ICON_ARTIFACT, ProjectProperties.ICON_BROKEN_BADGE, 7, 7 );
}
return ICON_BROKEN_ARTIFACT;
}
Expand All @@ -224,18 +222,18 @@ static Icon getIcon( ClassPathSupport.Item item ) {
case ClassPathSupport.Item.TYPE_JAR:
if ( item.isBroken() ) {
if ( ICON_BROKEN_JAR == null ) {
ICON_BROKEN_JAR = new ImageIcon( ImageUtilities.mergeImages( ProjectProperties.ICON_JAR.getImage(), ProjectProperties.ICON_BROKEN_BADGE.getImage(), 7, 7 ) );
ICON_BROKEN_JAR = ImageUtilities.mergeIcons(ProjectProperties.ICON_JAR, ProjectProperties.ICON_BROKEN_BADGE, 7, 7);
}
return ICON_BROKEN_JAR;
}
else {
File file = item.getResolvedFile();
ImageIcon icn = file.isDirectory() ? getFolderIcon() : ProjectProperties.ICON_JAR;
Icon icn = file.isDirectory() ? getFolderIcon() : ProjectProperties.ICON_JAR;
if (item.getSourceFilePath() != null) {
icn = new ImageIcon( ImageUtilities.mergeImages( icn.getImage(), ProjectProperties.ICON_SOURCE_BADGE.getImage(), 8, 8 ));
icn = ImageUtilities.mergeIcons(icn, ProjectProperties.ICON_SOURCE_BADGE, 8, 8 );
}
if (item.getJavadocFilePath() != null) {
icn = new ImageIcon( ImageUtilities.mergeImages( icn.getImage(), ProjectProperties.ICON_JAVADOC_BADGE.getImage(), 8, 0 ));
icn = ImageUtilities.mergeIcons(icn, ProjectProperties.ICON_JAVADOC_BADGE, 8, 0 );
}
return icn;
}
Expand Down Expand Up @@ -274,13 +272,11 @@ private String getToolTipText( ClassPathSupport.Item item ) {
return null;
}

private static ImageIcon getFolderIcon() {

private static Icon getFolderIcon() {
if ( ICON_FOLDER == null ) {
DataFolder dataFolder = DataFolder.findFolder( FileUtil.getConfigRoot() );
ICON_FOLDER = new ImageIcon( dataFolder.getNodeDelegate().getIcon( BeanInfo.ICON_COLOR_16x16 ) );
ICON_FOLDER = ImageUtilities.image2Icon(dataFolder.getNodeDelegate().getIcon( BeanInfo.ICON_COLOR_16x16 ) );
}

return ICON_FOLDER;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
*/
package org.netbeans.modules.refactoring.java;

import java.awt.Image;
import java.beans.BeanInfo;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.netbeans.modules.refactoring.java.spi.JavaWhereUsedFilters;
import org.netbeans.modules.refactoring.java.ui.tree.ElementGripFactory;
import org.netbeans.modules.refactoring.spi.FiltersManager;
Expand Down Expand Up @@ -64,14 +62,12 @@ public String getDisplayText() {
public Lookup getLookup() {
Icon icon = null;
try {
ImageIcon imageIcon = new ImageIcon(DataObject.find(fo).getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16));
icon = ImageUtilities.image2Icon(DataObject.find(fo).getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16));
Boolean inTestFile = ElementGripFactory.getDefault().inTestFile(fo);
if (Boolean.TRUE == inTestFile) {
Image mergeImages = ImageUtilities.mergeImages(imageIcon.getImage(),
ImageUtilities.loadImageIcon("org/netbeans/modules/refactoring/java/resources/found_item_test.png", false).getImage(), 4, 4);
imageIcon = new ImageIcon(mergeImages);
icon = ImageUtilities.mergeIcons(icon,
ImageUtilities.loadIcon("org/netbeans/modules/refactoring/java/resources/found_item_test.png"), 4, 4);
}
icon = imageIcon;
} catch (DataObjectNotFoundException ex) {
// ignore
}
Expand Down
Loading

0 comments on commit 8f8b150

Please sign in to comment.