View Javadoc

1   package net.sourceforge.simplegamenet.framework.gui;
2   
3   import java.beans.XMLDecoder;
4   import java.beans.XMLEncoder;
5   import java.io.BufferedInputStream;
6   import java.io.BufferedOutputStream;
7   import java.io.File;
8   import java.io.FileInputStream;
9   import java.io.FileNotFoundException;
10  import java.io.FileOutputStream;
11  import java.io.InputStream;
12  import java.io.OutputStream;
13  import java.io.Serializable;
14  import net.sourceforge.simplegamenet.framework.model.SimpleGameNetSettings;
15  import net.sourceforge.simplegamenet.specs.to.GameSettings;
16  import net.sourceforge.simplegamenet.specs.to.PlayerSettings;
17  
18  /***
19   * A bean that represents the data gathered form a ConnectionWizard
20   */
21  public class ConnectionConfig implements Serializable {
22  
23      private static final String CONNECTION_CONFIG_FILE_NAME = "ConnectionConfig.xml";
24  
25      private static final String DEFAULT_NICKNAME = "Player";
26      private static final String DEFAULT_HOST_ADDRESS = "127.0.0.1";
27      private static final int DEFAULT_PORT = 8119;
28      private static final boolean DEFAULT_BAD_LANGUAGE_FILTERED = false;
29  
30      public static ConnectionConfig load() {
31          ConnectionConfig connectionConfig = null;
32          File connectionConfigFile = getConnectionConfigFile();
33          if (connectionConfigFile.isFile()) {
34              InputStream in = null;
35              try {
36                  in = new BufferedInputStream(new FileInputStream(connectionConfigFile));
37                  XMLDecoder decoder = new XMLDecoder(in);
38                  connectionConfig = (ConnectionConfig) decoder.readObject();
39              } catch (FileNotFoundException e) {
40                  e.printStackTrace();
41              }
42          }
43          if (connectionConfig == null) {
44              connectionConfig = new ConnectionConfig();
45          }
46          return connectionConfig;
47      }
48  
49      public static void store(ConnectionConfig connectionConfig) {
50          File connectionConfigFile = getConnectionConfigFile();
51          if (connectionConfigFile != null) {
52              OutputStream out = null;
53              try {
54                  out = new BufferedOutputStream(new FileOutputStream(connectionConfigFile));
55                  XMLEncoder encoder = new XMLEncoder(out);
56                  encoder.writeObject(connectionConfig);
57                  encoder.close();
58              } catch (FileNotFoundException e) {
59                  e.printStackTrace();
60              }
61          }
62      }
63  
64      private static File getConnectionConfigFile() {
65          File appSettingsDir = SimpleGameNetSettings.getInstance().getAppSettingsDir();
66          File connectionConfigFile = appSettingsDir == null ? null :
67                  new File(appSettingsDir, CONNECTION_CONFIG_FILE_NAME);
68          return connectionConfigFile;
69      }
70  
71      private String nickname;
72      // InetAddress isn't a bean
73      private String hostAddress;
74      private int port;
75      private boolean badLanguageFiltered;
76  
77      private GameSettings gameSettings;
78  
79      public ConnectionConfig() {
80          setNickname(DEFAULT_NICKNAME);
81          setHostAddress(DEFAULT_HOST_ADDRESS);
82          setPort(DEFAULT_PORT);
83          setBadLanguageFiltered(DEFAULT_BAD_LANGUAGE_FILTERED);
84          setGameSettings(null);
85      }
86  
87      public String getNickname() {
88          return nickname;
89      }
90  
91      public void setNickname(String nickname) throws IllegalArgumentException {
92          nickname = nickname.replaceAll("//s", "_");
93          if (nickname.equals("")) {
94              throw new IllegalArgumentException("Your nickname is required.");
95          }
96          if (nickname.length() > PlayerSettings.MAXIMUM_NICKNAME_LENGTH) {
97              throw new IllegalArgumentException("Your nickname must not be longer then "
98                      + PlayerSettings.MAXIMUM_NICKNAME_LENGTH + " characters.");
99          }
100         this.nickname = nickname;
101     }
102 
103     public String getHostAddress() {
104         return hostAddress;
105     }
106 
107     public void setHostAddress(String hostAddress) {
108         this.hostAddress = hostAddress;
109     }
110 
111     public int getPort() {
112         return port;
113     }
114 
115     public void setPort(int port) throws IllegalArgumentException {
116         if (port <= 0) {
117             throw new IllegalArgumentException("The port number must be above 0.");
118         }
119         this.port = port;
120     }
121 
122     public boolean isBadLanguageFiltered() {
123         return badLanguageFiltered;
124     }
125 
126     public void setBadLanguageFiltered(boolean badLanguageFiltered) {
127         this.badLanguageFiltered = badLanguageFiltered;
128     }
129 
130     public GameSettings getGameSettings() {
131         return gameSettings;
132     }
133 
134     public void setGameSettings(GameSettings gameSettings) {
135         this.gameSettings = gameSettings;
136     }
137 
138 
139 }