Exemplo n.º 1
0
  @Override
  /** Starts the application. */
  public void start(final Stage primaryStage) {
    final EventManager eventManager = new EventManager();
    final IdGenerator connectionIdGenerator = new IdGenerator();

    try {
      final ConfigurationManager configurationManager =
          new ConfigurationManager(eventManager, connectionIdGenerator);

      // Load the main window
      FxmlUtils.setParentClass(getClass());
      final FXMLLoader loader = FxmlUtils.createFxmlLoaderForProjectFile("MainWindow.fxml");

      // Get the associated pane
      AnchorPane pane = (AnchorPane) loader.load();

      final Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

      // Set scene width, height and style
      final double height =
          Math.min(
              UiProperties.getApplicationHeight(configurationManager),
              primaryScreenBounds.getHeight());
      final double width =
          Math.min(
              UiProperties.getApplicationWidth(configurationManager),
              primaryScreenBounds.getWidth());

      final Scene scene = new Scene(pane, width, height);
      scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

      // Get the associated controller
      final MainController mainController = (MainController) loader.getController();
      mainController.setEventManager(eventManager);
      mainController.setConfigurationManager(configurationManager);
      mainController.setSelectedPerspective(
          UiProperties.getApplicationPerspective(configurationManager));
      mainController
          .getResizeMessagePaneMenu()
          .setSelected(UiProperties.getResizeMessagePane(configurationManager));

      // Set the stage's properties
      primaryStage.setScene(scene);
      primaryStage.setMaximized(UiProperties.getApplicationMaximized(configurationManager));

      // Initialise resources in the main controller
      mainController.setApplication(this);
      mainController.setStage(primaryStage);
      mainController.setLastHeight(height);
      mainController.setLastWidth(width);
      mainController.init();
      final Image applicationIcon =
          new Image(getClass().getResourceAsStream("/images/mqtt-spy-logo.png"));
      primaryStage.getIcons().add(applicationIcon);

      // Show the main window
      primaryStage.show();

      // Load the config file if specified
      final String noConfig = this.getParameters().getNamed().get(NO_CONFIGURATION_PARAMETER_NAME);
      final String configurationFileLocation =
          this.getParameters().getNamed().get(CONFIGURATION_PARAMETER_NAME);

      if (noConfig != null) {
        // Do nothing - no config wanted
      } else if (configurationFileLocation != null) {
        mainController.loadConfigurationFileAndShowErrorWhenApplicable(
            new File(configurationFileLocation));
      } else {
        // If no configuration parameter is specified, use the user's home directory and the default
        // configuration file name
        mainController.loadDefaultConfigurationFile();
      }
    } catch (Exception e) {
      LoggerFactory.getLogger(Main.class).error("Error while loading the main window", e);
    }
  }