Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
*
* Fosstrak LLRP Commander (www.fosstrak.org)
*
* Copyright (C) 2008 ETH Zurich
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
*
*/
package org.fosstrak.llrp.commander.util;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import org.llrp.ltk.types.*;
import org.llrp.ltkGenerator.generated.FieldDefinition;
/**
* This class constitutes the interface to llrp message objects.
* All modifications to llrp message objects are performed through this class.
*
* Users of this class can register to be notified when changes to the llrp message occur.
*
* This class has a reference to the LLRPMessage object and is not implemented
* as a static class, because the implementation of the getParent(...) method
* requires to know the root of the object tree.
*
* @author Ulrich Etter, ETHZ
*
*/
public class LLRPTreeMaintainer extends Observable {
private final String EMPTY_STRING = "";
private LLRPMessage root;
public LLRPTreeMaintainer(LLRPMessage root){
this.root = root;
}
/**
* Sets the llrp message this LLRPTreeMaintainer shall maintain.
*
* @param root the llrp message LLRPTreeMaintainer shall maintain
*/
public void setRoot(LLRPMessage root){
this.root = root;
}
/**
* Returns the llrp message associated with this LLRPTreeMaintainer.
*
* @return the llrp message associated with this LLRPTreeMaintainer.
*/
public LLRPMessage getRoot() {
return root;
}
/**
* Sets the given parameter as child of the given message/parameter.
* The message/parameter will know the parameter under the given name.
*
* @param messageOrParameter either a LLRPMessage or a LLRPParameter
* @param childName the name of the child
* @param child the parameter that should be set as a child
*/
public void setChild(Object messageOrParameter, String childName, LLRPParameter child){
String methodName = "set" + childName;
Object[] methodArguments = {child};
for (Method method : messageOrParameter.getClass().getMethods()){
if (method.getName().equals(methodName)){
try {
method.invoke(messageOrParameter, methodArguments);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
setChanged();
notifyObservers();
}
/**
* Adds the given parameter to the given parameter list.
*
* @param list the parameter list to which the child shall be added
* @param child the parameter to add to the parameter list
*/
public void addChild(List list, LLRPParameter child){
list.add(child);
setChanged();
notifyObservers();
}
/**
* Removes the given parameter from the given parameter list.
*
* @param list the parameter list from which the child shall be removed
* @param child the
*/
public void removeChild(List list, LLRPParameter child){
for (LLRPParameter parameter : list){
if (parameter == child){
list.remove(parameter);
break;
}
}
setChanged();
notifyObservers();
}
/**
* Returns the child with the given name of the given message/parameter.
*
* @param messageOrParameter either a LLRPMessage or a LLRPParameter
* @param childName the name of the child.
* @return the child to the given name, null if not existing.
*/
public Object getChild(Object messageOrParameter, String childName){
Object child = null;
String methodName = "get" + childName;
if (LLRP.canOccurMultipleTimes(getDefinition(messageOrParameter), childName)){
methodName = methodName + "List";
}
try {
child = messageOrParameter.getClass().getMethod(methodName, new Class[0]).invoke(messageOrParameter, new Object[0]);
} catch (Exception e) {
e.printStackTrace();
}
return child;
}
/**
* Returns all children of the given tree element that are not null.
*
* @param treeElement either a LLRPMessage or a LLRPParameter
* or a List<LLRPParameter>
* @return a list of children to a given tree element.
*/
public List