Skip to content

Package: AbstractViewerPanel$1

AbstractViewerPanel$1

nameinstructionbranchcomplexitylinemethod
isVisible()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
paint(Graphics2D, EditableImageRenderer)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.mistral.example;
28:
29: import java.io.FileNotFoundException;
30: import java.nio.file.Files;
31: import java.nio.file.Path;
32: import java.net.MalformedURLException;
33: import java.net.URL;
34: import java.awt.Color;
35: import java.awt.Font;
36: import java.awt.Graphics2D;
37: import java.awt.Insets;
38: import javax.swing.JLabel;
39: import javax.swing.JOptionPane;
40: import javax.swing.JPanel;
41: import javax.swing.SwingConstants;
42: import javax.swing.SwingUtilities;
43: import it.tidalwave.image.EditableImage;
44: import it.tidalwave.image.op.ReadOp;
45: import it.tidalwave.image.render.EditableImageRenderer;
46: import it.tidalwave.image.render.Overlay;
47:
48: /***********************************************************************************************************************
49: *
50: * @author Fabrizio Giudici
51: *
52: **********************************************************************************************************************/
53: public class AbstractViewerPanel extends JPanel
54: {
55: protected final EditableImageRenderer imageRenderer = new EditableImageRenderer();
56:
57: /*******************************************************************************************************************
58: *
59: *
60: ******************************************************************************************************************/
61: public void loadImage (final String fileName)
62: throws MalformedURLException
63: {
64: try // used during development
65: {
66: final var file = Path.of("../../../www/images/" + fileName);
67:
68: if (Files.exists(file) && Files.isReadable(file))
69: {
70: loadImage(file);
71: }
72:
73: else
74: {
75: throw new FileNotFoundException(file.toString());
76: }
77: }
78: catch (Exception e)
79: {
80: // used by the deployed examples
81: loadImage(new URL("https://mistral.dev.java.net/images/" + fileName));
82: }
83: }
84:
85: /*******************************************************************************************************************
86: *
87: *
88: ******************************************************************************************************************/
89: protected void loadImage (final Object input)
90: {
91: final var label = new JLabel("Loading " + input + "...");
92: label.setForeground(Color.WHITE);
93: label.setHorizontalAlignment(SwingConstants.CENTER);
94: label.setFont(new Font("sansserif", Font.BOLD, 14));
95:
96: final var overlay = new Overlay()
97: {
98: @Override
99: public boolean isVisible()
100: {
101: return true;
102: }
103:
104: @Override
105: public void paint (final Graphics2D g, final EditableImageRenderer imageRenderer)
106: {
107: label.setBounds(imageRenderer.getBounds());
108: label.paint(g);
109: }
110: };
111:
112: // final OverlaidPanel overlay = new OverlaidPanel();
113: // overlay.setLayout(new BorderLayout());
114: // overlay.add(label, BorderLayout.CENTER);
115:
116: imageRenderer.addOverlay(overlay);
117: imageRenderer.repaint(); // FIXME: should not be needed
118:
119: // A SwingWorker would be good here
120: final var loaderThread = new Thread(() ->
121: {
122: try
123: {
124: final var image = EditableImage.create(new ReadOp(input));
125:
126: SwingUtilities.invokeLater(() ->
127: {
128: imageRenderer.setImage(image);
129: imageRenderer.removeOverlay(overlay);
130: final var hMargin = image.getWidth() / 4;
131: final var vMargin = image.getHeight() / 4;
132: imageRenderer.setMargin(new Insets(vMargin, hMargin, vMargin, hMargin));
133: onImageLoaded(image);
134: });
135: }
136: catch (final Throwable e)
137: {
138: SwingUtilities.invokeLater(() ->
139: {
140: label.setText(e.toString());
141: imageRenderer.repaint(); // FIXME: should not be needed
142: // imageRenderer.removeOverlay(overlay);
143: e.printStackTrace();
144: final var message =
145: "<html>Cannot load the image.<br>An Internet connection is required.</html>";
146: final var title = "Error";
147: JOptionPane.showMessageDialog(AbstractViewerPanel.this,
148: message,
149: title,
150: JOptionPane.ERROR_MESSAGE);
151: });
152: }
153: });
154:
155: loaderThread.start();
156: }
157:
158: /*******************************************************************************************************************
159: *
160: *
161: ******************************************************************************************************************/
162: protected void onImageLoaded (final EditableImage image)
163: {
164: }
165: }