From 72c8d55df549739afacea5f3f3680c44a137cb3f Mon Sep 17 00:00:00 2001 From: amosshi Date: Sat, 21 Sep 2019 21:19:52 -0700 Subject: [PATCH] #4 Swing UI - retire legacy JTreeTable --- .../org/freeinternals/biv/JSplitPaneFile.java | 43 +- .../commonlib/ui/JTreeCellRenderer.java | 50 -- .../commonlib/ui/JXMLViewer.java | 1 - .../ui/jtreetable/AbstractTreeTableModel.java | 244 ------- .../ui/jtreetable/DynamicTreeTableModel.java | 338 --------- .../commonlib/ui/jtreetable/JTreeTable.java | 650 ------------------ .../ui/jtreetable/TreeTableModel.java | 114 --- .../ui/jtreetable/TreeTableModelAdapter.java | 169 ----- 8 files changed, 34 insertions(+), 1575 deletions(-) delete mode 100644 CommonLib/src/main/java/org/freeinternals/commonlib/ui/JTreeCellRenderer.java delete mode 100644 CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/AbstractTreeTableModel.java delete mode 100644 CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/DynamicTreeTableModel.java delete mode 100644 CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/JTreeTable.java delete mode 100644 CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/TreeTableModel.java delete mode 100644 CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/TreeTableModelAdapter.java diff --git a/BinaryInternalsViewer/src/main/java/org/freeinternals/biv/JSplitPaneFile.java b/BinaryInternalsViewer/src/main/java/org/freeinternals/biv/JSplitPaneFile.java index 8abccfe..9cab946 100644 --- a/BinaryInternalsViewer/src/main/java/org/freeinternals/biv/JSplitPaneFile.java +++ b/BinaryInternalsViewer/src/main/java/org/freeinternals/biv/JSplitPaneFile.java @@ -6,9 +6,11 @@ */ package org.freeinternals.biv; +import java.awt.Component; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; +import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; @@ -16,8 +18,8 @@ import javax.swing.JTabbedPane; import javax.swing.JTree; import javax.swing.event.TreeSelectionEvent; -import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; import org.freeinternals.commonlib.core.FileFormat; @@ -25,7 +27,6 @@ import org.freeinternals.commonlib.ui.JBinaryViewer; import org.freeinternals.commonlib.ui.UITool; import org.freeinternals.commonlib.ui.JPanelForTree; -import org.freeinternals.commonlib.ui.JTreeCellRenderer; import org.freeinternals.commonlib.ui.JTreeNodeFileComponent; import org.freeinternals.commonlib.core.FileFormatException; @@ -47,9 +48,9 @@ public class JSplitPaneFile extends JSplitPane { * * @param file * @param frame - * @throws FileFormatException + * @throws FileFormatException */ - public JSplitPaneFile(final File file, final JFrame frame) throws FileFormatException, Throwable{ + public JSplitPaneFile(final File file, final JFrame frame) throws FileFormatException, Throwable { this.file = PluginManager.getFile(file); this.topLevelFrame = frame; this.createAndShowGUI(); @@ -63,14 +64,39 @@ private void createAndShowGUI() { this.file.fileName)); this.file.generateTreeNode(root); final JTree tree = new JTree(new DefaultTreeModel(root)); - tree.setCellRenderer(new JTreeCellRenderer()); - tree.addTreeSelectionListener(new TreeSelectionListener() { + + tree.setCellRenderer(new DefaultTreeCellRenderer() { @Override - public void valueChanged(final javax.swing.event.TreeSelectionEvent evt) { - treeSelectionChanged(evt); + public Component getTreeCellRendererComponent(final JTree tree, final Object value, + final boolean sel, final boolean expanded, final boolean leaf, final int row, + final boolean hasFocus) { + + super.getTreeCellRendererComponent(tree, value, + sel, expanded, leaf, row, + hasFocus); + + if (value instanceof DefaultMutableTreeNode) { + if (((DefaultMutableTreeNode) value).getUserObject() instanceof JTreeNodeFileComponent) { + JTreeNodeFileComponent fileComp = (JTreeNodeFileComponent) ((DefaultMutableTreeNode) value).getUserObject(); + final Icon icon = fileComp.getIcon(); + if (icon != null) { + this.setIcon(icon); + } + + if (fileComp.isDetailAvailable()) { + this.setText("" + fileComp.getText()); + } else { + this.setText(fileComp.getText()); + } + } + } + + return this; } }); + + tree.addTreeSelectionListener(this::treeSelectionChanged); tree.addMouseListener(new MouseAdapter() { @Override @@ -91,7 +117,6 @@ public void mousePressed(MouseEvent e) { } }); - final JPanelForTree panel = new JPanelForTree(tree, this.topLevelFrame); final JTabbedPane tabbedPane = new JTabbedPane(); diff --git a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/JTreeCellRenderer.java b/CommonLib/src/main/java/org/freeinternals/commonlib/ui/JTreeCellRenderer.java deleted file mode 100644 index 02c9c90..0000000 --- a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/JTreeCellRenderer.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * JTreeCellRenderer.java Sep 12, 2010, 20:12 - * - * Copyright 2010, FreeInternals.org. All rights reserved. - * Use is subject to license terms. - */ -package org.freeinternals.commonlib.ui; - -import java.awt.Component; -import javax.swing.Icon; -import javax.swing.JTree; -import javax.swing.tree.DefaultMutableTreeNode; -import javax.swing.tree.DefaultTreeCellRenderer; - -/** - * - * @author Amos Shi - */ -public final class JTreeCellRenderer extends DefaultTreeCellRenderer { - - private static final long serialVersionUID = 4876543219876500000L; - - @Override - public Component getTreeCellRendererComponent(final JTree tree, final Object value, - final boolean sel, final boolean expanded, final boolean leaf, final int row, - final boolean hasFocus) { - - super.getTreeCellRendererComponent(tree, value, - sel, expanded, leaf, row, - hasFocus); - - if (value instanceof DefaultMutableTreeNode) { - if (((DefaultMutableTreeNode) value).getUserObject() instanceof JTreeNodeFileComponent) { - JTreeNodeFileComponent fileComp = (JTreeNodeFileComponent) ((DefaultMutableTreeNode) value).getUserObject(); - final Icon icon = fileComp.getIcon(); - if (icon != null) { - this.setIcon(icon); - } - - if (fileComp.isDetailAvailable()) { - this.setText("" + fileComp.getText()); - } else { - this.setText(fileComp.getText()); - } - } - } - - return this; - } -} diff --git a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/JXMLViewer.java b/CommonLib/src/main/java/org/freeinternals/commonlib/ui/JXMLViewer.java index 348e8f0..40f9a21 100644 --- a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/JXMLViewer.java +++ b/CommonLib/src/main/java/org/freeinternals/commonlib/ui/JXMLViewer.java @@ -7,7 +7,6 @@ package org.freeinternals.commonlib.ui; import java.awt.BorderLayout; -import java.awt.Component; import java.io.InputStream; import javax.swing.JPanel; import javax.swing.JScrollPane; diff --git a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/AbstractTreeTableModel.java b/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/AbstractTreeTableModel.java deleted file mode 100644 index 2587b92..0000000 --- a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/AbstractTreeTableModel.java +++ /dev/null @@ -1,244 +0,0 @@ -/* - * Copyright 1997-1999 Sun Microsystems, Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistribution in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * Neither the name of Sun Microsystems, Inc. or the names of - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * This software is provided "AS IS," without a warranty of any - * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND - * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY - * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY - * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR - * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR - * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE - * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, - * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER - * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF - * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS - * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - * - * You acknowledge that this software is not designed, licensed or - * intended for use in the design, construction, operation or - * maintenance of any nuclear facility. - */ -package org.freeinternals.commonlib.ui.jtreetable; - -import javax.swing.event.EventListenerList; -import javax.swing.event.TreeModelEvent; -import javax.swing.event.TreeModelListener; -import javax.swing.tree.TreePath; - -/** - * An abstract implementation of the TreeTableModel interface, handling the list - * of listeners. - * - *

