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.address;
12  
13  import com.jsql.util.I18nUtil;
14  import com.jsql.view.swing.manager.util.StateButton;
15  import com.jsql.view.swing.text.JToolTipI18n;
16  import com.jsql.view.swing.util.I18nViewUtil;
17  import com.jsql.view.swing.util.UiUtil;
18  
19  import javax.swing.*;
20  import java.awt.*;
21  import java.util.concurrent.atomic.AtomicReference;
22  
23  /**
24   * A button displayed in address.
25   */
26  public class ButtonStart extends JButton {
27  
28      private static final String BUTTON_START_TOOLTIP = "BUTTON_START_TOOLTIP";
29      public static final String BUTTON_IN_URL = "buttonInUrl";
30  
31      /**
32       * State of current injection.
33       */
34      private StateButton state = StateButton.STARTABLE;
35  
36      private final AtomicReference<JToolTipI18n> tooltip = new AtomicReference<>(
37          new JToolTipI18n(I18nUtil.valueByKey(ButtonStart.BUTTON_START_TOOLTIP))
38      );
39  
40      @Override
41      public JToolTip createToolTip() {
42          if (this.state == StateButton.STARTABLE) {
43              this.tooltip.get().setText(I18nUtil.valueByKey(ButtonStart.BUTTON_START_TOOLTIP));
44          } else if (this.state == StateButton.STOPPABLE) {
45              this.tooltip.get().setText(I18nUtil.valueByKey("BUTTON_STOP_TOOLTIP"));
46          } else if (this.state == StateButton.STOPPING) {
47              this.tooltip.get().setText(I18nUtil.valueByKey("BUTTON_STOPPING_TOOLTIP"));
48          }
49          return this.tooltip.get();
50      }
51  
52      /**
53       * Create a button in address bar.
54       */
55      public ButtonStart() {
56          this.setName(ButtonStart.BUTTON_IN_URL);
57          this.setToolTipText(I18nUtil.valueByKey(ButtonStart.BUTTON_START_TOOLTIP));
58          I18nViewUtil.addComponentForKey(ButtonStart.BUTTON_START_TOOLTIP, this.tooltip.get());
59  
60          this.setPreferredSize(new Dimension(18, 16));
61          this.setOpaque(false);
62          this.setContentAreaFilled(false);
63          this.setBorderPainted(false);
64          this.setRolloverEnabled(true);
65          this.setIcons();
66      }
67  
68      public void setIcons() {
69          // required to get correct color at startup instead of blu
70          if (ComponentOrientation.getOrientation(I18nUtil.getCurrentLocale()).isLeftToRight()) {
71              this.setIcon(UiUtil.ARROW.getIcon());
72              this.setRolloverIcon(UiUtil.ARROW_HOVER.getIcon());
73              this.setPressedIcon(UiUtil.ARROW_PRESSED.getIcon());
74          } else {
75              this.setIcon(UiUtil.ARROW_LEFT.getIcon());
76              this.setRolloverIcon(UiUtil.ARROW_LEFT_HOVER.getIcon());
77              this.setPressedIcon(UiUtil.ARROW_LEFT_PRESSED.getIcon());
78          }
79      }
80  
81      /**
82       * Replace button with Stop icon ; user can stop current process.
83       */
84      public void setInjectionReady() {
85          this.state = StateButton.STARTABLE;
86          this.setRolloverEnabled(true);
87          this.setEnabled(true);
88          this.setIcons();
89      }
90  
91      /**
92       * Replace button with Stop icon ; user can stop current process.
93       */
94      public void setInjectionRunning() {
95          this.state = StateButton.STOPPABLE;
96          this.setRolloverEnabled(false);
97          this.setEnabled(true);
98          this.setIcon(UiUtil.CROSS_RED.getIcon());
99      }
100 
101     /**
102      * Replace button with icon loader until injection process
103      * is finished ; user waits the end of process.
104      */
105     public void setInjectionStopping() {
106         this.state = StateButton.STOPPING;
107         this.setRolloverEnabled(false);
108         this.setEnabled(false);
109         this.setIcon(UiUtil.HOURGLASS.getIcon());
110     }
111 
112     /**
113      * Return the current state of current process.
114      * @return State of process
115      */
116     public StateButton getState() {
117         return this.state;
118     }
119 }