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