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