001package apps; 002 003import apps.gui3.tabbedpreferences.TabbedPreferences; 004import apps.gui3.tabbedpreferences.TabbedPreferencesAction; 005import apps.plaf.macosx.Application; 006import apps.util.Log4JUtil; 007 008import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 009 010import java.awt.*; 011import java.awt.event.*; 012import java.beans.PropertyChangeEvent; 013import java.beans.PropertyChangeListener; 014import java.io.*; 015import java.lang.reflect.InvocationTargetException; 016import java.net.*; 017import java.util.*; 018import javax.swing.*; 019import javax.swing.text.DefaultEditorKit; 020import javax.swing.text.JTextComponent; 021 022import jmri.*; 023import jmri.jmrit.jython.*; 024import jmri.jmrit.logixng.LogixNG_Manager; 025import jmri.jmrit.logixng.LogixNGPreferences; 026import jmri.jmrit.revhistory.FileHistory; 027import jmri.jmrit.throttle.ThrottleFrame; 028import jmri.jmrix.*; 029import jmri.profile.*; 030import jmri.script.JmriScriptEngineManager; 031import jmri.util.*; 032import jmri.util.iharder.dnd.URIDrop; 033import jmri.util.prefs.JmriPreferencesActionFactory; 034import jmri.util.swing.*; 035 036/** 037 * Base class for JMRI applications. 038 * 039 * @author Bob Jacobsen Copyright 2003, 2007, 2008, 2010 040 * @author Dennis Miller Copyright 2005 041 * @author Giorgio Terdina Copyright 2008 042 * @author Matthew Harris Copyright (C) 2011 043 */ 044public class Apps extends JPanel implements PropertyChangeListener, WindowListener { 045 046 static String profileFilename; 047 private Action prefsAction; // defer initialization until needed so that Bundle accesses translate 048 049 @SuppressFBWarnings(value = {"ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", "SC_START_IN_CTOR"}, 050 justification = "only one application at a time. The thread is only called to help improve user experiance when opening the preferences, it is not critical for it to be run at this stage") 051 public Apps() { 052 053 super(true); 054 long start = System.nanoTime(); 055 log.trace("starting ctor at {}", start); 056 057 splash(false); 058 splash(true, true); 059 log.trace("splash screens up, about to setButtonSpace"); 060 setButtonSpace(); 061 log.trace("about to setJynstrumentSpace"); 062 setJynstrumentSpace(); 063 064 log.trace("setLogo"); 065 jmri.Application.setLogo(logo()); 066 log.trace("setURL"); 067 jmri.Application.setURL(line2()); 068 069 // Get configuration profile 070 log.trace("start to get configuration profile - locate files"); 071 // Needs to be done before loading a ConfigManager or UserPreferencesManager 072 FileUtil.createDirectory(FileUtil.getPreferencesPath()); 073 // Load permission manager 074 InstanceManager.getDefault(PermissionManager.class); 075 // Needs to be declared final as we might need to 076 // refer to this on the Swing thread 077 final File profileFile; 078 profileFilename = configFilename.replaceFirst(".xml", ".properties"); 079 // decide whether name is absolute or relative 080 if (!new File(profileFilename).isAbsolute()) { 081 // must be relative, but we want it to 082 // be relative to the preferences directory 083 profileFile = new File(FileUtil.getPreferencesPath() + profileFilename); 084 } else { 085 profileFile = new File(profileFilename); 086 } 087 ProfileManager.getDefault().setConfigFile(profileFile); 088 // See if the profile to use has been specified on the command line as 089 // a system property org.jmri.profile as a profile id. 090 if (System.getProperties().containsKey(ProfileManager.SYSTEM_PROPERTY)) { 091 ProfileManager.getDefault().setActiveProfile(System.getProperty(ProfileManager.SYSTEM_PROPERTY)); 092 } 093 log.trace("check if profile exists"); 094 // @see jmri.profile.ProfileManager#migrateToProfiles Javadoc for conditions handled here 095 if (!profileFile.exists()) { // no profile config for this app 096 log.trace("profileFile {} doesn't exist", profileFile); 097 try { 098 if (ProfileManager.getDefault().migrateToProfiles(configFilename)) { // migration or first use 099 // notify user of change only if migration occurred 100 // TODO: a real migration message 101 JmriJOptionPane.showMessageDialog(sp, 102 Bundle.getMessage("ConfigMigratedToProfile"), 103 jmri.Application.getApplicationName(), 104 JmriJOptionPane.INFORMATION_MESSAGE); 105 } 106 } catch (IOException | IllegalArgumentException ex) { 107 JmriJOptionPane.showMessageDialog(sp, 108 ex.getLocalizedMessage(), 109 jmri.Application.getApplicationName(), 110 JmriJOptionPane.ERROR_MESSAGE); 111 log.error("Exception migrating configuration to profiles: {}",ex.getMessage()); 112 } 113 } 114 log.trace("about to try getStartingProfile"); 115 try { 116 ProfileManagerDialog.getStartingProfile(sp); 117 // Manually setting the configFilename property since calling 118 // Apps.setConfigFilename() does not reset the system property 119 configFilename = FileUtil.getProfilePath() + Profile.CONFIG_FILENAME; 120 System.setProperty("org.jmri.Apps.configFilename", Profile.CONFIG_FILENAME); 121 Profile profile = ProfileManager.getDefault().getActiveProfile(); 122 if (profile != null) { 123 log.info("Starting with profile {}", profile.getId()); 124 } else { 125 log.info("Starting without a profile"); 126 } 127 128 // rapid language set; must follow up later with full setting as part of preferences 129 jmri.util.gui.GuiLafPreferencesManager.setLocaleMinimally(profile); 130 } catch (IOException ex) { 131 log.info("Profiles not configurable. Using fallback per-application configuration. Error: {}", ex.getMessage()); 132 } 133 134 // install a Preferences Action Factory. 135 InstanceManager.store(new AppsPreferencesActionFactory(), JmriPreferencesActionFactory.class); 136 137 // Install configuration manager and Swing error handler 138 // Constructing the AppsConfigurationManager also loads various configuration services 139 ConfigureManager cm = InstanceManager.setDefault(ConfigureManager.class, new AppsConfigurationManager()); 140 141 // record startup 142 String appString = String.format("%s (v%s)", jmri.Application.getApplicationName(), Version.getCanonicalVersion()); 143 InstanceManager.getDefault(FileHistory.class).addOperation("app", appString, null); 144 145 // Install abstractActionModel 146 InstanceManager.store(new apps.CreateButtonModel(), apps.CreateButtonModel.class); 147 148 // find preference file and set location in configuration manager 149 // Needs to be declared final as we might need to 150 // refer to this on the Swing thread 151 final File file; 152 File singleConfig; 153 File sharedConfig = null; 154 // decide whether name is absolute or relative 155 if (!new File(configFilename).isAbsolute()) { 156 // must be relative, but we want it to 157 // be relative to the preferences directory 158 singleConfig = new File(FileUtil.getUserFilesPath() + configFilename); 159 } else { 160 singleConfig = new File(configFilename); 161 } 162 try { 163 // get preferences file 164 sharedConfig = FileUtil.getFile(FileUtil.PROFILE + Profile.SHARED_CONFIG); 165 if (!sharedConfig.canRead()) { 166 sharedConfig = null; 167 } 168 } catch (FileNotFoundException ex) { 169 // ignore - sharedConfig will remain null in this case 170 } 171 // load config file if it exists 172 if (sharedConfig != null) { 173 file = sharedConfig; 174 } else { 175 file = singleConfig; 176 } 177 178 // ensure the UserPreferencesManager has loaded. Done on GUI 179 // thread as it can modify GUI objects 180 log.debug("*** About to getDefault(jmri.UserPreferencesManager.class) with file {}", file); 181 ThreadingUtil.runOnGUI(() -> { 182 InstanceManager.getDefault(jmri.UserPreferencesManager.class); 183 }); 184 log.debug("*** Done"); 185 186 // now (attempt to) load the config file 187 log.debug("Using config file(s) {}", file.getPath()); 188 if (file.exists()) { 189 log.debug("start load config file {}", file.getPath()); 190 try { 191 configOK = cm.load(file, true); 192 } catch (JmriException e) { 193 log.error("Unhandled problem loading configuration", e); 194 configOK = false; 195 } 196 log.debug("end load config file, OK={}", configOK); 197 } else { 198 log.info("No saved preferences, will open preferences window. Searched for {}", file.getPath()); 199 configOK = false; 200 } 201 202 // populate GUI 203 log.debug("Start UI"); 204 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 205 206 // done 207 long end = System.nanoTime(); 208 209 long elapsedTime = (end - start) / 1000000; 210 /* 211 This ensures that the message is displayed on the screen for a minimum of 2.5seconds, if the time taken 212 to get to this point in the code is longer that 2.5seconds then the wait is not invoked. 213 */ 214 long sleep = 2500 - elapsedTime; 215 if (sleep > 0) { 216 log.debug("Debug message was displayed for less than 2500ms ({}ms). Sleeping for {}ms to allow user sufficient time to do something.", 217 elapsedTime, sleep); 218 try { 219 Thread.sleep(sleep); 220 } catch (InterruptedException e) { 221 log.error("uexpected ", e); 222 } 223 } 224 225 FileUtil.logFilePaths(); 226 227 splash(false); 228 splash(true, false); 229 Toolkit.getDefaultToolkit().removeAWTEventListener(debugListener); 230 while (debugmsg) { 231 /*The user has pressed the interupt key that allows them to disable logixs 232 at start up we do not want to process any more information until the user 233 has answered the question */ 234 try { 235 Thread.sleep(1000); 236 } catch (InterruptedException e) { 237 log.error("Unexpected:",e); 238 } 239 } 240 // Now load deferred config items 241 if (file.exists()) { 242 if (file.equals(singleConfig)) { 243 // To avoid possible locks, deferred load should be 244 // performed on the Swing thread 245 if (SwingUtilities.isEventDispatchThread()) { 246 configDeferredLoadOK = doDeferredLoad(file); 247 } else { 248 try { 249 // Use invokeAndWait method as we don't want to 250 // return until deferred load is completed 251 SwingUtilities.invokeAndWait(new Runnable() { 252 @Override 253 @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification = "configDeferredLoadOK write is semi-global") 254 public void run() { 255 configDeferredLoadOK = doDeferredLoad(file); 256 } 257 }); 258 } catch (InterruptedException | InvocationTargetException ex) { 259 log.error("Exception creating system console frame", ex); 260 } 261 } 262 } else { 263 // deferred loading is not done in the new config 264 configDeferredLoadOK = true; 265 } 266 } else { 267 configDeferredLoadOK = false; 268 } 269 // If preferences need to be migrated, do it now 270 if (sharedConfig == null && configOK == true && configDeferredLoadOK == true) { 271 log.info("Migrating preferences to new format..."); 272 // migrate preferences 273 InstanceManager.getOptionalDefault(TabbedPreferences.class).ifPresent(tp -> { 274 // tp.init(); 275 tp.saveContents(); 276 cm.storePrefs(); 277 }); 278 // notify user of change 279 log.info("Preferences have been migrated to new format."); 280 log.info("New preferences format will be used after JMRI is restarted."); 281 if (!GraphicsEnvironment.isHeadless()) { 282 Profile profile = ProfileManager.getDefault().getActiveProfile(); 283 JmriJOptionPane.showMessageDialog(sp, 284 Bundle.getMessage("SingleConfigMigratedToSharedConfig", profile), 285 jmri.Application.getApplicationName(), 286 JmriJOptionPane.INFORMATION_MESSAGE); 287 } 288 } 289 290 // Before starting to load preferences, make sure some managers are created. 291 // This is needed because these aren't particularly well-behaved during 292 // creation. 293 InstanceManager.getDefault(jmri.LogixManager.class); 294 InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class); 295 296 // preload script engines if requested 297 if (Boolean.getBoolean("org.jmri.python.preload")) { 298 new Thread(() -> { 299 try { 300 JmriScriptEngineManager.getDefault().initializeAllEngines(); 301 } catch (RuntimeException ex) { 302 log.error("Error in trying to initialize script interpreters {}", ex.getMessage()); 303 } 304 }, "initialize python interpreter").start(); 305 } 306 307 // kick off update of decoder index if needed 308 jmri.util.ThreadingUtil.runOnGUI(() -> { 309 try { 310 jmri.jmrit.decoderdefn.DecoderIndexFile.updateIndexIfNeeded(); 311 } catch (org.jdom2.JDOMException| java.io.IOException e) { 312 log.error("Exception trying to pre-load decoderIndex", e); 313 } 314 }); 315 316 // if the configuration didn't complete OK, pop the prefs frame and help 317 log.debug("Config OK? {}, deferred config OK? {}", configOK, configDeferredLoadOK); 318 if (!configOK || !configDeferredLoadOK) { 319 HelpUtil.displayHelpRef("package.apps.AppConfigPanelErrorPage"); 320 doPreferences(); 321 } 322 log.debug("Done with doPreferences, start statusPanel"); 323 324 add(statusPanel()); 325 log.debug("Done with statusPanel, start buttonSpace"); 326 add(buttonSpace()); 327 add(_jynstrumentSpace); 328 long eventMask = AWTEvent.MOUSE_EVENT_MASK; 329 330 Toolkit.getDefaultToolkit().addAWTEventListener((AWTEvent e) -> { 331 if (e instanceof MouseEvent) { 332 JmriMouseEvent me = new JmriMouseEvent((MouseEvent) e); 333 if (me.isPopupTrigger() && me.getComponent() instanceof JTextComponent) { 334 var tc = (JTextComponent)me.getComponent(); 335 // provide a pop up if one not already defined 336 if (tc.getComponentPopupMenu() == null) { 337 final JTextComponent component1 = (JTextComponent) me.getComponent(); 338 final JPopupMenu menu = new JPopupMenu(); 339 JMenuItem item; 340 item = new JMenuItem(new DefaultEditorKit.CopyAction()); 341 item.setText("Copy"); 342 item.setEnabled(component1.getSelectionStart() != component1.getSelectionEnd()); 343 menu.add(item); 344 item = new JMenuItem(new DefaultEditorKit.CutAction()); 345 item.setText("Cut"); 346 item.setEnabled(component1.isEditable() && component1.getSelectionStart() != component1.getSelectionEnd()); 347 menu.add(item); 348 item = new JMenuItem(new DefaultEditorKit.PasteAction()); 349 item.setText("Paste"); 350 item.setEnabled(component1.isEditable()); 351 menu.add(item); 352 menu.show(me.getComponent(), me.getX(), me.getY()); 353 } 354 } 355 } 356 }, eventMask); 357 358 // do final activation 359 InstanceManager.getDefault(jmri.LogixManager.class).activateAllLogixs(); 360 InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class).initializeLayoutBlockPaths(); 361 362 LogixNG_Manager logixNG_Manager = InstanceManager.getDefault(LogixNG_Manager.class); 363 logixNG_Manager.setupAllLogixNGs(); 364 if (InstanceManager.getDefault(LogixNGPreferences.class).getStartLogixNGOnStartup() 365 && InstanceManager.getDefault(jmri.jmrit.logixng.LogixNG_Manager.class).isStartLogixNGsOnLoad()) { 366 logixNG_Manager.activateAllLogixNGs(); 367 } 368 369 log.debug("End constructor"); 370 } 371 372 private boolean doDeferredLoad(File file) { 373 boolean result; 374 log.debug("start deferred load from config"); 375 try { 376 ConfigureManager cmOD = InstanceManager.getNullableDefault(jmri.ConfigureManager.class); 377 if (cmOD != null) { 378 result = cmOD.loadDeferred(file); 379 } else { 380 log.error("Failed to get default configure manager"); 381 result = false; 382 } 383 } catch (JmriException e) { 384 log.error("Unhandled problem loading deferred configuration", e); 385 result = false; 386 } 387 log.debug("end deferred load from config file, OK={}", result); 388 return result; 389 } 390 391 /** 392 * Prepare the JPanel to contain buttons in the startup GUI. Since it's 393 * possible to add buttons via the preferences, this space may have 394 * additional buttons appended to it later. The default implementation here 395 * just creates an empty space for these to be added to. 396 */ 397 @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", 398 justification = "only one application at a time") 399 protected void setButtonSpace() { 400 _buttonSpace = new JPanel(); 401 _buttonSpace.setLayout(new FlowLayout()); 402 } 403 static JComponent _jynstrumentSpace = null; 404 405 @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", 406 justification = "only one application at a time") 407 protected void setJynstrumentSpace() { 408 _jynstrumentSpace = new JPanel(); 409 _jynstrumentSpace.setLayout(new FlowLayout()); 410 new URIDrop(_jynstrumentSpace, (URI[] uris) -> { 411 for (URI uri : uris ) { 412 ynstrument(new File(uri).getPath()); 413 } 414 }); 415 } 416 417 public static void ynstrument(String path) { 418 Jynstrument it = JynstrumentFactory.createInstrument(path, _jynstrumentSpace); 419 if (it == null) { 420 log.error("Error while creating Jynstrument {}", path); 421 return; 422 } 423 ThrottleFrame.setTransparent(it); 424 it.setVisible(true); 425 _jynstrumentSpace.setVisible(true); 426 _jynstrumentSpace.add(it); 427 } 428 429 /** 430 * Create default menubar. 431 * <p> 432 * This does not include the development menu. 433 * 434 * @param menuBar Menu bar to be populated 435 * @param wi WindowInterface where this menu bar will appear 436 */ 437 protected void createMenus(JMenuBar menuBar, WindowInterface wi) { 438 if (SystemType.isMacOSX()) { 439 Application.getApplication().setQuitHandler((EventObject eo) -> handleQuit()); 440 } 441 442 AppsMainMenu.createMenus(menuBar, wi, this, mainWindowHelpID()); 443 } 444 445 /** 446 * Open Preferences action. Often done due to error 447 */ 448 public void doPreferences() { 449 if (prefsAction == null) { 450 prefsAction = new TabbedPreferencesAction(); 451 } 452 prefsAction.actionPerformed(null); 453 } 454 455 /** 456 * Set the location of the window-specific help for the preferences pane. 457 * Made a separate method so if can be overridden for application specific 458 * preferences help 459 * 460 * @param frame The frame being described in the help system 461 * @param location The location within the JavaHelp system 462 */ 463 protected void setPrefsFrameHelp(JmriJFrame frame, String location) { 464 frame.addHelpMenu(location, true); 465 } 466 467 /** 468 * Returns the ID for the main window's help, which is application specific 469 * 470 * @return help identifier for main window 471 */ 472 protected String mainWindowHelpID() { 473 return "package.apps.Apps"; 474 } 475 476 protected String line1() { 477 return Bundle.getMessage("DefaultVersionCredit", jmri.Version.name()); 478 } 479 480 protected String line2() { 481 return "http://jmri.org/"; 482 } 483 484 protected String line3() { 485 return " "; 486 } 487 // line 4 488 JLabel cs4 = new JLabel(); 489 490 protected void buildLine4(JPanel pane) { 491 if (connection[0] != null) { 492 buildLine(connection[0], cs4, pane); 493 } 494 } 495 // line 5 optional 496 JLabel cs5 = new JLabel(); 497 498 protected void buildLine5(JPanel pane) { 499 if (connection[1] != null) { 500 buildLine(connection[1], cs5, pane); 501 } 502 } 503 // line 6 optional 504 JLabel cs6 = new JLabel(); 505 506 protected void buildLine6(JPanel pane) { 507 if (connection[2] != null) { 508 buildLine(connection[2], cs6, pane); 509 } 510 } 511 // line 7 optional 512 JLabel cs7 = new JLabel(); 513 514 protected void buildLine7(JPanel pane) { 515 if (connection[3] != null) { 516 buildLine(connection[3], cs7, pane); 517 } 518 } 519 520 protected void buildLine(ConnectionConfig conn, JLabel cs, JPanel pane) { 521 if (conn.name().equals(JmrixConfigPane.NONE)) { 522 cs.setText(" "); 523 return; 524 } 525 526 log.debug("conn.name() is {} ", conn.name()); // eg CAN via MERG Network Interface 527 log.debug("conn.getConnectionName() is {} ", conn.getConnectionName()); // eg MERG2 528 log.debug("conn.getManufacturer() is {} ", conn.getManufacturer()); // eg MERG 529 530 ConnectionStatus.instance().addConnection(conn.getConnectionName(), conn.getInfo()); 531 cs.setFont(pane.getFont()); 532 updateLine(conn, cs); 533 pane.add(cs); 534 } 535 536 protected void updateLine(ConnectionConfig conn, JLabel cs) { 537 if (conn.getDisabled()) { 538 return; 539 } 540 String name = conn.getConnectionName(); 541 if (name == null) { 542 name = conn.getManufacturer(); 543 } 544 if (ConnectionStatus.instance().isConnectionOk(name, conn.getInfo())) { 545 cs.setForeground(Color.black); 546 String cf = Bundle.getMessage("ConnectionSucceeded", name, conn.name(), conn.getInfo()); 547 cs.setText(cf); 548 } else { 549 cs.setForeground(Color.red); 550 String cf = Bundle.getMessage("ConnectionFailed", name, conn.name(), conn.getInfo()); 551 cs.setText(cf); 552 } 553 554 this.revalidate(); 555 } 556 557 protected String line8() { 558 return " "; 559 } 560 561 protected String line9() { 562 return Bundle.getMessage("JavaVersionCredit", 563 System.getProperty("java.version", "<unknown>"), 564 Locale.getDefault()); 565 } 566 567 protected String logo() { 568 return "resources/logo.gif"; 569 } 570 571 /** 572 * Fill in the logo and status panel 573 * 574 * @return Properly-filled out JPanel 575 */ 576 protected JPanel statusPanel() { 577 JPanel pane1 = new JPanel(); 578 pane1.setLayout(new BoxLayout(pane1, BoxLayout.X_AXIS)); 579 log.debug("Fetch main logo: {}", logo()); 580 pane1.add(new JLabel(new ImageIcon(getToolkit().getImage(FileUtil.findURL(logo(), FileUtil.Location.ALL)), "JMRI logo"), JLabel.LEFT)); 581 pane1.add(Box.createRigidArea(new Dimension(15, 0))); // Some spacing between logo and status panel 582 583 log.debug("start labels"); 584 JPanel pane2 = new JPanel(); 585 586 pane2.setLayout(new BoxLayout(pane2, BoxLayout.Y_AXIS)); 587 pane2.add(new JLabel(line1())); 588 pane2.add(new JLabel(line2())); 589 pane2.add(new JLabel(line3())); 590 591 String name = ProfileManager.getDefault().getActiveProfileName(); 592 pane2.add(new JLabel(Bundle.getMessage("ActiveProfile", name))); 593 594 // add listener for Com port updates 595 ConnectionStatus.instance().addPropertyChangeListener(this); 596 int i = 0; 597 for (ConnectionConfig conn : InstanceManager.getDefault(ConnectionConfigManager.class)) { 598 if (!conn.getDisabled()) { 599 connection[i] = conn; 600 i++; 601 } 602 if (i > 3) { 603 break; 604 } 605 } 606 buildLine4(pane2); 607 buildLine5(pane2); 608 buildLine6(pane2); 609 buildLine7(pane2); 610 611 pane2.add(new JLabel(line8())); 612 pane2.add(new JLabel(line9())); 613 pane1.add(pane2); 614 return pane1; 615 } 616 //int[] connection = {-1,-1,-1,-1}; 617 ConnectionConfig[] connection = {null, null, null, null}; 618 619 /** 620 * Closing the main window is a shutdown request. 621 * 622 * @param e the event triggering the close 623 */ 624 @Override 625 public void windowClosing(WindowEvent e) { 626 if (!InstanceManager.getDefault(ShutDownManager.class).isShuttingDown() 627 && JmriJOptionPane.YES_OPTION == JmriJOptionPane.showConfirmDialog( 628 null, 629 Bundle.getMessage("MessageLongCloseWarning"), 630 Bundle.getMessage("MessageShortCloseWarning"), 631 JmriJOptionPane.YES_NO_OPTION)) { 632 handleQuit(); 633 } 634 // if get here, didn't quit, so don't close window 635 } 636 637 @Override 638 public void windowActivated(WindowEvent e) { 639 } 640 641 @Override 642 public void windowClosed(WindowEvent e) { 643 } 644 645 @Override 646 public void windowDeactivated(WindowEvent e) { 647 } 648 649 @Override 650 public void windowDeiconified(WindowEvent e) { 651 } 652 653 @Override 654 public void windowIconified(WindowEvent e) { 655 } 656 657 @Override 658 public void windowOpened(WindowEvent e) { 659 } 660 661 static protected void setJmriSystemProperty(String key, String value) { 662 try { 663 String current = System.getProperty("org.jmri.Apps." + key); 664 if (current == null) { 665 System.setProperty("org.jmri.Apps." + key, value); 666 } else if (!current.equals(value)) { 667 log.warn("JMRI property {} already set to {}, skipping reset to {}", key, current, value); 668 } 669 } catch (RuntimeException e) { 670 log.error("Unable to set JMRI property {} to {} due to exception", key, value, e); 671 } 672 } 673 674 /** 675 * Provide access to a place where applications can expect the configuration 676 * code to build run-time buttons. 677 * 678 * @see apps.startup.CreateButtonModelFactory 679 * @return null if no such space exists 680 */ 681 static public JComponent buttonSpace() { 682 return _buttonSpace; 683 } 684 static JComponent _buttonSpace = null; 685 static SplashWindow sp = null; 686 static AWTEventListener debugListener = null; 687 688 // TODO: Remove the "static" nature of much of the initialization someday. 689 // It exits to allow splash() to be called first-thing in main(), see 690 // apps.DecoderPro.DecoderPro.main(...) 691 // Or maybe, just not worry about this here, in the older base class, 692 // and address it in the newer apps.gui3.Apps3 as that's the base class of the future. 693 static boolean debugFired = false; // true if we've seen F8 during startup 694 static boolean debugmsg = false; // true while we're handling the "No Logix?" prompt window on startup 695 696 static protected void splash(boolean show) { 697 splash(show, false); 698 } 699 700 static protected void splash(boolean show, boolean debug) { 701 Log4JUtil.initLogging(); 702 if (debugListener == null && debug) { 703 // set a global listener for debug options 704 debugFired = false; 705 Toolkit.getDefaultToolkit().addAWTEventListener( 706 debugListener = (AWTEvent e) -> { 707 if (!debugFired) { 708 /*We set the debugmsg flag on the first instance of the user pressing any button 709 and the if the debugFired hasn't been set, this allows us to ensure that we don't 710 miss the user pressing F8, while we are checking*/ 711 debugmsg = true; 712 if (e.getID() == KeyEvent.KEY_PRESSED && e instanceof KeyEvent && ((KeyEvent) e).getKeyCode() == 119) { // F8 713 startupDebug(); 714 } else if (e.getID() == KeyEvent.KEY_PRESSED && e instanceof KeyEvent && ((KeyEvent) e).getKeyCode() == 120) { // F9 715 InstanceManager.getDefault(LogixNG_Manager.class).startLogixNGsOnLoad(false); 716 } else { 717 debugmsg = false; 718 } 719 } 720 }, 721 AWTEvent.KEY_EVENT_MASK); 722 } 723 724 // bring up splash window for startup 725 if (sp == null) { 726 if (debug) { 727 sp = new SplashWindow(splashDebugMsg()); 728 } else { 729 sp = new SplashWindow(); 730 } 731 } 732 sp.setVisible(show); 733 if (!show) { 734 sp.dispose(); 735 Toolkit.getDefaultToolkit().removeAWTEventListener(debugListener); 736 debugListener = null; 737 sp = null; 738 } 739 } 740 741 static protected JPanel splashDebugMsg() { 742 JLabel panelLabelDisableLogix = new JLabel(Bundle.getMessage("PressF8ToDebug")); 743 panelLabelDisableLogix.setFont(panelLabelDisableLogix.getFont().deriveFont(9f)); 744 JLabel panelLabelDisableLogixNG = new JLabel(Bundle.getMessage("PressF9ToInactivateLogixNG")); 745 panelLabelDisableLogixNG.setFont(panelLabelDisableLogix.getFont().deriveFont(9f)); 746 JPanel panel = new JPanel(); 747 panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); 748 panel.add(panelLabelDisableLogix); 749 panel.add(panelLabelDisableLogixNG); 750 return panel; 751 } 752 753 static protected void startupDebug() { 754 debugFired = true; 755 debugmsg = true; 756 757 Object[] options = {"Disable", "Enable"}; 758 759 int retval = JmriJOptionPane.showOptionDialog(null, 760 Bundle.getMessage("StartJMRIwithLogixEnabledDisabled"), 761 Bundle.getMessage("StartJMRIwithLogixEnabledDisabledTitle"), 762 JmriJOptionPane.DEFAULT_OPTION, 763 JmriJOptionPane.QUESTION_MESSAGE, null, options, options[0]); 764 765 if (retval != 0) { 766 debugmsg = false; 767 return; 768 } 769 InstanceManager.getDefault(jmri.LogixManager.class).setLoadDisabled(true); 770 InstanceManager.getDefault(LogixNG_Manager.class).setLoadDisabled(true); 771 log.info("Requested loading with Logixs and LogixNGs disabled."); 772 debugmsg = false; 773 } 774 775 /** 776 * The application decided to quit, handle that. 777 * 778 * @return always returns false 779 */ 780 static public boolean handleQuit() { 781 AppsBase.handleQuit(); 782 return false; 783 } 784 785 /** 786 * The application decided to restart, handle that. 787 */ 788 static public void handleRestart() { 789 AppsBase.handleRestart(); 790 } 791 792 /** 793 * Set up the configuration file name at startup. 794 * <p> 795 * The Configuration File name variable holds the name used to load the 796 * configuration file during later startup processing. Applications invoke 797 * this method to handle the usual startup hierarchy: 798 * <ul> 799 * <li>If an absolute filename was provided on the command line, use it 800 * <li>If a filename was provided that's not absolute, consider it to be in 801 * the preferences directory 802 * <li>If no filename provided, use a default name (that's application 803 * specific) 804 * </ul> 805 * This name will be used for reading and writing the preferences. It need 806 * not exist when the program first starts up. This name may be proceeded 807 * with <em>config=</em> and may not contain the equals sign (=). 808 * 809 * @param def Default value if no other is provided 810 * @param args Argument array from the main routine 811 */ 812 static protected void setConfigFilename(String def, String[] args) { 813 // skip if org.jmri.Apps.configFilename is set 814 if (System.getProperty("org.jmri.Apps.configFilename") != null) { 815 return; 816 } 817 // save the configuration filename if present on the command line 818 if (args.length >= 1 && args[0] != null && !args[0].contains("=")) { 819 def = args[0]; 820 log.debug("Config file was specified as: {}", args[0]); 821 } 822 for (String arg : args) { 823 String[] split = arg.split("=", 2); 824 if (split[0].equalsIgnoreCase("config")) { 825 def = split[1]; 826 log.debug("Config file was specified as: {}", arg); 827 } 828 } 829 Apps.configFilename = def; 830 setJmriSystemProperty("configFilename", def); 831 } 832 833 static public String getConfigFileName() { 834 return configFilename; 835 } 836 837 static protected void createFrame(Apps containedPane, JmriJFrame frame) { 838 // create the main frame and menus 839 // Create a WindowInterface object based on the passed-in Frame 840 JFrameInterface wi = new JFrameInterface(frame); 841 // Create a menu bar 842 containedPane.menuBar = new JMenuBar(); 843 844 // Create menu categories and add to the menu bar, add actions to menus 845 containedPane.createMenus(containedPane.menuBar, wi); 846 // connect Help target now that globalHelpBroker has been instantiated 847 containedPane.attachHelp(); 848 849 frame.setJMenuBar(containedPane.menuBar); 850 frame.getContentPane().add(containedPane); 851 852 // handle window close 853 frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 854 frame.addWindowListener(containedPane); 855 856 // pack and center this frame 857 frame.pack(); 858 Dimension screen = frame.getToolkit().getScreenSize(); 859 Dimension size = frame.getSize(); 860 861 // first set a default position and size 862 frame.setLocation((screen.width - size.width) / 2, (screen.height - size.height) / 2); 863 864 // then attempt set from stored preference 865 frame.setFrameLocation(); 866 867 // and finally show 868 frame.setVisible(true); 869 } 870 871 static protected void loadFile(String name) { 872 ConfigureManager cmOD = InstanceManager.getNullableDefault(jmri.ConfigureManager.class); 873 if (cmOD != null) { 874 URL pFile = cmOD.find(name); 875 if (pFile != null) { 876 try { 877 cmOD.load(pFile); 878 } catch (JmriException e) { 879 log.error("Unhandled problem in loadFile", e); 880 } 881 } else { 882 log.warn("Could not find {} config file", name); 883 } 884 } else { 885 log.error("Failed to get default configure manager"); 886 } 887 } 888 889 static String configFilename = System.getProperty("org.jmri.Apps.configFilename", "jmriconfig2.xml"); // usually overridden, this is default 890 // The following MUST be protected for 3rd party applications 891 // (such as CATS) which are derived from this class. 892 @SuppressFBWarnings(value = "MS_PKGPROTECT", 893 justification = "The following MUST be protected for 3rd party applications (such as CATS) which are derived from this class.") 894 protected static boolean configOK; 895 @SuppressFBWarnings(value = "MS_PKGPROTECT", 896 justification = "The following MUST be protected for 3rd party applications (such as CATS) which are derived from this class.") 897 protected static boolean configDeferredLoadOK; 898 // GUI members 899 private JMenuBar menuBar; 900 901 static String nameString = "JMRI program"; 902 903 protected static void setApplication(String name) { 904 try { 905 jmri.Application.setApplicationName(name); 906 } catch (IllegalArgumentException | IllegalAccessException ex) { 907 log.warn("Unable to set application name", ex); 908 } 909 } 910 911 /** 912 * Set and log some startup information. This is intended to be the central 913 * connection point for common startup and logging. 914 * 915 * @param name Program/application name as known by the user 916 */ 917 @SuppressFBWarnings(value = "SLF4J_SIGN_ONLY_FORMAT",justification = "info message contains context information") 918 protected static void setStartupInfo(String name) { 919 // Set the application name 920 try { 921 jmri.Application.setApplicationName(name); 922 } catch (IllegalArgumentException | IllegalAccessException ex) { 923 log.warn("Unable to set application name", ex); 924 } 925 926 // Log the startup information 927 log.info("{}",Log4JUtil.startupInfo(name)); 928 } 929 930 @Override 931 public void propertyChange(PropertyChangeEvent ev) { 932 log.debug("property change: comm port status update"); 933 if (connection[0] != null) { 934 updateLine(connection[0], cs4); 935 } 936 937 if (connection[1] != null) { 938 updateLine(connection[1], cs5); 939 } 940 941 if (connection[2] != null) { 942 updateLine(connection[2], cs6); 943 } 944 945 if (connection[3] != null) { 946 updateLine(connection[3], cs7); 947 } 948 949 } 950 951 /** 952 * Attach Help target to Help button on Main Screen. 953 */ 954 protected void attachHelp() { 955 } 956 957 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Apps.class); 958 959}