ManagerDatabase.java

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.manager;
12
13
import com.jsql.util.LogLevelUtil;
14
import com.jsql.view.swing.tree.CellEditorNode;
15
import com.jsql.view.swing.tree.CellRendererNode;
16
import com.jsql.view.swing.tree.TreeDatabase;
17
import com.jsql.view.swing.tree.model.AbstractNodeModel;
18
import com.jsql.view.swing.tree.model.NodeModelEmpty;
19
import com.jsql.view.swing.util.I18nViewUtil;
20
import com.jsql.view.swing.util.MediatorHelper;
21
import org.apache.logging.log4j.LogManager;
22
import org.apache.logging.log4j.Logger;
23
24
import javax.swing.*;
25
import javax.swing.event.TreeModelEvent;
26
import javax.swing.event.TreeModelListener;
27
import javax.swing.tree.DefaultMutableTreeNode;
28
import javax.swing.tree.TreePath;
29
import javax.swing.tree.TreeSelectionModel;
30
import java.awt.*;
31
import java.awt.event.KeyAdapter;
32
import java.awt.event.KeyEvent;
33
import java.awt.event.MouseAdapter;
34
import java.awt.event.MouseEvent;
35
36
/**
37
 * Manager to code/decode string in various methods.
38
 */
39
public class ManagerDatabase extends JPanel {
40
41
    private static final Logger LOGGER = LogManager.getRootLogger();
42
43
    private final TreeDatabase tree;
44
    public static final String TREE_DATABASES = "treeDatabases";
45
46
    /**
47
     * Create a panel to encode a string.
48
     */
49
    public ManagerDatabase() {
50
        super(new BorderLayout());
51
52
        // First node in tree
53
        AbstractNodeModel nodeModelEmpty = new NodeModelEmpty(I18nViewUtil.valueByKey("DATABASE_EMPTY"));
54
        var root = new DefaultMutableTreeNode(nodeModelEmpty);
55 1 1. <init> : removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE
        I18nViewUtil.addComponentForKey("DATABASE_EMPTY", nodeModelEmpty);
56
57
        this.tree = new TreeDatabase(root);
58 1 1. <init> : removed call to com/jsql/view/swing/tree/TreeDatabase::setName → NO_COVERAGE
        this.tree.setName(ManagerDatabase.TREE_DATABASES);
59 1 1. <init> : removed call to com/jsql/view/swing/util/MediatorHelper::register → NO_COVERAGE
        MediatorHelper.register(this.tree);
60
61 1 1. <init> : removed call to com/jsql/view/swing/tree/TreeDatabase::setCellRenderer → NO_COVERAGE
        this.tree.setCellRenderer(new CellRendererNode());
62 1 1. <init> : removed call to com/jsql/view/swing/tree/TreeDatabase::addMouseListener → NO_COVERAGE
        this.tree.addMouseListener(this.getTreeMouseListener());
63 1 1. <init> : removed call to com/jsql/view/swing/tree/TreeDatabase::addKeyListener → NO_COVERAGE
        this.tree.addKeyListener(this.getTreeKeyListener());
64 1 1. <init> : removed call to com/jsql/view/swing/tree/TreeDatabase::setCellEditor → NO_COVERAGE
        this.tree.setCellEditor(new CellEditorNode());
65 1 1. <init> : removed call to com/jsql/view/swing/tree/TreeDatabase::setEditable → NO_COVERAGE
        this.tree.setEditable(true);  // allows repaint nodes
66 1 1. <init> : removed call to com/jsql/view/swing/tree/TreeDatabase::setShowsRootHandles → NO_COVERAGE
        this.tree.setShowsRootHandles(true);
67 1 1. <init> : removed call to javax/swing/tree/TreeSelectionModel::setSelectionMode → NO_COVERAGE
        this.tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
68 1 1. <init> : removed call to javax/swing/tree/TreeModel::addTreeModelListener → NO_COVERAGE
        this.tree.getModel().addTreeModelListener(new TreeModelProgressListener());  // required to repaint progress bar
69
70 1 1. <init> : removed call to com/jsql/view/swing/manager/ManagerDatabase::add → NO_COVERAGE
        this.add(new JScrollPane(this.tree), BorderLayout.CENTER);
71
    }
72
73
    private KeyAdapter getTreeKeyListener() {
74 1 1. getTreeKeyListener : replaced return value with null for com/jsql/view/swing/manager/ManagerDatabase::getTreeKeyListener → NO_COVERAGE
        return new KeyAdapter() {
75
            @Override
76
            public void keyPressed(KeyEvent e) {
77 1 1. keyPressed : negated conditional → NO_COVERAGE
                if (e.getKeyCode() == KeyEvent.VK_F2) {
78
                    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) ManagerDatabase.this.tree.getLastSelectedPathComponent();
79 1 1. keyPressed : negated conditional → NO_COVERAGE
                    if (treeNode != null) {
80
                        AbstractNodeModel nodeModel = (AbstractNodeModel) treeNode.getUserObject();
81 3 1. keyPressed : negated conditional → NO_COVERAGE
2. keyPressed : negated conditional → NO_COVERAGE
3. keyPressed : negated conditional → NO_COVERAGE
                        if (nodeModel != null && nodeModel.getPanel() != null && !nodeModel.isRunning()) {
82 1 1. keyPressed : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setIsEdited → NO_COVERAGE
                            nodeModel.setIsEdited(true);
83
                        }
84
                    }
85
                }
86
            }
87
        };
88
    }
