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