Issue #2609: Drop abstract class and interface in gui package

This commit is contained in:
Vladislav Lisetskiy 2015-12-08 09:07:24 +03:00 committed by Roman Ivanov
parent 6ca2d5279e
commit af4ecbf9b5
6 changed files with 122 additions and 328 deletions

View File

@ -1,233 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2015 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.gui;
import javax.swing.event.EventListenerList;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
/**
* An abstract implementation of the TreeTableModel interface, handling
* the list of listeners.
*
* <a href=
* "https://docs.oracle.com/cd/E48246_01/apirefs.1111/e13403/oracle/ide/controls/TreeTableModel.html">
* Original&nbsp;Source&nbsp;Location</a>
*
* @author Philip Milne
*/
public abstract class AbstractTreeTableModel implements TreeTableModel {
/**
* The root node of the tree table model.
*/
private final Object root;
/**
* A list of event listeners for the tree model.
*/
private final EventListenerList listenerList = new EventListenerList();
/**
* Initializes the root node for the tree table model.
*
* @param root Root node.
*/
AbstractTreeTableModel(Object root) {
this.root = root;
}
//
// Default implementations for methods in the TreeModel interface.
//
@Override
public Object getRoot() {
return root;
}
@Override
public boolean isLeaf(Object node) {
return getChildCount(node) == 0;
}
// This is not called in the JTree's default mode: use a naive implementation.
@Override
public int getIndexOfChild(Object parent, Object child) {
for (int i = 0; i < getChildCount(parent); i++) {
if (getChild(parent, i).equals(child)) {
return i;
}
}
return -1;
}
@Override
public void addTreeModelListener(TreeModelListener listener) {
listenerList.add(TreeModelListener.class, listener);
}
@Override
public void removeTreeModelListener(TreeModelListener listener) {
listenerList.remove(TreeModelListener.class, listener);
}
/**
* Notifies all listeners that have registered interest for
* 'tree nodes changed' event. The event instance
* is lazily created using the parameters passed into
* the fire method.
* @param source The Object responsible for generating the event.
* @param path An array of Object identifying the path to the parent of the modified items.
* @param childIndices An array of int that specifies the index values of the removed items.
* @param children An array of Object containing the inserted, removed, or changed objects.
* @see EventListenerList
*/
protected void fireTreeNodesChanged(Object source, Object[] path,
int[] childIndices,
Object... children) {
// Guaranteed to return a non-null array
final Object[] listeners = listenerList.getListenerList();
TreeModelEvent event = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == TreeModelListener.class) {
// Lazily create the event:
if (event == null) {
event = new TreeModelEvent(source, path,
childIndices, children);
}
((TreeModelListener) listeners[i + 1]).treeNodesChanged(event);
}
}
}
/**
* Notify all listeners that have registered interest for
* 'tree nodes inserted' event. The event instance
* is lazily created using the parameters passed into
* the fire method.
* @param source The Object responsible for generating the event.
* @param path An array of Object identifying the path to the parent of the modified items.
* @param childIndices An array of int that specifies the index values of the removed items.
* @param children An array of Object containing the inserted, removed, or changed objects.
* @see EventListenerList
*/
protected void fireTreeNodesInserted(Object source, Object[] path,
int[] childIndices,
Object... children) {
// Guaranteed to return a non-null array
final Object[] listeners = listenerList.getListenerList();
TreeModelEvent event = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == TreeModelListener.class) {
// Lazily create the event:
if (event == null) {
event = new TreeModelEvent(source, path,
childIndices, children);
}
((TreeModelListener) listeners[i + 1]).treeNodesInserted(event);
}
}
}
/**
* Notify all listeners that have registered interest for
* 'tree nodes removed' event. The event instance
* is lazily created using the parameters passed into
* the fire method.
* @param source The Object responsible for generating the event.
* @param path An array of Object identifying the path to the parent of the modified items.
* @param childIndices An array of int that specifies the index values of the removed items.
* @param children An array of Object containing the inserted, removed, or changed objects.
* @see EventListenerList
*/
protected void fireTreeNodesRemoved(Object source, Object[] path,
int[] childIndices,
Object... children) {
// Guaranteed to return a non-null array
final Object[] listeners = listenerList.getListenerList();
TreeModelEvent event = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == TreeModelListener.class) {
// Lazily create the event:
if (event == null) {
event = new TreeModelEvent(source, path,
childIndices, children);
}
((TreeModelListener) listeners[i + 1]).treeNodesRemoved(event);
}
}
}
/**
* Notify all listeners that have registered interest for
* 'tree structure changed' event. The event instance
* is lazily created using the parameters passed into
* the fire method.
* @param source The Object responsible for generating the event.
* @param path An array of Object identifying the path to the parent of the modified items.
* @param childIndices An array of int that specifies the index values of the removed items.
* @param children An array of Object containing the inserted, removed, or changed objects.
* @see EventListenerList
*/
void fireTreeStructureChanged(Object source, Object[] path,
int[] childIndices,
Object... children) {
// Guaranteed to return a non-null array
final Object[] listeners = listenerList.getListenerList();
TreeModelEvent event = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == TreeModelListener.class) {
// Lazily create the event:
if (event == null) {
event = new TreeModelEvent(source, path,
childIndices, children);
}
((TreeModelListener) listeners[i + 1]).treeStructureChanged(event);
}
}
}
//
// Default implementations for methods in the TreeTableModel interface.
//
@Override
public Class<?> getColumnClass(int column) {
return Object.class;
}
/** By default, make the column with the Tree in it the only editable one.
* Making this column editable causes the JTable to forward mouse
* and keyboard events in the Tree column to the underlying JTree.
*/
@Override
public boolean isCellEditable(int column) {
return getColumnClass(column) == TreeTableModel.class;
}
}

