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.tree.action;
12  
13  import com.jsql.model.bean.database.Column;
14  import com.jsql.model.suspendable.AbstractSuspendable;
15  import com.jsql.view.swing.tree.model.AbstractNodeModel;
16  import com.jsql.view.swing.util.MediatorHelper;
17  
18  import javax.swing.*;
19  import javax.swing.tree.DefaultMutableTreeNode;
20  import javax.swing.tree.DefaultTreeModel;
21  import java.awt.event.ActionEvent;
22  import java.awt.event.ActionListener;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * Action to start and stop injection process.
28   */
29  public class ActionLoadStop implements ActionListener {
30      
31      private final AbstractNodeModel nodeModel;
32      private final DefaultMutableTreeNode currentTableNode;
33  
34      public ActionLoadStop(AbstractNodeModel nodeModel, DefaultMutableTreeNode currentTableNode) {
35          this.nodeModel = nodeModel;
36          this.currentTableNode = currentTableNode;
37      }
38  
39      @Override
40      public void actionPerformed(ActionEvent e) {
41          final List<Column> columnsToSearch = this.getSelectedColumns();
42          if (!this.nodeModel.isRunning() && columnsToSearch.isEmpty()) {
43              return;
44          }
45          if (!this.nodeModel.isRunning()) {
46              this.startListValues(columnsToSearch);
47          } else {
48              this.stopAbstractNode();
49          }
50          this.nodeModel.setRunning(!this.nodeModel.isRunning());
51      }
52  
53      private void startListValues(final List<Column> columnsToSearch) {
54          new SwingWorker<>() {
55              @Override
56              protected Object doInBackground() throws Exception {
57                  Thread.currentThread().setName("SwingWorkerActionLoadStop");
58                  MediatorHelper.model().getDataAccess().listValues(columnsToSearch);
59                  return null;
60              }
61          }.execute();
62      }
63  
64      private void stopAbstractNode() {
65          AbstractSuspendable suspendableTask = MediatorHelper.model().getMediatorUtils().getThreadUtil().get(this.nodeModel.getElementDatabase());
66          // Fix #21394: NullPointerException on stop()
67          if (suspendableTask != null) {
68              suspendableTask.stop();
69          }
70          
71          this.nodeModel.setIndexProgress(0);
72          this.nodeModel.setProgressing(false);
73          this.nodeModel.setLoading(false);
74          
75          MediatorHelper.model().getMediatorUtils().getThreadUtil().remove(this.nodeModel.getElementDatabase());
76      }
77  
78      private List<Column> getSelectedColumns() {
79          DefaultTreeModel treeModel = (DefaultTreeModel) MediatorHelper.treeDatabase().getModel();
80          DefaultMutableTreeNode tableNode = this.currentTableNode;
81          final List<Column> columnsToSearch = new ArrayList<>();
82  
83          int tableChildCount = treeModel.getChildCount(tableNode);
84          for (var i = 0 ; i < tableChildCount ; i++) {
85              DefaultMutableTreeNode currentChild = (DefaultMutableTreeNode) treeModel.getChild(tableNode, i);
86              if (currentChild.getUserObject() instanceof AbstractNodeModel) {
87                  AbstractNodeModel columnTreeNodeModel = (AbstractNodeModel) currentChild.getUserObject();
88                  if (columnTreeNodeModel.isSelected()) {
89                      columnsToSearch.add((Column) columnTreeNodeModel.getElementDatabase());
90                  }
91              }
92          }
93          return columnsToSearch;
94      }
95  }