]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/GpsPrune.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / GpsPrune.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  * GpsPrune 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-2012 and made available through the Gnu GPL version 2.
32  * For license details please see the included license.txt.
33  * GpsPrune is the main entry point to the application, including initialisation and launch
34  */
35 public class GpsPrune
36 {
37         /** Version number of application, used in about screen and for version check */
38         public static final String VERSION_NUMBER = "14";
39         /** Build number, just used for about screen */
40         public static final String BUILD_NUMBER = "265a";
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 = "GpsPrune";
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("--lang="))
71                         {
72                                 localeCode = arg.substring(7);
73                                 locale = getLanguage(localeCode);
74                         }
75                         else if (arg.startsWith("--langfile="))
76                         {
77                                 langFilename = arg.substring(11);
78                         }
79                         else if (arg.startsWith("--configfile="))
80                         {
81                                 configFilename = arg.substring(13);
82                         }
83                         else if (arg.startsWith("--help")) {
84                                 showUsage = true;
85                         }
86                         else
87                         {
88                                 // Check if a data file has been given
89                                 File f = new File(arg);
90                                 if (f.exists() && f.isFile() && f.canRead()) {
91                                         dataFiles.add(f);
92                                 }
93                                 else
94                                 {
95                                         // Make a useful String from the unknown parameter - could it be a mistyped filename?
96                                         System.out.println(makeUnknownParameterString(arg));
97                                         showUsage = true;
98                                 }
99                         }
100                 }
101                 if (showUsage) {
102                         System.out.println("Possible parameters:"
103                                 + "\n   --configfile=<file> used to specify a configuration file"
104                                 + "\n   --lang=<code>       used to specify language code such as DE"
105                                 + "\n   --langfile=<file>   used to specify an alternative language file\n");
106                 }
107                 // Initialise configuration if selected
108                 try
109                 {
110                         if (configFilename != null) {
111                                 Config.loadFile(new File(configFilename));
112                         }
113                         else {
114                                 Config.loadDefaultFile();
115                         }
116                 }
117                 catch (ConfigException ce) {
118                         System.err.println("Failed to load config file: " + configFilename);
119                 }
120                 boolean overrideLang = (locale != null);
121                 if (overrideLang) {
122                         // Make sure Config holds chosen language
123                         Config.setConfigString(Config.KEY_LANGUAGE_CODE, localeCode);
124                 }
125                 else {
126                         // Set locale according to Config's language property
127                         String configLang = Config.getConfigString(Config.KEY_LANGUAGE_CODE);
128                         if (configLang != null) {
129                                 Locale configLocale = getLanguage(configLang);
130                                 if (configLocale != null) {locale = configLocale;}
131                         }
132                 }
133                 I18nManager.init(locale);
134                 // Load the external language file, either from config file or from command line params
135                 if (langFilename == null && !overrideLang) {
136                         // If langfilename is blank on command line parameters then don't use setting from config
137                         langFilename = Config.getConfigString(Config.KEY_LANGUAGE_FILE);
138                 }
139                 if (langFilename != null)
140                 {
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                                 Config.setConfigString(Config.KEY_LANGUAGE_FILE, "");
148                         }
149                 }
150                 // Set up the window and go
151                 launch(dataFiles);
152         }
153
154
155         /**
156          * Choose a locale based on the given code
157          * @param inString code for locale
158          * @return Locale object if available, otherwise null
159          */
160         private static Locale getLanguage(String inString)
161         {
162                 if (inString.length() == 2)
163                 {
164                         return new Locale(inString);
165                 }
166                 else if (inString.length() == 5 && inString.charAt(2) == '_')
167                 {
168                         return new Locale(inString.substring(0, 2), inString.substring(3));
169                 }
170                 System.out.println("Unrecognised locale '" + inString
171                         + "' - value should be eg 'DE' or 'DE_ch'");
172                 return null;
173         }
174
175
176         /**
177          * Launch the main application
178          * @param inDataFiles list of data files to load on startup
179          */
180         private static void launch(ArrayList<File> inDataFiles)
181         {
182                 // Initialise Frame
183                 JFrame frame = new JFrame(PROGRAM_NAME);
184                 APP = new App(frame);
185
186                 // make menu
187                 MenuManager menuManager = new MenuManager(APP, APP.getTrackInfo());
188                 frame.setJMenuBar(menuManager.createMenuBar());
189                 APP.setMenuManager(menuManager);
190                 UpdateMessageBroker.addSubscriber(menuManager);
191                 // Make toolbar for buttons
192                 JToolBar toolbar = menuManager.createToolBar();
193
194                 // Make main GUI components and add as listeners
195                 SelectorDisplay leftPanel = new SelectorDisplay(APP.getTrackInfo());
196                 UpdateMessageBroker.addSubscriber(leftPanel);
197                 DetailsDisplay rightPanel = new DetailsDisplay(APP.getTrackInfo());
198                 UpdateMessageBroker.addSubscriber(rightPanel);
199                 MapCanvas mapDisp = new MapCanvas(APP, APP.getTrackInfo());
200                 UpdateMessageBroker.addSubscriber(mapDisp);
201                 Viewport viewport = new Viewport(mapDisp);
202                 APP.setViewport(viewport);
203                 ProfileChart profileDisp = new ProfileChart(APP.getTrackInfo());
204                 UpdateMessageBroker.addSubscriber(profileDisp);
205                 StatusBar statusBar = new StatusBar();
206                 UpdateMessageBroker.addSubscriber(statusBar);
207                 UpdateMessageBroker.informSubscribers("GpsPrune v" + VERSION_NUMBER);
208
209                 // Arrange in the frame using split panes
210                 JSplitPane midSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, mapDisp, profileDisp);
211                 midSplit.setResizeWeight(1.0); // allocate as much space as poss to map
212                 JSplitPane rightSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, midSplit, rightPanel);
213                 rightSplit.setResizeWeight(1.0); // allocate as much space as poss to map
214
215                 frame.getContentPane().setLayout(new BorderLayout());
216                 frame.getContentPane().add(toolbar, BorderLayout.NORTH);
217                 JSplitPane leftSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightSplit);
218                 frame.getContentPane().add(leftSplit, BorderLayout.CENTER);
219                 frame.getContentPane().add(statusBar, BorderLayout.SOUTH);
220
221                 // add closing listener
222                 frame.addWindowListener(new WindowAdapter() {
223                         public void windowClosing(WindowEvent e) {
224                                 APP.exit();
225                         }
226                 });
227                 // Avoid automatically shutting down if window closed
228                 frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
229
230                 // set icon
231                 try {
232                         frame.setIconImage(IconManager.getImageIcon(IconManager.WINDOW_ICON).getImage());
233                 }
234                 catch (Exception e) {} // ignore
235
236                 // finish off and display frame
237                 frame.pack();
238                 frame.setSize(650, 450);
239                 frame.setVisible(true);
240                 // Set position of map/profile splitter
241                 midSplit.setDividerLocation(0.75);
242                 // Update menu (only needed for recent file list)
243                 UpdateMessageBroker.informSubscribers();
244
245                 // Make a full screen toggler
246                 SidebarController fsc = new SidebarController(new Component[] {leftPanel, profileDisp, rightPanel},
247                         new JSplitPane[] {leftSplit, midSplit, rightSplit});
248                 APP.setSidebarController(fsc);
249                 // Finally, give the files to load to the App
250                 APP.loadDataFiles(inDataFiles);
251         }
252
253         /**
254          * Try to guess whether it's a mistyped parameter or a mistyped filename
255          * @param inParam command line argument
256          * @return error message
257          */
258         private static String makeUnknownParameterString(String inParam)
259         {
260                 File file = new File(inParam);
261                 if (file.exists())
262                 {
263                         if (file.isDirectory()) return "'" + inParam + "' is a directory";
264                         if (!file.canRead())    return "Cannot read file '" + inParam + "'";
265                         return "Something wrong with file '" + inParam + "'";
266                 }
267                 do
268                 {
269                         String name = file.getName();
270                         file = file.getParentFile();
271                         if (file != null && file.exists() && file.canRead()) return "Tried to load file '" + inParam + "' but cannot find '" + name + "'";
272                 }
273                 while (file != null);
274
275                 return "Unknown parameter '" + inParam + "'";
276         }
277 }