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 javax.swing.event.EventListenerList; 023import javax.swing.event.TreeModelEvent; 024import javax.swing.event.TreeModelListener; 025import javax.swing.tree.TreeModel; 026import javax.swing.tree.TreePath; 027 028import com.puppycrawl.tools.checkstyle.api.DetailAST; 029import com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode; 030 031/** 032 * The model that backs the parse tree in the GUI. 033 * 034 * @author Lars Kühne 035 */ 036public class ParseTreeTableModel implements TreeModel { 037 /** Presentation model. */ 038 private final ParseTreeTablePModel pModel; 039 040 /** 041 * A list of event listeners for the tree model. 042 */ 043 private final EventListenerList listenerList = new EventListenerList(); 044 045 /** 046 * @param parseTree DetailAST parse tree. 047 */ 048 public ParseTreeTableModel(DetailAST parseTree) { 049 pModel = new ParseTreeTablePModel(parseTree); 050 setParseTree(parseTree); 051 } 052 053 /** 054 * Sets parse tree. 055 * @param parseTree DetailAST parse tree. 056 */ 057 protected final void setParseTree(DetailAST parseTree) { 058 pModel.setParseTree(parseTree); 059 final Object[] path = {pModel.getRoot()}; 060 // no need to setup remaining info, as the call results in a 061 // table structure changed event anyway - we just pass nulls 062 fireTreeStructureChanged(this, path, null, (Object[]) null); 063 } 064 065 /** 066 * Set parse mode. 067 * @param mode ParseMode enum 068 */ 069 protected void setParseMode(ParseMode mode) { 070 pModel.setParseMode(mode); 071 } 072 073 /** 074 * @return the number of available column. 075 */ 076 public int getColumnCount() { 077 return pModel.getColumnCount(); 078 } 079 080 /** 081 * @param column the column number 082 * @return the name for column number {@code column}. 083 */ 084 public String getColumnName(int column) { 085 return pModel.getColumnName(column); 086 } 087 088 /** 089 * @param column the column number 090 * @return the type for column number {@code column}. 091 */ 092 // -@cs[ForbidWildcardAsReturnType] We need to satisfy javax.swing.table.AbstractTableModel 093 // public Class<?> getColumnClass(int columnIndex) {...} 094 public Class<?> getColumnClass(int column) { 095 return pModel.getColumnClass(column); 096 } 097 098 /** 099 * @param node the node 100 * @param column the column number 101 * @return the value to be displayed for node {@code node}, 102 * at column number {@code column}. 103 */ 104 public Object getValueAt(Object node, int column) { 105 return pModel.getValueAt(node, column); 106 } 107 108 @Override 109 public Object getChild(Object parent, int index) { 110 return pModel.getChild(parent, index); 111 } 112 113 @Override 114 public int getChildCount(Object parent) { 115 return pModel.getChildCount(parent); 116 } 117 118 @Override 119 public void valueForPathChanged(TreePath path, Object newValue) { 120 // No Code, as tree is read-only 121 } 122 123 @Override 124 public Object getRoot() { 125 return pModel.getRoot(); 126 } 127 128 @Override 129 public boolean isLeaf(Object node) { 130 return pModel.isLeaf(node); 131 } 132 133 // This is not called in the JTree's default mode: use a naive implementation. 134 @Override 135 public int getIndexOfChild(Object parent, Object child) { 136 return pModel.getIndexOfChild(parent, child); 137 } 138 139 @Override 140 public void addTreeModelListener(TreeModelListener listener) { 141 listenerList.add(TreeModelListener.class, listener); 142 } 143 144 @Override 145 public void removeTreeModelListener(TreeModelListener listener) { 146 listenerList.remove(TreeModelListener.class, listener); 147 } 148 149 /** 150 * Notify all listeners that have registered interest for 151 * 'tree structure changed' event. The event instance 152 * is lazily created using the parameters passed into 153 * the fire method. 154 * @param source The Object responsible for generating the event. 155 * @param path An array of Object identifying the path to the parent of the modified items. 156 * @param childIndices An array of int that specifies the index values of the removed items. 157 * @param children An array of Object containing the inserted, removed, or changed objects. 158 * @see EventListenerList 159 */ 160 private void fireTreeStructureChanged(Object source, Object[] path, 161 int[] childIndices, 162 Object... children) { 163 // Guaranteed to return a non-null array 164 final Object[] listeners = listenerList.getListenerList(); 165 TreeModelEvent event = null; 166 // Process the listeners last to first, notifying 167 // those that are interested in this event 168 for (int i = listeners.length - 2; i >= 0; i -= 2) { 169 if (listeners[i] == TreeModelListener.class) { 170 // Lazily create the event: 171 if (event == null) { 172 event = new TreeModelEvent(source, path, 173 childIndices, children); 174 } 175 ((TreeModelListener) listeners[i + 1]).treeStructureChanged(event); 176 } 177 } 178 } 179 180 /** 181 * Indicates whether the the value for node {@code node}, 182 * at column number {@code column} is editable. 183 * 184 * @param column the column number 185 * @return true if editable 186 */ 187 public boolean isCellEditable(int column) { 188 return pModel.isCellEditable(column); 189 } 190}