Browse Source

Config saving/loading

git-svn-id: http://chris.smith.name/svn/japoker/trunk@8 b8598caf-53df-4c1b-98b8-507a84a220
tags/v12
Chris Smith 16 years ago
parent
commit
43cc7e36bb

+ 31
- 0
src/com/md87/cardgame/config/AppearancePanel.java View File

@@ -124,6 +124,36 @@ public class AppearancePanel extends JPanel implements ActionListener, ChangeLis
124 124
         return new Color((Integer) redSpinner.getValue(),
125 125
                 (Integer) greenSpinner.getValue(), (Integer) blueSpinner.getValue());
126 126
     }
127
+    
128
+    /**
129
+     * Retrieves the current state of this appearance panel as an array of objects.
130
+     * 
131
+     * @return This panel's current state
132
+     */
133
+    public Object[] getData() {
134
+        return new Object[]{
135
+            frontCombo.getSelectedIndex(),
136
+            backCombo.getSelectedIndex(),
137
+            redSpinner.getValue(),
138
+            greenSpinner.getValue(),
139
+            blueSpinner.getValue()
140
+        };
141
+    }
142
+    
143
+    /**
144
+     * Sets the state of this appearance panel to a previously saved state.
145
+     * 
146
+     * @param data The new state of the panel
147
+     */
148
+    public void setData(final Object[] data) {
149
+        frontCombo.setSelectedIndex((Integer) data[0]);
150
+        backCombo.setSelectedIndex((Integer) data[1]);
151
+        redSpinner.setValue(data[2]);
152
+        greenSpinner.setValue(data[3]);
153
+        blueSpinner.setValue(data[4]);
154
+        
155
+        preview.repaint();
156
+    }
127 157
 
128 158
     private class PreviewPanel extends JPanel {
129 159
 
@@ -131,6 +161,7 @@ public class AppearancePanel extends JPanel implements ActionListener, ChangeLis
131 161
             super(new MigLayout("fill"));
132 162
         }
133 163
 
164
+        /** {@inheritDoc} */
134 165
         @Override
135 166
         public void paint(final Graphics g) {
136 167
             g.setColor(AppearancePanel.this.getColour());

+ 20
- 0
src/com/md87/cardgame/config/BettingPanel.java View File

@@ -123,5 +123,25 @@ public class BettingPanel extends JPanel implements ActionListener {
123 123
             }
124 124
         }
125 125
     }
126
+    
127
+    /**
128
+     * Retrieves an array of data representing the state of this betting panel.
129
+     * 
130
+     * @return The current state of this betting panel
131
+     */
132
+    public Object[] getData() {
133
+        return new Object[]{getAnte(), getBigBlind(), getRaises()};
134
+    }
135
+    
136
+    /**
137
+     * Sets the current state of this betting panel.
138
+     * 
139
+     * @param data The new state of the betting panel
140
+     */
141
+    public void setData(final Object[] data) {
142
+        ante.setValue(data[0]);
143
+        bigblind.setValue(data[1]);
144
+        raises.setValue(data[2]);
145
+    }
126 146
 
127 147
 }

+ 176
- 30
src/com/md87/cardgame/config/ConfigScreen.java View File

@@ -16,8 +16,15 @@ import java.awt.BorderLayout;
16 16
 import java.awt.Image;
17 17
 import java.awt.event.ActionEvent;
18 18
 import java.awt.event.ActionListener;
19
+import java.io.File;
20
+import java.io.FileInputStream;
21
+import java.io.FileOutputStream;
19 22
 import java.io.IOException;
20 23
 
24
+import java.io.ObjectInputStream;
25
+import java.io.ObjectOutputStream;
26
+import java.io.Serializable;
27
+import java.util.List;
21 28
 import javax.imageio.ImageIO;
22 29
 import javax.swing.BorderFactory;
23 30
 import javax.swing.ImageIcon;
@@ -28,45 +35,66 @@ import javax.swing.JTabbedPane;
28 35
 import javax.swing.WindowConstants;
29 36
 
30 37
 /**
38
+ * Allows the user to customise options before starting a game.
31 39
  *
32 40
  * @author Chris
33 41
  */
