Skip to content

Commit

Permalink
Try to catch error from JSVG load method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris2011 committed Dec 29, 2024
1 parent 70a5764 commit 552e5b6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
28 changes: 22 additions & 6 deletions ide/svg/src/org/netbeans/modules/svg/SVGPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import org.openide.util.NbBundle;

/**
*
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
}
}
8 changes: 7 additions & 1 deletion ide/svg/src/org/netbeans/modules/svg/SVGViewerElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 552e5b6

Please sign in to comment.