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.beans.AbstractBean; |
|
28 import org.jdesktop.swingx.designer.utils.HasResources; |
|
29 import org.jdesktop.swingx.designer.utils.HasUIDefaults; |
|
30 import org.jibx.runtime.IUnmarshallingContext; |
|
31 |
|
32 import javax.swing.UIDefaults; |
|
33 import java.awt.AlphaComposite; |
|
34 import java.awt.Dimension; |
|
35 import java.awt.Graphics2D; |
|
36 import java.awt.Insets; |
|
37 import java.awt.RenderingHints; |
|
38 import java.awt.image.BufferedImage; |
|
39 import java.beans.PropertyChangeEvent; |
|
40 import java.beans.PropertyChangeListener; |
|
41 import java.io.File; |
|
42 import java.util.ArrayList; |
|
43 import java.util.Collection; |
|
44 import java.util.Collections; |
|
45 import java.util.Iterator; |
|
46 import java.util.List; |
|
47 |
|
48 /** |
|
49 * ComponentRegion |
|
50 * |
|
51 * @author Created by Jasper Potts (May 22, 2007) |
|
52 */ |
|
53 public class Canvas extends AbstractBean implements LayerContainer, HasUIDefaults, HasResources { |
|
54 private Dimension size; |
|
55 /** list of all layers in the canvas, the first layer is painted on top */ |
|
56 private List<Layer> layers; |
|
57 private int nextLayerNameIndex = 1; |
|
58 private BufferedImage buffer; |
|
59 private boolean isValid = false; |
|
60 private Insets stretchingInsets = null; |
|
61 private Layer workingLayer = null; |
|
62 private PropertyChangeListener layersPropertyChangeListener; |
|
63 private UIDefaults canvasUIDefaults = null; |
|
64 private transient File resourcesDir; |
|
65 private transient File imagesDir; |
|
66 private transient File templatesDir; |
|
67 |
|
68 // ================================================================================================================= |
|
69 // Constructor |
|
70 |
|
71 /** Private constructor for JIBX */ |
|
72 protected Canvas() { |
|
73 layersPropertyChangeListener = new PropertyChangeListener() { |
|
74 public void propertyChange(PropertyChangeEvent evt) { |
|
75 isValid = false; |
|
76 // pass on layer change |
|
77 int index = layers.indexOf((Layer) evt.getSource()); |
|
78 if (index != -1) { |
|
79 firePropertyChange("layers[" + index + "]." + evt.getPropertyName(), evt.getOldValue(), |
|
80 evt.getNewValue()); |
|
81 } |
|
82 } |
|
83 }; |
|
84 } |
|
85 |
|
86 public Canvas(int width, int height) { |
|
87 this(); |
|
88 stretchingInsets = new Insets(1, 1, 1, 1); |
|
89 layers = new ArrayList<Layer>(); |
|
90 setSize(new Dimension(width, height)); |
|
91 addLayer(new Layer()); |
|
92 } |
|
93 |
|
94 // ================================================================================================================= |
|
95 // JIBX Methods |
|
96 |
|
97 /** |
|
98 * Called by JIBX before all fields have been set |
|
99 * |
|
100 * @param context The JIBX Unmarshalling Context |
|
101 */ |
|
102 private void preSet(IUnmarshallingContext context) { |
|
103 canvasUIDefaults = (UIDefaults) context.getUserContext(); |
|
104 } |
|
105 |
|
106 // ================================================================================================================= |
|
107 // Bean Methods |
|
108 |
|
109 /** |
|
110 * Get the UIDefaults for this canvas. The UIDefaults is used to store default pallet of colors, fonts etc. |
|
111 * |
|
112 * @return Canvas UIDefaults |
|
113 */ |
|
114 public UIDefaults getUiDefaults() { |
|
115 return canvasUIDefaults; |
|
116 } |
|
117 |
|
118 /** |
|
119 * Set the UIDefaults for this canvas. The UIDefaults is used to store default pallet of colors, fonts etc. |
|
120 * |
|
121 * @param canvasUIDefaults Canvas UIDefaults |
|
122 */ |
|
123 public void setUiDefaults(UIDefaults canvasUIDefaults) { |
|
124 this.canvasUIDefaults = canvasUIDefaults; |
|
125 } |
|
126 |
|
127 /** |
|
128 * Get the current working layer, is is the layer that new shapes will be drawn into |
|
129 * |
|
130 * @return The current working layer, may be null if there is no working layer |
|
131 */ |
|
132 public Layer getWorkingLayer() { |
|
133 return workingLayer; |
|
134 } |
|
135 |
|
136 /** |
|
137 * Set the current working layer, is is the layer that new shapes will be drawn into |
|
138 * |
|
139 * @param workingLayer the new working layer, must be a child of this canvas |
|
140 */ |
|
141 public void setWorkingLayer(Layer workingLayer) { |
|
142 Layer old = getWorkingLayer(); |
|
143 this.workingLayer = workingLayer; |
|
144 firePropertyChange("workingLayer", old, getWorkingLayer()); |
|
145 } |
|
146 |
|
147 public int getNextLayerNameIndex() { |
|
148 return nextLayerNameIndex++; |
|
149 } |
|
150 |
|
151 public Dimension getSize() { |
|
152 return size; |
|
153 } |
|
154 |
|
155 public void setSize(Dimension size) { |
|
156 Dimension old = getSize(); |
|
157 this.size = size; |
|
158 buffer = new BufferedImage(this.size.width, this.size.height, BufferedImage.TYPE_INT_ARGB); |
|
159 isValid = false; |
|
160 firePropertyChange("size", old, getSize()); |
|
161 } |
|
162 |
|
163 |
|
164 public Insets getStretchingInsets() { |
|
165 return stretchingInsets; |
|
166 } |
|
167 |
|
168 public void setStretchingInsets(Insets stretchingInsets) { |
|
169 Insets old = getStretchingInsets(); |
|
170 this.stretchingInsets = stretchingInsets; |
|
171 firePropertyChange("stretchingInsets", old, getStretchingInsets()); |
|
172 } |
|
173 |
|
174 public BufferedImage getRenderedImage() { |
|
175 if (!isValid) { |
|
176 Graphics2D g2 = buffer.createGraphics(); |
|
177 // clear |
|
178 g2.setComposite(AlphaComposite.Clear); |
|
179 g2.fillRect(0, 0, buffer.getWidth(), buffer.getHeight()); |
|
180 // paint |
|
181 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
|
182 g2.setComposite(AlphaComposite.SrcOver); |
|
183 for (int i = layers.size() - 1; i >= 0; i--) { |
|
184 layers.get(i).paint(g2, 1); |
|
185 } |
|
186 g2.dispose(); |
|
187 } |
|
188 return buffer; |
|
189 } |
|
190 |
|
191 /** |
|
192 * @return true if this Canvas has not been edited. |
|
193 * <p/> |
|
194 * TODO Currently this is not a bound property, but should be. That is, when the Canvas becomes edited |
|
195 * (usually due to the Layer having a shape added to it), then a property change event should be fired. |
|
196 */ |
|
197 public boolean isBlank() { |
|
198 return layers.size() == 0 || (layers.size() == 1 && layers.get(0).isEmpty()); |
|
199 } |
|
200 |
|
201 public File getResourcesDir() { |
|
202 return resourcesDir; |
|
203 } |
|
204 |
|
205 public void setResourcesDir(File resourcesDir) { |
|
206 File old = getResourcesDir(); |
|
207 this.resourcesDir = resourcesDir; |
|
208 firePropertyChange("resourcesDir", old, getResourcesDir()); |
|
209 } |
|
210 |
|
211 public File getImagesDir() { |
|
212 return imagesDir; |
|
213 } |
|
214 |
|
215 public void setImagesDir(File imagesDir) { |
|
216 File old = getImagesDir(); |
|
217 this.imagesDir = imagesDir; |
|
218 firePropertyChange("imagesDir", old, getImagesDir()); |
|
219 } |
|
220 |
|
221 public File getTemplatesDir() { |
|
222 return templatesDir; |
|
223 } |
|
224 |
|
225 public void setTemplatesDir(File templatesDir) { |
|
226 File old = getTemplatesDir(); |
|
227 this.templatesDir = templatesDir; |
|
228 firePropertyChange("templatesDir", old, getTemplatesDir()); |
|
229 } |
|
230 |
|
231 // ================================================================================================================= |
|
232 // LayerContainer Methods |
|
233 |
|
234 public LayerContainer getParent() { |
|
235 // we are root so null |
|
236 return null; |
|
237 } |
|
238 |
|
239 public void addLayerToBottom(Layer layer) { |
|
240 layers.add(layer); |
|
241 layer.setParent(this); |
|
242 layer.addPropertyChangeListener(layersPropertyChangeListener); |
|
243 // no single layer changes so fire all changed event |
|
244 firePropertyChange("layers", null, layers); |
|
245 } |
|
246 |
|
247 public void addLayer(int i, Layer layer) { |
|
248 layers.add(i, layer); |
|
249 layer.setParent(this); |
|
250 layer.addPropertyChangeListener(layersPropertyChangeListener); |
|
251 // no single layer changes so fire all changed event |
|
252 firePropertyChange("layers", null, layers); |
|
253 } |
|
254 |
|
255 public void addLayer(Layer layer) { |
|
256 layers.add(0, layer); |
|
257 layer.setParent(this); |
|
258 layer.addPropertyChangeListener(layersPropertyChangeListener); |
|
259 // no single layer changes so fire all changed event |
|
260 firePropertyChange("layers", null, layers); |
|
261 } |
|
262 |
|
263 public Layer getLayer(int index) { |
|
264 return layers.get(index); |
|
265 } |
|
266 |
|
267 public int getLayerCount() { |
|
268 return layers.size(); |
|
269 } |
|
270 |
|
271 public Iterator<Layer> getLayerIterator() { |
|
272 return Collections.unmodifiableList(layers).iterator(); |
|
273 } |
|
274 |
|
275 public Collection<Layer> getLayers() { |
|
276 return Collections.unmodifiableList(layers); |
|
277 } |
|
278 |
|
279 public int indexOfLayer(Layer layer) { |
|
280 return layers.indexOf(layer); |
|
281 } |
|
282 |
|
283 public void removeLayer(Layer layer) { |
|
284 int index = layers.indexOf(layer); |
|
285 if (index != -1) { |
|
286 layers.remove(layer); |
|
287 layer.removePropertyChangeListener(layersPropertyChangeListener); |
|
288 fireIndexedPropertyChange("layers", index, layer, null); |
|
289 } |
|
290 } |
|
291 |
|
292 public Dimension getRootSize() { |
|
293 return getSize(); |
|
294 } |
|
295 |
|
296 // ================================================================================================================= |
|
297 // JIBX Helper Methods |
|
298 |
|
299 /** Called by JIBX after "layers" has been filled so we can set parents and listeners */ |
|
300 private void setupLayers() { |
|
301 for (Layer layer : layers) { |
|
302 layer.setParent(this); |
|
303 layer.addPropertyChangeListener(layersPropertyChangeListener); |
|
304 } |
|
305 // no single layer changes so fire all changed event |
|
306 firePropertyChange("layers", null, layers); |
|
307 } |
|
308 } |
|