001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.gui; 021 022import java.awt.Component; 023import java.awt.Dimension; 024import java.awt.FontMetrics; 025import java.awt.event.ActionEvent; 026import java.awt.event.MouseAdapter; 027import java.awt.event.MouseEvent; 028import java.util.ArrayList; 029import java.util.Collections; 030import java.util.EventObject; 031import java.util.List; 032 033import javax.swing.AbstractAction; 034import javax.swing.Action; 035import javax.swing.JTable; 036import javax.swing.JTextArea; 037import javax.swing.JTree; 038import javax.swing.KeyStroke; 039import javax.swing.LookAndFeel; 040import javax.swing.table.TableCellEditor; 041import javax.swing.tree.TreePath; 042 043/** 044 * This example shows how to create a simple TreeTable component, 045 * by using a JTree as a renderer (and editor) for the cells in a 046 * particular column in the JTable. 047 * 048 * <a href= 049 * "https://docs.oracle.com/cd/E48246_01/apirefs.1111/e13403/oracle/ide/controls/TreeTableModel.html"> 050 * Original Source Location</a> 051 * 052 * @author Philip Milne 053 * @author Scott Violet 054 * @author Lars Kühne 055 */ 056public class TreeTable extends JTable { 057 private static final long serialVersionUID = -8493693409423365387L; 058 /** A subclass of JTree. */ 059 private final TreeTableCellRenderer tree; 060 /** JTextArea editor. */ 061 private JTextArea editor; 062 /** Line position map. */ 063 private List<Integer> linePositionMap; 064 065 /** 066 * Creates TreeTable base on TreeTableModel. 067 * @param treeTableModel Tree table model 068 */ 069 public TreeTable(ParseTreeTableModel treeTableModel) { 070 071 // Create the tree. It will be used as a renderer and editor. 072 tree = new TreeTableCellRenderer(this, treeTableModel); 073 074 // Install a tableModel representing the visible rows in the tree. 075 setModel(new TreeTableModelAdapter(treeTableModel, tree)); 076 077 // Force the JTable and JTree to share their row selection models. 078 final ListToTreeSelectionModelWrapper selectionWrapper = new 079 ListToTreeSelectionModelWrapper(this); 080 tree.setSelectionModel(selectionWrapper); 081 setSelectionModel(selectionWrapper.getListSelectionModel()); 082 083 // Install the tree editor renderer and editor. 084 setDefaultRenderer(ParseTreeTableModel.class, tree); 085 setDefaultEditor(ParseTreeTableModel.class, new TreeTableCellEditor()); 086 087 // No grid. 088 setShowGrid(false); 089 090 // No intercell spacing 091 setIntercellSpacing(new Dimension(0, 0)); 092 093 // And update the height of the trees row to match that of 094 // the table. 095 if (tree.getRowHeight() < 1) { 096 // Metal looks better like this. 097 setRowHeight(getRowHeight()); 098 } 099 100 setColumnsInitialWidth(); 101 102 final Action expand = new AbstractAction() { 103 private static final long serialVersionUID = -5859674518660156121L; 104 105 @Override 106 public void actionPerformed(ActionEvent event) { 107 expandSelectedNode(); 108 } 109 }; 110 final KeyStroke stroke = KeyStroke.getKeyStroke("ENTER"); 111 final String command = "expand/collapse"; 112 getInputMap().put(stroke, command); 113 getActionMap().put(command, expand); 114 115 addMouseListener(new MouseAdapter() { 116 @Override 117 public void mouseClicked(MouseEvent event) { 118 if (event.getClickCount() == 2) { 119 expandSelectedNode(); 120 } 121 } 122 }); 123 } 124 125 /** 126 * Do expansion of a tree node. 127 */ 128 private void expandSelectedNode() { 129 final TreePath selected = tree.getSelectionPath(); 130 makeCodeSelection(); 131 132 if (tree.isExpanded(selected)) { 133 tree.collapsePath(selected); 134 } 135 else { 136 tree.expandPath(selected); 137 } 138 tree.setSelectionPath(selected); 139 } 140 141 /** 142 * Make selection of code in a text area. 143 */ 144 private void makeCodeSelection() { 145 new CodeSelector(tree.getLastSelectedPathComponent(), editor, linePositionMap).select(); 146 } 147 148 /** 149 * Set initial value of width for columns in table. 150 */ 151 private void setColumnsInitialWidth() { 152 final FontMetrics fontMetrics = getFontMetrics(getFont()); 153 // Six character string to contain "Column" column. 154 final int widthOfSixCharacterString = fontMetrics.stringWidth("XXXXXX"); 155 // Padding must be added to width for columns to make them fully 156 // visible in table header. 157 final int padding = 10; 158 final int widthOfColumnContainingSixCharacterString = 159 widthOfSixCharacterString + padding; 160 getColumn("Line").setMaxWidth(widthOfColumnContainingSixCharacterString); 161 getColumn("Column").setMaxWidth(widthOfColumnContainingSixCharacterString); 162 final int preferredTreeColumnWidth = 163 Math.toIntExact(Math.round(getPreferredSize().getWidth() * 0.6)); 164 getColumn("Tree").setPreferredWidth(preferredTreeColumnWidth); 165 // Twenty eight character string to contain "Type" column 166 final int widthOfTwentyEightCharacterString = 167 fontMetrics.stringWidth("XXXXXXXXXXXXXXXXXXXXXXXXXXXX"); 168 final int preferredTypeColumnWidth = widthOfTwentyEightCharacterString + padding; 169 getColumn("Type").setPreferredWidth(preferredTypeColumnWidth); 170 } 171 172 /** 173 * Overridden to message super and forward the method to the tree. 174 * Since the tree is not actually in the component hierarchy it will 175 * never receive this unless we forward it in this manner. 176 */ 177 @Override 178 public void updateUI() { 179 super.updateUI(); 180 if (tree != null) { 181 tree.updateUI(); 182 } 183 // Use the tree's default foreground and background colors in the 184 // table. 185 LookAndFeel.installColorsAndFont(this, "Tree.background", 186 "Tree.foreground", "Tree.font"); 187 } 188 189 /* Workaround for BasicTableUI anomaly. Make sure the UI never tries to 190 * paint the editor. The UI currently uses different techniques to 191 * paint the renderers and editors and overriding setBounds() below 192 * is not the right thing to do for an editor. Returning -1 for the 193 * editing row in this case, ensures the editor is never painted. 194 */ 195 @Override 196 public int getEditingRow() { 197 int rowIndex = -1; 198 final Class<?> editingClass = getColumnClass(editingColumn); 199 if (editingClass != ParseTreeTableModel.class) { 200 rowIndex = editingRow; 201 } 202 return rowIndex; 203 } 204 205 /** 206 * Overridden to pass the new rowHeight to the tree. 207 */ 208 @Override 209 public final void setRowHeight(int newRowHeight) { 210 super.setRowHeight(newRowHeight); 211 if (tree != null && tree.getRowHeight() != newRowHeight) { 212 tree.setRowHeight(getRowHeight()); 213 } 214 } 215 216 /** 217 * Returns tree. 218 * @return the tree that is being shared between the model. 219 */ 220 public JTree getTree() { 221 return tree; 222 } 223 224 /** 225 * Sets text area editor. 226 * @param textArea JTextArea component. 227 */ 228 public void setEditor(JTextArea textArea) { 229 editor = textArea; 230 } 231 232 /** 233 * Sets line position map. 234 * @param linePositionMap Line position map. 235 */ 236 public void setLinePositionMap(List<Integer> linePositionMap) { 237 final List<Integer> copy = new ArrayList<>(linePositionMap); 238 this.linePositionMap = Collections.unmodifiableList(copy); 239 } 240 241 /** 242 * TreeTableCellEditor implementation. Component returned is the 243 * JTree. 244 */ 245 private class TreeTableCellEditor extends BaseCellEditor implements 246 TableCellEditor { 247 @Override 248 public Component getTableCellEditorComponent(JTable table, 249 Object value, 250 boolean isSelected, 251 int row, int column) { 252 return tree; 253 } 254 255 /** 256 * Overridden to return false, and if the event is a mouse event 257 * it is forwarded to the tree. 258 * 259 * <p>The behavior for this is debatable, and should really be offered 260 * as a property. By returning false, all keyboard actions are 261 * implemented in terms of the table. By returning true, the 262 * tree would get a chance to do something with the keyboard 263 * events. For the most part this is ok. But for certain keys, 264 * such as left/right, the tree will expand/collapse where as 265 * the table focus should really move to a different column. Page 266 * up/down should also be implemented in terms of the table. 267 * By returning false this also has the added benefit that clicking 268 * outside of the bounds of the tree node, but still in the tree 269 * column will select the row, whereas if this returned true 270 * that wouldn't be the case. 271 * 272 * <p>By returning false we are also enforcing the policy that 273 * the tree will never be editable (at least by a key sequence). 274 * 275 * @see TableCellEditor 276 */ 277 @Override 278 public boolean isCellEditable(EventObject event) { 279 if (event instanceof MouseEvent) { 280 for (int counter = getColumnCount() - 1; counter >= 0; 281 counter--) { 282 if (getColumnClass(counter) == ParseTreeTableModel.class) { 283 final MouseEvent mouseEvent = (MouseEvent) event; 284 final MouseEvent newMouseEvent = new MouseEvent(tree, mouseEvent.getID(), 285 mouseEvent.getWhen(), mouseEvent.getModifiers(), 286 mouseEvent.getX() - getCellRect(0, counter, true).x, 287 mouseEvent.getY(), mouseEvent.getClickCount(), 288 mouseEvent.isPopupTrigger()); 289 tree.dispatchEvent(newMouseEvent); 290 break; 291 } 292 } 293 } 294 295 return false; 296 } 297 } 298}