|
32 | 32 | import org.aavso.tools.vstar.ui.dialog.plugin.manager.PluginManager; |
33 | 33 | import org.aavso.tools.vstar.ui.mediator.Mediator; |
34 | 34 | import org.aavso.tools.vstar.ui.resources.PluginLoader; |
| 35 | +import org.aavso.tools.vstar.ui.vela.VeLaDialog; |
35 | 36 | import org.aavso.tools.vstar.util.locale.LocaleProps; |
36 | 37 | import org.aavso.tools.vstar.util.property.ApplicationProperties; |
37 | 38 |
|
|
40 | 41 | */ |
41 | 42 | public class VStar { |
42 | 43 |
|
43 | | - public static final String LOG_DIR = System.getProperty("user.home") |
44 | | - + File.separator + "vstar_log"; |
45 | | - |
46 | | - public static final String LOG_PATH = LOG_DIR + File.separator + "vstar.log"; |
47 | | - |
48 | | - public static Logger LOGGER; |
49 | | - |
50 | | - static { |
51 | | - try { |
52 | | - File logDir = new File(LOG_DIR); |
53 | | - if (!logDir.isDirectory()) { |
54 | | - logDir.mkdir(); |
55 | | - } |
56 | | - Handler fh = new FileHandler(LOG_PATH); |
57 | | - fh.setFormatter(new SimpleFormatter()); |
58 | | - LOGGER = Logger.getLogger("VStar Logger"); |
59 | | - LOGGER.setUseParentHandlers(false); |
60 | | - LOGGER.addHandler(fh); |
61 | | - |
62 | | - } catch (Exception e) { |
63 | | - // Default to console? |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - private static boolean setNativeLookAndFeel = true; |
68 | | - |
69 | | - private static boolean loadPlugins = true; |
70 | | - |
71 | | - private static boolean runScript = false; |
72 | | - private static String scriptPath = null; |
73 | | - |
74 | | - public static void main(String[] args) { |
75 | | - |
76 | | - String os_name = ""; |
77 | | - // For Mac OS X, make it look more native by using the screen |
78 | | - // menu bar. Suggested by Adam Weber. |
79 | | - try { |
80 | | - os_name = System.getProperty("os.name"); |
81 | | - if (os_name.startsWith("Mac OS X")) { |
82 | | - System.setProperty("apple.laf.useScreenMenuBar", "true"); |
83 | | - System.setProperty( |
84 | | - "com.apple.mrj.application.apple.menu.about.name", |
85 | | - "VStar"); |
86 | | - } |
87 | | - } catch (Exception e) { |
88 | | - System.err.println("Unable to detect operating system. Exiting."); |
89 | | - System.exit(1); |
90 | | - } |
91 | | - |
92 | | - processCmdLineArgs(args); |
93 | | - |
94 | | - if (setNativeLookAndFeel) { |
95 | | - // Set the Look & Feel of the application to be native. |
96 | | - try { |
97 | | - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
98 | | - // Under Windows, the default TextArea font is too small. |
99 | | - // Making TextArea the same as TextField fixes this. |
100 | | - // We should make this fix AFTER setting native Look & Feel! |
101 | | - if (os_name.startsWith("Windows")) { |
102 | | - // [https://stackoverflow.com/questions/6461506/jtextarea-default-font-very-small-in-windows] |
103 | | - UIManager.getDefaults().put("TextArea.font", UIManager.getFont("TextField.font")); |
104 | | - } |
105 | | - } catch (Exception e) { |
106 | | - System.err.println("Unable to set native look & feel. Exiting."); |
107 | | - System.exit(1); |
108 | | - } |
109 | | - } |
110 | | - |
111 | | - // Classes SeriesType and LocaleProps have static prefs members of Preferences type; |
112 | | - // those members cannot be created from within plug-ins in WebStart mode due to security restrictions. |
113 | | - // As far as 'prefs' in both classes are created with a 'static' block of code, any reference to the class |
114 | | - // initializes 'prefs'. This is ensured by a call of the empty initClass() method. |
115 | | - // After initialization, 'prefs' can be accessed from within plug-ins. |
116 | | - SeriesType.initClass(); |
117 | | - LocaleProps.initClass(); |
118 | | - |
119 | | - // If there's no command-line option that says we shouldn't load |
120 | | - // plug-ins and the plug-in manager says it's okay to load them, then go |
121 | | - // ahead. |
122 | | - if (loadPlugins && PluginManager.shouldLoadPlugins()) { |
123 | | - // Load plug-ins, if any exist and plug-in loading is enabled. |
124 | | - PluginLoader.loadPlugins(); |
125 | | - } |
126 | | - |
127 | | - // Create an uncaught exception handler. |
128 | | - Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { |
129 | | - public void uncaughtException(Thread th, Throwable ex) { |
130 | | - // LOGGER.log(Level.SEVERE, "Uncaught Exception", ex); |
131 | | - MessageBox.showErrorDialog("Error", ex); |
132 | | - } |
133 | | - }); |
134 | | - |
135 | | - // Schedule a job for the event-dispatching thread: |
136 | | - // creating and showing this application's GUI. |
137 | | - javax.swing.SwingUtilities.invokeLater(new Runnable() { |
138 | | - public void run() { |
139 | | - createAndShowGUI(); |
140 | | - } |
141 | | - }); |
142 | | - } |
143 | | - |
144 | | - /** |
145 | | - * Create and display the main window. |
146 | | - */ |
147 | | - private static void createAndShowGUI() { |
148 | | - try { |
149 | | - MainFrame frame = new MainFrame(); |
150 | | - final ApplicationProperties appProps = new ApplicationProperties( |
151 | | - frame); |
152 | | - |
153 | | - frame.setSize(appProps.getMainWdwWidth(), |
154 | | - appProps.getMainWdwHeight()); |
155 | | - frame.setLocation(appProps.getMainWdwUpperLeftX(), |
156 | | - appProps.getMainWdwUpperLeftY()); |
157 | | - |
158 | | - frame.setVisible(true); |
159 | | - |
160 | | - // We create a shutdown task rather than a window listener |
161 | | - // to store application properties, otherwise on the Mac, we |
162 | | - // would also have to trap the VStar (vs File) menu Quit item. |
163 | | - // This shutdown task should work uniformly across operating |
164 | | - // systems. The frame stored within appProps cannot be GC'd |
165 | | - // until appProps is, so its state will still be valid at the |
166 | | - // time run() is invoked. |
167 | | - Runnable shutdownTask = new Runnable() { |
168 | | - public void run() { |
169 | | - appProps.update(); |
170 | | - } |
171 | | - }; |
172 | | - |
173 | | - Runtime.getRuntime().addShutdownHook( |
174 | | - new Thread(shutdownTask, "Application shutdown task")); |
175 | | - |
176 | | - if (scriptPath != null) { |
177 | | - new ScriptRunner(false).runScript(new File(scriptPath)); |
178 | | - } |
179 | | - |
180 | | - } catch (Throwable t) { |
181 | | - MessageBox.showErrorDialog(Mediator.getUI().getComponent(), |
182 | | - "Error", t.getLocalizedMessage()); |
183 | | - } |
184 | | - } |
185 | | - |
186 | | - /** |
187 | | - * Process the command-line arguments. Note: If we do anything more complex |
188 | | - * than this, consideration should be given to using a library such as: |
189 | | - * http://commons.apache.org/cli/ |
190 | | - * |
191 | | - * @param args |
192 | | - * The command-line arguments; may be empty. |
193 | | - */ |
194 | | - private static void processCmdLineArgs(String[] args) { |
195 | | - for (String arg : args) { |
196 | | - if ("--help".equals(arg)) { |
197 | | - System.out.println("usage: vstar [--default-look-and-feel] [--noplugins] [--script path]"); |
198 | | - System.exit(0); |
199 | | - } else if ("--default-look-and-feel".equals(arg)) { |
200 | | - setNativeLookAndFeel = false; |
201 | | - } else if ("--noplugins".equals(arg)) { |
202 | | - loadPlugins = false; |
203 | | - } else if ("--script".equals(arg)) { |
204 | | - runScript = true; |
205 | | - } else if (runScript) { |
206 | | - scriptPath = arg; |
207 | | - } |
208 | | - } |
209 | | - } |
| 44 | + public static final String LOG_DIR = System.getProperty("user.home") + File.separator + "vstar_log"; |
| 45 | + |
| 46 | + public static final String LOG_PATH = LOG_DIR + File.separator + "vstar.log"; |
| 47 | + |
| 48 | + public static Logger LOGGER; |
| 49 | + |
| 50 | + static { |
| 51 | + try { |
| 52 | + File logDir = new File(LOG_DIR); |
| 53 | + if (!logDir.isDirectory()) { |
| 54 | + logDir.mkdir(); |
| 55 | + } |
| 56 | + Handler fh = new FileHandler(LOG_PATH); |
| 57 | + fh.setFormatter(new SimpleFormatter()); |
| 58 | + LOGGER = Logger.getLogger("VStar Logger"); |
| 59 | + LOGGER.setUseParentHandlers(false); |
| 60 | + LOGGER.addHandler(fh); |
| 61 | + |
| 62 | + } catch (Exception e) { |
| 63 | + // Default to console? |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static boolean setNativeLookAndFeel = true; |
| 68 | + |
| 69 | + private static boolean loadPlugins = true; |
| 70 | + |
| 71 | + private static boolean runScript = false; |
| 72 | + |
| 73 | + private static String scriptPath = null; |
| 74 | + |
| 75 | + private static boolean openVeLaDialog = false; |
| 76 | + |
| 77 | + public static void main(String[] args) { |
| 78 | + |
| 79 | + String os_name = ""; |
| 80 | + // For Mac OS X, make it look more native by using the screen |
| 81 | + // menu bar. Suggested by Adam Weber. |
| 82 | + try { |
| 83 | + os_name = System.getProperty("os.name"); |
| 84 | + if (os_name.startsWith("Mac OS X")) { |
| 85 | + System.setProperty("apple.laf.useScreenMenuBar", "true"); |
| 86 | + System.setProperty("com.apple.mrj.application.apple.menu.about.name", "VStar"); |
| 87 | + } |
| 88 | + } catch (Exception e) { |
| 89 | + System.err.println("Unable to detect operating system. Exiting."); |
| 90 | + System.exit(1); |
| 91 | + } |
| 92 | + |
| 93 | + processCmdLineArgs(args); |
| 94 | + |
| 95 | + if (setNativeLookAndFeel) { |
| 96 | + // Set the Look & Feel of the application to be native. |
| 97 | + try { |
| 98 | + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
| 99 | + // Under Windows, the default TextArea font is too small. |
| 100 | + // Making TextArea the same as TextField fixes this. |
| 101 | + // We should make this fix AFTER setting native Look & Feel! |
| 102 | + if (os_name.startsWith("Windows")) { |
| 103 | + // [https://stackoverflow.com/questions/6461506/jtextarea-default-font-very-small-in-windows] |
| 104 | + UIManager.getDefaults().put("TextArea.font", UIManager.getFont("TextField.font")); |
| 105 | + } |
| 106 | + } catch (Exception e) { |
| 107 | + System.err.println("Unable to set native look & feel. Exiting."); |
| 108 | + System.exit(1); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Classes SeriesType and LocaleProps have static prefs members of Preferences |
| 113 | + // type; those members cannot be created from within plug-ins in WebStart mode due to |
| 114 | + // security restrictions. |
| 115 | + // |
| 116 | + // As far as 'prefs' in both classes are created with a 'static' block of code, |
| 117 | + // any reference to the class |
| 118 | + // initializes 'prefs'. This is ensured by a call of the empty initClass() |
| 119 | + // method. |
| 120 | + // |
| 121 | + // After initialization, 'prefs' can be accessed from within plug-ins. |
| 122 | + SeriesType.initClass(); |
| 123 | + LocaleProps.initClass(); |
| 124 | + |
| 125 | + // If there's no command-line option that says we shouldn't load |
| 126 | + // plug-ins and the plug-in manager says it's okay to load them, then go |
| 127 | + // ahead. |
| 128 | + if (loadPlugins && PluginManager.shouldLoadPlugins()) { |
| 129 | + // Load plug-ins, if any exist and plug-in loading is enabled. |
| 130 | + PluginLoader.loadPlugins(); |
| 131 | + } |
| 132 | + |
| 133 | + // Create an uncaught exception handler. |
| 134 | + Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { |
| 135 | + public void uncaughtException(Thread th, Throwable ex) { |
| 136 | + // LOGGER.log(Level.SEVERE, "Uncaught Exception", ex); |
| 137 | + MessageBox.showErrorDialog("Error", ex); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + // Schedule a job for the event-dispatching thread: |
| 142 | + // creating and showing this application's GUI. |
| 143 | + javax.swing.SwingUtilities.invokeLater(new Runnable() { |
| 144 | + public void run() { |
| 145 | + createAndShowGUI(); |
| 146 | + } |
| 147 | + }); |
| 148 | + |
| 149 | + if (openVeLaDialog) { |
| 150 | + showVeLaDialog(); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Create and display the main window. |
| 156 | + */ |
| 157 | + private static void createAndShowGUI() { |
| 158 | + try { |
| 159 | + MainFrame frame = new MainFrame(); |
| 160 | + final ApplicationProperties appProps = new ApplicationProperties(frame); |
| 161 | + |
| 162 | + frame.setSize(appProps.getMainWdwWidth(), appProps.getMainWdwHeight()); |
| 163 | + frame.setLocation(appProps.getMainWdwUpperLeftX(), appProps.getMainWdwUpperLeftY()); |
| 164 | + |
| 165 | + // We need all the underlying main frame apparatus, but we may just want the |
| 166 | + // VeLa dialog. |
| 167 | + frame.setVisible(!openVeLaDialog); |
| 168 | + |
| 169 | + // We create a shutdown task rather than a window listener |
| 170 | + // to store application properties, otherwise on the Mac, we |
| 171 | + // would also have to trap the VStar (vs File) menu Quit item. |
| 172 | + // This shutdown task should work uniformly across operating |
| 173 | + // systems. The frame stored within appProps cannot be GC'd |
| 174 | + // until appProps is, so its state will still be valid at the |
| 175 | + // time run() is invoked. |
| 176 | + Runnable shutdownTask = new Runnable() { |
| 177 | + public void run() { |
| 178 | + appProps.update(); |
| 179 | + } |
| 180 | + }; |
| 181 | + |
| 182 | + Runtime.getRuntime().addShutdownHook(new Thread(shutdownTask, "Application shutdown task")); |
| 183 | + |
| 184 | + if (scriptPath != null) { |
| 185 | + new ScriptRunner(false).runScript(new File(scriptPath)); |
| 186 | + } |
| 187 | + } catch (Throwable t) { |
| 188 | + MessageBox.showErrorDialog(Mediator.getUI().getComponent(), "Error", t.getLocalizedMessage()); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Open the VeLa dialog. |
| 194 | + */ |
| 195 | + private static void showVeLaDialog() { |
| 196 | + javax.swing.SwingUtilities.invokeLater(new Runnable() { |
| 197 | + public void run() { |
| 198 | + new VeLaDialog(); |
| 199 | + System.exit(0); |
| 200 | + } |
| 201 | + }); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Process the command-line arguments. Note: If we do anything more complex than |
| 206 | + * this, consideration should be given to using a library such as: |
| 207 | + * http://commons.apache.org/cli/ |
| 208 | + * |
| 209 | + * @param args The command-line arguments; may be empty. |
| 210 | + */ |
| 211 | + private static void processCmdLineArgs(String[] args) { |
| 212 | + for (String arg : args) { |
| 213 | + if ("--help".equals(arg)) { |
| 214 | + System.out.println( |
| 215 | + "usage: vstar [--default-look-and-feel] [--noplugins] [--script path] [--VeLa-dialog]"); |
| 216 | + System.exit(0); |
| 217 | + } else if ("--default-look-and-feel".equals(arg)) { |
| 218 | + setNativeLookAndFeel = false; |
| 219 | + } else if ("--noplugins".equals(arg)) { |
| 220 | + loadPlugins = false; |
| 221 | + } else if ("--script".equals(arg)) { |
| 222 | + runScript = true; |
| 223 | + } else if (runScript) { |
| 224 | + scriptPath = arg; |
| 225 | + } else if ("--VeLa-dialog".equalsIgnoreCase(arg)) { |
| 226 | + openVeLaDialog = true; |
| 227 | + } |
| 228 | + } |
| 229 | + } |
210 | 230 | } |
0 commit comments