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