1 | package com.jsql.view.swing.tree; | |
2 | ||
3 | import com.jsql.model.bean.database.AbstractElementDatabase; | |
4 | import com.jsql.model.bean.database.Column; | |
5 | import com.jsql.model.bean.database.Database; | |
6 | import com.jsql.model.bean.database.Table; | |
7 | import com.jsql.util.LogLevelUtil; | |
8 | import com.jsql.view.swing.tree.model.AbstractNodeModel; | |
9 | import com.jsql.view.swing.tree.model.NodeModelColumn; | |
10 | import com.jsql.view.swing.tree.model.NodeModelDatabase; | |
11 | import com.jsql.view.swing.tree.model.NodeModelTable; | |
12 | import com.jsql.view.swing.util.MediatorHelper; | |
13 | import org.apache.logging.log4j.LogManager; | |
14 | import org.apache.logging.log4j.Logger; | |
15 | ||
16 | import javax.swing.*; | |
17 | import javax.swing.tree.DefaultMutableTreeNode; | |
18 | import javax.swing.tree.DefaultTreeModel; | |
19 | import javax.swing.tree.TreePath; | |
20 | import java.util.HashMap; | |
21 | import java.util.List; | |
22 | import java.util.Map; | |
23 | ||
24 | public class TreeDatabase extends JTree { | |
25 | | |
26 | /** | |
27 | * Log4j logger sent to view. | |
28 | */ | |
29 | private static final Logger LOGGER = LogManager.getRootLogger(); | |
30 | ||
31 | /** | |
32 | * Map a database element with the corresponding tree node.<br> | |
33 | * The injection model send a database element to the view, then | |
34 | * the view access its graphic component to update. | |
35 | */ | |
36 | private final transient Map<AbstractElementDatabase, DefaultMutableTreeNode> mapNodes = new HashMap<>(); | |
37 | ||
38 | public TreeDatabase(DefaultMutableTreeNode root) { | |
39 | super(root); | |
40 | } | |
41 | | |
42 | public void reloadNodes() { | |
43 | | |
44 | // I18n of tree empty node | |
45 |
1
1. reloadNodes : negated conditional → NO_COVERAGE |
if (this.isRootVisible()) { |
46 | | |
47 | DefaultTreeModel model = (DefaultTreeModel) this.getModel(); | |
48 | DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot(); | |
49 |
1
1. reloadNodes : removed call to javax/swing/tree/DefaultTreeModel::reload → NO_COVERAGE |
model.reload(root); |
50 |
1
1. reloadNodes : removed call to com/jsql/view/swing/tree/TreeDatabase::revalidate → NO_COVERAGE |
this.revalidate(); |
51 | } | |
52 | } | |
53 | ||
54 | public void reset() { | |
55 | | |
56 | DefaultTreeModel treeModel = (DefaultTreeModel) this.getModel(); // Tree model for refreshing the tree | |
57 | DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel.getRoot(); // The tree root | |
58 | ||
59 |
1
1. reset : removed call to javax/swing/tree/DefaultMutableTreeNode::removeAllChildren → NO_COVERAGE |
root.removeAllChildren(); // Remove tree nodes |
60 |
1
1. reset : removed call to javax/swing/tree/DefaultTreeModel::nodeChanged → NO_COVERAGE |
treeModel.nodeChanged(root); // Refresh the root |
61 |
1
1. reset : removed call to javax/swing/tree/DefaultTreeModel::reload → NO_COVERAGE |
treeModel.reload(); // Refresh the tree |
62 | | |
63 |
1
1. reset : removed call to com/jsql/view/swing/tree/TreeDatabase::setRootVisible → NO_COVERAGE |
this.setRootVisible(true); |
64 | } | |
65 | | |
66 | public void addColumns(List<Column> columns) { | |
67 | | |
68 | DefaultTreeModel treeModel = (DefaultTreeModel) this.getModel(); // Tree model, update the tree (refresh, add node, etc.) | |
69 | DefaultMutableTreeNode tableNode = null; // The table to update | |
70 | ||
71 | // Loop into the list of columns | |
72 | for (Column column: columns) { | |
73 | | |
74 | AbstractNodeModel newTreeNodeModel = new NodeModelColumn(column); // Create a node model with the column element | |
75 | var newNode = new DefaultMutableTreeNode(newTreeNodeModel); // Create the node | |
76 | tableNode = this.getTreeNodeModels().get(column.getParent()); // Get the parent table | |
77 | | |
78 | // Fix #1805 : NullPointerException on tableNode.getChildCount() | |
79 |
1
1. addColumns : negated conditional → NO_COVERAGE |
if (tableNode != null) { |
80 |
1
1. addColumns : removed call to javax/swing/tree/DefaultTreeModel::insertNodeInto → NO_COVERAGE |
treeModel.insertNodeInto(newNode, tableNode, tableNode.getChildCount()); // Add the column to the table |
81 | } | |
82 | } | |
83 | ||
84 |
1
1. addColumns : negated conditional → NO_COVERAGE |
if (tableNode != null) { |
85 | | |
86 |
1
1. addColumns : removed call to com/jsql/view/swing/tree/TreeDatabase::expandPath → NO_COVERAGE |
this.expandPath(new TreePath(tableNode.getPath())); // Open the table node |
87 |
1
1. addColumns : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setLoaded → NO_COVERAGE |
((AbstractNodeModel) tableNode.getUserObject()).setLoaded(true); // The table has just been search (avoid double check) |
88 | } | |
89 | } | |
90 | | |
91 | public void addDatabases(List<Database> databases) { | |
92 | | |
93 | DefaultTreeModel treeModel = (DefaultTreeModel) this.getModel(); // Tree model, update the tree (refresh, add node, etc.) | |
94 | DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel.getRoot(); // First node in tree | |
95 | ||
96 | // Loop into the list of databases | |
97 | for (Database database: databases) { | |
98 | ||
99 | AbstractNodeModel newTreeNodeModel = new NodeModelDatabase(database); // Create a node model with the database element | |
100 | var newNode = new DefaultMutableTreeNode(newTreeNodeModel); // Create the node | |
101 | this.getTreeNodeModels().put(database, newNode); // Save the node | |
102 |
1
1. addDatabases : removed call to javax/swing/tree/DefaultMutableTreeNode::add → NO_COVERAGE |
root.add(newNode); // Add the node to the tree |
103 | } | |
104 | ||
105 | // Refresh the tree | |
106 |
1
1. addDatabases : removed call to javax/swing/tree/DefaultTreeModel::reload → NO_COVERAGE |
treeModel.reload(root); |
107 | | |
108 | // Open the root node | |
109 |
1
1. addDatabases : removed call to com/jsql/view/swing/tree/TreeDatabase::expandPath → NO_COVERAGE |
this.expandPath(new TreePath(root.getPath())); |
110 |
1
1. addDatabases : removed call to com/jsql/view/swing/tree/TreeDatabase::setRootVisible → NO_COVERAGE |
this.setRootVisible(false); |
111 | } | |
112 | | |
113 | public void addTables(List<Table> tables) { | |
114 | | |
115 | DefaultTreeModel treeModel = (DefaultTreeModel) this.getModel(); // Tree model, update the tree (refresh, add node, etc.) | |
116 | DefaultMutableTreeNode databaseNode = null; // The database to update | |
117 | ||
118 | // Loop into the list of tables | |
119 | for (Table table: tables) { | |
120 | ||
121 | AbstractNodeModel newTreeNodeModel = new NodeModelTable(table); // Create a node model with the table element | |
122 | var newNode = new DefaultMutableTreeNode(newTreeNodeModel); // Create the node | |
123 | this.getTreeNodeModels().put(table, newNode); // Save the node | |
124 | databaseNode = this.getTreeNodeModels().get(table.getParent()); // Get the parent database | |
125 | | |
126 | // Report NullPointerException #1670 | |
127 |
1
1. addTables : negated conditional → NO_COVERAGE |
if (databaseNode != null) { |
128 |
1
1. addTables : removed call to javax/swing/tree/DefaultTreeModel::insertNodeInto → NO_COVERAGE |
treeModel.insertNodeInto(newNode, databaseNode, databaseNode.getChildCount()); // Add the table to the database |
129 | } else { | |
130 | LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Missing database for table {}.", () -> table); | |
131 | } | |
132 | } | |
133 | ||
134 |
1
1. addTables : negated conditional → NO_COVERAGE |
if (databaseNode != null) { |
135 | | |
136 |
1
1. addTables : removed call to com/jsql/view/swing/tree/TreeDatabase::expandPath → NO_COVERAGE |
this.expandPath(new TreePath(databaseNode.getPath())); // Open the database node |
137 |
1
1. addTables : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setLoaded → NO_COVERAGE |
((AbstractNodeModel) databaseNode.getUserObject()).setLoaded(true); // The database has just been search (avoid double check) |
138 | } | |
139 | } | |
140 | | |
141 | public void createValuesTab(String[][] data, String[] columnNames, AbstractElementDatabase table) { | |
142 | ||
143 | // Report NullPointerException #1683 | |
144 | DefaultMutableTreeNode node = this.getTreeNodeModels().get(table); | |
145 | | |
146 |
1
1. createValuesTab : negated conditional → NO_COVERAGE |
if (node != null) { |
147 | | |
148 | AbstractNodeModel progressingTreeNodeModel = (AbstractNodeModel) node.getUserObject(); // Get the node | |
149 |
1
1. createValuesTab : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setIndexProgress → NO_COVERAGE |
progressingTreeNodeModel.setIndexProgress(table.getChildCount()); // Update the progress value of the model, end the progress |
150 |
1
1. createValuesTab : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setRunning → NO_COVERAGE |
progressingTreeNodeModel.setRunning(false); // Mark the node model as 'no stop/pause/resume button' |
151 |
1
1. createValuesTab : removed call to com/jsql/view/swing/tab/TabResults::createValuesTab → NO_COVERAGE |
MediatorHelper.tabResults().createValuesTab(data, columnNames, table); |
152 | } | |
153 | } | |
154 | | |
155 | public void endIndeterminateProgress(AbstractElementDatabase dataElementDatabase) { | |
156 | | |
157 | // Tree model, update the tree (refresh, add node, etc.) | |
158 | DefaultTreeModel treeModel = (DefaultTreeModel) this.getModel(); | |
159 | ||
160 | DefaultMutableTreeNode nodeModel = this.getTreeNodeModels().get(dataElementDatabase); | |
161 | | |
162 | // Fix #1806 : NullPointerException on ...odels().get(dataElementDatabase).getUserObject() | |
163 |
1
1. endIndeterminateProgress : negated conditional → NO_COVERAGE |
if (nodeModel != null) { |
164 | | |
165 | AbstractNodeModel progressingTreeNodeModel = (AbstractNodeModel) nodeModel.getUserObject(); // Get the node | |
166 |
1
1. endIndeterminateProgress : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setProgressing → NO_COVERAGE |
progressingTreeNodeModel.setProgressing(false); // Mark the node model as 'no loading bar' |
167 |
1
1. endIndeterminateProgress : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setRunning → NO_COVERAGE |
progressingTreeNodeModel.setRunning(false); // Mark the node model as 'no stop/pause/resume button' |
168 |
1
1. endIndeterminateProgress : removed call to javax/swing/tree/DefaultTreeModel::nodeChanged → NO_COVERAGE |
treeModel.nodeChanged(nodeModel); // Update the node |
169 | } | |
170 | } | |
171 | | |
172 | public void endProgress(AbstractElementDatabase dataElementDatabase) { | |
173 | | |
174 | // Tree model, update the tree (refresh, add node, etc.) | |
175 | DefaultTreeModel treeModel = (DefaultTreeModel) this.getModel(); | |
176 | ||
177 | // Report NullPointerException #1671 | |
178 | DefaultMutableTreeNode node = this.getTreeNodeModels().get(dataElementDatabase); | |
179 | | |
180 |
1
1. endProgress : negated conditional → NO_COVERAGE |
if (node != null) { |
181 | | |
182 | AbstractNodeModel progressingTreeNodeModel = (AbstractNodeModel) node.getUserObject(); // Get the node | |
183 |
1
1. endProgress : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setLoading → NO_COVERAGE |
progressingTreeNodeModel.setLoading(false); // Mark the node model as 'no progress bar' |
184 |
1
1. endProgress : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setRunning → NO_COVERAGE |
progressingTreeNodeModel.setRunning(false); // Mark the node model as 'no stop/pause/resume button' |
185 |
1
1. endProgress : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setIndexProgress → NO_COVERAGE |
progressingTreeNodeModel.setIndexProgress(0); // Reset the progress value of the model |
186 |
1
1. endProgress : removed call to javax/swing/tree/DefaultTreeModel::nodeChanged → NO_COVERAGE |
treeModel.nodeChanged(node); // Update the node and progress bar |
187 | } | |
188 | } | |
189 | | |
190 | public void startIndeterminateProgress(AbstractElementDatabase dataElementDatabase) { | |
191 | | |
192 | DefaultMutableTreeNode node = this.getTreeNodeModels().get(dataElementDatabase); | |
193 | | |
194 | // Fix #45540: NullPointerException on node.getUserObject() | |
195 |
1
1. startIndeterminateProgress : negated conditional → NO_COVERAGE |
if (node != null) { |
196 | | |
197 | AbstractNodeModel progressingTreeNodeModel = (AbstractNodeModel) node.getUserObject(); // Get the node | |
198 |
1
1. startIndeterminateProgress : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setProgressing → NO_COVERAGE |
progressingTreeNodeModel.setProgressing(true); // Mark the node model as 'loading' |
199 | DefaultTreeModel treeModel = (DefaultTreeModel) this.getModel(); // Tree model, update the tree (refresh, add node, etc.) | |
200 |
1
1. startIndeterminateProgress : removed call to javax/swing/tree/DefaultTreeModel::nodeChanged → NO_COVERAGE |
treeModel.nodeChanged(node); // Update the node |
201 | } | |
202 | } | |
203 | | |
204 | public void startProgress(AbstractElementDatabase dataElementDatabase) { | |
205 | | |
206 | DefaultMutableTreeNode node = this.getTreeNodeModels().get(dataElementDatabase); | |
207 | | |
208 | // Fix rare #73981 | |
209 |
1
1. startProgress : negated conditional → NO_COVERAGE |
if (node != null) { |
210 | | |
211 | AbstractNodeModel progressingTreeNodeModel = (AbstractNodeModel) node.getUserObject(); // Get the node | |
212 | | |
213 | // Fix rare Unhandled NullPointerException #66340 | |
214 |
1
1. startProgress : negated conditional → NO_COVERAGE |
if (progressingTreeNodeModel != null) { |
215 |
1
1. startProgress : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setLoading → NO_COVERAGE |
progressingTreeNodeModel.setLoading(true); // Mark the node model as 'display progress bar' |
216 | } | |
217 | ||
218 | DefaultTreeModel treeModel = (DefaultTreeModel) this.getModel(); // Tree model, update the tree (refresh, add node, etc.) | |
219 |
1
1. startProgress : removed call to javax/swing/tree/DefaultTreeModel::nodeChanged → NO_COVERAGE |
treeModel.nodeChanged(node); // Update the node |
220 | } | |
221 | } | |
222 | | |
223 | public void updateProgress(AbstractElementDatabase dataElementDatabase, int dataCount) { | |
224 | | |
225 | DefaultMutableTreeNode node = this.getTreeNodeModels().get(dataElementDatabase); | |
226 | | |
227 | // Fix Report #1368: ignore if no element database, usually for mock (eg. metadata, file, shell, list databases) | |
228 |
1
1. updateProgress : negated conditional → NO_COVERAGE |
if (node != null) { |
229 | | |
230 | AbstractNodeModel progressingTreeNodeModel = (AbstractNodeModel) node.getUserObject(); // Get the node | |
231 |
1
1. updateProgress : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setIndexProgress → NO_COVERAGE |
progressingTreeNodeModel.setIndexProgress(dataCount); // Update the progress value of the model |
232 | DefaultTreeModel treeModel = (DefaultTreeModel) this.getModel(); // Tree model, update the tree (refresh, add node, etc.) | |
233 |
1
1. updateProgress : removed call to javax/swing/tree/DefaultTreeModel::nodeChanged → NO_COVERAGE |
treeModel.nodeChanged(node); // Update the node |
234 | } | |
235 | } | |
236 | | |
237 | public final Map<AbstractElementDatabase, DefaultMutableTreeNode> getTreeNodeModels() { | |
238 |
1
1. getTreeNodeModels : replaced return value with Collections.emptyMap for com/jsql/view/swing/tree/TreeDatabase::getTreeNodeModels → NO_COVERAGE |
return this.mapNodes; |
239 | } | |
240 | } | |
Mutations | ||
45 |
1.1 |
|
49 |
1.1 |
|
50 |
1.1 |
|
59 |
1.1 |
|
60 |
1.1 |
|
61 |
1.1 |
|
63 |
1.1 |
|
79 |
1.1 |
|
80 |
1.1 |
|
84 |
1.1 |
|
86 |
1.1 |
|
87 |
1.1 |
|
102 |
1.1 |
|
106 |
1.1 |
|
109 |
1.1 |
|
110 |
1.1 |
|
127 |
1.1 |
|
128 |
1.1 |
|
134 |
1.1 |
|
136 |
1.1 |
|
137 |
1.1 |
|
146 |
1.1 |
|
149 |
1.1 |
|
150 |
1.1 |
|
151 |
1.1 |
|
163 |
1.1 |
|
166 |
1.1 |
|
167 |
1.1 |
|
168 |
1.1 |
|
180 |
1.1 |
|
183 |
1.1 |
|
184 |
1.1 |
|
185 |
1.1 |
|
186 |
1.1 |
|
195 |
1.1 |
|
198 |
1.1 |
|
200 |
1.1 |
|
209 |
1.1 |
|
214 |
1.1 |
|
215 |
1.1 |
|
219 |
1.1 |
|
228 |
1.1 |
|
231 |
1.1 |
|
233 |
1.1 |
|
238 |
1.1 |