1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
31
32
33
34
35
36
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
46
47
48
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
65
66
67
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
78
79
80 public void removeConsumer(DataConsumer consumer)
81 {
82 if (consumers == null) return;
83 consumers.remove(consumer);
84 }
85
86
87
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
97
98 public void removeAllConsumers()
99 {
100 if (consumers != null) {
101 consumers.clear();
102 }
103 }
104
105
106
107
108 public Class<DataConsumer>[] getAcceptableConsumerTypes()
109 {
110 return types;
111 }
112
113
114
115
116
117
118 public String[] extractSupportedCharacteristics()
119 {
120
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
140
141 public void clear()
142 {
143 consumers.clear();
144 }
145
146
147
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