3 import java.awt.event.WindowAdapter;
4 import java.awt.BorderLayout;
5 import java.awt.event.WindowEvent;
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;
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;
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
33 public class GpsPruner
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;
42 /** Program name, used for Frame title and for Macs also on the system bar */
43 private static final String PROGRAM_NAME = "Prune";
48 * @param args command line arguments
50 public static void main(String[] args)
53 String localeCode = null;
54 String langFilename = null;
55 String configFilename = null;
56 ArrayList<File> dataFiles = new ArrayList<File>();
57 boolean showUsage = false;
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);
64 // Loop over given arguments, if any
65 for (int i=0; i<args.length; i++)
68 if (arg.startsWith("--locale="))
70 localeCode = arg.substring(9);
71 locale = getLanguage(localeCode);
73 else if (arg.startsWith("--lang="))
75 localeCode = arg.substring(7);
76 locale = getLanguage(localeCode);
78 else if (arg.startsWith("--langfile="))
80 langFilename = arg.substring(11);
82 else if (arg.startsWith("--configfile="))
84 configFilename = arg.substring(13);
86 else if (arg.startsWith("--help")) {
91 // Check if a data file has been given
92 File f = new File(arg);
93 if (f.exists() && f.canRead()) {
97 System.out.println("Unknown parameter '" + arg + "'.");
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");
108 // Initialise configuration if selected
111 if (configFilename != null) {
112 Config.loadFile(new File(configFilename));
115 Config.loadDefaultFile();
118 catch (ConfigException ce) {
119 System.err.println("Failed to load config file: " + configFilename);
121 boolean overrideLang = (locale != null);
123 // Make sure Config holds chosen language
124 Config.setConfigString(Config.KEY_LANGUAGE_CODE, localeCode);
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;}
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);
140 if (langFilename != null && !langFilename.equals("")) {
142 I18nManager.addLanguageFile(langFilename);
143 Config.setConfigString(Config.KEY_LANGUAGE_FILE, langFilename);
145 catch (FileNotFoundException fnfe) {
146 System.err.println("Failed to load language file: " + langFilename);
149 // Set up the window and go
155 * Choose a locale based on the given code
156 * @param inString code for locale
157 * @return Locale object if available, otherwise null
159 private static Locale getLanguage(String inString)
161 if (inString.length() == 2)
163 return new Locale(inString);
165 else if (inString.length() == 5)
167 return new Locale(inString.substring(0, 2), inString.substring(3));
169 System.out.println("Unrecognised locale '" + inString
170 + "' - value should be eg 'DE' or 'DE_ch'");
176 * Launch the main application
177 * @param inDataFiles list of data files to load on startup
179 private static void launch(ArrayList<File> inDataFiles)
182 JFrame frame = new JFrame(PROGRAM_NAME);
183 APP = new App(frame);
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();
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);
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
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);
220 // add closing listener
221 frame.addWindowListener(new WindowAdapter() {
222 public void windowClosing(WindowEvent e) {
226 // Avoid automatically shutting down if window closed
227 frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
231 frame.setIconImage(IconManager.getImageIcon(IconManager.WINDOW_ICON).getImage());
233 catch (Exception e) {} // ignore
235 // finish off and display frame
237 frame.setSize(650, 450);
238 frame.setVisible(true);
239 // Set position of map/profile splitter
240 midPane.setDividerLocation(0.75);
242 // Finally, give the files to load to the App
243 APP.loadDataFiles(inDataFiles);