1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.fosstrak.llrp.commander.views;
23
24 import java.rmi.RemoteException;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import org.apache.log4j.Logger;
30 import org.eclipse.jface.viewers.IStructuredContentProvider;
31 import org.eclipse.jface.viewers.ITreeContentProvider;
32 import org.eclipse.jface.viewers.Viewer;
33 import org.fosstrak.llrp.adaptor.Adaptor;
34 import org.fosstrak.llrp.adaptor.AdaptorManagement;
35 import org.fosstrak.llrp.adaptor.exception.LLRPRuntimeException;
36 import org.fosstrak.llrp.client.Constants;
37 import org.fosstrak.llrp.commander.ResourceCenter;
38
39
40
41
42
43
44
45
46
47
48 public class ReaderExplorerViewContentProvider implements
49 IStructuredContentProvider, ITreeContentProvider {
50
51
52
53
54 private static Logger log = Logger.getLogger(ReaderExplorerViewContentProvider.class);
55
56
57
58
59 private ReaderTreeObject invisibleRoot;
60
61
62
63
64 private HashMap<String, ReaderTreeObject> adapters;
65
66
67
68
69 public Object[] getElements(Object parent) {
70 log.debug("Loading Readers...");
71 loadReaders();
72 return getChildren(invisibleRoot);
73 }
74
75
76
77
78 public Object getParent(Object child) {
79 if (child instanceof MessageBoxTreeObject) {
80 return ((MessageBoxTreeObject) child).getParent();
81 }
82 return null;
83 }
84
85
86
87
88 public Object[] getChildren(Object parent) {
89 if (parent instanceof ReaderTreeObject) {
90 return ((ReaderTreeObject) parent).getChildren();
91 }
92 return new Object[0];
93 }
94
95
96
97
98 public boolean hasChildren(Object parent) {
99 if (parent instanceof ReaderTreeObject)
100 return ((ReaderTreeObject) parent).hasChildren();
101 return false;
102 }
103
104
105
106
107
108
109
110 private ReaderTreeObject addAdapter(ReaderTreeObject aRoot, String aName) {
111 ReaderTreeObject adapter = new ReaderTreeObject(aName);
112 aRoot.addChild(adapter);
113
114 adapters.put(aName, adapter);
115 return adapter;
116 }
117
118
119
120
121
122
123
124 private ReaderTreeObject getAdapterNode(ReaderTreeObject aRoot, String aName) {
125 ReaderTreeObject adapter = adapters.get(aName);
126 if (null == adapter) {
127 adapter = addAdapter(aRoot, aName);
128 }
129 return adapter;
130 }
131
132
133
134
135
136
137
138
139 private void addReader(ReaderTreeObject aRoot, String aAdaptorName, String aReaderName, boolean isConnected) {
140
141 log.debug("addAdaptor " + aReaderName + " on " + aAdaptorName);
142
143 ReaderTreeObject readerNode = new ReaderTreeObject(aReaderName);
144 readerNode.setReader(true);
145 readerNode.setConnected(isConnected);
146
147 if (ResourceCenter.getInstance().existReaderConfig(aAdaptorName, aReaderName)) {
148 readerNode.setGetReaderConfig(true);
149 } else {
150 readerNode.setGetReaderConfig(false);
151 }
152
153 if (ResourceCenter.getInstance().existReaderROSpec(aAdaptorName, aReaderName)) {
154 readerNode.setGetReaderROSpec(true);
155 } else {
156 readerNode.setGetReaderROSpec(false);
157 }
158
159 ReaderTreeObject adapterNode = getAdapterNode(aRoot, aAdaptorName);
160 adapterNode.addChild(readerNode);
161
162
163
164
165
166
167 }
168
169
170
171
172 private void loadReaders() {
173
174
175 adapters = new HashMap<String, ReaderTreeObject>();
176
177
178 ReaderTreeObject rootAdapters = new ReaderTreeObject(
179 Constants.ROOT_NAME);
180
181 log.debug("Retrieving Adaptor Lists from Adapter Management");
182 try {
183 List<String> adaptorList = AdaptorManagement.getInstance().getAdaptorNames();
184 Iterator<String> i = adaptorList.iterator();
185 while (i.hasNext()) {
186 String adaptorName = i.next();
187
188 log.debug("Get Adaptor-" + adaptorName + " ...");
189 Adaptor adaptor = AdaptorManagement.getInstance().getAdaptor(adaptorName);
190
191 List<String> readerList = adaptor.getReaderNames();
192 if (readerList.size() == 0) {
193
194 getAdapterNode(rootAdapters, adaptorName);
195 } else {
196 Iterator<String> j = readerList.iterator();
197 while (j.hasNext()) {
198 String readerName = j.next();
199
200 addReader(rootAdapters, adaptorName, readerName, adaptor.getReader(readerName).isConnected());
201 }
202 }
203 }
204 } catch (LLRPRuntimeException llrpe) {
205 llrpe.printStackTrace();
206 } catch (RemoteException re) {
207 re.printStackTrace();
208 }
209
210
211 invisibleRoot = new ReaderTreeObject("");
212 invisibleRoot.addChild(rootAdapters);
213 }
214
215
216
217
218 public void inputChanged(Viewer v, Object oldInput, Object newInput) {
219 }
220
221
222
223
224 public void dispose() {
225 }
226 }