34 42
 public class ConfigScreen extends JDialog {
35
-    
43
+
44
+    /**
45
+     * A version number for this class. It should be changed whenever the class
46
+     * structure is changed (or anything else that would prevent serialized
47
+     * objects being unserialized with the new class).
48
+     */
36 49
     private static final long serialVersionUID = 1;
37
-    
38
-    private final JTabbedPane tabbedPane = new JTabbedPane();
50
+
51
+    /** The URL to prefix icons with. */
52
+    private static final String ICON_URL = "/com/md87/cardgame/res/icons/";
53
+
54
+    /** The configuration directory. */
55
+    private static String configdir;
56
+
57
+    /** The panel that allows the user to customise the players. */
39 58
     private final PlayerPanel playerPanel = new PlayerPanel();
59
+    /** The panel that allows the user to select the game type. */
40 60
     private final GamePanel gamePanel = new GamePanel(this);
61
+    /** The panel that allows the user to alter betting options. */
41 62
     private final BettingPanel bettingPanel = new BettingPanel();
63
+    /** The panel that allows the user to alter apperance options. */
42 64
     private final AppearancePanel stylePanel = new AppearancePanel();
65
+    /** The panel that allows the user to save/load settings. */
43 66
     private final JPanel savePanel = new JPanel();
44
-    
67
+
68
+    /** The type of game that is currently selected. */
45 69
     private GameInfo game;
46 70
 
71
+    /**
72
+     * Creates and displays a new config screen.
73
+     */
47 74
     public ConfigScreen() {
48 75
         super();
49
-        
76
+
50 77
         Image gameIcon = null;
51 78
         Image playerIcon = null;
52 79
         Image moneyIcon = null;
53 80
         Image saveIcon = null;
54 81
         Image styleIcon = null;
55
-        
82
+
56 83
         try {
57
-            gameIcon = ImageIO.read(getClass().getResource("/com/md87/cardgame/res/icons/icon.png"));
58
-            playerIcon = ImageIO.read(getClass().getResource("/com/md87/cardgame/res/icons/user.png"));
59
-            moneyIcon = ImageIO.read(getClass().getResource("/com/md87/cardgame/res/icons/money.png"));
60
-            saveIcon = ImageIO.read(getClass().getResource("/com/md87/cardgame/res/icons/save.png"));
61
-            styleIcon = ImageIO.read(getClass().getResource("/com/md87/cardgame/res/icons/style.png"));
84
+            gameIcon = ImageIO.read(getClass().getResource(ICON_URL + "icon.png"));
85
+            playerIcon = ImageIO.read(getClass().getResource(ICON_URL + "user.png"));
86
+            moneyIcon = ImageIO.read(getClass().getResource(ICON_URL + "money.png"));
87
+            saveIcon = ImageIO.read(getClass().getResource(ICON_URL + "save.png"));
88
+            styleIcon = ImageIO.read(getClass().getResource(ICON_URL + "style.png"));
62 89
         } catch (IOException ex) {
63 90
             System.err.println("Unable to load icons");
64
-        }   
65
-        
91
+        }
92
+
66 93
         setIconImage(gameIcon);
67
-        
94
+
68 95
         setTitle("JaPoker: Game configuration");
69
-        
96
+
97
+        final JTabbedPane tabbedPane = new JTabbedPane();
70 98
         tabbedPane.addTab("Game", new ImageIcon(gameIcon), gamePanel);
71 99
         tabbedPane.addTab("Players", new ImageIcon(playerIcon), playerPanel);
72 100
         tabbedPane.addTab("Betting", new ImageIcon(moneyIcon), bettingPanel);
@@ -74,61 +102,179 @@ public class ConfigScreen extends JDialog {
74 102
         tabbedPane.addTab("Save/Load", new ImageIcon(saveIcon), savePanel);
75 103
         tabbedPane.setEnabledAt(4, false);
76 104
         tabbedPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
77
-        
105
+
78 106
         setLayout(new BorderLayout());
79 107
         add(tabbedPane, BorderLayout.NORTH);
80
-        
108
+
81 109
         final JButton netButton = new JButton("Join Network Game");
82 110
         netButton.setEnabled(false);
83 111
         JPanel buttonPanel = new JPanel();
84 112
         buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));
85 113
         buttonPanel.add(netButton);