89
90
    private MouseAdapter getTreeMouseListener() {
91 1 1. getTreeMouseListener : replaced return value with null for com/jsql/view/swing/manager/ManagerDatabase::getTreeMouseListener → NO_COVERAGE
        return new MouseAdapter() {
92
            @Override
93
            public void mousePressed(MouseEvent event) {
94
                int selRow = ManagerDatabase.this.tree.getRowForLocation(event.getX(), event.getY());
95
                TreePath selPath = ManagerDatabase.this.tree.getPathForLocation(event.getX(), event.getY());
96 2 1. mousePressed : negated conditional → NO_COVERAGE
2. mousePressed : negated conditional → NO_COVERAGE
                if (selRow != -1 && event.getClickCount() == 2) {
97
                    // Fix ArrayIndexOutOfBoundsException on collapsePath()
98
                    try {
99 1 1. mousePressed : negated conditional → NO_COVERAGE
                        if (ManagerDatabase.this.tree.isExpanded(selPath)) {
100 1 1. mousePressed : removed call to com/jsql/view/swing/tree/TreeDatabase::collapsePath → NO_COVERAGE
                            ManagerDatabase.this.tree.collapsePath(selPath);
101
                        } else {
102 1 1. mousePressed : removed call to com/jsql/view/swing/tree/TreeDatabase::expandPath → NO_COVERAGE
                            ManagerDatabase.this.tree.expandPath(selPath);
103
                        }
104
                    } catch (ArrayIndexOutOfBoundsException e) {
105
                        LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
106
                    }
107
                }
108
            }
109
        };
110
    }
111
    
