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