- * Created by Philip Milne. Miner change may be done on formatting, - * annotation, java doc, etc, for check style. - *

- * - * @version 1.2 10/27/98 - * @author Philip Milne - * @see The - * Swing HTML Parser - */ -public abstract class AbstractTreeTableModel implements TreeTableModel { - - @SuppressWarnings("ProtectedField") - protected Object root; - @SuppressWarnings("ProtectedField") - protected EventListenerList listenerList = new EventListenerList(); - - public 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; - } - - @Override - public void valueForPathChanged(TreePath path, Object newValue) { - } - - // 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 l) { - listenerList.add(TreeModelListener.class, l); - } - - @Override - public void removeTreeModelListener(TreeModelListener l) { - listenerList.remove(TreeModelListener.class, l); - } - - /* - * Notifies all listeners that have registered interest for - * notification on this event type. The event instance - * is lazily created using the parameters passed into - * the fire method. - * @see EventListenerList - */ - protected void fireTreeNodesChanged(final Object source, final Object[] path, - final int[] childIndices, - final Object[] children) { - // Guaranteed to return a non-null array - Object[] listeners = listenerList.getListenerList(); - TreeModelEvent e = 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 (e == null) { - e = new TreeModelEvent(source, path, childIndices, children); - } - ((TreeModelListener) listeners[i + 1]).treeNodesChanged(e); - } - } - } - - /* - * Notifies all listeners that have registered interest for - * notification on this event type. The event instance - * is lazily created using the parameters passed into - * the fire method. - * @see EventListenerList - */ - protected void fireTreeNodesInserted(final Object source, final Object[] path, - final int[] childIndices, - final Object[] children) { - // Guaranteed to return a non-null array - Object[] listeners = listenerList.getListenerList(); - TreeModelEvent e = 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 (e == null) { - e = new TreeModelEvent(source, path, childIndices, children); - } - ((TreeModelListener) listeners[i + 1]).treeNodesInserted(e); - } - } - } - - /* - * Notifies all listeners that have registered interest for - * notification on this event type. The event instance - * is lazily created using the parameters passed into - * the fire method. - * @see EventListenerList - */ - protected void fireTreeNodesRemoved(final Object source, final Object[] path, - final int[] childIndices, - final Object[] children) { - // Guaranteed to return a non-null array - Object[] listeners = listenerList.getListenerList(); - TreeModelEvent e = 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 (e == null) { - e = new TreeModelEvent(source, path, childIndices, children); - } - ((TreeModelListener) listeners[i + 1]).treeNodesRemoved(e); - } - } - } - - /* - * Notifies all listeners that have registered interest for - * notification on this event type. The event instance - * is lazily created using the parameters passed into - * the fire method. - * @see EventListenerList - */ - protected void fireTreeStructureChanged(final Object source, final Object[] path, - final int[] childIndices, - final Object[] children) { - // Guaranteed to return a non-null array - Object[] listeners = listenerList.getListenerList(); - TreeModelEvent e = 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 (e == null) { - e = new TreeModelEvent(source, path, childIndices, children); - } - ((TreeModelListener) listeners[i + 1]).treeStructureChanged(e); - } - } - } - - // - // Default impelmentations for methods in the TreeTableModel interface. - // - @Override - public Class getColumnClass(final 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(final Object node, final int column) { - return getColumnClass(column) == TreeTableModel.class; - } - - @Override - public void setValueAt(final Object aValue, final Object node, final int column) { - } - - // Left to be implemented in the subclass: - - /* - * public Object getChild(Object parent, int index) - * public int getChildCount(Object parent) - * public int getColumnCount() - * public String getColumnName(Object node, int column) - * public Object getValueAt(Object node, int column) - */ -} diff --git a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/DynamicTreeTableModel.java b/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/DynamicTreeTableModel.java deleted file mode 100644 index 35cd3ba..0000000 --- a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/DynamicTreeTableModel.java +++ /dev/null @@ -1,338 +0,0 @@ -/* - * Copyright 1997-1999 Sun Microsystems, Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistribution in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * Neither the name of Sun Microsystems, Inc. or the names of - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * This software is provided "AS IS," without a warranty of any - * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND - * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY - * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY - * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR - * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR - * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE - * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, - * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER - * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF - * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS - * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - * - * You acknowledge that this software is not designed, licensed or - * intended for use in the design, construction, operation or - * maintenance of any nuclear facility. - */ -package org.freeinternals.commonlib.ui.jtreetable; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import javax.swing.tree.TreeNode; - - -/** - * An implementation of TreeTableModel that uses reflection to answer - * TableModel methods. This works off a handful - * of values. A TreeNode is used to answer all the TreeModel related - * methods (similar to AbstractTreeTableModel and DefaultTreeModel). - * The column names are specified in the constructor. The values for - * the columns are dynamically obtained via reflection, you simply - * provide the method names. The methods used to set a particular value are - * also specified as an array of method names, a null method name, or - * null array indicates the column isn't editable. And the class types, - * used for the TableModel method getColumnClass are specified in the - * constructor. - * - *