86 114
         add(buttonPanel, BorderLayout.WEST);
87
-        
115
+
88 116
         final JButton startButton = new JButton("Start Game");
89 117
         buttonPanel = new JPanel();
90 118
         buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));
91
-        buttonPanel.add(startButton);        
119
+        buttonPanel.add(startButton);
92 120
         add(buttonPanel, BorderLayout.EAST);
93
-        
121
+
94 122
         startButton.addActionListener(new ActionListener() {
95 123
             @Override
96 124
             public void actionPerformed(final ActionEvent e) {
97 125
                 startGame();
98 126
             }
99 127
         });
100
-        
128
+
129
+        final File configDir = new File(getConfigDir());
130
+        if (!configDir.isDirectory()) {
131
+            configDir.mkdirs();
132
+        } else if (new File(configDir, "last_used.config").exists()) {
133
+            loadSettings("last_used");
134
+        }
135
+
101 136
         pack();
102 137
         setVisible(true);
103 138
         setResizable(false);
104
-        
139
+
105 140
         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
106 141
     }
107
-    
142
+
143
+    /**
144
+     * Sets the game type that is currently selected.
145
+     *
146
+     * @param newGame The type of game that is now selected
147
+     */
108 148
     public void setGame(final GameInfo newGame) {
109 149
         game = newGame;
110
-        
150
+
111 151
         playerPanel.setMaxPlayers(game.getNumPlayers());
112 152
     }
113
-    
153
+
154
+    /**
155
+     * Retrieves the current game type.
156
+     *
157
+     * @return The type of game that is currently selected
158
+     */
114 159
     public GameInfo getGame() {
115 160
         return game;
116 161
     }
117
-    
162
+
163
+    /**
164
+     * Starts a game with the given settings.
165
+     */
118 166
     private void startGame() {
119
-        final Game myGame = game.getGame(playerPanel.getNumPlayers(), 
120
-                bettingPanel.getBigBlind(), bettingPanel.getAnte(), 
167
+        final Game myGame = game.getGame(playerPanel.getNumPlayers(),
168
+                bettingPanel.getBigBlind(), bettingPanel.getAnte(),
121 169
                 bettingPanel.getRaises());
122 170
         final GameWindow window = new GameWindow(myGame,
123 171
                 stylePanel.getFrontName(), stylePanel.getBackName(),
124 172
                 stylePanel.getColour());
125
-        
173
+
126 174
         for (Player player : playerPanel.getPlayers(myGame, window)) {
127 175
             myGame.addPlayer(player);
128 176
         }
129
-        
177
+
130 178
         myGame.startTournament();
179
+        
180
+        saveSettings("last_used");
131 181
         dispose();
132 182
     }
133 183
 
184
+    /**
185
+     * Loads and applies the settings from the specified file.
186
+     *
187
+     * @param name The name of the file
188
+     */
189
+    @SuppressWarnings("unchecked")
190
+    public void loadSettings(final String name) {
191
+        try {
192
+            final ObjectInputStream ois
193
+                    = new ObjectInputStream(new FileInputStream(
194
+                    new File(getConfigDir(), name + ".config")));
195
+
196
+            ois.readObject();
197
+            
198
+            final GameInfo gameInfo = (GameInfo) ois.readObject();
199
+            final List<Object[]> playerData = (List<Object[]>) ois.readObject();
200
+            final Object[] bettingData = (Object[]) ois.readObject();
201
+            final Object[] styleData = (Object[]) ois.readObject();
202
+            
203
+            gamePanel.setGame(gameInfo);
204
+            playerPanel.setData(playerData);
205
+            bettingPanel.setData(bettingData);
206
+            stylePanel.setData(styleData);
207
+        } catch (IOException ex) {
208
+            System.err.println("Unable to load settings from " + name);
209
+            ex.printStackTrace();
210
+        } catch (ClassNotFoundException ex) {
211
+            System.err.println("Unable to load settings from " + name);
212
+            ex.printStackTrace();
213
+        } catch (ClassCastException ex) {
214
+            System.err.println("Unable to load settings from " + name);
215
+            ex.printStackTrace();
216
+        }
217
+    }
218
+
219
+    /**
220
+     * Saves the current settings to the specified file.
221
+     * 
222
+     * @param name The name of the file
223
+     */
224
+    public void saveSettings(final String name) {
225
+        try {
226
+            final ObjectOutputStream oos
227
+                    = new ObjectOutputStream(new FileOutputStream(
228
+                    new File(getConfigDir(), name + ".config")));
229
+            oos.writeObject(new ConfigVersioner());
230
+            oos.writeObject(game);
231
+            oos.writeObject(playerPanel.getData());
232
+            oos.writeObject(bettingPanel.getData());
233
+            oos.writeObject(stylePanel.getData());
234
+        } catch (IOException ex) {
235
+            System.err.println("Unable to save settings to " + name);
236
+            ex.printStackTrace();
237
+        }
238
+    }
239
+
240
+    /**
241
+     * Returns the application's config directory.
242
+     *
243
+     * @return configuration directory
244
+     */
245
+    public static String getConfigDir() {
246
+        if (configdir == null) {
247
+            final String fs = System.getProperty("file.separator");
248
+            final String osName = System.getProperty("os.name");
249
+            if (osName.startsWith("Mac OS")) {
250
+                configdir = System.getProperty("user.home") + fs + "Library"
251
+                        + fs + "Preferences" + fs + "japoker" + fs;
252
+            } else if (osName.startsWith("Windows")) {
253
+                if (System.getenv("APPDATA") == null) {
254
+                    configdir = System.getProperty("user.home") + fs + "japoker" + fs;
255
+                } else {
256
+                    configdir = System.getenv("APPDATA") + fs + "japoker" + fs;
257
+                }
258
+            } else {
259
+                configdir = System.getProperty("user.home") + fs + ".japoker" + fs;
260
+            }
261
+        }
262
+
263
+        return configdir;
264
+    }
265
+    
266
+}
267
+
268
+/**
269
+ * A class used for versioning config files.
270
+ */
271
+class ConfigVersioner implements Serializable {
272
+
273
+    /**
274
+     * A version number for this class. It should be changed whenever the class
275
+     * structure is changed (or anything else that would prevent serialized
276
+     * objects being unserialized with the new class).
277
+     */
278
+    private static final long serialVersionUID = 2;
279
+
134 280
 }

