]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/GpsPruner.java
Version 7, February 2009
[GpsPrune.git] / tim / prune / GpsPruner.java
1 package tim.prune;
2
3 import java.awt.event.WindowAdapter;
4 import java.awt.BorderLayout;
5 import java.awt.event.WindowEvent;
6 import java.io.File;
7 import java.util.Locale;
8 import javax.swing.JFrame;
9 import javax.swing.JSplitPane;
10 import javax.swing.JToolBar;
11 import javax.swing.WindowConstants;
12
13 import tim.prune.gui.DetailsDisplay;
14 import tim.prune.gui.IconManager;
15 import tim.prune.gui.MenuManager;
16 import tim.prune.gui.ProfileChart;
17 import tim.prune.gui.SelectorDisplay;
18 import tim.prune.gui.StatusBar;
19 import tim.prune.gui.map.MapCanvas;
20
21 /**
22  * Tool to visualize, edit, convert and prune GPS data
23  * Please see the included readme.txt or http://activityworkshop.net
24  * This software is copyright activityworkshop.net and made available through the Gnu GPL
25  */
26 public class GpsPruner
27 {
28         /** Version number of application, used in about screen and for version check */
29         public static final String VERSION_NUMBER = "7";
30         /** Build number, just used for about screen */
31         public static final String BUILD_NUMBER = "136";
32         /** Static reference to App object */
33         private static App APP = null;
34
35
36         /**
37          * Main method
38          * @param args command line arguments
39          */
40         public static void main(String[] args)
41         {
42                 Locale locale = null;
43                 String langFilename = null;
44                 String configFilename = null;
45                 boolean showUsage = false;
46                 for (int i=0; i<args.length; i++)
47                 {
48                         String arg = args[i];
49                         if (arg.startsWith("--locale="))
50                         {
51                                 locale = getLanguage(arg.substring(9));
52                         }
53                         else if (arg.startsWith("--lang="))
54                         {
55                                 locale = getLanguage(arg.substring(7));
56                         }
57                         else if (arg.startsWith("--langfile="))
58                         {
59                                 langFilename = arg.substring(11);
60                         }
61                         else if (arg.startsWith("--configfile="))
62                         {
63                                 configFilename = arg.substring(13);
64                         }
65                         else
66                         {
67                                 if (!arg.startsWith("--help")) {
68                                         System.out.println("Unknown parameter '" + arg + "'.");
69                                 }
70                                 showUsage = true;
71                         }
72                 }
73                 if (showUsage) {
74                         System.out.println("Possible parameters:"
75                                 + "\n   --configfile=<file> used to specify a configuration file"
76                                 + "\n   --lang=<code> or --locale=<code>  used to specify language"
77                                 + "\n   --langfile=<file>   used to specify an alternative language file\n");
78                 }
79                 // Initialise configuration if selected
80                 try
81                 {
82                         if (configFilename != null) {
83                                 Config.loadFile(new File(configFilename));
84                         }
85                         else {
86                                 Config.loadDefaultFile();
87                         }
88                 }
89                 catch (ConfigException ce) {
90                         System.err.println("Failed to load config file: " + configFilename);
91                 }
92                 // Set locale according to Config's language property
93                 String langCode = Config.getLanguageCode();
94                 if (locale == null && langCode != null) {
95                         Locale configLocale = getLanguage(langCode);
96                         if (configLocale != null) {locale = configLocale;}
97                 }
98                 I18nManager.init(locale);
99                 if (langFilename != null) {
100                         I18nManager.addLanguageFile(langFilename);
101                 }
102                 // Set up the window and go
103                 launch();
104         }
105
106
107         /**
108          * Choose a locale based on the given code
109          * @param inString code for locale
110          * @return Locale object if available, otherwise null
111          */
112         private static Locale getLanguage(String inString)
113         {
114                 if (inString.length() == 2)
115                 {
116                         return new Locale(inString);
117                 }
118                 else if (inString.length() == 5)
119                 {
120                         return new Locale(inString.substring(0, 2), inString.substring(3));
121                 }
122                 System.out.println("Unrecognised locale '" + inString
123                         + "' - value should be eg 'DE' or 'DE_ch'");
124                 return null;
125         }
126
127
128         /**
129          * Launch the main application
130          */
131         private static void launch()
132         {
133                 JFrame frame = new JFrame("Prune");
134                 APP = new App(frame);
135
136                 // make menu
137                 MenuManager menuManager = new MenuManager(APP, APP.getTrackInfo());
138                 frame.setJMenuBar(menuManager.createMenuBar());
139                 APP.setMenuManager(menuManager);
140                 UpdateMessageBroker.addSubscriber(menuManager);
141                 // Make toolbar for buttons
142                 JToolBar toolbar = menuManager.createToolBar();
143
144                 // Make main GUI components and add as listeners
145                 SelectorDisplay leftPanel = new SelectorDisplay(APP.getTrackInfo());
146                 UpdateMessageBroker.addSubscriber(leftPanel);
147                 DetailsDisplay rightPanel = new DetailsDisplay(APP.getTrackInfo());
148                 UpdateMessageBroker.addSubscriber(rightPanel);
149                 MapCanvas mapDisp = new MapCanvas(APP, APP.getTrackInfo());
150                 UpdateMessageBroker.addSubscriber(mapDisp);
151                 ProfileChart profileDisp = new ProfileChart(APP.getTrackInfo());
152                 UpdateMessageBroker.addSubscriber(profileDisp);
153                 StatusBar statusBar = new StatusBar();
154                 UpdateMessageBroker.addSubscriber(statusBar);
155                 UpdateMessageBroker.informSubscribers("Prune v" + VERSION_NUMBER);
156
157                 // Arrange in the frame using split panes
158                 JSplitPane midPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, mapDisp, profileDisp);
159                 midPane.setResizeWeight(1.0); // allocate as much space as poss to map
160                 JSplitPane triplePane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, midPane, rightPanel);
161                 triplePane.setResizeWeight(1.0); // allocate as much space as poss to map
162
163                 frame.getContentPane().setLayout(new BorderLayout());
164                 frame.getContentPane().add(toolbar, BorderLayout.NORTH);
165                 frame.getContentPane().add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel,
166                         triplePane), BorderLayout.CENTER);
167                 frame.getContentPane().add(statusBar, BorderLayout.SOUTH);
168
169                 // add closing listener
170                 frame.addWindowListener(new WindowAdapter() {
171                         public void windowClosing(WindowEvent e) {
172                                 APP.exit();
173                         }
174                 });
175                 // Avoid automatically shutting down if window closed
176                 frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
177
178                 // set icon
179                 try {
180                         frame.setIconImage(IconManager.getImageIcon(IconManager.WINDOW_ICON).getImage());
181                 }
182                 catch (Exception e) {} // ignore
183
184                 // finish off and display frame
185                 frame.pack();
186                 frame.setSize(650, 450);
187                 frame.setVisible(true);
188                 // Set position of map/profile splitter
189                 midPane.setDividerLocation(0.75);
190         }
191 }