- * Created by Scott Violet. Miner change may be done on formatting, - * annotation, java doc, etc, for check style. - *

- * - * @author Scott Violet - * @see The Swing HTML Parser - */ -public class DynamicTreeTableModel extends AbstractTreeTableModel { - - /** Names of the columns, used for the TableModel getColumnName method. */ - private final String[] columnNames; - /** Method names used to determine a particular value. Used for the - * TableModel method getValueAt. */ - private final String[] methodNames; - /** Setter method names, used to set a particular value. Used for the - * TableModel method setValueAt. A null entry, or array, indicates the - * column is not editable. - */ - private final String[] setterMethodNames; - /** Column classes, used for the TableModel method getColumnClass. */ - private final Class[] cTypes; - - /** - * Constructor for creating a DynamicTreeTableModel. - * - * @param root - * @param columnNames - * @param getterMethodNames - * @param setterMethodNames - * @param cTypes - */ - public DynamicTreeTableModel(final TreeNode root, final String[] columnNames, - final String[] getterMethodNames, - final String[] setterMethodNames, - final Class[] cTypes) { - super(root); - this.columnNames = columnNames; - this.methodNames = getterMethodNames; - this.setterMethodNames = setterMethodNames; - this.cTypes = cTypes; - } - - // - // TreeModel interface - // - /** - * TreeModel method to return the number of children of a particular - * node. Since node is a TreeNode, this can be answered - * via the TreeNode method getChildCount. - * - * @param node - * @return - */ - @Override - public int getChildCount(final Object node) { - return ((TreeNode) node).getChildCount(); - } - - /** - * TreeModel method to locate a particular child of the specified - * node. Since node is a TreeNode, this can be answered - * via the TreeNode method getChild. - * - * @param node - * @param i - * @return - */ - @Override - public Object getChild(final Object node, final int i) { - return ((TreeNode) node).getChildAt(i); - } - - /** - * TreeModel method to determine if a node is a leaf. - * Since node is a TreeNode, this can be answered - * via the TreeNode method isLeaf. - * - * @param node - * @return - */ - @Override - public boolean isLeaf(final Object node) { - return ((TreeNode) node).isLeaf(); - } - - // - // The TreeTable interface. - // - /** - * Returns the number of column names passed into the constructor. - * - * @return - */ - @Override - public int getColumnCount() { - return columnNames.length; - } - - /** - * Returns the column name passed into the constructor. - * - * @param column - * @return - */ - @Override - public String getColumnName(int column) { - if (cTypes == null || column < 0 || column >= cTypes.length) { - return null; - } - return columnNames[column]; - } - - /** - * Returns the column class for column column. This - * is set in the constructor. - * - * @param column - * @return - */ - @Override - public Class getColumnClass(final int column) { - if (cTypes == null || column < 0 || column >= cTypes.length) { - return null; - } - return cTypes[column]; - } - - /** - * Returns the value for the column column and object - * node. The return value is determined by invoking - * the method specified in constructor for the passed in column. - * @param node - * @param column - * @return - */ - @Override - public Object getValueAt(final Object node, final int column) { - try { - Method method = node.getClass().getMethod(methodNames[column], (Class[]) null); - if (method != null) { - return method.invoke(node, (Object[]) null); - } - } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException th) { - } - - return null; - } - - /** - * Returns true if there is a setter method name for column - * column. This is set in the constructor. - * - * @param node - * @param column - * @return - */ - @Override - public boolean isCellEditable(final Object node, final int column) { - return (setterMethodNames != null - && setterMethodNames[column] != null); - } - - /** - * Sets the value to aValue for the object - * node in column column. This is done - * by using the setter method name, and coercing the passed in - * value to the specified type. - * - * @param aValue - * @param node - * @param column - */ - // Note: This looks up the methods each time! This is rather inefficient; - // it should really be changed to cache matching methods/constructors - // based on node's class, and aValue's class. - @Override - public void setValueAt(Object aValue, final Object node, final int column) { - boolean found = false; - try { - // We have to search through all the methods since the - // types may not match up. - Method[] methods = node.getClass().getMethods(); - - for (int counter = methods.length - 1; counter >= 0; counter--) { - if (methods[counter].getName().equals(setterMethodNames[column]) && methods[counter].getParameterTypes() != null && methods[counter].getParameterTypes().length == 1) { - // We found a matching method - Class param = methods[counter].getParameterTypes()[0]; - if (!param.isInstance(aValue)) { - // Yes, we can use the value passed in directly, - // no coercision is necessary! - if (aValue instanceof String - && ((String) aValue).length() == 0) { - // Assume an empty string is null, this is - // probably bogus for here. - aValue = null; - } else { - // Have to attempt some sort of coercision. - // See if the expected parameter type has - // a constructor that takes a String. - @SuppressWarnings("unchecked") - Constructor cs = param.getConstructor(new Class[]{String.class}); - if (cs != null) { - aValue = cs.newInstance(new Object[]{aValue}); - } else { - aValue = null; - } - } - } - // null either means it was an empty string, or there - // was no translation. Could potentially deal with these - // differently. - methods[counter].invoke(node, new Object[]{aValue}); - found = true; - break; - } - } - } catch (SecurityException | NoSuchMethodException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException th) { - System.out.println("exception: " + th); - } - if (found) { - // The value changed, fire an event to notify listeners. - TreeNode parent = ((TreeNode) node).getParent(); - fireTreeNodesChanged(this, getPathToRoot(parent), - new int[]{getIndexOfChild(parent, node)}, - new Object[]{node}); - } - } - - /** - * Builds the parents of the node up to and including the root node, - * where the original node is the last element in the returned array. - * The length of the returned array gives the node's depth in the - * tree. - * - * @param aNode the TreeNode to get the path for - * @return - */ - public TreeNode[] getPathToRoot(final TreeNode aNode) { - return getPathToRoot(aNode, 0); - } - - /** - * Builds the parents of the node up to and including the root node, - * where the original node is the last element in the returned array. - * The length of the returned array gives the node's depth in the - * tree. - * - * @param aNode the TreeNode to get the path for - * @param depth an int giving the number of steps already taken towards - * the root (on recursive calls), used to size the returned array - * @return an array of TreeNodes giving the path from the root to the - * specified node - */ - private TreeNode[] getPathToRoot(final TreeNode aNode, int depth) { - TreeNode[] retNodes; - // This method recurses, traversing towards the root in order - // size the array. On the way back, it fills in the nodes, - // starting from the root and working back to the original node. - - /* Check for null, in case someone passed in a null node, or - they passed in an element that isn't rooted at root. */ - if (aNode == null) { - if (depth == 0) { - return null; - } else { - retNodes = new TreeNode[depth]; - } - } else { - depth++; - if (aNode == root) { - retNodes = new TreeNode[depth]; - } else { - retNodes = getPathToRoot(aNode.getParent(), depth); - } - retNodes[retNodes.length - depth] = aNode; - } - return retNodes; - } -} diff --git a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/JTreeTable.java b/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/JTreeTable.java deleted file mode 100644 index 0df1f84..0000000 --- a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/JTreeTable.java +++ /dev/null @@ -1,650 +0,0 @@ -/* - * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistribution in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * Neither the name of Sun Microsystems, Inc. or the names of - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * This software is provided "AS IS," without a warranty of any - * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND - * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY - * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY - * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR - * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR - * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE - * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, - * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER - * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF - * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS - * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - * - * You acknowledge that this software is not designed, licensed or - * intended for use in the design, construction, operation or - * maintenance of any nuclear facility. - */ -package org.freeinternals.commonlib.ui.jtreetable; - -import java.awt.Color; -import java.awt.Component; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Rectangle; -import java.awt.event.InputEvent; -import java.awt.event.MouseEvent; -import java.util.EventObject; -import javax.swing.DefaultCellEditor; -import javax.swing.Icon; -import javax.swing.JTable; -import javax.swing.JTextField; -import javax.swing.JTree; -import javax.swing.ListSelectionModel; -import javax.swing.LookAndFeel; -import javax.swing.UIManager; -import javax.swing.border.Border; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; -import javax.swing.table.TableCellRenderer; -import javax.swing.tree.DefaultTreeCellRenderer; -import javax.swing.tree.DefaultTreeSelectionModel; -import javax.swing.tree.TreeCellRenderer; -import javax.swing.tree.TreeModel; -import javax.swing.tree.TreePath; - -/** - * This example shows how to create a simple JTreeTable component, by using a - * JTree as a renderer (and editor) for the cells in a particular column in the - * JTable. - * - *

- * Created by Philip Milne | Scott Violet. Miner change may be done - * on formatting, annotation, java doc, etc, for check style. - *

- * - * @version 1.2 10/27/98 - * - * @author Philip Milne - * @author Scott Violet - * @see The - * Swing HTML Parser - */ -public class JTreeTable extends JTable { - - private static final long serialVersionUID = 4876543219876500005L; - /** - * A subclass of JTree. - */ - @SuppressWarnings("ProtectedField") - protected TreeTableCellRenderer tree; - - public JTreeTable(TreeTableModel treeTableModel) { - super(); - - // Creates the tree. It will be used as a renderer and editor. - tree = new TreeTableCellRenderer(treeTableModel); - - // Installs a tableModel representing the visible rows in the tree. - super.setModel(new TreeTableModelAdapter(treeTableModel, tree)); - - // Forces the JTable and JTree to share their row selection models. - ListToTreeSelectionModelWrapper selectionWrapper = new ListToTreeSelectionModelWrapper(); - tree.setSelectionModel(selectionWrapper); - setSelectionModel(selectionWrapper.getListSelectionModel()); - - // Installs the tree editor renderer and editor. - super.setDefaultRenderer(TreeTableModel.class, tree); - super.setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor()); - - // No grid. - super.setShowGrid(false); - - // No intercell spacing - super.setIntercellSpacing(new Dimension(0, 0)); - - // And update the height of the trees row to match that of - // the table. - if (tree.getRowHeight() < 1) { - // Metal looks better like this. - this.setRowHeight(20); - } - } - - /** - * Overridden to message super and forward the method to the tree. Since the - * tree is not actually in the component hierarchy it will never receive - * this unless we forward it in this manner. - */ - @Override - public void updateUI() { - super.updateUI(); - if (tree != null) { - tree.updateUI(); - // Do this so that the editor is referencing the current renderer - // from the tree. The renderer can potentially change each time - // laf changes. - super.setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor()); - } - // Use the tree's default foreground and background colors in the - // table. - LookAndFeel.installColorsAndFont(this, "Tree.background", - "Tree.foreground", "Tree.font"); - } - - /** - * Workaround for BasicTableUI anomaly.Make sure the UI never tries to - * resize the editor. The UI currently uses different techniques to paint - * the renderers and editors; overriding setBounds() below is not the right - * thing to do for an editor. Returning -1 for the editing row in this case, - * ensures the editor is never painted. - * - * @return Get editing row - */ - @Override - public int getEditingRow() { - return (getColumnClass(editingColumn) == TreeTableModel.class) ? -1 - : editingRow; - } - - /** - * Returns the actual row that is editing as getEditingRow will - * always return -1. - */ - private int realEditingRow() { - return editingRow; - } - - /** - * This is overridden to invoke super's implementation, and then, if the - * receiver is editing a Tree column, the editor's bounds is reset.The - * reason we have to do this is because JTable doesn't think the table is - * being edited, as getEditingRow returns -1, and therefore - * doesn't automatically resize the editor for us. - * - * @param resizingColumn Resizing column - */ - @Override - public void sizeColumnsToFit(final int resizingColumn) { - super.sizeColumnsToFit(resizingColumn); - if (getEditingColumn() != -1 && getColumnClass(editingColumn) - == TreeTableModel.class) { - Rectangle cellRect = getCellRect(realEditingRow(), - getEditingColumn(), false); - Component component = getEditorComponent(); - component.setBounds(cellRect); - component.validate(); - } - } - - /** - * Overridden to pass the new rowHeight to the tree. - * - * @param rowHeight Table row height - */ - @Override - public final void setRowHeight(final int rowHeight) { - super.setRowHeight(rowHeight); - if (tree != null && tree.getRowHeight() != rowHeight) { - tree.setRowHeight(getRowHeight()); - } - } - - /** - * Returns the tree that is being shared between the model. - * - * @return the shared tree - */ - public JTree getTree() { - return tree; - } - - /** - * Overridden to invoke repaint for the particular location if the column - * contains the tree.This is done as the tree editor does not fill the - * bounds of the cell, we need the renderer to paint the tree in the - * background, and then draw the editor over it. - * - * @param row Table row - * @param column Table column - * @param e Event object - * @return Cell is editable or not - */ - @Override - public boolean editCellAt(final int row, final int column, final EventObject e) { - boolean retValue = super.editCellAt(row, column, e); - if (retValue && getColumnClass(column) == TreeTableModel.class) { - repaint(getCellRect(row, column, false)); - } - return retValue; - } - - /** - * A TreeCellRenderer that displays a JTree. - */ - @SuppressWarnings("PublicInnerClass") - public class TreeTableCellRenderer extends JTree implements - TableCellRenderer { - - private static final long serialVersionUID = 4876543219876500005L; - /** - * Last table/tree row asked to renderer. - */ - @SuppressWarnings("ProtectedField") - protected int visibleRow; - /** - * Border to draw around the tree, if this is non-null, it will be - * painted. - */ - @SuppressWarnings("ProtectedField") - protected Border highlightBorder; - - public TreeTableCellRenderer(final TreeModel model) { - super(model); - } - - /** - * updateUI is overridden to set the colors of the Tree's renderer to - * match that of the table. - */ - @Override - public void updateUI() { - super.updateUI(); - // Make the tree's cell renderer use the table's cell selection - // colors. - TreeCellRenderer tcr = getCellRenderer(); - if (tcr instanceof DefaultTreeCellRenderer) { - DefaultTreeCellRenderer dtcr = ((DefaultTreeCellRenderer) tcr); - // For 1.1 uncomment this, 1.2 has a bug that will cause an - // exception to be thrown if the border selection color is - // null. - // dtcr.setBorderSelectionColor(null); - dtcr.setTextSelectionColor(UIManager.getColor("Table.selectionForeground")); - dtcr.setBackgroundSelectionColor(UIManager.getColor("Table.selectionBackground")); - } - } - - /** - * Sets the row height of the tree, and forwards the row height to the - * table. - * - * @param rowHeight Table row height - */ - @Override - public void setRowHeight(final int rowHeight) { - if (rowHeight > 0) { - super.setRowHeight(rowHeight); - if (JTreeTable.this != null - && JTreeTable.this.getRowHeight() != rowHeight) { - JTreeTable.this.setRowHeight(getRowHeight()); - } - } - } - - /** - * This is overridden to set the height to match that of the JTable. - * - * @param x x position - * @param y x position - * @param w width - * @param h height - */ - @Override - public void setBounds(final int x, final int y, final int w, final int h) { - super.setBounds(x, 0, w, JTreeTable.this.getHeight()); - } - - /** - * Sub-classed to translate the graphics such that the last visible row - * will be drawn at 0,0. - * - * @param g UI painter - */ - @Override - public void paint(final Graphics g) { - g.translate(0, -visibleRow * getRowHeight()); - super.paint(g); - // Draw the Table border if we have focus. - if (highlightBorder != null) { - highlightBorder.paintBorder(this, g, 0, visibleRow - * getRowHeight(), getWidth(), - getRowHeight()); - } - } - - /** - * TreeCellRenderer method.Overridden to update the visible row. - * - * @param table Table control - * @param value Cell value - * @param isSelected Cell is selected or not - * @param hasFocus Cell has focus or not - * @param row Table row - * @param column Table column - * @return the cell render - */ - @Override - public Component getTableCellRendererComponent(final JTable table, - final Object value, - final boolean isSelected, - final boolean hasFocus, - final int row, final int column) { - Color background; - Color foreground; - - if (isSelected) { - background = table.getSelectionBackground(); - foreground = table.getSelectionForeground(); - } else { - background = table.getBackground(); - foreground = table.getForeground(); - } - highlightBorder = null; - if (realEditingRow() == row && getEditingColumn() == column) { - background = UIManager.getColor("Table.focusCellBackground"); - foreground = UIManager.getColor("Table.focusCellForeground"); - } else if (hasFocus) { - highlightBorder = UIManager.getBorder("Table.focusCellHighlightBorder"); - if (isCellEditable(row, column)) { - background = UIManager.getColor("Table.focusCellBackground"); - foreground = UIManager.getColor("Table.focusCellForeground"); - } - } - - visibleRow = row; - setBackground(background); - - TreeCellRenderer tcr = getCellRenderer(); - if (tcr instanceof DefaultTreeCellRenderer) { - DefaultTreeCellRenderer dtcr = ((DefaultTreeCellRenderer) tcr); - if (isSelected) { - dtcr.setTextSelectionColor(foreground); - dtcr.setBackgroundSelectionColor(background); - } else { - dtcr.setTextNonSelectionColor(foreground); - dtcr.setBackgroundNonSelectionColor(background); - } - } - return this; - } - } - - /** - * An editor that can be used to edit the tree column. This extends - * DefaultCellEditor and uses a JTextField (actually, TreeTableTextField) to - * perform the actual editing. - *

- * To support editing of the tree column we can not make the tree editable. - * The reason this doesn't work is that you can not use the same component - * for editing and renderering. The table may have the need to paint cells, - * while a cell is being edited. If the same component were used for the - * rendering and editing the component would be moved around, and the - * contents would change. When editing, this is undesirable, the contents of - * the text field must stay the same, including the caret blinking, and - * selections persisting. For this reason the editing is done via a - * TableCellEditor. - *

- * Another interesting thing to be aware of is how tree positions its render - * and editor. The render/editor is responsible for drawing the icon - * indicating the type of node (leaf, branch...). The tree is responsible - * for drawing any other indicators, perhaps an additional +/- sign, or - * lines connecting the various nodes. So, the renderer is positioned based - * on depth. On the other hand, table always makes its editor fill the - * contents of the cell. To get the allusion that the table cell editor is - * part of the tree, we don't want the table cell editor to fill the cell - * bounds. We want it to be placed in the same manner as tree places it - * editor, and have table message the tree to paint any decorations the tree - * wants. Then, we would only have to worry about the editing part. The - * approach taken here is to determine where tree would place the editor, - * and to override the reshape method in the JTextField - * component to nudge the text field to the location tree would place it. - * Since JTreeTable will paint the tree behind the editor everything should - * just work. So, that is what we are doing here. Determining of the icon - * position will only work if the TreeCellRenderer is an instance of - * DefaultTreeCellRenderer. If you need custom TreeCellRenderers, that don't - * descend from DefaultTreeCellRenderer, and you want to support editing in - * JTreeTable, you will have to do something similar. - */ - @SuppressWarnings("PublicInnerClass") - public class TreeTableCellEditor extends DefaultCellEditor { - - private static final long serialVersionUID = 4876543219876500005L; - - public TreeTableCellEditor() { - super(new TreeTableTextField()); - } - - /** - * Overridden to determine an offset that tree would place the editor - * at.The offset is determined from the getRowBounds JTree - * method, and additionally from the icon DefaultTreeCellRenderer will - * use.

- * The offset is then set on the TreeTableTextField component created in - * the constructor, and returned. - * - * @param table Table control - * @param value Cell value - * @param isSelected Is selected - * @param r Row - * @param c Column - * @return Table cell editor - */ - @Override - public Component getTableCellEditorComponent(final JTable table, - final Object value, - final boolean isSelected, - final int r, int c) { - Component component = super.getTableCellEditorComponent(table, value, isSelected, r, c); - JTree t = getTree(); - boolean rv = t.isRootVisible(); - int offsetRow = rv ? r : r - 1; - Rectangle bounds = t.getRowBounds(offsetRow); - int offset = bounds.x; - TreeCellRenderer tcr = t.getCellRenderer(); - if (tcr instanceof DefaultTreeCellRenderer) { - Object node = t.getPathForRow(offsetRow). - getLastPathComponent(); - Icon icon; - if (t.getModel().isLeaf(node)) { - icon = ((DefaultTreeCellRenderer) tcr).getLeafIcon(); - } else if (tree.isExpanded(offsetRow)) { - icon = ((DefaultTreeCellRenderer) tcr).getOpenIcon(); - } else { - icon = ((DefaultTreeCellRenderer) tcr).getClosedIcon(); - } - if (icon != null) { - offset += ((DefaultTreeCellRenderer) tcr).getIconTextGap() - + icon.getIconWidth(); - } - } - ((TreeTableTextField) getComponent()).offset = offset; - return component; - } - - /** - * This is overridden to forward the event to the tree. This will return - * true if the click count Greater Equals 3, or the event is null. - * - * @param e Event object - * @return If cell is editable or not - */ - @Override - public boolean isCellEditable(final EventObject e) { - if (e instanceof MouseEvent) { - MouseEvent me = (MouseEvent) e; - // If the modifiers are not 0 (or the left mouse button), - // tree may try and toggle the selection, and table - // will then try and toggle, resulting in the - // selection remaining the same. To avoid this, we - // only dispatch when the modifiers are 0 (or the left mouse - // button). - if (me.getModifiers() == 0 - || me.getModifiers() == InputEvent.BUTTON1_MASK) { - for (int counter = getColumnCount() - 1; counter >= 0; - counter--) { - if (getColumnClass(counter) == TreeTableModel.class) { - MouseEvent newME = new MouseEvent(JTreeTable.this.tree, me.getID(), - me.getWhen(), me.getModifiers(), - me.getX() - getCellRect(0, counter, true).x, - me.getY(), me.getClickCount(), - me.isPopupTrigger()); - JTreeTable.this.tree.dispatchEvent(newME); - break; - } - } - } - if (me.getClickCount() >= 3) { - return true; - } - return false; - } - if (e == null) { - return true; - } - return false; - } - } - - /** - * Component used by TreeTableCellEditor. The only thing this does is to - * override the reshape method, and to ALWAYS make the x - * location be offset. - */ - @SuppressWarnings("PackageVisibleInnerClass") - static class TreeTableTextField extends JTextField { - - private static final long serialVersionUID = 4876543219876500005L; - private int offset; - - @Override - @SuppressWarnings("deprecation") - public void reshape(final int x, final int y, final int w, final int h) { - int newX = Math.max(x, offset); - //super.reshape(newX, y, w - (newX - x), h); - super.setBounds(newX, y, w - (newX - x), h); - } - - @Override - public void setBounds(final int x, final int y, final int width, final int height) { - int newX = Math.max(x, offset); - super.setBounds(newX, y, width - (newX - x), height); - } - } - - /** - * ListToTreeSelectionModelWrapper extends DefaultTreeSelectionModel to - * listen for changes in the ListSelectionModel it maintains. Once a change - * in the ListSelectionModel happens, the paths are updated in the - * DefaultTreeSelectionModel. - */ - @SuppressWarnings("PackageVisibleInnerClass") - class ListToTreeSelectionModelWrapper extends DefaultTreeSelectionModel { - - private static final long serialVersionUID = 4876543219876500005L; - /** - * Set to true when we are updating the ListSelectionModel. - */ - private boolean updatingListSelectionModel; - - ListToTreeSelectionModelWrapper() { - super(); - this.getListSelectionModel().addListSelectionListener(this.createListSelectionListener()); - } - - /** - * Returns the list selection model. ListToTreeSelectionModelWrapper - * listens for changes to this model and updates the selected paths - * accordingly. - * - * @return {@link ListSelectionModel} - */ - final ListSelectionModel getListSelectionModel() { - return listSelectionModel; - } - - /** - * This is overridden to set updatingListSelectionModel and - * message super. This is the only place DefaultTreeSelectionModel - * alters the ListSelectionModel. - */ - @Override - public void resetRowSelection() { - if (!updatingListSelectionModel) { - updatingListSelectionModel = true; - try { - super.resetRowSelection(); - } finally { - updatingListSelectionModel = false; - } - } - // Notice how we don't message super if - // updatingListSelectionModel is true. If - // updatingListSelectionModel is true, it implies the - // ListSelectionModel has already been updated and the - // paths are the only thing that needs to be updated. - } - - /** - * Creates and returns an instance of ListSelectionHandler. - * - * @return {@link ListSelectionHandler} - */ - protected final ListSelectionListener createListSelectionListener() { - return new ListSelectionHandler(); - } - - /** - * If updatingListSelectionModel is false, this will reset - * the selected paths from the selected rows in the list selection - * model. - */ - protected void updateSelectedPathsFromSelectedRows() { - if (!updatingListSelectionModel) { - updatingListSelectionModel = true; - try { - // This is way expensive, ListSelectionModel needs an - // enumerator for iterating. - int min = listSelectionModel.getMinSelectionIndex(); - int max = listSelectionModel.getMaxSelectionIndex(); - - clearSelection(); - if (min != -1 && max != -1) { - for (int counter = min; counter <= max; counter++) { - if (listSelectionModel.isSelectedIndex(counter)) { - TreePath selPath = tree.getPathForRow(counter); - - if (selPath != null) { - addSelectionPath(selPath); - } - } - } - } - } finally { - updatingListSelectionModel = false; - } - } - } - - /** - * Class responsible for calling updateSelectedPathsFromSelectedRows - * when the selection of the list changes. - */ - class ListSelectionHandler implements ListSelectionListener { - - @Override - public void valueChanged(final ListSelectionEvent e) { - updateSelectedPathsFromSelectedRows(); - } - } - } -} diff --git a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/TreeTableModel.java b/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/TreeTableModel.java deleted file mode 100644 index b966bd7..0000000 --- a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/TreeTableModel.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 1998-1999 Sun Microsystems, Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistribution in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * Neither the name of Sun Microsystems, Inc. or the names of - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * This software is provided "AS IS," without a warranty of any - * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND - * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY - * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY - * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR - * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR - * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE - * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, - * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER - * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF - * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS - * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - * - * You acknowledge that this software is not designed, licensed or - * intended for use in the design, construction, operation or - * maintenance of any nuclear facility. - */ - -package org.freeinternals.commonlib.ui.jtreetable; - -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. - * - *

- * Created by Philip Milne | Scott Violet. Miner change may be done - * on formatting, annotation, java doc, etc, for check style. - *

- * - * @author Philip Milne - * @author Scott Violet - * @see The Swing HTML Parser - */ -public interface TreeTableModel extends TreeModel -{ - /** - * Returns the number of available columns. - * - * @return column number - */ - int getColumnCount(); - - /** - * Returns the name for column number column. - * @param column column number - * @return column name - */ - String getColumnName(int column); - - /** - * Returns the type for column number column. - * @param column number - * @return column type - */ - Class getColumnClass(int column); - - /** - * Returns the value to be displayed for node node, - * at column number column. - * - * - * @param node - * @param column - * @return - */ - Object getValueAt(Object node, int column); - - /** - * Indicates whether the the value for node node, - * at column number column is editable. - * - * @param node - * @param column - * @return - */ - boolean isCellEditable(Object node, int column); - - /** - * Sets the value for node node, - * at column number column. - * - * @param aValue - * @param node - * @param column - */ - void setValueAt(Object aValue, Object node, int column); -} - diff --git a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/TreeTableModelAdapter.java b/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/TreeTableModelAdapter.java deleted file mode 100644 index b1a854f..0000000 --- a/CommonLib/src/main/java/org/freeinternals/commonlib/ui/jtreetable/TreeTableModelAdapter.java +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright 1997-1999 Sun Microsystems, Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistribution in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * Neither the name of Sun Microsystems, Inc. or the names of - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * This software is provided "AS IS," without a warranty of any - * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND - * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY - * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY - * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR - * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR - * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE - * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, - * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER - * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF - * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS - * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - * - * You acknowledge that this software is not designed, licensed or - * intended for use in the design, construction, operation or - * maintenance of any nuclear facility. - */ -package org.freeinternals.commonlib.ui.jtreetable; - -import javax.swing.JTree; -import javax.swing.SwingUtilities; -import javax.swing.event.TreeExpansionEvent; -import javax.swing.event.TreeExpansionListener; -import javax.swing.event.TreeModelEvent; -import javax.swing.event.TreeModelListener; -import javax.swing.table.AbstractTableModel; -import javax.swing.tree.TreePath; - -/** - * This is a wrapper class takes a TreeTableModel and implements the table model - * interface. The implementation is trivial, with all of the event dispatching - * support provided by the superclass: the AbstractTableModel. - * - *

- * Created by Philip Milne | Scott Violet. Miner change may be done - * on formatting, annotation, java doc, etc, for check style. - *

- * - * @version 1.2 10/27/98 - * - * @author Philip Milne - * @author Scott Violet - * @see The - * Swing HTML Parser - */ -public final class TreeTableModelAdapter extends AbstractTableModel { - - JTree tree; - TreeTableModel treeTableModel; - - public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree tree) { - this.tree = tree; - this.treeTableModel = treeTableModel; - - tree.addTreeExpansionListener(new TreeExpansionListener() { - // Don't use fireTableRowsInserted() here; the selection model - // would get updated twice. - @Override - public void treeExpanded(final TreeExpansionEvent event) { - fireTableDataChanged(); - } - - @Override - public void treeCollapsed(final TreeExpansionEvent event) { - fireTableDataChanged(); - } - }); - - // Installs a TreeModelListener that can update the table when - // the tree changes. We use delayedFireTableDataChanged as we can - // not be guaranteed the tree will have finished processing - // the event before us. - treeTableModel.addTreeModelListener(new TreeModelListener() { - @Override - public void treeNodesChanged(final TreeModelEvent e) { - delayedFireTableDataChanged(); - } - - @Override - public void treeNodesInserted(final TreeModelEvent e) { - delayedFireTableDataChanged(); - } - - @Override - public void treeNodesRemoved(final TreeModelEvent e) { - delayedFireTableDataChanged(); - } - - @Override - public void treeStructureChanged(final TreeModelEvent e) { - delayedFireTableDataChanged(); - } - }); - } - - // Wrappers, implementing TableModel interface. - @Override - public int getColumnCount() { - return treeTableModel.getColumnCount(); - } - - @Override - public String getColumnName(final int column) { - return treeTableModel.getColumnName(column); - } - - @Override - public Class getColumnClass(final int column) { - return treeTableModel.getColumnClass(column); - } - - @Override - public int getRowCount() { - return tree.getRowCount(); - } - - protected Object nodeForRow(final int row) { - TreePath treePath = tree.getPathForRow(row); - return treePath.getLastPathComponent(); - } - - @Override - public Object getValueAt(final int row, final int column) { - return treeTableModel.getValueAt(nodeForRow(row), column); - } - - @Override - public boolean isCellEditable(final int row, final int column) { - return treeTableModel.isCellEditable(nodeForRow(row), column); - } - - @Override - public void setValueAt(final Object value, final int row, final int column) { - treeTableModel.setValueAt(value, nodeForRow(row), column); - } - - /** - * Invokes fireTableDataChanged after all the pending events have been - * processed. SwingUtilities.invokeLater is used to handle this. - */ - protected void delayedFireTableDataChanged() { - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - fireTableDataChanged(); - } - }); - } -}