112
    private class TreeModelProgressListener implements TreeModelListener {
113
        @Override
114
        public void treeNodesChanged(TreeModelEvent treeModelEvent) {
115 1 1. treeNodesChanged : negated conditional → NO_COVERAGE
            if (treeModelEvent == null) {
116
                return;
117
            }
118
            try {  // Fix #96324: ArrayIndexOutOfBoundsException on firePropertyChange()
119 1 1. treeNodesChanged : removed call to com/jsql/view/swing/tree/TreeDatabase::firePropertyChange → NO_COVERAGE
                ManagerDatabase.this.tree.firePropertyChange(
120
                    JTree.ROOT_VISIBLE_PROPERTY,
121 1 1. treeNodesChanged : negated conditional → NO_COVERAGE
                    !ManagerDatabase.this.tree.isRootVisible(),
122
                    ManagerDatabase.this.tree.isRootVisible()
123
                );
124
            } catch (ArrayIndexOutOfBoundsException e) {
125
                LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
126
            }
127 1 1. treeNodesChanged : removed call to com/jsql/view/swing/tree/TreeDatabase::treeDidChange → NO_COVERAGE
            ManagerDatabase.this.tree.treeDidChange();
128
        }
129
        @Override
130
        public void treeStructureChanged(TreeModelEvent treeModelEvent) {
131
            // Do nothing
132
        }
133
        @Override
134
        public void treeNodesRemoved(TreeModelEvent treeModelEvent) {
135
            // Do nothing
136
        }
137
        @Override
138
        public void treeNodesInserted(TreeModelEvent treeModelEvent) {
139
            // Do nothing
140
        }
141
    }
142
}

Mutations

55

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE

58

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/tree/TreeDatabase::setName → NO_COVERAGE

59

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/util/MediatorHelper::register → NO_COVERAGE

61

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/tree/TreeDatabase::setCellRenderer → NO_COVERAGE

62

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/tree/TreeDatabase::addMouseListener → NO_COVERAGE

63

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/tree/TreeDatabase::addKeyListener → NO_COVERAGE

64

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/tree/TreeDatabase::setCellEditor → NO_COVERAGE

65

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/tree/TreeDatabase::setEditable → NO_COVERAGE

66

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/tree/TreeDatabase::setShowsRootHandles → NO_COVERAGE

67

1.1
Location : <init>
Killed by : none
removed call to javax/swing/tree/TreeSelectionModel::setSelectionMode → NO_COVERAGE

68

1.1
Location : <init>
Killed by : none
removed call to javax/swing/tree/TreeModel::addTreeModelListener → NO_COVERAGE

70

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/manager/ManagerDatabase::add → NO_COVERAGE

74

1.1
Location : getTreeKeyListener
Killed by : none
replaced return value with null for com/jsql/view/swing/manager/ManagerDatabase::getTreeKeyListener → NO_COVERAGE

77

1.1
Location : keyPressed
Killed by : none
negated conditional → NO_COVERAGE

79

1.1
Location : keyPressed
Killed by : none
negated conditional → NO_COVERAGE

81

1.1
Location : keyPressed
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : keyPressed
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : keyPressed
Killed by : none
negated conditional → NO_COVERAGE

82

1.1
Location : keyPressed
Killed by : none
removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::setIsEdited → NO_COVERAGE

91

1.1
Location : getTreeMouseListener
Killed by : none
replaced return value with null for com/jsql/view/swing/manager/ManagerDatabase::getTreeMouseListener → NO_COVERAGE

96

1.1
Location : mousePressed
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : mousePressed
Killed by : none
negated conditional → NO_COVERAGE

99

1.1
Location : mousePressed
Killed by : none
negated conditional → NO_COVERAGE

100

1.1
Location : mousePressed
Killed by : none
removed call to com/jsql/view/swing/tree/TreeDatabase::collapsePath → NO_COVERAGE

102

1.1
Location : mousePressed
Killed by : none
removed call to com/jsql/view/swing/tree/TreeDatabase::expandPath → NO_COVERAGE

115

1.1
Location : treeNodesChanged
Killed by : none
negated conditional → NO_COVERAGE

119

1.1
Location : treeNodesChanged
Killed by : none
removed call to com/jsql/view/swing/tree/TreeDatabase::firePropertyChange → NO_COVERAGE

121

1.1
Location : treeNodesChanged
Killed by : none
negated conditional → NO_COVERAGE

127

1.1
Location : treeNodesChanged
Killed by : none
removed call to com/jsql/view/swing/tree/TreeDatabase::treeDidChange → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.22.1