View File

@ -65,7 +65,7 @@ public class JTreeTable extends JTable {
* Creates JTreeTable base on TreeTableModel.
* @param treeTableModel Tree table model
*/
public JTreeTable(TreeTableModel treeTableModel) {
public JTreeTable(ParseTreeTableModel treeTableModel) {
// Create the tree. It will be used as a renderer and editor.
tree = new TreeTableCellRenderer(this, treeTableModel);
@ -80,8 +80,8 @@ public class JTreeTable extends JTable {
setSelectionModel(selectionWrapper.getListSelectionModel());
// Install the tree editor renderer and editor.
setDefaultRenderer(TreeTableModel.class, tree);
setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor());
setDefaultRenderer(ParseTreeTableModel.class, tree);
setDefaultEditor(ParseTreeTableModel.class, new TreeTableCellEditor());
// No grid.
setShowGrid(false);
@ -147,7 +147,7 @@ public class JTreeTable extends JTable {
public int getEditingRow() {
final Class<?> editingClass = getColumnClass(editingColumn);
if (editingClass == TreeTableModel.class) {
if (editingClass == ParseTreeTableModel.class) {
return -1;
}
else {
@ -230,7 +230,7 @@ public class JTreeTable extends JTable {
if (event instanceof MouseEvent) {
for (int counter = getColumnCount() - 1; counter >= 0;
counter--) {
if (getColumnClass(counter) == TreeTableModel.class) {
if (getColumnClass(counter) == ParseTreeTableModel.class) {
final MouseEvent mouseEvent = (MouseEvent) event;
final MouseEvent newMouseEvent = new MouseEvent(tree, mouseEvent.getID(),
mouseEvent.getWhen(), mouseEvent.getModifiers(),

View File

@ -58,7 +58,7 @@ public class ParseTreeInfoPanel extends JPanel {
private static final long serialVersionUID = -4243405131202059043L;
/** Parse tree model. */
private final transient ParseTreeModel parseTreeModel;
private final transient ParseTreeTableModel parseTreeTableModel;
/** JTextArea component. */
private final JTextArea textArea;
/** Last directory. */
@ -76,8 +76,8 @@ public class ParseTreeInfoPanel extends JPanel {
public ParseTreeInfoPanel() {
setLayout(new BorderLayout());
parseTreeModel = new ParseTreeModel(null);
final JTreeTable treeTable = new JTreeTable(parseTreeModel);
parseTreeTableModel = new ParseTreeTableModel(null);
final JTreeTable treeTable = new JTreeTable(parseTreeTableModel);
final JScrollPane scrollPane = new JScrollPane(treeTable);
add(scrollPane, BorderLayout.PAGE_START);
@ -108,7 +108,7 @@ public class ParseTreeInfoPanel extends JPanel {
* @param parseTree DetailAST tree.
*/
public void openAst(DetailAST parseTree) {
parseTreeModel.setParseTree(parseTree);
parseTreeTableModel.setParseTree(parseTree);
reloadAction.setEnabled(true);
// clear for each new file
@ -138,7 +138,7 @@ public class ParseTreeInfoPanel extends JPanel {
final FileText text = new FileText(file.getAbsoluteFile(),
getEncoding());
final DetailAST parseTree = parseFile(text);
parseTreeModel.setParseTree(parseTree);
parseTreeTableModel.setParseTree(parseTree);
currentFile = file;
lastDirectory = file.getParentFile();
reloadAction.setEnabled(true);

View File

@ -19,6 +19,10 @@
package com.puppycrawl.tools.checkstyle.gui;
import javax.swing.event.EventListenerList;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import antlr.ASTFactory;
@ -33,17 +37,28 @@ import com.puppycrawl.tools.checkstyle.utils.TokenUtils;
*
* @author Lars Kühne
*/
public class ParseTreeModel extends AbstractTreeTableModel {
public class ParseTreeTableModel implements TreeModel {
/** Column names. */
private static final String[] COLUMN_NAMES = {
"Tree", "Type", "Line", "Column", "Text",
};
/**
* The root node of the tree table model.
*/
private final Object root;
/**
* A list of event listeners for the tree model.
*/
private final EventListenerList listenerList = new EventListenerList();
/**
* @param parseTree DetailAST parse tree.
*/
public ParseTreeModel(DetailAST parseTree) {
super(createArtificialTreeRoot());
public ParseTreeTableModel(DetailAST parseTree) {
root = createArtificialTreeRoot();
setParseTree(parseTree);
}
@ -62,31 +77,38 @@ public class ParseTreeModel extends AbstractTreeTableModel {
* @param parseTree DetailAST parse tree.
*/
final void setParseTree(DetailAST parseTree) {
final DetailAST root = (DetailAST) getRoot();
root.setFirstChild(parseTree);
((AST) root).setFirstChild(parseTree);
final Object[] path = {root};
// no need to setup remaining info, as the call results in a
// table structure changed event anyway - we just pass nulls
fireTreeStructureChanged(this, path, null, (Object[]) null);
}
@Override
/**
* @return the number of available column.
*/
public int getColumnCount() {
return COLUMN_NAMES.length;
}
@Override
/**
* @param column the column number
* @return the name for column number {@code column}.
*/
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}
@Override
/**
* @param column the column number
* @return the type for column number {@code column}.
*/
public Class<?> getColumnClass(int column) {
Class<?> columnClass;
switch (column) {
case 0:
columnClass = TreeTableModel.class;
columnClass = ParseTreeTableModel.class;
break;
case 1:
columnClass = String.class;
@ -106,7 +128,12 @@ public class ParseTreeModel extends AbstractTreeTableModel {
return columnClass;
}
@Override
/**
* @param node the node
* @param column the column number
* @return the value to be displayed for node {@code node},
* at column number {@code column}.
*/
public Object getValueAt(Object node, int column) {
final DetailAST ast = (DetailAST) node;
Object value;
@ -152,4 +179,77 @@ public class ParseTreeModel extends AbstractTreeTableModel {
public void valueForPathChanged(TreePath path, Object newValue) {
//No Code, as tree is read-only
}
@Override
public Object getRoot() {
return root;
}
@Override
public boolean isLeaf(Object node) {
return getChildCount(node) == 0;
}
// This is not called in the JTree's default mode: use a naive implementation.
@Override
public int getIndexOfChild(Object parent, Object child) {
for (int i = 0; i < getChildCount(parent); i++) {
if (getChild(parent, i).equals(child)) {
return i;
}
}
return -1;
}
@Override
public void addTreeModelListener(TreeModelListener listener) {
listenerList.add(TreeModelListener.class, listener);
}
@Override
public void removeTreeModelListener(TreeModelListener listener) {
listenerList.remove(TreeModelListener.class, listener);
}
/**
* Notify all listeners that have registered interest for
* 'tree structure changed' event. The event instance
* is lazily created using the parameters passed into
* the fire method.
* @param source The Object responsible for generating the event.
* @param path An array of Object identifying the path to the parent of the modified items.
* @param childIndices An array of int that specifies the index values of the removed items.
* @param children An array of Object containing the inserted, removed, or changed objects.
* @see EventListenerList
*/
void fireTreeStructureChanged(Object source, Object[] path,
int[] childIndices,
Object... children) {
// Guaranteed to return a non-null array
final Object[] listeners = listenerList.getListenerList();
TreeModelEvent event = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == TreeModelListener.class) {
// Lazily create the event:
if (event == null) {
event = new TreeModelEvent(source, path,
childIndices, children);
}
((TreeModelListener) listeners[i + 1]).treeStructureChanged(event);
}
}
}
/**
* Indicates whether the the value for node {@code node},
* at column number {@code column} is editable.
*
* @param column the column number
* @return true if editable
*/
public boolean isCellEditable(int column) {
return getColumnClass(column) == ParseTreeTableModel.class;
}
}

View File

@ -1,73 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2015 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.gui;
import javax.swing.tree.TreeModel;
/**
* TreeTableModel is the model used by a JTreeTable. It extends TreeModel
* to add methods for getting information about the set of columns each
* node in the TreeTableModel may have. Each column, like a column in
* a TableModel, has a name and a type associated with it. Each node in
* the TreeTableModel can return a value for each of the columns and
* set that value if isCellEditable() returns true.
*
* <a href=
* "http://docs.oracle.com/cd/E16162_01/apirefs.1112/e17493/oracle/ide/controls/TreeTableModel.html">
* Original&nbsp;Source&nbsp;Location</a>
*
* @author Philip Milne
* @author Scott Violet
*/
public interface TreeTableModel extends TreeModel {
/**
* @return the number of available column.
*/
int getColumnCount();
/**
* @param column the column number
* @return the name for column number {@code column}.
*/
String getColumnName(int column);
/**
* @param column the column number
* @return the type for column number {@code column}.
*/
Class<?> getColumnClass(int column);
/**
* @param node the node
* @param column the column number
* @return the value to be displayed for node {@code node},
* at column number {@code column}.
*/
Object getValueAt(Object node, int column);
/**
* Indicates whether the the value for node {@code node},
* at column number {@code column} is editable.
*
* @param column the column number
* @return true if editable
*/
boolean isCellEditable(int column);
}

View File

@ -47,13 +47,13 @@ public class TreeTableModelAdapter extends AbstractTableModel {
/** JTree component. */
private final JTree tree;
/** Tree table model. */
private final transient TreeTableModel treeTableModel;
private final transient ParseTreeTableModel treeTableModel;
/**
* @param treeTableModel Tree table model.
* @param tree JTree component.
*/
public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree tree) {
public TreeTableModelAdapter(ParseTreeTableModel treeTableModel, JTree tree) {
this.tree = tree;
this.treeTableModel = treeTableModel;