View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   
6   package nl.tudelft.goal.ut2004.visualizer.timeline.map;
7   
8   import java.util.ArrayList;
9   import java.util.Collection;
10  import java.util.Collections;
11  import java.util.HashSet;
12  import java.util.List;
13  import java.util.Set;
14  
15  import javax.media.opengl.GL;
16  
17  /**
18   * Basically list of {@link ISubGLRenderer}s with stuff for manipulating it and
19   * rendering it.
20   * 
21   * Holds order of subrenderers.
22   * 
23   * @param <T>
24   *            Type of object subrenderers of this collection draws.
25   * @author Honza
26   */
27  public class CollectionRenderer<T> implements ISubGLRenderer<Collection<T>> {
28  
29  	private List<ISubGLRenderer<T>> renderers = Collections.synchronizedList(new ArrayList<ISubGLRenderer<T>>());
30  
31  	/**
32  	 * Add renderer to collection. It will be rendered after all previous
33  	 * renders are drawn.
34  	 * 
35  	 * @param subrenderer
36  	 */
37  	public synchronized void addRenderer(ISubGLRenderer<? extends T> subrenderer) {
38  		renderers.add((ISubGLRenderer<T>) subrenderer);
39  	}
40  
41  	/**
42  	 * Remove subrenderer from collection
43  	 * 
44  	 * @param subrenderer
45  	 *            renderer to be removed
46  	 * @return true if subrenderes was in collection
47  	 */
48  	public synchronized boolean removeRenderer(ISubGLRenderer<? extends T> subrenderer) {
49  		return renderers.remove(subrenderer);
50  	}
51  
52  	/**
53  	 * Remove all renderers that draw object o.
54  	 * 
55  	 * @param o
56  	 *            object that may have renderers in collection we want to remove
57  	 */
58  	public synchronized void removeRenderersOf(Object o) {
59  		Collection<ISubGLRenderer<T>> toRemove = new ArrayList<ISubGLRenderer<T>>();
60  
61  		for (ISubGLRenderer<T> renderer : renderers) {
62  			if (renderer.getObject() == o) {
63  				toRemove.add(renderer);
64  			}
65  
66  		}
67  
68  		renderers.removeAll(toRemove);
69  	}
70  
71  	/**
72  	 * Get set of all objects this collection draws.
73  	 * 
74  	 * @return all objects this collection draws
75  	 */
76  	public synchronized Set<T> getDrawnObjects() {
77  		HashSet<T> set = new HashSet<T>();
78  		for (ISubGLRenderer<T> renderer : renderers) {
79  			set.add(renderer.getObject());
80  		}
81  		return set;
82  	}
83  
84  	@Override
85  	public synchronized void render(GL gl) {
86  		for (ISubGLRenderer<T> subrenderer : renderers) {
87  			subrenderer.render(gl);
88  		}
89  	}
90  
91  	@Override
92  	public synchronized Collection<T> getObject() {
93  		Collection<T> objects = new ArrayList<T>();
94  		for (ISubGLRenderer<T> subrenderer : renderers) {
95  			objects.add(subrenderer.getObject());
96  		}
97  
98  		return objects;
99  	}
100 
101 	/**
102 	 * Prepare all renderers for rendering
103 	 * 
104 	 * @param gl
105 	 */
106 	@Override
107 	public synchronized void prepare(GL gl) {
108 		for (ISubGLRenderer<T> renderer : renderers) {
109 			renderer.prepare(gl);
110 		}
111 	}
112 
113 	public synchronized T getObjectsByGLName(int glName) {
114 		// find that miserable renderer in agents
115 		for (ISubGLRenderer<T> renderer : renderers) {
116 			if (renderer.getGLName() == glName) {
117 				return renderer.getObject();
118 			}
119 
120 		}
121 
122 		return null;
123 
124 	}
125 
126 	public synchronized Collection<T> getObjectsByGLName(int[] list) {
127 
128 		Collection<T> results = new ArrayList<T>();
129 		for (int glName : list) {
130 			T result = getObjectsByGLName(glName);
131 			if (result != null) {
132 				results.add(result);
133 			}
134 		}
135 
136 		return results;
137 
138 	}
139 
140 	public void destroy() {
141 		for(ISubGLRenderer<T> renderer : renderers){
142 			renderer.destroy();
143 		}
144 		
145 	}
146 
147 	@Override
148 	public int getGLName() {
149 		return -1;
150 	}
151 }