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