1 | /******************************************************************************* | |
2 | * Copyhacked (H) 2012-2020. | |
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 about 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; | |
12 | ||
13 | import com.jsql.util.LogLevelUtil; | |
14 | import com.jsql.view.swing.tree.model.AbstractNodeModel; | |
15 | import com.jsql.view.swing.util.MediatorHelper; | |
16 | import org.apache.logging.log4j.LogManager; | |
17 | import org.apache.logging.log4j.Logger; | |
18 | ||
19 | import javax.swing.*; | |
20 | import javax.swing.event.TreeSelectionEvent; | |
21 | import javax.swing.event.TreeSelectionListener; | |
22 | import javax.swing.tree.DefaultMutableTreeNode; | |
23 | import javax.swing.tree.TreeCellEditor; | |
24 | import javax.swing.tree.TreePath; | |
25 | import java.awt.*; | |
26 | import java.awt.event.MouseEvent; | |
27 | import java.awt.event.MouseListener; | |
28 | ||
29 | /** | |
30 | * Tree cell editor responsible for mouse action on nodes. | |
31 | */ | |
32 | public class CellEditorNode extends AbstractCellEditor implements TreeCellEditor, TreeSelectionListener, MouseListener { | |
33 | | |
34 | /** | |
35 | * Log4j logger sent to view. | |
36 | */ | |
37 | private static final Logger LOGGER = LogManager.getRootLogger(); | |
38 | ||
39 | /** | |
40 | * Renderer for nodes included JPanel, button, checkbox, icons... | |
41 | */ | |
42 | private final CellRendererNode defaultTreeRenderer; | |
43 | ||
44 | /** | |
45 | * Value contained in the editor. | |
46 | * Returned by getCellEditorValue(). | |
47 | */ | |
48 | private transient AbstractNodeModel nodeModel; | |
49 | ||
50 | /** | |
51 | * Build editor, add tree and mouse listener. | |
52 | */ | |
53 | public CellEditorNode() { | |
54 | | |
55 | this.defaultTreeRenderer = new CellRendererNode(); | |
56 |
1
1. <init> : removed call to com/jsql/view/swing/tree/TreeDatabase::addTreeSelectionListener → NO_COVERAGE |
MediatorHelper.treeDatabase().addTreeSelectionListener(this); |
57 |
1
1. <init> : removed call to com/jsql/view/swing/tree/TreeDatabase::addMouseListener → NO_COVERAGE |
MediatorHelper.treeDatabase().addMouseListener(this); |
58 | } | |
59 | ||
60 | @Override | |
61 | public Component getTreeCellEditorComponent( | |
62 | JTree tree, | |
63 | Object nodeRenderer, | |
64 | boolean selected, | |
65 | boolean expanded, | |
66 | boolean leaf, | |
67 | int row | |
68 | ) { | |
69 | ||
70 | var componentRenderer = this.defaultTreeRenderer.getTreeCellRendererComponent( | |
71 | tree, nodeRenderer, true, expanded, leaf, row, true | |
72 | ); | |
73 | ||
74 | final DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) nodeRenderer; | |
75 | var currentNodeModel = currentNode.getUserObject(); | |
76 | | |
77 | try { | |
78 | this.nodeModel = (AbstractNodeModel) currentNodeModel; | |
79 | | |
80 |
1
1. getTreeCellEditorComponent : negated conditional → NO_COVERAGE |
if (componentRenderer instanceof JCheckBox) { |
81 |
1
1. getTreeCellEditorComponent : removed call to javax/swing/JCheckBox::addActionListener → NO_COVERAGE |
((JCheckBox) componentRenderer).addActionListener( |
82 | new ActionCheckUncheck(this.nodeModel, currentNode) | |
83 | ); | |
84 | } | |
85 | } catch (Exception e) { | |
86 | LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e); | |
87 | } | |
88 | ||
89 |
1
1. getTreeCellEditorComponent : replaced return value with null for com/jsql/view/swing/tree/CellEditorNode::getTreeCellEditorComponent → NO_COVERAGE |
return componentRenderer; |
90 | } | |
91 | ||
92 | @Override | |
93 | public Object getCellEditorValue() { | |
94 |
1
1. getCellEditorValue : replaced return value with null for com/jsql/view/swing/tree/CellEditorNode::getCellEditorValue → NO_COVERAGE |
return this.nodeModel; |
95 | } | |
96 | | |
97 | @Override | |
98 | public void valueChanged(TreeSelectionEvent treeSelectionEvent) { | |
99 | | |
100 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) MediatorHelper.treeDatabase().getLastSelectedPathComponent(); | |
101 | ||
102 | // Get rid of java.lang.NullPointerException | |
103 |
1
1. valueChanged : negated conditional → NO_COVERAGE |
if (node == null) { |
104 | return; | |
105 | } | |
106 | ||
107 |
1
1. valueChanged : negated conditional → NO_COVERAGE |
if (node.getUserObject() instanceof AbstractNodeModel) { |
108 | | |
109 | AbstractNodeModel dataModel = (AbstractNodeModel) node.getUserObject(); | |
110 |
1
1. valueChanged : negated conditional → NO_COVERAGE |
if (!dataModel.isLoaded()) { |
111 |
1
1. valueChanged : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::runAction → NO_COVERAGE |
dataModel.runAction(); |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | /** | |
117 | * Fix compatibility issue with right click on Linux. | |
118 | * @param mouseEvent Mouse event | |
119 | */ | |
120 | private void showPopup(MouseEvent mouseEvent) { | |
121 | | |
122 |
1
1. showPopup : negated conditional → NO_COVERAGE |
if (!mouseEvent.isPopupTrigger()) { |
123 | return; | |
124 | } | |
125 | | |
126 | JTree tree = (JTree) mouseEvent.getSource(); | |
127 | TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY()); | |
128 | | |
129 |
1
1. showPopup : negated conditional → NO_COVERAGE |
if (path == null) { |
130 | return; | |
131 | } | |
132 | ||
133 | DefaultMutableTreeNode currentTableNode = (DefaultMutableTreeNode) path.getLastPathComponent(); | |
134 | ||
135 |
1
1. showPopup : negated conditional → NO_COVERAGE |
if (currentTableNode.getUserObject() instanceof AbstractNodeModel) { |
136 | | |
137 | AbstractNodeModel currentTableModel = (AbstractNodeModel) currentTableNode.getUserObject(); | |
138 | | |
139 |
1
1. showPopup : negated conditional → NO_COVERAGE |
if (currentTableModel.isPopupDisplayable()) { |
140 |
1
1. showPopup : removed call to com/jsql/view/swing/tree/model/AbstractNodeModel::showPopup → NO_COVERAGE |
currentTableModel.showPopup(currentTableNode, path, mouseEvent); |
141 | } | |
142 | } | |
143 | } | |
144 | ||
145 | @Override | |
146 | public void mousePressed(MouseEvent e) { | |
147 |
1
1. mousePressed : removed call to com/jsql/view/swing/tree/CellEditorNode::showPopup → NO_COVERAGE |
this.showPopup(e); |
148 | } | |
149 | | |
150 | @Override | |
151 | public void mouseReleased(MouseEvent e) { | |
152 |
1
1. mouseReleased : removed call to com/jsql/view/swing/tree/CellEditorNode::showPopup → NO_COVERAGE |
this.showPopup(e); |
153 | } | |
154 | ||
155 | @Override | |
156 | public void mouseClicked(MouseEvent e) { | |
157 | // Do nothing | |
158 | } | |
159 | | |
160 | @Override | |
161 | public void mouseEntered(MouseEvent e) { | |
162 | // Do nothing | |
163 | } | |
164 | | |
165 | @Override | |
166 | public void mouseExited(MouseEvent e) { | |
167 | // Do nothing | |
168 | } | |
169 | } | |
Mutations | ||
56 |
1.1 |
|
57 |
1.1 |
|
80 |
1.1 |
|
81 |
1.1 |
|
89 |
1.1 |
|
94 |
1.1 |
|
103 |
1.1 |
|
107 |
1.1 |
|
110 |
1.1 |
|
111 |
1.1 |
|
122 |
1.1 |
|
129 |
1.1 |
|
135 |
1.1 |
|
139 |
1.1 |
|
140 |
1.1 |
|
147 |
1.1 |
|
152 |
1.1 |