|
|
|
@ -1,9 +1,11 @@
|
|
|
|
|
package com.fine.theme.icon.svg; |
|
|
|
|
|
|
|
|
|
import com.fine.theme.icon.AbstractIconSource; |
|
|
|
|
import com.fine.theme.icon.DisabledIcon; |
|
|
|
|
import com.fine.theme.icon.GraphicsFilter; |
|
|
|
|
import com.fine.theme.icon.IconResource; |
|
|
|
|
import com.fine.theme.icon.IconType; |
|
|
|
|
import com.fine.theme.icon.UrlIconResource; |
|
|
|
|
import com.fine.theme.icon.WhiteIcon; |
|
|
|
|
import com.formdev.flatlaf.FlatLaf; |
|
|
|
|
import com.formdev.flatlaf.ui.FlatUIUtils; |
|
|
|
@ -69,8 +71,29 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon {
|
|
|
|
|
g = grayGraphics(g); |
|
|
|
|
} |
|
|
|
|
Object[] oldRenderingHints = FlatUIUtils.setRenderingHints(g); |
|
|
|
|
render(c, g, x, y); |
|
|
|
|
FlatUIUtils.resetRenderingHints(g, oldRenderingHints); |
|
|
|
|
try { |
|
|
|
|
render(c, g, x, y, this::fallbackRender); |
|
|
|
|
} finally { |
|
|
|
|
FlatUIUtils.resetRenderingHints(g, oldRenderingHints); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 用于fallback渲染disable图像,这段代码来自异常分支,使用率要保持较低水平 |
|
|
|
|
*/ |
|
|
|
|
private void fallbackRender(Component c, Graphics g, int x, int y) { |
|
|
|
|
if (resource instanceof UrlIconResource) { |
|
|
|
|
String path = ((UrlIconResource) resource).getPath(); |
|
|
|
|
String[] names = path.split(AbstractIconSource.ICON_DISABLE_SUFFIX); |
|
|
|
|
if (path.contains(AbstractIconSource.ICON_DISABLE_SUFFIX) && names.length > 1) { |
|
|
|
|
SVGLoader loader = new SVGLoader(); |
|
|
|
|
SVGDocument document = loader.load(new UrlIconResource(names[0] + names[1]).getInputStream()); |
|
|
|
|
if (document != null) { |
|
|
|
|
document.render((JComponent) c, grayGraphics(g), |
|
|
|
|
new ViewBox(x, y, scaleSize.width, scaleSize.height)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Graphics2D grayGraphics(Graphics g) { |
|
|
|
@ -92,7 +115,7 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon {
|
|
|
|
|
return scaleSize.height; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void render(Component c, Graphics g, int x, int y) { |
|
|
|
|
private void render(Component c, Graphics g, int x, int y, FallbackRender fallbackRender) { |
|
|
|
|
try { |
|
|
|
|
if (type == IconType.white) { |
|
|
|
|
Objects.requireNonNull(whiteSvgDocument.getValue()) |
|
|
|
@ -103,6 +126,7 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon {
|
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
FineLoggerFactory.getLogger().error("SvgIcon from url: " + resource + "can not paint.", e); |
|
|
|
|
fallbackRender.render(c, g, x, y); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -141,4 +165,10 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon {
|
|
|
|
|
public @NotNull SvgIcon disabled() { |
|
|
|
|
return new SvgIcon(resource, size, IconType.disable); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@FunctionalInterface |
|
|
|
|
interface FallbackRender { |
|
|
|
|
void render(Component c, Graphics g, int x, int y); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|