1 package rero.dialogs.server; 2 3 import java.util.*; 4 import java.io.*; 5 6 import rero.util.*; 7 import rero.config.*; 8 9 import javax.swing.*; 10 11 public class ServerData 12 { 13 protected static ServerData data = null; 14 15 protected ServerGroup allServers = new ServerGroup("All Servers"); 16 protected ServerGroup randomServers = new ServerGroup("Random Servers"); 17 18 protected TreeMap groups = new TreeMap(); 19 protected ArrayList groupModel = new ArrayList(); 20 21 protected ServerGroup active; 22 23 protected ServerData() 24 { 25 load(); 26 } 27 28 public Server getServerByName(String host) 29 { 30 return allServers.getServerByName(host); 31 } 32 33 public static ServerData getServerData() 34 { 35 if (data == null) 36 data = new ServerData(); 37 38 return data; 39 } 40 41 public void setActive(ServerGroup a) 42 { 43 active = a; 44 45 if (active == allServers || active == null) 46 { 47 buildGroupList(); 48 return; 49 } 50 51 active.clear(); 52 groups.clear(); 53 randomServers.clear(); 54 55 if (active != randomServers) 56 { 57 active = getGroup(active.getName()); } 59 60 Iterator i = allServers.getServers().iterator(); 61 while (i.hasNext()) 62 { 63 Server temp = (Server)i.next(); 64 getGroup(temp.getNetwork()).addServer(temp); 65 } 66 67 buildGroupList(); 68 } 69 70 public void update() 71 { 72 setActive(active); 73 } 74 75 public ArrayList getGroups() 76 { 77 return groupModel; 78 } 79 80 public ArrayList getAllServers() 81 { 82 return allServers.getServers(); 83 } 84 85 public ArrayList getServers() 86 { 87 if (active != null) 88 { 89 return active.getServers(); 90 } 91 92 return allServers.getServers(); 93 } 94 95 public ServerGroup getGroup(String name) 96 { 97 if (name.length() <= 2) 98 { 99 return randomServers; 100 } 101 102 ServerGroup temp = (ServerGroup)groups.get(name.toUpperCase()); 103 104 if (temp == null) 105 { 106 temp = new ServerGroup(name); 107 groups.put(name.toUpperCase(), temp); 108 } 109 110 return temp; 111 } 112 113 114 public void addServer(Server server) 115 { 116 getGroup(server.getNetwork()).addServer(server); 117 allServers.addServer(server); 118 } 119 120 public void sort() 121 { 122 Collections.sort(allServers.getServers()); 123 } 124 125 public void removeServer(Server server) 126 { 127 allServers.removeServer(server); 128 getGroup(server.getNetwork()).removeServer(server); 129 } 130 131 public void buildGroupList() 132 { 133 groupModel.clear(); 134 135 groupModel.add(allServers); 136 allServers.setNumber(0); 137 138 groupModel.add(randomServers); 139 randomServers.setNumber(1); 140 141 int x = 2; 142 143 Iterator i = groups.values().iterator(); 144 while (i.hasNext()) 145 { 146 ServerGroup temp = (ServerGroup)i.next(); 147 if (temp.isValid()) 148 { 149 groupModel.add(temp); 150 temp.setNumber(x); 151 x++; 152 } 153 } 154 } 155 156 public void load() 157 { 158 groups.clear(); 159 allServers = new ServerGroup("All Servers"); 160 randomServers = new ServerGroup("Random Servers"); 161 162 try 163 { 164 BufferedReader in = new BufferedReader(new InputStreamReader(ClientState.getClientState().getResourceAsStream("servers.ini"))); 165 166 String data = in.readLine(); 167 while (data != null) 168 { 169 Server temp = Server.decode(data); 170 171 if (temp != null) 172 { 173 addServer(temp); 174 } 175 176 data = in.readLine(); 177 } 178 } 179 catch (Exception ex) 180 { 181 ex.printStackTrace(); 182 } 183 184 setActive(allServers); 185 buildGroupList(); 186 } 187 188 public void save() 189 { 190 try 191 { 192 PrintWriter out = new PrintWriter(new FileOutputStream(ClientState.getClientState().getFile("servers.ini"), false)); 193 194 int x = 0; 195 196 out.println("; Who thinks mIRC sucks?"); 197 out.println("[servers]"); 198 199 Iterator i = allServers.getServers().iterator(); 200 while (i.hasNext()) 201 { 202 Server temp = (Server)i.next(); 203 out.println(temp.toString(x)); 204 x++; 205 } 206 207 out.flush(); 208 out.close(); 209 } 210 catch (Exception ex) 211 { 212 ex.printStackTrace(); 213 } 214 } 215 216 public static void main(String args[]) 217 { 218 ServerData temp = new ServerData(); 219 temp.load(); 220 } 221 } 222 | Popular Tags |