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.tab;
12  
13  import com.jsql.util.I18nUtil;
14  import com.jsql.view.swing.text.JToolTipI18n;
15  import com.jsql.view.swing.util.I18nViewUtil;
16  import com.jsql.view.swing.util.MediatorHelper;
17  import com.jsql.view.swing.util.ModelSvgIcon;
18  import com.jsql.view.swing.util.UiUtil;
19  
20  import javax.swing.*;
21  import java.awt.*;
22  import java.awt.event.MouseAdapter;
23  import java.awt.event.MouseEvent;
24  import java.util.Arrays;
25  import java.util.List;
26  import java.util.concurrent.atomic.AtomicInteger;
27  import java.util.concurrent.atomic.AtomicReference;
28  
29  /**
30   * Panel on the left with functionalities like webshell, file reading and admin page finder.
31   */
32  public class TabManagers extends TabbedPaneWheeled {
33  
34      private final transient List<ModelSvgIcon> iconsTabs = Arrays.asList(
35          UiUtil.DATABASE_BOLD, UiUtil.ADMIN, UiUtil.DOWNLOAD, UiUtil.TERMINAL, UiUtil.LOCK, UiUtil.TEXTFIELD, UiUtil.BATCH
36      );
37  
38      /**
39       * Create manager panel.
40       */
41      public TabManagers() {
42          this.setName("tabManagers");
43          this.setMaximumSize(new Dimension(this.getMaximumSize().width, 35));
44          this.setPreferredSize(new Dimension(this.getPreferredSize().width, 35));
45  
46          AtomicInteger indexTab = new AtomicInteger();
47          this.iconsTabs.forEach(modelSvgIcon -> this.buildI18nTab(modelSvgIcon, indexTab.getAndIncrement()));
48          this.addChangeListener(e -> {
49              CardLayout cardLayout = (CardLayout) MediatorHelper.tabManagersCards().getLayout();
50              cardLayout.show(MediatorHelper.tabManagersCards(), this.getTabComponentAt(this.getSelectedIndex()).getName());
51          });
52      }
53      
54      private void buildI18nTab(ModelSvgIcon modelSvgIcon, int index) {
55          Icon icon = modelSvgIcon.getIcon();
56          String keyLabel = modelSvgIcon.getKeyLabel();
57          String keyTooltip = modelSvgIcon.getKeyTooltip();
58          AtomicReference<JToolTipI18n> tooltipAtomic = new AtomicReference<>(new JToolTipI18n(I18nUtil.valueByKey(keyTooltip)));
59          JLabel labelTab = new JLabel(I18nUtil.valueByKey(keyLabel), icon, SwingConstants.CENTER) {
60              @Override
61              public JToolTip createToolTip() {
62                  tooltipAtomic.set(new JToolTipI18n(I18nUtil.valueByKey(keyTooltip)));
63                  return tooltipAtomic.get();
64              }
65          };
66          
67          labelTab.setName(keyLabel);
68          labelTab.addMouseListener(new MouseAdapter() {
69              @Override
70              public void mousePressed(MouseEvent e) {
71                  CardLayout cardLayout = (CardLayout) MediatorHelper.tabManagersCards().getLayout();
72                  cardLayout.show(MediatorHelper.tabManagersCards(), TabManagers.this.getTabComponentAt(index).getName());
73                  TabManagers.this.setSelectedIndex(index);
74              }
75          });
76  
77          this.addTab(I18nUtil.valueByKey(keyLabel), icon, null);  // Required for i18n to work
78          this.setTabComponentAt(  // TODO Break focus, should not be used
79              this.indexOfTab(I18nUtil.valueByKey(keyLabel)),
80              labelTab
81          );
82  
83          I18nViewUtil.addComponentForKey(keyLabel, labelTab);
84          I18nViewUtil.addComponentForKey(keyTooltip, tooltipAtomic.get());
85          
86          labelTab.setToolTipText(I18nUtil.valueByKey(keyTooltip));
87      }
88  
89      public List<ModelSvgIcon> getIconsTabs() {
90          return this.iconsTabs;
91      }
92  }