1 Reply Latest reply: Jun 26, 2013 8:46 AM by shakir.gusaroff RSS

    How to minimize the usage of static variables and objects

    d5e39d0d-aae7-42fe-87c6-d1de6084adfe

      I'm trying to implement this JavaFX code where I want to call remote Java class and pass boolean flag:

       

      Small part of the code which I want to optimize:

      final CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
        toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>()
         {
         @Override
         public void handle(ActionEvent e)
         {
      
         //DataTabs.renderTab = toolbarSubMenuNavigation.isSelected();
         DataTabs.setRenderTab(toolbarSubMenuNavigation.isSelected());
         // call here the getter setter and send boolean flag
         System.out.println("subsystem1 #1 Enabled!");
         }
         });
      

       

      Second part that I want to call:

      public class DataTabs
      {   
         private static boolean renderTab; // make members *private*
         private static TabPane tabPane;
      
         public static boolean isRenderTab()
         {
         return DataTabs.renderTab;
         }
      
         public static void setRenderTab(boolean renderTab)
         {
         DataTabs.renderTab = renderTab;
        tabPane.setVisible(renderTab);
         }
      
         // somewhere below
         // set visible the tab pane
         TabPane tabPane = DataTabs.tabPane = new TabPane();
        tabPane.setVisible(renderTab);
      }
      
      
      
      
      

      The complete code:

       

      import com.dx57dc.gui.panels.SettingsPanel;
      import com.dx57dc.gui.tabs.DataTabs;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.geometry.Insets;
      import javafx.geometry.Pos;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.CheckMenuItem;
      import javafx.scene.control.MenuBar;
      import javafx.scene.control.MenuItem;
      import javafx.scene.control.SeparatorMenuItem;
      import javafx.scene.control.TextArea;
      import javafx.scene.image.ImageView;
      import javafx.scene.layout.GridPane;
      import javafx.scene.layout.HBox;
      import javafx.scene.paint.Color;
      import javafx.scene.text.Font;
      import javafx.scene.text.Text;
      import javafx.stage.Modality;
      import javafx.stage.Stage;
      
      /*
       * User navigation menu which will be used from the user for navigation
       */
      public class Menu
      {
          // Generate Menu
      
          public MenuBar initMenu(final Stage primaryStage)
          {
      
              MenuBar menuBar = new MenuBar();
      
              // File menu - new, save, close, exit
              javafx.scene.control.Menu file = new javafx.scene.control.Menu("File");
              MenuItem subButtonNew = new MenuItem("New");
      
              // New connection to DX-Kernel
              subButtonNew.setOnAction(new EventHandler<ActionEvent>()
              {
                  @Override
                  public void handle(ActionEvent e)
                  {
                      // This Java Method is called from the Java Class MenuActions
                  }
              });
      
              file.getItems().add(subButtonNew);
      
              file.getItems().add(new MenuItem("Save"));
              file.getItems().add(new MenuItem("Close"));
              file.getItems().add(new SeparatorMenuItem());
      
              MenuItem menuItem = new MenuItem("Exit");
      
              // Exit from the application
              menuItem.setOnAction(new EventHandler<ActionEvent>()
              {
                  @Override
                  public void handle(ActionEvent e)
                  {
                      // Exit from the application
                      System.exit(0);
                  }
              });
      
              file.getItems().add(menuItem);
      
              menuBar.getMenus().add(file);
      
              // Edit menu - Preferences
              javafx.scene.control.Menu edit = new javafx.scene.control.Menu("Edit");
              edit.getItems().add(new MenuItem("Preferences"));
      
              menuBar.getMenus().add(edit);
      
              // View menu - Preferences
              javafx.scene.control.Menu view = new javafx.scene.control.Menu("View");
      
              javafx.scene.control.Menu toolbars = new javafx.scene.control.Menu("Toolbars");
      
              // View sub menu - Navigation, Actions, Events
              final CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
              toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>()
              {
                  @Override
                  public void handle(ActionEvent e)
                  {
      
                      // Visible or not visible tabs !!!
                      DataTabs.setRenderTab(toolbarSubMenuNavigation.isSelected());
                      // call here the getter setter and send boolean flag
                      System.out.println("subsystem1 #1 Enabled!");
                  }
              });
              CheckMenuItem toolbarSubMenuActions = new CheckMenuItem("Actions");
              toolbarSubMenuActions.setOnAction(new EventHandler<ActionEvent>()
              {
                  @Override
                  public void handle(ActionEvent e)
                  {
                      System.out.println("subsystem2 #2 Enabled!");
                  }
              });
              CheckMenuItem toolbarSubMenuEvents = new CheckMenuItem("Events");
              toolbarSubMenuEvents.setOnAction(new EventHandler<ActionEvent>()
              {
                  @Override
                  public void handle(ActionEvent e)
                  {
                      System.out.println("subsystem3 #3 Enabled!");
                  }
              });
              toolbars.getItems().addAll(toolbarSubMenuNavigation, toolbarSubMenuActions, toolbarSubMenuEvents);
      
              // TODO Da se dobavi scheckbox na submenus ot tozi link - ima primer
              // http://docs.oracle.com/javafx/2/ui_controls/menu_controls.htm
      
              view.getItems().add(toolbars);
      
              menuBar.getMenus().add(view);
      
              // Navigate menu - Preferences
              javafx.scene.control.Menu navigate = new javafx.scene.control.Menu("Navigate");
              navigate.getItems().add(new MenuItem("Preferences"));
      
              menuBar.getMenus().add(navigate);
      
              // Actions menu - Preferences
              javafx.scene.control.Menu actions = new javafx.scene.control.Menu("Actions");
              actions.getItems().add(new MenuItem("Preferences"));
      
              menuBar.getMenus().add(actions);
      
              // Tools menu - Options
              javafx.scene.control.Menu tools = new javafx.scene.control.Menu("Tools");
      
              MenuItem options = new MenuItem("Options");
      
              // Options Panel
              options.setOnAction(new EventHandler<ActionEvent>()
              {
                  @Override
                  public void handle(ActionEvent e)
                  {
                     
                      SettingsPanel spanel = new SettingsPanel();
                      spanel.GeneralConfiguration();
                  }
              });
      
              tools.getItems().add(options);
      
              menuBar.getMenus().add(tools);
      
              // Window menu - Preferences
              javafx.scene.control.Menu window = new javafx.scene.control.Menu("Window");
              window.getItems().add(new MenuItem("Preferences"));
      
              menuBar.getMenus().add(window);
      
              // Help menu - Help Contents, Report Issue, About
              javafx.scene.control.Menu help = new javafx.scene.control.Menu("Help");
              help.getItems().add(new MenuItem("Help Contents"));
              help.getItems().add(new MenuItem("Report Issue"));
      
              MenuItem helpItem = new MenuItem("About");
      
              // Exit from the application
              helpItem.setOnAction(new EventHandler<ActionEvent>()
              {
                  @Override
                  public void handle(ActionEvent e)
                  {
                  }
              });
      
              help.getItems().add(helpItem);
      
              menuBar.getMenus().add(help);
      
              menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
      
              menuBar.setLayoutX(0);
              menuBar.setLayoutY(0);
      
              return menuBar;
          }
      
         
      }
      
      

       

      Java class which I want to call:

       

      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;
      import javafx.beans.value.ChangeListener;
      import javafx.beans.value.ObservableValue;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.geometry.Bounds;
      import javafx.geometry.Pos;
      import javafx.geometry.Rectangle2D;
      import javafx.geometry.Side;
      import javafx.scene.Scene;
      import javafx.scene.control.ContextMenu;
      import javafx.scene.control.Label;
      import javafx.scene.control.ListCell;
      import javafx.scene.control.ListView;
      import javafx.scene.control.Menu;
      import javafx.scene.control.MenuItem;
      import javafx.scene.control.ScrollPane;
      import javafx.scene.control.ScrollPane.ScrollBarPolicy;
      import javafx.scene.control.Tab;
      import javafx.scene.control.TabPane;
      import javafx.scene.control.TitledPane;
      import javafx.scene.control.TreeItem;
      import javafx.scene.control.TreeView;
      import javafx.scene.input.MouseButton;
      import javafx.scene.input.MouseEvent;
      import javafx.scene.layout.AnchorPane;
      import javafx.scene.layout.BorderPane;
      import javafx.scene.layout.StackPane;
      import javafx.scene.layout.VBox;
      import javafx.scene.paint.Color;
      import javafx.stage.Screen;
      import javafx.stage.Stage;
      import javafx.util.Callback;
      
      public class DataTabs
      {
      
          private static boolean renderTab;
          private static TabPane tabPane;
      
          public static boolean isRenderTab()
          {
              return DataTabs.renderTab;
          }
      
          public static void setRenderTab(boolean renderTab)
          {
              DataTabs.renderTab = renderTab;
              tabPane.setVisible(renderTab);
          }
      
      
          public BorderPane initNavigation(Stage primaryStage)
          {
              initObject();   // Add some test data
      
              VBox stackedTitledPanes = createStackedTitledPanes();
      
              ScrollPane scroll = makeScrollable(stackedTitledPanes);
      
              // Visible or not visible
              TabPane tabPane = DataTabs.tabPane = new TabPane();
              tabPane.setVisible(renderTab);
      
              BorderPane mainPane = new BorderPane();
      
              tabPane.setStyle("-fx-font-size: 12pt;"); // Set global size for the font
              // Create Tabs
              Tab tabA = new Tab();
              Label tabALabel = new Label("Main Component");
              tabA.setGraphic(makeDoubleClickResize(tabALabel, mainPane));
              tabA.setContextMenu(makeTabContextMenu(tabA, tabPane));  // Set right mouse click menu
              // Add something in Tab
              StackPane tabA_stack = new StackPane();
              tabA_stack.setAlignment(Pos.CENTER);
              tabA_stack.getChildren().add(scroll);
              tabA.setContent(tabA_stack);
              tabPane.getTabs().add(tabA);
      
              Tab tabB = new Tab();
              Label tabBLabel = new Label("Second Component");
              tabB.setGraphic(makeDoubleClickResize(tabBLabel, mainPane));
              tabB.setContextMenu(makeTabContextMenu(tabB, tabPane));  // Set right mouse click menu
              // Add something in Tab
              StackPane tabB_stack = new StackPane();
              tabB_stack.setAlignment(Pos.CENTER);
              tabB_stack.getChildren().add(new Label("Label@Tab B"));
              tabB.setContent(tabB_stack);
              tabPane.getTabs().add(tabB);
      
              Tab tabC = new Tab();
              Label tabCLabel = new Label("Last Component");
              tabC.setGraphic(makeDoubleClickResize(tabCLabel, mainPane));
              tabC.setContextMenu(makeTabContextMenu(tabC, tabPane));  // Set right mouse click menu
              // Add something in Tab
              StackPane tabC_vBox = new StackPane();
              tabC_vBox.setAlignment(Pos.CENTER);
              tabC_vBox.getChildren().add(new Label("Label@Tab C"));
              tabC.setContent(tabC_vBox);
              tabPane.getTabs().add(tabC);
      
              mainPane.setCenter(tabPane);
              mainPane.setPrefSize(300, 500);
              //mainPane.setLayoutX(5);     // Horizontal Position
              mainPane.setLayoutY(32);    // Vertical Position
      
              scroll.setPrefSize(395, 580);
              scroll.setLayoutX(5);
              scroll.setLayoutY(32);
      
              return mainPane;
          }
      
      }
      
      

       

      This implementation works but I want to optimize it to use less static variables and objects. Can you tell me which sections of the code how can be optimized?