From 552e5b6c8d06e7ef6ac1611163dcb0302a599864 Mon Sep 17 00:00:00 2001 From: Christian Lenz Date: Mon, 30 Dec 2024 00:47:22 +0100 Subject: [PATCH] Try to catch error from JSVG load method. --- .../org/netbeans/modules/svg/SVGPanel.java | 28 +++++++++++++---- .../modules/svg/SVGViewerElement.java | 8 ++++- .../svg/navigation/SVGPreviewPanel.java | 30 ++++++++++++------- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/ide/svg/src/org/netbeans/modules/svg/SVGPanel.java b/ide/svg/src/org/netbeans/modules/svg/SVGPanel.java index 3687eb00c0e8..56c25758fc60 100644 --- a/ide/svg/src/org/netbeans/modules/svg/SVGPanel.java +++ b/ide/svg/src/org/netbeans/modules/svg/SVGPanel.java @@ -24,6 +24,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; +import org.openide.util.NbBundle; /** * @@ -80,21 +81,25 @@ protected void paintComponent(Graphics g) { } if (svgDocument == null) { + drawErrorMesage(g); + return; } Graphics2D g2d = (Graphics2D) g.create(); - try { - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); - g2d.setRenderingHint(SVGRenderingHints.KEY_MASK_CLIP_RENDERING, SVGRenderingHints.VALUE_MASK_CLIP_RENDERING_ACCURACY); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); + g2d.setRenderingHint(SVGRenderingHints.KEY_MASK_CLIP_RENDERING, SVGRenderingHints.VALUE_MASK_CLIP_RENDERING_ACCURACY); - g2d.scale(scale, scale); + g2d.scale(scale, scale); + try { svgDocument.render(this, g2d); } catch (Exception ex) { - LOG.log(Level.SEVERE, ex.getMessage()); + LOG.log(Level.INFO, ex.getMessage()); + + drawErrorMesage(g); } finally { g2d.dispose(); } @@ -134,4 +139,15 @@ private JViewport getViewport() { return null; } + + // HINT: Not yet finished + private void drawErrorMesage(Graphics g) { + g.setColor(Color.RED); + + FontMetrics fm = this.getFontMetrics(g.getFont()); + String errMessage = NbBundle.getMessage(SVGPanel.class, "ERR_Thumbnail"); + int stringWidth = fm.stringWidth(errMessage); + + g.drawString(errMessage, (this.getWidth() - stringWidth) / 2, this.getHeight() / 2); + } } diff --git a/ide/svg/src/org/netbeans/modules/svg/SVGViewerElement.java b/ide/svg/src/org/netbeans/modules/svg/SVGViewerElement.java index f651bf9f0b13..7cf76624bace 100644 --- a/ide/svg/src/org/netbeans/modules/svg/SVGViewerElement.java +++ b/ide/svg/src/org/netbeans/modules/svg/SVGViewerElement.java @@ -29,6 +29,7 @@ import java.util.logging.Logger; import javax.swing.Action; import javax.swing.JComponent; +import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JToolBar; @@ -230,7 +231,12 @@ private void updateView() { return; } - svgDocument = svgLoader.load(fo.toURL()); + try { + svgDocument = svgLoader.load(fo.toURL()); + } catch (Exception ex) { + // HINT: Just a test. + JOptionPane.showMessageDialog(null, ex.getMessage()); + } if (svgDocument == null) { return; diff --git a/ide/svg/src/org/netbeans/modules/svg/navigation/SVGPreviewPanel.java b/ide/svg/src/org/netbeans/modules/svg/navigation/SVGPreviewPanel.java index aefa08ee0de4..81df6b061548 100644 --- a/ide/svg/src/org/netbeans/modules/svg/navigation/SVGPreviewPanel.java +++ b/ide/svg/src/org/netbeans/modules/svg/navigation/SVGPreviewPanel.java @@ -85,26 +85,34 @@ protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g.create((this.getWidth() - scaledWidth) / 2, (this.getHeight() - scaledHeight) / 2, scaledWidth, scaledHeight); - try { - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); - g2d.setRenderingHint(SVGRenderingHints.KEY_MASK_CLIP_RENDERING, SVGRenderingHints.VALUE_MASK_CLIP_RENDERING_ACCURACY); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); + g2d.setRenderingHint(SVGRenderingHints.KEY_MASK_CLIP_RENDERING, SVGRenderingHints.VALUE_MASK_CLIP_RENDERING_ACCURACY); - g2d.scale(1.0 / ratio, 1.0 / ratio); + g2d.scale(1.0 / ratio, 1.0 / ratio); + try { svgDocument.render(this, g2d); } catch (Exception ex) { - LOG.log(Level.SEVERE, ex.getMessage()); + LOG.log(Level.INFO, ex.getMessage()); + + drawErrorMesage(g); } finally { g2d.dispose(); } } else { - g.setColor(Color.RED); - FontMetrics fm = this.getFontMetrics(g.getFont()); - String errMessage = NbBundle.getMessage(SVGPreviewPanel.class, "ERR_Thumbnail"); - int stringWidth = fm.stringWidth(errMessage); - g.drawString(errMessage, (this.getWidth() - stringWidth) / 2, this.getHeight() / 2); + drawErrorMesage(g); } } + // HINT: Not yet finished + private void drawErrorMesage(Graphics g) { + g.setColor(Color.RED); + + FontMetrics fm = this.getFontMetrics(g.getFont()); + String errMessage = NbBundle.getMessage(SVGPreviewPanel.class, "ERR_Thumbnail"); + int stringWidth = fm.stringWidth(errMessage); + + g.drawString(errMessage, (this.getWidth() - stringWidth) / 2, this.getHeight() / 2); + } }