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.adapters;
21  
22  import java.beans.PropertyVetoException;
23  import java.util.Map;
24  
25  import com.cosylab.gui.displayers.DataConsumer;
26  import com.cosylab.gui.displayers.DataState;
27  
28  
29  /**
30   * 
31   * <code>ConverterChain</code> combines several converter into a single one.
32   * Single converters can be added to this chain. The value will then be converted
33   * by each of this single converters resulting in an arbitrary function applied
34   * to the input value.
35   * 
36   * @author Igor Kriznar (igor.kriznarATcosylab.com)
37   *
38   */
39  public class ConverterChain implements Converter {
40  	
41  	private static final long serialVersionUID = 1785528428091352421L;
42  	private Converter[] converters;
43  	private Converter first;
44  	private Converter last;
45  	
46  	/**
47  	 * Creates new instance of ConverterChain.
48  	 */
49  	public ConverterChain() throws PropertyVetoException {
50  		this(new Converter[]{new IdentityConverter()});
51  	}
52  
53  	/**
54  	 * Creates new instance of ConverterChain.
55  	 */
56  	public ConverterChain(Converter[] converters) throws PropertyVetoException {
57  		initialize(converters);
58  	}
59  
60  	/**
61  	 * @param converters
62  	 * @throws PropertyVetoException
63  	 */
64  	private void initialize(Converter[] converters) throws PropertyVetoException {
65  		if (converters == null || converters.length ==0) {
66  			converters = new Converter[]{new IdentityConverter()};
67  		}
68  		this.converters=converters;
69  		first= converters[0];
70  		last= converters[converters.length-1];
71  		for (int i = 1; i < converters.length; i++) {
72  			Converter c1 = converters[i-1];
73  			Converter c2 = converters[i];
74  			c1.addConsumer(c2);
75  		}
76  	}
77  
78  	/* (non-Javadoc)
79  	 * @see com.cosylab.gui.displayers.DataSource#addConsumer(com.cosylab.gui.displayers.DataConsumer)
80  	 */
81  	public void addConsumer(DataConsumer consumer) throws PropertyVetoException {
82  		last.addConsumer(consumer);
83  	}
84  
85  	/* (non-Javadoc)
86  	 * @see com.cosylab.gui.displayers.DataSource#removeConsumer(com.cosylab.gui.displayers.DataConsumer)
87  	 */
88  	public void removeConsumer(DataConsumer consumer) {
89  		last.removeConsumer(consumer);
90  	}
91  
92  	/* (non-Javadoc)
93  	 * @see com.cosylab.gui.displayers.DataSource#getConsumers()
94  	 */
95  	public DataConsumer[] getConsumers() {
96  		return last.getConsumers();
97  	}
98  
99  	/* (non-Javadoc)
100 	 * @see com.cosylab.gui.displayers.DataSource#getAcceptableConsumerTypes()
101 	 */
102 	public Class<DataConsumer>[] getAcceptableConsumerTypes() {
103 		return last.getAcceptableConsumerTypes();
104 	}
105 
106 	/* (non-Javadoc)
107 	 * @see com.cosylab.gui.displayers.DataSource#removeAllConsumers()
108 	 */
109 	public void removeAllConsumers() {
110 		last.removeAllConsumers();
111 	}
112 
113 	/* (non-Javadoc)
114 	 * @see com.cosylab.gui.displayers.DataConsumer#getDataConsumer(java.lang.Class)
115 	 */
116 	public <D extends DataConsumer>D getDataConsumer(Class<D> type) {
117 		return first.getDataConsumer(type);
118 	}
119 
120 	/* (non-Javadoc)
121 	 * @see com.cosylab.gui.displayers.DataConsumer#getDefaultDataConsumer()
122 	 */
123 	public DataConsumer getDefaultDataConsumer() {
124 		return first.getDefaultDataConsumer();
125 	}
126 
127 	/* (non-Javadoc)
128 	 * @see com.cosylab.gui.displayers.DataConsumer#updateDataState(com.cosylab.gui.displayers.DataState)
129 	 */
130 	public void updateDataState(DataState state) {
131 		first.updateDataState(state);
132 	}
133 
134 	/* (non-Javadoc)
135 	 * @see com.cosylab.gui.displayers.DataConsumer#setCharacteristics(java.util.Map)
136 	 */
137 	public void setCharacteristics(Map characteristics) {
138 		first.setCharacteristics(characteristics);
139 	}
140 
141 	/* (non-Javadoc)
142 	 * @see com.cosylab.gui.displayers.DataConsumer#getName()
143 	 */
144 	public String getName() {
145 		return first.getName();
146 	}
147 
148 	/* (non-Javadoc)
149 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedCharacteristics()
150 	 */
151 	public String[] getSupportedCharacteristics() {
152 		return first.getSupportedCharacteristics();
153 	}
154 
155 	/* (non-Javadoc)
156 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedConsumerTypes()
157 	 */
158 	public Class<DataConsumer>[] getSupportedConsumerTypes() {
159 		return first.getSupportedConsumerTypes();
160 	}
161 
162 	/**
163 	 * Returns the last converter in the chain.
164 	 * 
165 	 * @return
166 	 */
167 	public Converter getLast() {
168 		return last;
169 	}
170 
171 	/**
172 	 * Returns the first converter in the chain.
173 	 * 
174 	 * @return
175 	 */
176 	public Converter getFirst() {
177 		return first;
178 	}
179 
180 	/**
181 	 * Returns the converter under the specified index.
182 	 * 
183 	 * @param index index of the requested converter
184 	 * @return the converter
185 	 */
186 	public Converter get(int index) {
187 		return converters[index];
188 	}
189 
190 	/**
191 	 * Returns the number of all converters in the chain.
192 	 * 
193 	 * @return
194 	 */
195 	public int size() {
196 		return converters.length;
197 	}
198 
199 	/**
200 	 * Returns all converters in the chain.
201 	 * 
202 	 * @return
203 	 */
204 	public Converter[] getConverters() {
205 		return converters;
206 	}
207 
208 	/*
209 	 * (non-Javadoc)
210 	 * @see java.lang.Object#toString()
211 	 */
212 	public String toString() {
213 		StringBuffer buffer = new StringBuffer();
214 		for (Converter c : converters) {
215 			buffer.append(c.toString() + ";");
216 		}
217 		return buffer.substring(0, buffer.length()-1);
218 	}
219 	
220 	/* (non-Javadoc)
221 	 * @see java.lang.Object#clone()
222 	 */
223 	@Override
224 	public Object clone() throws CloneNotSupportedException {
225 		try {
226 			ConverterChain c = new ConverterChain();
227 			Converter[] cc= new Converter[converters.length];
228 			for (int i=0; i<cc.length; i++) {
229 				cc[i]=(Converter)converters[i].clone();
230 			}
231 			try {
232 				c.initialize(cc);
233 			} catch (PropertyVetoException e) {
234 				e.printStackTrace();
235 			}
236 			return c;
237 		} catch (PropertyVetoException e) {
238 			throw new CloneNotSupportedException();
239 		}
240 	}
241 }
242