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