+ 15
- 2
src/com/md87/cardgame/config/GamePanel.java View File

@@ -1,5 +1,5 @@
1 1
 /*
2
- * Copyright (c) Chris 'MD87' Smith, 2007. All rights reserved.
2
+ * Copyright (c) Chris 'MD87' Smith, 2007-2008. All rights reserved.
3 3
  *
4 4
  * This code may not be redistributed without prior permission from the
5 5
  * aforementioned copyright holder(s).
@@ -12,7 +12,6 @@ import java.awt.event.ActionEvent;
12 12
 import java.awt.event.ActionListener;
13 13
 import java.util.HashMap;
14 14
 import java.util.Map;
15
-import java.util.Map;
16 15
 
17 16
 import javax.swing.JPanel;
18 17
 import javax.swing.JRadioButton;
@@ -26,6 +25,11 @@ import net.miginfocom.swing.MigLayout;
26 25
  */
27 26
 public class GamePanel extends JPanel implements ActionListener {
28 27
     
28
+    /**
29
+     * A version number for this class. It should be changed whenever the class
30
+     * structure is changed (or anything else that would prevent serialized
31
+     * objects being unserialized with the new class).
32
+     */
29 33
     private static final long serialVersionUID = 1;
30 34
 
31 35
     private final Map<GameInfo, JRadioButton> games = new HashMap<GameInfo, JRadioButton>();
@@ -77,4 +81,13 @@ public class GamePanel extends JPanel implements ActionListener {
77 81
         }
78 82
     }
79 83
     
84
+    /**
85
+     * Sets the game that's currently selected.
86
+     * 
87
+     * @param target The game to select.
88
+     */
89
+    public void setGame(final GameInfo target) {
90
+        actionPerformed(new ActionEvent(games.get(target), 0, ""));
91
+    }
92
+    
80 93
 }

+ 51
- 1
src/com/md87/cardgame/config/PlayerPanel.java View File

