View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.util;
2   
3   import java.rmi.RemoteException;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   
7   import nl.tudelft.goal.ut2004.visualizer.connection.EnvironmentService;
8   
9   import cz.cuni.amis.pogamut.unreal.bot.IUnrealBot;
10  
11  public class SelectableEnvironment {
12  
13  	private final EnvironmentService environment;
14  
15  	public SelectableEnvironment(EnvironmentService environment) {
16  		this.environment = environment;
17  	}
18  
19  	public EnvironmentService getItem() {
20  		return environment;
21  	}
22  	
23  	/**
24  	 * Simplifies a given string representation of an AgentID by removing the
25  	 * UUID making it human readable.
26  	 * 
27  	 * By the specs of the agentID this should still result in a unique but
28  	 * readable ID.
29  	 * 
30  	 * @param id
31  	 * @return the agentID without the UUID.
32  	 */
33  	private String simplefyID(String id) {
34  		//TODO: Duplicate code as in Unreal Environment
35  		int index = id.lastIndexOf('/');
36  		// If the expected separator could not be found, use the whole string.
37  		if (index < 0) {
38  			index = id.length();
39  			//log.severe("Could not find UUID seperator in Agent ID: " + id);
40  		}
41  		return id.substring(0, index);
42  	}
43  	
44  	@Override
45  	public String toString() {
46  		try {
47  			return simplefyID(environment.getAgentId().getToken());
48  		} catch (RemoteException e) {
49  			return "Unconnected Environment";
50  		}
51  	}
52  
53  	public static Collection<SelectableEnvironment> fromCollection(
54  			Collection<? extends EnvironmentService> environments) {
55  
56  		Collection<SelectableEnvironment> ret = new ArrayList<SelectableEnvironment>(
57  				environments.size());
58  
59  		for (EnvironmentService env : environments) {
60  			ret.add(new SelectableEnvironment(env));
61  		}
62  
63  		return ret;
64  	}
65  
66  }