View Javadoc
1   package com.jsql.view.swing.panel.address;
2   
3   import com.jsql.util.I18nUtil;
4   import com.jsql.util.LogLevelUtil;
5   import com.jsql.view.swing.manager.util.StateButton;
6   import com.jsql.view.swing.panel.PanelAddressBar;
7   import com.jsql.view.swing.util.MediatorHelper;
8   import org.apache.logging.log4j.LogManager;
9   import org.apache.logging.log4j.Logger;
10  
11  import javax.swing.*;
12  import java.awt.event.ActionEvent;
13  import java.awt.event.ActionListener;
14  
15  public class ActionStart implements ActionListener {
16      
17      /**
18       * Log4j logger sent to view.
19       */
20      private static final Logger LOGGER = LogManager.getRootLogger();
21      
22      protected final PanelAddressBar panelAddressBar;
23      
24      public ActionStart(PanelAddressBar panelAddressBar) {
25          this.panelAddressBar = panelAddressBar;
26      }
27  
28      @Override
29      public void actionPerformed(ActionEvent e) {
30          // No injection running
31          if (this.panelAddressBar.getPanelTrailingAddress().getButtonStart().getState() == StateButton.STARTABLE) {
32              this.startInjection();
33          } else if (this.panelAddressBar.getPanelTrailingAddress().getButtonStart().getState() == StateButton.STOPPABLE) {
34              this.stopInjection();  // Injection currently running, stop the process
35          }
36      }
37      
38      protected void startInjection() {
39          int option = JOptionPane.OK_OPTION;
40          if (MediatorHelper.model().shouldErasePreviousInjection()) {  // Ask the user confirmation if injection already built
41              // Fix #93469: IllegalArgumentException on showConfirmDialog()
42              // Fix #33930: ClassCastException on showConfirmDialog()
43              // Implementation by sun.awt.image
44              try {
45                  option = JOptionPane.showConfirmDialog(
46                      MediatorHelper.frame(),
47                      I18nUtil.valueByKey("DIALOG_NEW_INJECTION_TEXT"),
48                      I18nUtil.valueByKey("DIALOG_NEW_INJECTION_TITLE"),
49                      JOptionPane.OK_CANCEL_OPTION
50                  );
51              } catch (IllegalArgumentException | ClassCastException e) {
52                  LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
53              }
54          }
55  
56          if (option == JOptionPane.OK_OPTION) {  // Then start injection
57              this.panelAddressBar.getPanelTrailingAddress().getButtonStart().setToolTipText(I18nUtil.valueByKey("BUTTON_STOP_TOOLTIP"));
58              this.panelAddressBar.getPanelTrailingAddress().getButtonStart().setInjectionRunning();
59              this.panelAddressBar.getPanelTrailingAddress().getLoader().setVisible(true);
60  
61              MediatorHelper.frame().resetInterface();  // Erase everything in the view from a previous injection
62  
63              MediatorHelper.model().getMediatorUtils().getParameterUtil().controlInput(
64                  this.panelAddressBar.getTextFieldAddress().getText().trim(),
65                  this.panelAddressBar.getTextFieldRequest().getText().trim(),
66                  this.panelAddressBar.getTextFieldHeader().getText().trim(),
67                  this.panelAddressBar.getMethodInjection(),
68                  this.panelAddressBar.getTypeRequest(),
69                  false
70              );
71          }
72      }
73      
74      private void stopInjection() {
75          this.panelAddressBar.getPanelTrailingAddress().getLoader().setVisible(false);
76          this.panelAddressBar.getPanelTrailingAddress().getButtonStart().setInjectionStopping();
77          MediatorHelper.model().setIsStoppedByUser(true);
78      }
79  }