001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2015 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.event.ActionEvent; 025import java.awt.event.MouseEvent; 026import java.util.EventObject; 027import java.util.List; 028 029import javax.swing.AbstractAction; 030import javax.swing.Action; 031import javax.swing.JTable; 032import javax.swing.JTextArea; 033import javax.swing.JTree; 034import javax.swing.KeyStroke; 035import javax.swing.LookAndFeel; 036import javax.swing.table.TableCellEditor; 037import javax.swing.tree.TreePath; 038 039import com.google.common.collect.ImmutableList; 040import com.puppycrawl.tools.checkstyle.api.DetailAST; 041 042/** 043 * This example shows how to create a simple JTreeTable component, 044 * by using a JTree as a renderer (and editor) for the cells in a 045 * particular column in the JTable. 046 * 047 * <a href= 048 * "https://docs.oracle.com/cd/E48246_01/apirefs.1111/e13403/oracle/ide/controls/TreeTableModel.html"> 049 * Original Source Location</a> 050 * 051 * @author Philip Milne 052 * @author Scott Violet 053 * @author Lars Kühne 054 */ 055public class JTreeTable extends JTable { 056 private static final long serialVersionUID = -8493693409423365387L; 057 /** A subclass of JTree. */ 058 private final TreeTableCellRenderer tree; 059 /** JTextArea editor. */ 060 private JTextArea editor; 061 /** Line position map. */ 062 private List<Integer> linePositionMap; 063 064 /** 065 * Creates JTreeTable base on TreeTableModel. 066 * @param treeTableModel Tree table model 067 */ 068 public JTreeTable(TreeTableModel treeTableModel) { 069 070 // Create the tree. It will be used as a renderer and editor. 071 tree = new TreeTableCellRenderer(this, treeTableModel); 072 073 // Install a tableModel representing the visible rows in the tree. 074 setModel(new TreeTableModelAdapter(treeTableModel, tree)); 075 076 // Force the JTable and JTree to share their row selection models. 077 final ListToTreeSelectionModelWrapper selectionWrapper = new 078 ListToTreeSelectionModelWrapper(this); 079 tree.setSelectionModel(selectionWrapper); 080 setSelectionModel(selectionWrapper.getListSelectionModel()); 081 082 // Install the tree editor renderer and editor. 083 setDefaultRenderer(TreeTableModel.class, tree); 084 setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor()); 085 086 // No grid. 087 setShowGrid(false); 088 089 // No intercell spacing 090 setIntercellSpacing(new Dimension(0, 0)); 091 092 // And update the height of the trees row to match that of 093 // the table. 094 if (tree.getRowHeight() < 1) { 095 // Metal looks better like this. 096 setRowHeight(getRowHeight()); 097 } 098 099 final Action expand = new AbstractAction() { 100 private static final long serialVersionUID = -5859674518660156121L; 101 102 @Override 103 public void actionPerformed(ActionEvent event) { 104 final TreePath selected = tree.getSelectionPath(); 105 final DetailAST ast = (DetailAST) selected.getLastPathComponent(); 106 new CodeSelector(ast, editor, linePositionMap).select(); 107 108 if (tree.isExpanded(selected)) { 109 tree.collapsePath(selected); 110 } 111 else { 112 tree.expandPath(selected); 113 } 114 tree.setSelectionPath(selected); 115 } 116 }; 117 final KeyStroke stroke = KeyStroke.getKeyStroke("ENTER"); 118 final String command = "expand/collapse"; 119 getInputMap().put(stroke, command); 120 getActionMap().put(command, expand); 121 } 122 123 /** 124 * Overridden to message super and forward the method to the tree. 125 * Since the tree is not actually in the component hierarchy it will 126 * never receive this unless we forward it in this manner. 127 */ 128 @Override 129 public void updateUI() { 130 super.updateUI(); 131 if (tree != null) { 132 tree.updateUI(); 133 } 134 // Use the tree's default foreground and background colors in the 135 // table. 136 LookAndFeel.installColorsAndFont(this, "Tree.background", 137 "Tree.foreground", "Tree.font"); 138 } 139 140 /* Workaround for BasicTableUI anomaly. Make sure the UI never tries to 141 * paint the editor. The UI currently uses different techniques to 142 * paint the renderers and editors and overriding setBounds() below 143 * is not the right thing to do for an editor. Returning -1 for the 144 * editing row in this case, ensures the editor is never painted. 145 */ 146 @Override 147 public int getEditingRow() { 148 final Class<?> editingClass = getColumnClass(editingColumn); 149 150 if (editingClass == TreeTableModel.class) { 151 return -1; 152 } 153 else { 154 return editingRow; 155 } 156 } 157 158 /** 159 * Overridden to pass the new rowHeight to the tree. 160 */ 161 @Override 162 public final void setRowHeight(int newRowHeight) { 163 super.setRowHeight(newRowHeight); 164 if (tree != null && tree.getRowHeight() != newRowHeight) { 165 tree.setRowHeight(getRowHeight()); 166 } 167 } 168 169 /** 170 * @return the tree that is being shared between the model. 171 */ 172 public JTree getTree() { 173 return tree; 174 } 175 176 /** 177 * Sets text area editor. 178 * @param textArea JTextArea component. 179 */ 180 public void setEditor(JTextArea textArea) { 181 editor = textArea; 182 } 183 184 /** 185 * Sets line position map. 186 * @param linePositionMap Line position map. 187 */ 188 public void setLinePositionMap(List<Integer> linePositionMap) { 189 this.linePositionMap = ImmutableList.copyOf(linePositionMap); 190 } 191 192 /** 193 * TreeTableCellEditor implementation. Component returned is the 194 * JTree. 195 */ 196 private class TreeTableCellEditor extends BaseCellEditor implements 197 TableCellEditor { 198 @Override 199 public Component getTableCellEditorComponent(JTable table, 200 Object value, 201 boolean isSelected, 202 int row, int column) { 203 return tree; 204 } 205 206 /** 207 * Overridden to return false, and if the event is a mouse event 208 * it is forwarded to the tree. 209 * 210 * <p>The behavior for this is debatable, and should really be offered 211 * as a property. By returning false, all keyboard actions are 212 * implemented in terms of the table. By returning true, the 213 * tree would get a chance to do something with the keyboard 214 * events. For the most part this is ok. But for certain keys, 215 * such as left/right, the tree will expand/collapse where as 216 * the table focus should really move to a different column. Page 217 * up/down should also be implemented in terms of the table. 218 * By returning false this also has the added benefit that clicking 219 * outside of the bounds of the tree node, but still in the tree 220 * column will select the row, whereas if this returned true 221 * that wouldn't be the case. 222 * 223 * <p>By returning false we are also enforcing the policy that 224 * the tree will never be editable (at least by a key sequence). 225 * 226 * @see TableCellEditor 227 */ 228 @Override 229 public boolean isCellEditable(EventObject event) { 230 if (event instanceof MouseEvent) { 231 for (int counter = getColumnCount() - 1; counter >= 0; 232 counter--) { 233 if (getColumnClass(counter) == TreeTableModel.class) { 234 final MouseEvent mouseEvent = (MouseEvent) event; 235 final MouseEvent newMouseEvent = new MouseEvent(tree, mouseEvent.getID(), 236 mouseEvent.getWhen(), mouseEvent.getModifiers(), 237 mouseEvent.getX() - getCellRect(0, counter, true).x, 238 mouseEvent.getY(), mouseEvent.getClickCount(), 239 mouseEvent.isPopupTrigger()); 240 tree.dispatchEvent(newMouseEvent); 241 break; 242 } 243 } 244 } 245 246 return false; 247 } 248 } 249}