View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    * 
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   ******************************************************************************/
11  package com.jsql.view.swing.panel.split;
12  
13  import com.jsql.view.swing.util.JSplitPaneWithZeroSizeDivider;
14  import com.jsql.model.InjectionModel;
15  import com.jsql.util.I18nUtil;
16  import com.jsql.util.PreferencesUtil;
17  import com.jsql.view.swing.panel.PanelConsoles;
18  import com.jsql.view.swing.tab.TabManagersCards;
19  import com.jsql.view.swing.tab.TabResults;
20  import com.jsql.view.swing.util.MediatorHelper;
21  import com.jsql.view.swing.util.UiUtil;
22  
23  import javax.swing.*;
24  import java.awt.*;
25  import java.awt.event.MouseAdapter;
26  import java.awt.event.MouseEvent;
27  import java.util.prefs.Preferences;
28  
29  /**
30   * SplitPane composed of tree and tabs on top, and info tabs on bottom.
31   */
32  public class SplitNS extends JSplitPaneWithZeroSizeDivider {
33  
34      /**
35       * SplitPane containing Manager panels on the left and result tabs on the right.
36       */
37      private final JSplitPane splitEW = new JSplitPaneWithZeroSizeDivider(JSplitPane.HORIZONTAL_SPLIT);
38  
39      private static final JPanel PANEL_HIDDEN_CONSOLES = new JPanel();
40      
41      /**
42       * MouseAdapter used on arrow on tabbedpane header and on
43       * ersatz button when bottom panel is hidden.
44       */
45      private static final ActionHideShowConsole ACTION_HIDE_SHOW_CONSOLE = new ActionHideShowConsole(SplitNS.PANEL_HIDDEN_CONSOLES);
46      private static final ActionHideShowResult ACTION_HIDE_SHOW_RESULT= new ActionHideShowResult();
47  
48      /**
49       * Create main panel with Manager panels on the left, result tabs on the right,
50       * and consoles in the bottom.
51       */
52      public SplitNS() {
53          super(JSplitPane.VERTICAL_SPLIT);
54          var preferences = Preferences.userRoot().node(InjectionModel.class.getName());
55          var tabManagersProxy = new TabManagersCards();
56          new TabResults();  // initialized but hidden
57  
58          // Tree and tabs on top
59          this.splitEW.setLeftComponent(tabManagersProxy);
60          JLabel labelApp = new JLabel(UiUtil.APP_BIG.getIcon());
61          labelApp.setMinimumSize(new Dimension(100, 0));
62          this.splitEW.setRightComponent(labelApp);
63          var verticalLeftRightSplitter = preferences.getDouble(PreferencesUtil.EW_SPLIT, 0.33);
64          this.splitEW.setDividerLocation(Math.max(0.0, Math.min(1.0, verticalLeftRightSplitter)));
65  
66          JLabel labelShowConsoles = new JLabel(UiUtil.ARROW_UP.getIcon());
67          labelShowConsoles.setBorder(BorderFactory.createEmptyBorder());
68          labelShowConsoles.addMouseListener(new MouseAdapter() {
69              @Override
70              public void mouseClicked(MouseEvent e) {
71                  SplitNS.ACTION_HIDE_SHOW_CONSOLE.actionPerformed(null);
72              }
73          });
74          labelShowConsoles.setName("buttonShowConsolesHidden");
75          SplitNS.PANEL_HIDDEN_CONSOLES.setLayout(new BorderLayout());
76          SplitNS.PANEL_HIDDEN_CONSOLES.add(labelShowConsoles, BorderLayout.LINE_END);
77          SplitNS.PANEL_HIDDEN_CONSOLES.setVisible(false);
78          SplitNS.PANEL_HIDDEN_CONSOLES.addMouseListener(new MouseAdapter() {
79              @Override
80              public void mousePressed(MouseEvent e) {
81                  if (e.getButton() == MouseEvent.BUTTON2) {  // middle click on header with no tab
82                      SplitNS.ACTION_HIDE_SHOW_CONSOLE.actionPerformed(null);
83                  }
84              }
85          });
86  
87          var panelManagerResult = new JPanel(new BorderLayout());
88          panelManagerResult.add(this.splitEW, BorderLayout.CENTER);
89          panelManagerResult.add(SplitNS.PANEL_HIDDEN_CONSOLES, BorderLayout.SOUTH);
90          this.setTopComponent(panelManagerResult);
91  
92          var panelConsoles = new PanelConsoles();
93          MediatorHelper.register(panelConsoles);
94          this.setBottomComponent(panelConsoles);
95  
96          this.setResizeWeight(1);
97      }
98  
99      /**
100      * Switch left component with right component when locale orientation requires it.
101      */
102     public void initSplitOrientation() {
103         if (MediatorHelper.tabResults().getTabCount() == 0) {
104             int dividerLocation = this.splitEW.getDividerLocation();
105             if (ComponentOrientation.RIGHT_TO_LEFT.equals(ComponentOrientation.getOrientation(I18nUtil.getCurrentLocale()))) {
106                 this.splitEW.setLeftComponent(MediatorHelper.tabResults());
107             } else {
108                 this.splitEW.setRightComponent(MediatorHelper.tabResults());
109             }
110             this.splitEW.setDividerLocation(dividerLocation);
111         }
112     }
113 
114 
115     // Getter and setter
116 
117     public JSplitPane getSplitEW() {
118         return this.splitEW;
119     }
120 
121     public static ActionHideShowConsole getActionHideShowConsole() {
122         return SplitNS.ACTION_HIDE_SHOW_CONSOLE;
123     }
124     
125     public static ActionHideShowResult getActionHideShowResult() {
126         return SplitNS.ACTION_HIDE_SHOW_RESULT;
127     }
128 }