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