1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.gui;
21
22 import java.io.Serial;
23
24 import javax.swing.ListSelectionModel;
25 import javax.swing.tree.DefaultTreeSelectionModel;
26 import javax.swing.tree.TreePath;
27
28 /**
29 * ListToTreeSelectionModelWrapper extends DefaultTreeSelectionModel
30 * to listen for changes in the ListSelectionModel it maintains. Once
31 * a change in the ListSelectionModel happens, the paths are updated
32 * in the DefaultTreeSelectionModel.
33 *
34 */
35 public final class ListToTreeSelectionModelWrapper extends DefaultTreeSelectionModel {
36
37 /** A unique serial version identifier. */
38 @Serial
39 private static final long serialVersionUID = 2267930983939339510L;
40 /** TreeTable to perform updates on. */
41 private final TreeTable treeTable;
42 /** Set to true when we are updating the ListSelectionModel. */
43 private boolean updatingListSelectionModel;
44
45 /**
46 * Constructor to initialise treeTable.
47 *
48 * @param treeTable TreeTable to perform updates on.
49 */
50 /* package */ ListToTreeSelectionModelWrapper(TreeTable treeTable) {
51 this.treeTable = treeTable;
52 getListSelectionModel().addListSelectionListener(event -> {
53 updateSelectedPathsFromSelectedRows();
54 });
55 }
56
57 /**
58 * Returns the list selection model. ListToTreeSelectionModelWrapper
59 * listens for changes to this model and updates the selected paths accordingly.
60 *
61 * @return the list selection model
62 */
63 public ListSelectionModel getListSelectionModel() {
64 return listSelectionModel;
65 }
66
67 /**
68 * This is overridden to set {@code updatingListSelectionModel}
69 * and message super. This is the only place DefaultTreeSelectionModel
70 * alters the ListSelectionModel.
71 */
72 @Override
73 public void resetRowSelection() {
74 if (!updatingListSelectionModel) {
75 updatingListSelectionModel = true;
76 try {
77 super.resetRowSelection();
78 }
79 finally {
80 updatingListSelectionModel = false;
81 }
82 }
83 // Notice how we don't message super if
84 // updatingListSelectionModel is true. If
85 // updatingListSelectionModel is true, it implies the
86 // ListSelectionModel has already been updated and the
87 // paths are the only thing that needs to be updated.
88 }
89
90 /**
91 * If {@code updatingListSelectionModel} is false, this will
92 * reset the selected paths from the selected rows in the list selection model.
93 */
94 private void updateSelectedPathsFromSelectedRows() {
95 if (!updatingListSelectionModel) {
96 updatingListSelectionModel = true;
97 try {
98 // This is way expensive, ListSelectionModel needs an
99 // enumerator for iterating.
100 final int min = listSelectionModel.getMinSelectionIndex();
101 final int max = listSelectionModel.getMaxSelectionIndex();
102
103 clearSelection();
104 if (min != -1 && max != -1) {
105 for (int counter = min; counter <= max; counter++) {
106 updateSelectedPathIfRowIsSelected(counter);
107 }
108 }
109 }
110 finally {
111 updatingListSelectionModel = false;
112 }
113 }
114 }
115
116 /**
117 * If the row at given index is selected, selected paths are updated.
118 *
119 * @param counter number of row.
120 */
121 private void updateSelectedPathIfRowIsSelected(int counter) {
122 if (listSelectionModel.isSelectedIndex(counter)) {
123 final TreePath selPath = treeTable.getTree().getPathForRow(counter);
124
125 if (selPath != null) {
126 addSelectionPath(selPath);
127 }
128 }
129 }
130
131 }