1 /* |
|
2 * Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 package org.jdesktop.swingx.designer; |
|
26 |
|
27 import org.jdesktop.swingx.designer.effects.Effect; |
|
28 |
|
29 import javax.imageio.ImageIO; |
|
30 import javax.swing.JOptionPane; |
|
31 import javax.swing.SwingUtilities; |
|
32 import java.awt.Color; |
|
33 import java.awt.FontMetrics; |
|
34 import java.awt.Graphics2D; |
|
35 import java.awt.GraphicsConfiguration; |
|
36 import java.awt.Image; |
|
37 import java.awt.geom.Rectangle2D; |
|
38 import java.awt.image.BufferedImage; |
|
39 import java.io.File; |
|
40 import java.io.IOException; |
|
41 import java.lang.ref.SoftReference; |
|
42 |
|
43 /** |
|
44 * TemplateLayer |
|
45 * |
|
46 * @author Created by Jasper Potts (Jul 2, 2007) |
|
47 */ |
|
48 public class TemplateLayer extends Layer { |
|
49 |
|
50 private String fileName; |
|
51 private transient SoftReference<BufferedImage> imgRef = null; |
|
52 |
|
53 public TemplateLayer() { |
|
54 type = LayerType.template; |
|
55 } |
|
56 |
|
57 public TemplateLayer(String fileName, BufferedImage templateImage) { |
|
58 super("Template"); |
|
59 this.fileName = fileName; |
|
60 type = LayerType.template; |
|
61 if (templateImage != null) { |
|
62 imgRef = new SoftReference<BufferedImage>(templateImage); |
|
63 } |
|
64 } |
|
65 |
|
66 // ================================================================================================================= |
|
67 // Methods |
|
68 |
|
69 public String getName() { |
|
70 return super.getName(); |
|
71 } |
|
72 |
|
73 /** |
|
74 * template layers are always locked |
|
75 * |
|
76 * @return <code>true</code> |
|
77 */ |
|
78 public boolean isLocked() { |
|
79 return true; |
|
80 } |
|
81 |
|
82 public void add(SimpleShape shape) { |
|
83 throw new IllegalStateException("Template layers can't contain shapes"); |
|
84 } |
|
85 |
|
86 public void addEffect(Effect effect) { |
|
87 throw new IllegalStateException("Template layers can't contain effects"); |
|
88 } |
|
89 |
|
90 public void addLayer(int i, Layer layer) { |
|
91 throw new IllegalStateException("Template layers can't contain sub layers"); |
|
92 } |
|
93 |
|
94 public void addLayer(Layer layer) { |
|
95 throw new IllegalStateException("Template layers can't contain sub layers"); |
|
96 } |
|
97 |
|
98 public void paint(Graphics2D g2, double pixelSize) { |
|
99 if (isVisible()) { |
|
100 BufferedImage img = getTemplateImage(); |
|
101 if (img != null) g2.drawImage(img, 0, 0, null); |
|
102 } |
|
103 } |
|
104 |
|
105 |
|
106 public Image getBuffer(GraphicsConfiguration graphicsConfiguration) { |
|
107 return getTemplateImage(); |
|
108 } |
|
109 |
|
110 public BufferedImage getTemplateImage() { |
|
111 BufferedImage img = null; |
|
112 if (imgRef == null || (img = imgRef.get()) == null) { |
|
113 |
|
114 // can not access canvas |
|
115 final File templateImgFile = new File(getCanvas().getTemplatesDir(), fileName); |
|
116 System.out.println("templateImgFile = " + templateImgFile.getAbsolutePath()); |
|
117 System.out.println("templateImgFile.exists = " + templateImgFile.exists()); |
|
118 try { |
|
119 img = ImageIO.read(templateImgFile); |
|
120 imgRef = new SoftReference<BufferedImage>(img); |
|
121 } catch (IOException e) { |
|
122 e.printStackTrace(); |
|
123 // create error image |
|
124 img = new BufferedImage(getCanvas().getSize().width, getCanvas().getSize().height, |
|
125 BufferedImage.TYPE_INT_RGB); |
|
126 Graphics2D g2 = img.createGraphics(); |
|
127 g2.setColor(Color.RED); |
|
128 g2.fillRect(0, 0, img.getWidth(), img.getHeight()); |
|
129 g2.setColor(Color.WHITE); |
|
130 g2.setFont(g2.getFont().deriveFont(8f)); |
|
131 FontMetrics fontMetrics = g2.getFontMetrics(); |
|
132 Rectangle2D stringBounds = fontMetrics.getStringBounds("Missing Image", g2); |
|
133 int offsetX = (int) ((img.getWidth() - stringBounds.getWidth()) / 2d); |
|
134 int offsetY = (int) (((img.getHeight() - stringBounds.getHeight()) / 2d) - stringBounds.getY()); |
|
135 g2.drawString("Missing Image", offsetX, offsetY); |
|
136 g2.dispose(); |
|
137 imgRef = new SoftReference<BufferedImage>(img); |
|
138 } |
|
139 } |
|
140 return img; |
|
141 } |
|
142 } |
|