~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/games/strategy/engine/framework/GameRunner2.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package games.strategy.engine.framework;
 
2
 
 
3
import games.strategy.debug.Console;
 
4
import games.strategy.engine.EngineVersion;
 
5
import games.strategy.engine.framework.startup.ui.MainFrame;
 
6
import games.strategy.triplea.ui.ErrorHandler;
 
7
 
 
8
import java.util.logging.LogManager;
 
9
import java.util.prefs.BackingStoreException;
 
10
import java.util.prefs.Preferences;
 
11
 
 
12
import javax.swing.LookAndFeel;
 
13
import javax.swing.SwingUtilities;
 
14
import javax.swing.UIManager;
 
15
 
 
16
 
 
17
public class GameRunner2
 
18
{
 
19
    public static final String LOOK_AND_FEEL_PREF = "LookAndFeel";
 
20
    
 
21
    public static final String TRIPLEA_GAME_PROPERTY = "triplea.game";
 
22
    public static final String TRIPLEA_HOST_PROPERTY = "triplea.host";
 
23
    public static final String TRIPLEA_PORT_PROPERTY = "triplea.port";
 
24
    public static final String TRIPLEA_SERVER_PASSWORD_PROPERTY = "triplea.server.password";
 
25
    public static final String TRIPLEA_CLIENT_PROPERTY = "triplea.client";
 
26
    public static final String TRIPLEA_SERVER_PROPERTY = "triplea.server";
 
27
    public static final String TRIPLEA_NAME_PROPERTY = "triplea.name";
 
28
    public static final String TRIPLEA_STARTED = "triplea.started";
 
29
 
 
30
    //these properties are for games that should connect to the Lobby Server
 
31
    public static final String LOBBY_PORT = "triplea.lobby.port";
 
32
    public static final String LOBBY_HOST = "triplea.lobby.host";
 
33
    public static final String LOBBY_GAME_COMMENTS = "triplea.lobby.game.comments";
 
34
    public static final String LOBBY_GAME_HOSTED_BY = "triplea.lobby.game.hostedBy";
 
35
    
 
36
    
 
37
    
 
38
    public static void main(String[] args)
 
39
    {
 
40
        setupLogging();
 
41
        
 
42
        Console.getConsole().displayStandardError();
 
43
        Console.getConsole().displayStandardOutput();
 
44
 
 
45
        System.setProperty("sun.awt.exception.handler", ErrorHandler.class.getName());
 
46
        System.setProperty("triplea.engine.version", EngineVersion.VERSION.toString());
 
47
        
 
48
        setupLookAndFeel();
 
49
 
 
50
        handleCommandLineArgs(args);
 
51
        showMainFrame();
 
52
    }
 
53
 
 
54
    private static void showMainFrame()
 
55
    {
 
56
        SwingUtilities.invokeLater(new Runnable()
 
57
        {
 
58
        
 
59
            public void run()
 
60
            {
 
61
                MainFrame frame = new MainFrame();        
 
62
                frame.start();
 
63
                frame.requestFocus();
 
64
                frame.toFront();
 
65
 
 
66
            }
 
67
        
 
68
        });
 
69
    }
 
70
    
 
71
    /**
 
72
     * Move command line arguments to System.properties
 
73
     */
 
74
    private static void handleCommandLineArgs(String[] args)
 
75
    {
 
76
        String[] properties = new String[] {TRIPLEA_SERVER_PROPERTY, TRIPLEA_CLIENT_PROPERTY, TRIPLEA_PORT_PROPERTY, TRIPLEA_HOST_PROPERTY, TRIPLEA_GAME_PROPERTY, TRIPLEA_NAME_PROPERTY, TRIPLEA_SERVER_PASSWORD_PROPERTY};
 
77
        
 
78
        
 
79
        //if only 1 arg, it must be the game name
 
80
        //find it
 
81
        //optionally, it may not start with the property name
 
82
        if(args.length == 1)
 
83
        {
 
84
            String value;
 
85
            if(args[0].startsWith(TRIPLEA_GAME_PROPERTY))
 
86
            {
 
87
                value = getValue( TRIPLEA_GAME_PROPERTY);
 
88
            }
 
89
            else
 
90
            {
 
91
                value = args[0];
 
92
            }
 
93
            System.setProperty(TRIPLEA_GAME_PROPERTY, value);
 
94
            return;
 
95
        }
 
96
        
 
97
        boolean usagePrinted = false;
 
98
        for(int argIndex = 0; argIndex < args.length; argIndex++)
 
99
        {
 
100
            boolean found = false;
 
101
            for(int propIndex = 0; propIndex < properties.length; propIndex++)
 
102
            {
 
103
                if(args[argIndex].startsWith(properties[propIndex]))
 
104
                {
 
105
                    String value = getValue(args[argIndex]);
 
106
                    System.getProperties().setProperty(properties[propIndex], value);
 
107
                    System.out.println(properties[propIndex] + ":" + value );
 
108
                    found = true;
 
109
                }
 
110
            }
 
111
            if(!found)
 
112
            {
 
113
                System.out.println("Unrecogized:" + args[argIndex]);
 
114
                if(!usagePrinted)
 
115
                {
 
116
                    usagePrinted = true;
 
117
                    usage();
 
118
                }
 
119
            }
 
120
        }
 
121
    }
 
122
    
 
123
    private static void usage()
 
124
    {
 
125
        System.out.println("Arguments\n" + 
 
126
                "   triplea.game=<FILE_NAME>\n" + 
 
127
                "   triplea.server=true\n" + 
 
128
                "   triplea.client=true\n" + 
 
129
                "   triplea.port=<PORT>\n" + 
 
130
                "   triplea.host=<HOST>\n" + 
 
131
                "   triplea.name=<PLAYER_NAME>\n" + 
 
132
                "\n" + 
 
133
                "if there is only one argument, and it does not start with triplea.game, the argument will be \n" + 
 
134
                "taken as the name of the file to load.\n" + 
 
135
                "\n" + 
 
136
                "Example\n" + 
 
137
                "   to start a game using the given file:\n" + 
 
138
                "\n" + 
 
139
                "   triplea /home/sgb/games/test.xml\n" + 
 
140
                "\n" + 
 
141
                "   or\n" + 
 
142
                "\n" + 
 
143
                "   triplea triplea.game=/home/sgb/games/test.xml\n" + 
 
144
                "\n" + 
 
145
                "   to connect to a remote host:\n" + 
 
146
                "\n" + 
 
147
                "   triplea triplea.client=true triplea.host=127.0.0.0 triplea.port=3300 triplea.name=Paul\n" + 
 
148
                "\n" + 
 
149
                "   to start a server with the given game\n" + 
 
150
                "\n" + 
 
151
                "   triplea triplea.game=/home/sgb/games/test.xml triplea.server=true triplea.port=3300 triplea.name=Allan" + 
 
152
                "\n" +
 
153
                "   to start a server, you can optionally password protect the game using triplea.server.password=foo" 
 
154
                );
 
155
    }
 
156
    
 
157
    private static String getValue(String arg)
 
158
    {
 
159
        int index = arg.indexOf('=');
 
160
        if(index == -1)
 
161
            return "";
 
162
        return arg.substring(index +1);
 
163
    }
 
164
 
 
165
    public static void setupLookAndFeel()
 
166
    {
 
167
        try
 
168
        {
 
169
                SwingUtilities.invokeAndWait(new Runnable() {
 
170
                                
 
171
                                public void run() {
 
172
                                        try
 
173
                                        {
 
174
                                                UIManager.setLookAndFeel(getDefaultLookAndFeel());
 
175
                                        } catch(Throwable t) {
 
176
                                if(!GameRunner.isMac()) {   
 
177
                                        try
 
178
                                        {
 
179
                                                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 
180
                                        } catch(Exception e) {}
 
181
                                }
 
182
                            }
 
183
                                }
 
184
                        });
 
185
        } catch(Throwable t) {
 
186
            t.printStackTrace(System.out);
 
187
        }
 
188
    }
 
189
    
 
190
    public static void setupLogging()
 
191
    {
 
192
        //setup logging to read our logging.properties
 
193
        try
 
194
        {
 
195
            LogManager.getLogManager().readConfiguration(ClassLoader.getSystemResourceAsStream("logging.properties"));
 
196
        } catch (Exception e)
 
197
        {
 
198
            e.printStackTrace();
 
199
        }
 
200
    }
 
201
    
 
202
    public static String getDefaultLookAndFeel() {
 
203
        Preferences pref = Preferences.userNodeForPackage(GameRunner2.class);
 
204
        String defaultLokAndFeel =  "org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel";
 
205
        //macs are already beautiful
 
206
        if(GameRunner.isMac()) {
 
207
            defaultLokAndFeel = UIManager.getSystemLookAndFeelClassName();
 
208
        }        
 
209
        return pref.get(LOOK_AND_FEEL_PREF, defaultLokAndFeel);
 
210
    }
 
211
    
 
212
    public static void setDefaultLookAndFeel(String lookAndFeelClassName) { 
 
213
        Preferences pref = Preferences.userNodeForPackage(GameRunner2.class);
 
214
        pref.put(LOOK_AND_FEEL_PREF, lookAndFeelClassName);
 
215
        try {
 
216
            pref.sync();
 
217
        } catch (BackingStoreException e) {
 
218
            e.printStackTrace();
 
219
        }
 
220
    }
 
221
    
 
222
}
 
223