View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
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   * This class perform as <strong>Content Provider</strong> for RFID Reader's View.
42   * It implements Eclipse's IStructuredContentProvider and ITreeContentProvider. The
43   * Reader lists would load from <strong>ResourceCenter</strong>.
44   *
45   * @author Haoning Zhang
46   * @version 1.0
47   */
48  public class ReaderExplorerViewContentProvider implements
49  		IStructuredContentProvider, ITreeContentProvider {
50  	
51  	/**
52  	 * Log4j instance.
53  	 */
54  	private static Logger log = Logger.getLogger(ReaderExplorerViewContentProvider.class);
55  	
56  	/**
57  	 * The root node, which is invisible in tree view.
58  	 */
59  	private ReaderTreeObject invisibleRoot;
60  	
61  	/**
62  	 * Container to hold the adapter tree nodes.
63  	 */
64  	private HashMap<String, ReaderTreeObject> adapters;
65  
66  	/**
67  	 * Retrieve all the child nodes.
68  	 */
69  	public Object[] getElements(Object parent) {
70  		log.debug("Loading Readers...");
71  		loadReaders();
72  		return getChildren(invisibleRoot);
73  	}
74  
75  	/**
76  	 * Get the parent node.
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  	 * Retrieve all the child nodes.
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  	 * Judge whether the tree node has children.
97  	 */
98  	public boolean hasChildren(Object parent) {
99  		if (parent instanceof ReaderTreeObject)
100 			return ((ReaderTreeObject) parent).hasChildren();
101 		return false;
102 	}
103 	
104 	/**
105 	 * Create the tree node refer to the Adapter. 
106 	 * @param aRoot Root node in the tree
107 	 * @param aName Logical Name for the adapter
108 	 * @return
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 	 * Get the tree node refer to the Adapter. If there is no instance, then create it.
120 	 * @param aRoot Root node in the tree
121 	 * @param aName Logical Name for the adapter
122 	 * @return
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      * Generate one Reader node instance.
134      *
135      * @param aAdapterParent ReaderTreeObject the parent Adapter node for this Reader
136      * @param aName String Reader's name, which will display in the tree.
137      * @param isConnected Whether the reader is connected.
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 		//MessageBoxTreeObject inboxNode = new MessageBoxTreeObject(MessageBoxTreeObject.INBOX_NAME);
163 		//MessageBoxTreeObject outboxNode = new MessageBoxTreeObject(MessageBoxTreeObject.OUTBOX_NAME);
164 		
165 		//readerNode.addChild(inboxNode);
166 		//readerNode.addChild(outboxNode);
167 	}
168 
169 	/**
170 	 * Load reader information from Reader Management. 
171 	 */
172 	private void loadReaders() {
173 		
174 		// Initial the Adapter nodes
175 		adapters = new HashMap<String, ReaderTreeObject>();
176 		
177 		// Create the reserved nodes in the tree view
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 					// add empty adaptor
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 	 * The handler to process content change.
217 	 */
218 	public void inputChanged(Viewer v, Object oldInput, Object newInput) {
219 	}
220 
221 	/**
222 	 * Dispose resources in the tree.
223 	 */
224 	public void dispose() {
225 	}
226 }