]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/GpsPruner.java
Version 11.1, August 2010
[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.Component;
6 import java.awt.event.WindowEvent;
7 import java.io.File;
8 import java.io.FileNotFoundException;
9 import java.util.Locale;
10 import java.util.ArrayList;
11 import javax.swing.JFrame;
12 import javax.swing.JSplitPane;
13 import javax.swing.JToolBar;
14 import javax.swing.WindowConstants;
15
16 import tim.prune.config.Config;
17 import tim.prune.config.ConfigException;
18 import tim.prune.gui.DetailsDisplay;
19 import tim.prune.gui.SidebarController;
20 import tim.prune.gui.IconManager;
21 import tim.prune.gui.MenuManager;
22 import tim.prune.gui.SelectorDisplay;
23 import tim.prune.gui.StatusBar;
24 import tim.prune.gui.Viewport;
25 import tim.prune.gui.map.MapCanvas;
26 import tim.prune.gui.profile.ProfileChart;
27
28 /**
29  * Prune is a tool to visualize, edit, convert and prune GPS data
30  * Please see the included readme.txt or http://activityworkshop.net
31  * This software is copyright activityworkshop.net 2006-2010 and made available through the Gnu GPL version 2.
32  * For license details please see the included license.txt.
33  * GpsPruner is the main entry point to the application, including initialisation and launch
34  */
35 public class GpsPruner
36 {
37         /** Version number of application, used in about screen and for version check */
38         public static final String VERSION_NUMBER = "11.1";
39         /** Build number, just used for about screen */
40         public static final String BUILD_NUMBER = "205";
41         /** Static reference to App object */
42         private static App APP = null;
43
44         /** Program name, used for Frame title and for Macs also on the system bar */
45         private static final String PROGRAM_NAME = "Prune";
46
47
48         /**
49          * Main method
50          * @param args command line arguments
51          */
52         public static void main(String[] args)
53         {
54                 Locale locale = null;
55                 String localeCode = null;
56                 String langFilename = null;
57                 String configFilename = null;
58                 ArrayList<File> dataFiles = new ArrayList<File>();
59                 boolean showUsage = false;
60
61                 // Mac OSX - specific properties (Mac insists that this is done as soon as possible)
62                 if (System.getProperty("mrj.version") != null) {
63                         System.setProperty("apple.laf.useScreenMenuBar", "true"); // menu at top of screen
64                         System.setProperty("com.apple.mrj.application.apple.menu.about.name", PROGRAM_NAME);
65                 }
66                 // Loop over given arguments, if any
67                 for (int i=0; i<args.length; i++)
68                 {
69                         String arg = args[i];
70                         if (arg.startsWith("--locale="))
71                         {
72                                 localeCode = arg.substring(9);
73                                 locale = getLanguage(localeCode);
74                         }
75                         else if (arg.startsWith("--lang="))
76                         {
77                                 localeCode = arg.substring(7);
78                                 locale = getLanguage(localeCode);
79                         }
80                         else if (arg.startsWith("--langfile="))
81                         {
82                                 langFilename = arg.substring(11);
83                         }
84                         else if (arg.startsWith("--configfile="))
85                         {
86                                 configFilename = arg.substring(13);
87                         }
88                         else if (arg.startsWith("--help")) {
89                                 showUsage = true;
90                         }
91                         else
92                         {
93                                 // Check if a data file has been given
94                                 File f = new File(arg);
95                                 if (f.exists() && f.canRead()) {
96                                         dataFiles.add(f);
97                                 }
98                                 else {
99                                         System.out.println("Unknown parameter '" + arg + "'.");
100                                         showUsage = true;
101                                 }
102                         }
103                 }
104                 if (showUsage) {
105                         System.out.println("Possible parameters:"
106                                 + "\n   --configfile=<file> used to specify a configuration file"
107                                 + "\n   --lang=<code> or --locale=<code>  used to specify language"
108                                 + "\n   --langfile=<file>   used to specify an alternative language file\n");
109                 }
110                 // Initialise configuration if selected
111                 try
112                 {
113                         if (configFilename != null) {
114                                 Config.loadFile(new File(configFilename));
115                         }
116                         else {
117                                 Config.loadDefaultFile();
118                         }
119                 }
120                 catch (ConfigException ce) {
121                         System.err.println("Failed to load config file: " + configFilename);
122                 }
123                 boolean overrideLang = (locale != null);
124                 if (overrideLang) {
125                         // Make sure Config holds chosen language
126                         Config.setConfigString(Config.KEY_LANGUAGE_CODE, localeCode);
127                 }
128                 else {
129                         // Set locale according to Config's language property
130                         String configLang = Config.getConfigString(Config.KEY_LANGUAGE_CODE);
131                         if (configLang != null) {
132                                 Locale configLocale = getLanguage(configLang);
133                                 if (configLocale != null) {locale = configLocale;}
134                         }
135                 }
136                 I18nManager.init(locale);
137                 // Load the external language file, either from config file or from command line params
138                 if (langFilename == null && !overrideLang) {
139                         // If langfilename is blank on command line parameters then don't use setting from config
140                         langFilename = Config.getConfigString(Config.KEY_LANGUAGE_FILE);
141                 }
142                 if (langFilename != null && !langFilename.equals("")) {
143                         try {
144                                 I18nManager.addLanguageFile(langFilename);
145                                 Config.setConfigString(Config.KEY_LANGUAGE_FILE, langFilename);
146                         }
147                         catch (FileNotFoundException fnfe) {
148                                 System.err.println("Failed to load language file: " + langFilename);
149                         }
150                 }
151                 // Set up the window and go
152                 launch(dataFiles);
153         }
154
155
156         /**
157          * Choose a locale based on the given code
158          * @param inString code for locale
159          * @return Locale object if available, otherwise null
160          */
161         private static Locale getLanguage(String inString)
162         {
163                 if (inString.length() == 2)
164                 {
165                         return new Locale(inString);
166                 }
167                 else if (inString.length() == 5)
168                 {
169                         return new Locale(inString.substring(0, 2), inString.substring(3));
170                 }
171                 System.out.println("Unrecognised locale '" + inString
172                         + "' - value should be eg 'DE' or 'DE_ch'");
173                 return null;
174         }
175
176
177         /**
178          * Launch the main application
179          * @param inDataFiles list of data files to load on startup
180          */
181         private static void launch(ArrayList<File> inDataFiles)
182         {
183                 // Initialise Frame
184                 JFrame frame = new JFrame(PROGRAM_NAME);
185                 APP = new App(frame);
186
187                 // make menu
188                 MenuManager menuManager = new MenuManager(APP, APP.getTrackInfo());
189                 frame.setJMenuBar(menuManager.createMenuBar());
190                 APP.setMenuManager(menuManager);
191                 UpdateMessageBroker.addSubscriber(menuManager);
192                 // Make toolbar for buttons
193                 JToolBar toolbar = menuManager.createToolBar();
194
195                 // Make main GUI components and add as listeners
196                 SelectorDisplay leftPanel = new SelectorDisplay(APP.getTrackInfo());
197                 UpdateMessageBroker.addSubscriber(leftPanel);
198                 DetailsDisplay rightPanel = new DetailsDisplay(APP.getTrackInfo());
199                 UpdateMessageBroker.addSubscriber(rightPanel);
200                 MapCanvas mapDisp = new MapCanvas(APP, APP.getTrackInfo());
201                 UpdateMessageBroker.addSubscriber(mapDisp);
202                 Viewport viewport = new Viewport(mapDisp);
203                 APP.setViewport(viewport);
204                 ProfileChart profileDisp = new ProfileChart(APP.getTrackInfo());
205                 UpdateMessageBroker.addSubscriber(profileDisp);
206                 StatusBar statusBar = new StatusBar();
207                 UpdateMessageBroker.addSubscriber(statusBar);
208                 UpdateMessageBroker.informSubscribers("Prune v" + VERSION_NUMBER);
209
210                 // Arrange in the frame using split panes
211                 JSplitPane midSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, mapDisp, profileDisp);
212                 midSplit.setResizeWeight(1.0); // allocate as much space as poss to map
213                 JSplitPane rightSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, midSplit, rightPanel);
214                 rightSplit.setResizeWeight(1.0); // allocate as much space as poss to map
215
216                 frame.getContentPane().setLayout(new BorderLayout());
217                 frame.getContentPane().add(toolbar, BorderLayout.NORTH);
218                 JSplitPane leftSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightSplit);
219                 frame.getContentPane().add(leftSplit, BorderLayout.CENTER);
220                 frame.getContentPane().add(statusBar, BorderLayout.SOUTH);
221
222                 // add closing listener
223                 frame.addWindowListener(new WindowAdapter() {
224                         public void windowClosing(WindowEvent e) {
225                                 APP.exit();
226                         }
227                 });
228                 // Avoid automatically shutting down if window closed
229                 frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
230
231                 // set icon
232                 try {
233                         frame.setIconImage(IconManager.getImageIcon(IconManager.WINDOW_ICON).getImage());
234                 }
235                 catch (Exception e) {} // ignore
236
237                 // finish off and display frame
238                 frame.pack();
239                 frame.setSize(650, 450);
240                 frame.setVisible(true);
241                 // Set position of map/profile splitter
242                 midSplit.setDividerLocation(0.75);
243
244                 // Make a full screen toggler
245                 SidebarController fsc = new SidebarController(new Component[] {leftPanel, profileDisp, rightPanel},
246                         new JSplitPane[] {leftSplit, midSplit, rightSplit});
247                 APP.setSidebarController(fsc);
248                 // Finally, give the files to load to the App
249                 APP.loadDataFiles(inDataFiles);
250         }
251 }