Help - Added a task to find unused help images; fixed showing in-memory

images in help
This commit is contained in:
dragonmacher 2019-04-24 17:48:44 -04:00
parent e8dcb3d0c4
commit 49436c44a9
12 changed files with 275 additions and 131 deletions

View file

@ -15,17 +15,30 @@
*/
package resources;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.swing.ImageIcon;
import generic.Images;
import generic.util.image.ImageUtils;
import ghidra.util.Msg;
/**
* A class that knows how to provide an icon and the URL for that icon
* A class that knows how to provide an icon and the URL for that icon. If {@link #getUrl()}
* returns a non-null value, then that is the URL used to originally load the icon in this class.
*
* <p>If {@link #getUrl()} returns null, then {@link #getOrCreateUrl()} can be used to create a
* value URL by writing out the image for this class's icon.
*/
public class IconProvider {
private ImageIcon icon;
private URL url;
private URL tempUrl;
private boolean tempFileFailed;
public IconProvider(ImageIcon icon, URL url) {
this.icon = icon;
@ -36,11 +49,76 @@ public class IconProvider {
return icon;
}
public boolean isInvalid() {
return icon == null; // as long as we have an icon, we are valid, url or not
}
public URL getUrl() {
return url;
}
public boolean isInvalid() {
return icon == null || url == null;
/**
* Returns the value of {@link #getUrl()} if it is non-null. Otherwise, this class will
* attempt to create a temporary file containing the image of this class in order to return
* a URL for that temp file. If a temporary file could not be created, then the URL
* returned from this class will point to the
* {@link ResourceManager#getDefaultIcon() default icon}.
*
* @return the URL
*/
public URL getOrCreateUrl() {
if (url != null) {
return url;
}
createTempUrlAsNeeded();
return tempUrl;
}
private void createTempUrlAsNeeded() {
if (testUrl(tempUrl)) {
return;
}
tempUrl = createTempUrl();
if (tempUrl == null) {
tempUrl = getDefaultUrl();
}
}
private URL createTempUrl() {
if (tempFileFailed) {
return null; // don't repeatedly attempt to create a temp file
}
try {
File imageFile = File.createTempFile("temp.help.icon", null);
imageFile.deleteOnExit(); // don't let this linger
ImageUtils.writeFile(icon.getImage(), imageFile);
return imageFile.toURI().toURL();
}
catch (IOException e) {
tempFileFailed = true;
Msg.error(this, "Unable to write temp image to display in help for " +
ResourceManager.getIconName(icon));
}
return null;
}
private boolean testUrl(URL testUrl) {
if (testUrl == null) {
return false;
}
try {
return new File(testUrl.toURI()).exists();
}
catch (URISyntaxException e) {
return false;
}
}
private URL getDefaultUrl() {
return ResourceManager.getResource(Images.BOMB);
}
}

View file

@ -91,7 +91,7 @@ public class Icons {
ResourceManager.loadImage("images/dialog-cancel.png", 10, 10), 6, 6)));
public static final ImageIcon APPLY_BLOCKED_MATCH_ICON = ResourceManager.getImageIcon(
new MultiIcon(ResourceManager.loadImage("images/kgpg.png"), new TranslateIcon(
ResourceManager.loadImage("images/checkmark_green.png", 12, 12), 4, 0)));
ResourceManager.loadImage("images/checkmark_green.gif", 12, 12), 4, 0)));
/**
* Returns true if the given string is a Java code snippet that references this class
@ -126,24 +126,6 @@ public class Icons {
return new IconProvider(icon, url);
}
/**
* Returns a URL for the given code snippet if it is a field reference on this class
*
* @param snippet the snippet of Java code that references a field of this class
* @return the URL; null if the snippet does not refer to a field of this class
*/
public static URL getUrlForIconsReference(String snippet) {
String fieldName = getIconName(snippet);
if (fieldName == null) {
return null;
}
ImageIcon icon = getIconByFieldName(fieldName);
URL url = getUrlFromIcon(icon);
return url;
}
private static String getIconName(String snippet) {
if (!isIconsReference(snippet)) {
return null;
@ -185,7 +167,7 @@ public class Icons {
return url;
}
catch (MalformedURLException e) {
Msg.debug(Icons.class, "Unable to get URL for icon: " + description, e);
Msg.trace(Icons.class, "Unable to get URL for icon: " + description);
return null;
}

View file

@ -160,19 +160,18 @@ public class MultiIcon implements Icon {
@Override
public String toString() {
// return getClass().getSimpleName() + "[" + getIconNames() + "]";
return getDescription();
return getClass().getSimpleName() + "[" + getIconNames() + "]";
}
// private String getIconNames() {
// StringBuffer buffy = new StringBuffer();
// for (Icon icon : iconList) {
// if (buffy.length() > 0) {
// buffy.append(", ");
// }
// buffy.append(ResourceManager.getIconName(icon));
// }
//
// return buffy.toString();
// }
private String getIconNames() {
StringBuffer buffy = new StringBuffer();
for (Icon icon : iconList) {
if (buffy.length() > 0) {
buffy.append(", ");
}
buffy.append(ResourceManager.getIconName(icon));
}
return buffy.toString();
}
}

View file

@ -400,14 +400,18 @@ public class ResourceManager {
}
/**
* Get the name of this icon. If icon is an ImageIcon, its getDescription() is called to
* get the name
* Get the name of this icon. The value is usually going to be the URL from which the icon
* was loaded
*
* @param icon the icon for which the name is desired
* @return the name
* @return the name
*/
public static String getIconName(Icon icon) {
String iconName = icon.toString();
if (icon instanceof FileBasedIcon) {
return ((FileBasedIcon) icon).getFilename();
}
if (icon instanceof ImageIcon) {
iconName = ((ImageIcon) icon).getDescription();
}