View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans.
5    *
6    * CosyBeans is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.displayers;
21  
22  import java.beans.PropertyVetoException;
23  import java.io.Serializable;
24  import java.util.ArrayList;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  
29  /**
30   * <code>DataSourceSupport</code> is conveniencee implementation base for
31   * <code>DataSource</code> interface.
32   *
33   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
34   * @version $Id: DataSourceSupport.java,v 1.11 2008-04-22 12:31:02 jbobnar Exp $
35   *
36   * @since Nov 24, 2003.
37   */
38  public class DataSourceSupport implements DataSource, Serializable
39  {
40  	private static final long serialVersionUID = -7615919185970956618L;
41  	protected Class<DataConsumer>[] types;
42  	transient ArrayList<DataConsumer> consumers;
43  
44  	/**
45  	 * Constructs support with defined supported types.
46  	 *
47  	 * @param supportedTypes types of DataConsumer that this source will
48  	 *        support
49  	 */
50  	public DataSourceSupport(Class<DataConsumer>[] supportedTypes)
51  	{
52  		consumers = new ArrayList<DataConsumer>();
53  		types = supportedTypes;
54  		for (int i = 0; i < supportedTypes.length; i++) {
55  			if (!DataConsumer.class.isAssignableFrom(supportedTypes[i])) {
56  				throw new IllegalArgumentException("Parameter type '"
57  				    + supportedTypes[i].getName() + "' is not DataConsumer.");
58  			}
59  		}
60  
61  	}
62  
63  	/**
64  	 * This support implementation does not perform any type check on consumer,
65  	 * it just adds it to list of consumers.
66  	 *
67  	 * @see com.cosylab.gui.displayers.DataSource#addConsumer(com.cosylab.gui.displayers.DataConsumer)
68  	 */
69  	public void addConsumer(DataConsumer consumer) throws PropertyVetoException
70  	{
71  		if (consumers == null) {
72  			consumers = new ArrayList<DataConsumer>();
73  		}
74  		consumers.add(consumer);
75  	}
76  
77  	/* (non-Javadoc)
78  	 * @see com.cosylab.gui.displayers.DataSource#removeConsumer(com.cosylab.gui.displayers.DataConsumer)
79  	 */
80  	public void removeConsumer(DataConsumer consumer)
81  	{
82  		if (consumers == null) return;
83  		consumers.remove(consumer);
84  	}
85  
86  	/* (non-Javadoc)
87  	 * @see com.cosylab.gui.displayers.DataSource#getConsumers()
88  	 */
89  	public DataConsumer[] getConsumers()
90  	{
91  		if (consumers == null) return new DataConsumer[0];
92  		return consumers.toArray(new DataConsumer[consumers.size()]);
93  	}
94  
95  	/**
96  	 * Clears all consumers from this DataSource.
97  	 */
98  	public void removeAllConsumers()
99  	{
100 		if (consumers != null) {
101 			consumers.clear();
102 		}
103 	}
104 
105 	/* (non-Javadoc)
106 	 * @see com.cosylab.gui.displayers.DataSource#getSupportedConsumersTypes()
107 	 */
108 	public Class<DataConsumer>[] getAcceptableConsumerTypes()
109 	{
110 		return types;
111 	}
112 
113 	/**
114 	 * Form all contained consumers extracts supported characteristics.
115 	 *
116 	 * @return characteristic names from all contained consumers
117 	 */
118 	public String[] extractSupportedCharacteristics()
119 	{
120 		// recast consumers to new type
121 		DataConsumer[] c = getConsumers();
122 		Set<String> names = new HashSet<String>();
123 
124 		for (int i = 0; i < c.length; i++) {
125 			String[] s = c[i].getSupportedCharacteristics();
126 
127 			if (s != null) {
128 				for (int j = 0; j < s.length; j++) {
129 					names.add(s[j]);
130 				}
131 			} else {
132 				return null;
133 			}
134 		}
135 		return names.toArray(new String[names.size()]);
136 	}
137 
138 	/**
139 	 * Releases all consumers and goes into initial state.
140 	 */
141 	public void clear()
142 	{
143 		consumers.clear();
144 	}
145 	
146 	/* (non-Javadoc)
147 	 * @see java.lang.Object#clone()
148 	 */
149 	@SuppressWarnings("unchecked")
150 	@Override
151 	protected Object clone() throws CloneNotSupportedException {
152 		DataSourceSupport c= (DataSourceSupport)super.clone();
153 		if (types!=null) {
154 			c.types= new Class[types.length];
155 			System.arraycopy(types,0,c.types,0,types.length);
156 		}
157 		c.consumers= new ArrayList<DataConsumer>();
158 		return c;
159 	}
160 }
161 
162 /* __oOo__ */