#778 Updates Icon Manager to JavaFX. Resolves issue with displaying custom icons in alias editor. (#796)

Co-authored-by: Denny <denny@denny-desktop>
This commit is contained in:
Denny Sheirer
2020-05-06 08:08:05 +00:00
committed by GitHub
parent 2aff9ee817
commit 2723926f83
36 changed files with 1129 additions and 1051 deletions

View File

@@ -1,6 +1,6 @@
/*******************************************************************************
* sdrtrunk
* Copyright (C) 2014-2017 Dennis Sheirer
/*
* *****************************************************************************
* Copyright (C) 2014-2020 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -14,64 +14,154 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/
* ****************************************************************************
*/
package io.github.dsheirer.icon;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import javafx.beans.Observable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.util.Callback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.ImageIcon;
import java.net.URL;
import java.util.Objects;
@JacksonXmlRootElement(localName = "icon")
public class Icon implements Comparable<Icon>
{
private final static Logger mLog = LoggerFactory.getLogger(Icon.class);
protected String mName;
private String mPath;
private StringProperty mName = new SimpleStringProperty();
private StringProperty mPath = new SimpleStringProperty();
private BooleanProperty mDefaultIcon = new SimpleBooleanProperty();
private BooleanProperty mStandardIcon = new SimpleBooleanProperty();
private ImageIcon mImageIcon;
/**
* JAXB constructor - do not use
*/
public Icon()
{
//No-arg JAXB constructor
}
/**
* Constructs an instance
* @param name for the icon
* @param path to the icon
*/
public Icon(String name, String path)
{
mName = name;
mPath = path;
setName(name);
setPath(path);
}
@JacksonXmlProperty(isAttribute = true, localName = "name")
public String getName()
@JsonIgnore
public StringProperty nameProperty()
{
return mName;
}
public void setName(String name)
{
mName = name;
}
public String toString()
{
return mName;
}
@JacksonXmlProperty(isAttribute = true, localName = "path")
public String getPath()
@JsonIgnore
public StringProperty pathProperty()
{
return mPath;
}
@JsonIgnore
public BooleanProperty defaultIconProperty()
{
return mDefaultIcon;
}
@JsonIgnore
public BooleanProperty standardIconProperty()
{
return mStandardIcon;
}
/**
* Name of the icon
*/
@JacksonXmlProperty(isAttribute = true, localName = "name")
public String getName()
{
return mName.get();
}
/**
* Sets the name of the icon
*/
public void setName(String name)
{
mName.set(name);
}
/**
* Indicates if this is a standard icon
*/
@JsonIgnore
public boolean getStandardIcon()
{
return mStandardIcon.get();
}
/**
* Sets or flags this icon as a standard icon indicating that it should not be deleted
*/
void setStandardIcon(boolean standardIcon)
{
mStandardIcon.set(standardIcon);
}
/**
* Indicates if this icon is the default icon
*/
@JsonIgnore
public boolean getDefaultIcon()
{
return mDefaultIcon.get();
}
/**
* Sets the default icon state.
*
* Note: uniqueness of default flag is only enforced through the icon model and package private access
*/
void setDefaultIcon(boolean defaultIcon)
{
mDefaultIcon.set(defaultIcon);
}
public String toString()
{
return getName();
}
/**
* Path to the icon
*/
@JacksonXmlProperty(isAttribute = true, localName = "path")
public String getPath()
{
return mPath.get();
}
/**
* Sets the path to the icon
*/
public void setPath(String path)
{
mPath = path;
mPath.set(path);
}
@JsonIgnore
@@ -81,17 +171,17 @@ public class Icon implements Comparable<Icon>
{
try
{
if(!mPath.startsWith("images"))
if(!getPath().startsWith("images"))
{
mImageIcon = new ImageIcon(mPath);
mImageIcon = new ImageIcon(getPath());
}
else
{
URL imageURL = Icon.class.getResource(mPath);
URL imageURL = Icon.class.getResource(getPath());
if(imageURL == null && !mPath.startsWith("/"))
if(imageURL == null && !getPath().startsWith("/"))
{
imageURL = (Icon.class.getResource("/" + mPath));
imageURL = (Icon.class.getResource("/" + getPath()));
}
if(imageURL != null)
@@ -102,7 +192,7 @@ public class Icon implements Comparable<Icon>
}
catch(Exception e)
{
mLog.error("Error loading Icon [" + mPath + "]", e);
mLog.error("Error loading Icon [" + getPath() + "]", e);
}
}
@@ -110,9 +200,52 @@ public class Icon implements Comparable<Icon>
}
@Override
public int compareTo(Icon otherIcon)
public int compareTo(Icon other)
{
return getName().compareTo(otherIcon.getName());
if(other == null)
{
return -1;
}
else if(hashCode() == other.hashCode())
{
return 0;
}
else if(getName() != null && other.getName() != null)
{
if(getName().contentEquals(other.getName()))
{
if(getPath() != null && other.getPath() != null)
{
return getPath().compareTo(other.getPath());
}
else if(getPath() != null)
{
return -1;
}
else
{
return 1;
}
}
else
{
return getName().compareTo(other.getName());
}
}
else if(getName() != null)
{
return -1;
}
else
{
return 1;
}
}
@Override
public int hashCode()
{
return Objects.hash(getName(), getPath());
}
@Override
@@ -120,4 +253,13 @@ public class Icon implements Comparable<Icon>
if (!(o instanceof Icon)) return false;
return compareTo((Icon) o) == 0;
}
/**
* Creates an observable property extractor for use with observable lists to detect changes internal to this object.
*/
@JsonIgnore
public static Callback<Icon,Observable[]> extractor()
{
return (Icon i) -> new Observable[] {i.nameProperty(), i.pathProperty(), i.standardIconProperty(), i.defaultIconProperty()};
}
}