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