@@ -1,5 +1,5 @@
1 1
 /*
2
- * Copyright (c) Chris 'MD87' Smith, 2007. All rights reserved.
2
+ * Copyright (c) Chris 'MD87' Smith, 2007-2008. All rights reserved.
3 3
  *
4 4
  * This code may not be redistributed without prior permission from the
5 5
  * aforementioned copyright holder(s).
@@ -32,6 +32,14 @@ import javax.swing.SpinnerNumberModel;
32 32
  */
33 33
 public class PlayerPanel extends JPanel {
34 34
     
35
+    /**
36
+     * A version number for this class. It should be changed whenever the class
37
+     * structure is changed (or anything else that would prevent serialized
38
+     * objects being unserialized with the new class).
39
+     */
40
+    private static final long serialVersionUID = 1;
41
+    
42
+    /** The maximum maximum number of players. */
35 43
     private static final int MAXPLAYERS = 14;
36 44
     
37 45
     private final JPanel innerPanel = new JPanel();
@@ -122,6 +130,48 @@ public class PlayerPanel extends JPanel {
122 130
         return res;
123 131
     }
124 132
     
133
+    /**
134
+     * Retrieves a list of data representing the state of this panel.
135
+     * 
136
+     * @return The state of this panel
137
+     */
138
+    public List<Object[]> getData() {
139
+        final List<Object[]> res = new ArrayList<Object[]>();
140
+        
141
+        for (int i = 0; i < MAXPLAYERS; i++) {
142
+            res.add(new Object[]{
143
+                checkBoxes.get(i).isSelected(),
144
+                textFields.get(i).getText(),
145
+                spinners.get(i).getValue(),
146
+                comboBoxes.get(i).getSelectedIndex(),
147
+            });
148
+        }
149
+        
150
+        return res;
151
+    }
152
+    
153
+    /**
154
+     * Sets the state of this panel to that described in the specified data list.
155
+     * 
156
+     * @param data The data to be loaded
157
+     */
158
+    public void setData(final List<Object[]> data) {
159
+        for (int i = 0; i < MAXPLAYERS; i++) {
160
+            final Object[] ldata = data.get(i);
161
+            checkBoxes.get(i).setSelected((Boolean) ldata[0]);
162
+            textFields.get(i).setText((String) ldata[1]);
163
+            spinners.get(i).setValue(ldata[2]);
164
+            comboBoxes.get(i).setSelectedIndex((Integer) ldata[3]);
165
+        }
166
+    }
167
+    
168
+    /**
169
+     * Retrieves a list of players that should be added to the game.
170
+     * 
171
+     * @param game The game that players will take part in
172
+     * @param window The game window shown locally
173
+     * @return A list of players to play in the game
174
+     */
125 175
     public List<Player> getPlayers(final Game game, final GameWindow window) {
126 176
         final List<Player> res = new ArrayList<Player>();
127 177
         

+ 22
- 1
src/com/md87/cardgame/config/games/GameInfo.java View File

@@ -9,11 +9,20 @@ package com.md87.cardgame.config.games;
9 9
 
10 10
 import com.md87.cardgame.interfaces.Game;
11 11
 
12
+import java.io.Serializable;
13
+
12 14
 /**
13 15
  *
14 16
  * @author Chris
15 17
  */
16
-public abstract class GameInfo {
18
+public abstract class GameInfo implements Serializable {
19
+    
20
+    /**
21
+     * A version number for this class. It should be changed whenever the class
22
+     * structure is changed (or anything else that would prevent serialized
23
+     * objects being unserialized with the new class).
24
+     */
25
+    private static final long serialVersionUID = 1;
17 26
     
18 27
     public static enum GameType  {
19 28
         STUD, DRAW, HOLDEM
@@ -48,5 +57,17 @@ public abstract class GameInfo {
48 57
             new AsianFiveCardStudInfo(),
49 58
         };
50 59
     }
60
+
61
+    /** {@inheritDoc} */
62
+    @Override
63
+    public boolean equals(final Object obj) {
64
+        return getClass().equals(obj.getClass());
65
+    }
66
+
67
+    /** {@inheritDoc} */
68
+    @Override
69
+    public int hashCode() {
70
+        return getClass().hashCode();
71
+    }
51 72
     
52 73
 }

Loading…
Cancel
Save