KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > graphic > GraphicalComposite


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.graphic;
20
21 import java.util.Vector JavaDoc;
22
23 /**
24  * Container for {@link GraphicalElement GraphicalElements}.
25  *
26  * @author Franz-Josef Elmer
27  */

28 public class GraphicalComposite implements GraphicalElement {
29   private final Vector JavaDoc _elements = new Vector JavaDoc();
30   private final ClippingShape _clippingShape;
31
32   /**
33    * Creates an instance with the specified clipping shape.
34    * @param clippingShape Clipping shape or <tt>null</tt> if no clipping.
35    */

36   public GraphicalComposite(ClippingShape clippingShape) {
37     _clippingShape = clippingShape;
38   }
39
40   /**
41    * Returns the clipping shape.
42    * @return <tt>null</tt> if no clipping should be applied.
43    */

44   public ClippingShape getClippingShape() {
45     return _clippingShape;
46   }
47
48   /**
49    * Adds the specified element at the end of the list of elements.
50    * @param element Element to be added. <tt>null</tt> is not allowed.
51    * @throws NullPointerException if <tt>element == null</tt>
52    */

53   public void addElement(GraphicalElement element) {
54     if (element == null) {
55       throwNullPointerException();
56     } else {
57       _elements.addElement(element);
58     }
59   }
60
61   /** Remove all elements. */
62   public void removeAllElements() {
63     _elements.removeAllElements();
64   }
65
66   /**
67    * Replaces the specified element at the specified index of
68    * the list of elements.
69    * @param element New element. <tt>null</tt> is not allowed.
70    * @throws NullPointerException if <tt>element == null</tt>
71    */

72   public void replaceElementAt(int index, GraphicalElement element) {
73     if (element == null) {
74       throwNullPointerException();
75     } else {
76       _elements.setElementAt(element, index);
77     }
78   }
79   
80   private void throwNullPointerException() {
81     throw new NullPointerException JavaDoc(
82         "A null as an GraphicalElement is not allowed");
83   }
84
85   /**
86    * Renders all {@link GraphicalElement GraphicalElements} in the sequence
87    * they have been added.
88    * @param renderer Renderer which implements all renderer interfaces
89    * necessary to render the child elements.
90    * @throws IllegalArgumentException if <tt>renderer</tt> is not
91    * an instance of <tt>GraphicalCompositeRenderer</tt>.
92    */

93   public void renderWith(Renderer renderer) {
94     if (renderer instanceof GraphicalCompositeRenderer) {
95       GraphicalCompositeRenderer r = (GraphicalCompositeRenderer) renderer;
96       r.startRendering(this);
97       for (int i = 0, n = _elements.size(); i < n; i++) {
98         ((GraphicalElement) _elements.elementAt(i)).renderWith(r);
99       }
100       r.finishRendering(this);
101     } else {
102       throw new IllegalArgumentException JavaDoc(renderer
103                       + " does not implements GraphicalCompositeRenderer.");
104     }
105   }
106 }
107
Popular Tags