1 package wjhk.jupload2.gui.filepanel.treeview; 2 3 import javax.swing.event.TreeModelListener; 4 import javax.swing.tree.TreeModel; 5 import javax.swing.tree.TreePath; 6 7 import wjhk.jupload2.exception.JUploadExceptionStopAddingFiles; 8 import wjhk.jupload2.gui.filepanel.FilePanelTableImp; 9 10 /** 11 * This code is taken from the tutorial written by Jörn Hameister, <A 12 * HREF="http://www.hameister.org/JavaSwingTreeTable.html">available here</A>.<BR/> 13 * <BR/> 14 * From Jörn sample, the main modifications are: 15 * <UL> 16 * <LI>Add of the getRoot and setRoot methods</LI> 17 * <LI>Capability to remove an item</LI> 18 * <LI>Transformation in a generic type</LI> 19 * <LI>Add of the reload method, to help refreshing the display.</LI> 20 * <LI>Add of the {@link #getColumnSizePercentage(int)} method, to allow the resizing of the column, along with the 21 * table resizing (see {@link FilePanelTableImp#componentResized(java.awt.event.ComponentEvent)} 22 * <UL> 23 * <BR/> 24 * First, the interface is TreeModel through the interface MyTreeTableModel expanded. This extension creates the 25 * possibility that a node (Node) multiple columns (Columns) can have. <BR/> 26 * <BR/> 27 * This class can not be generic, as TreeModel is not generic 28 * 29 * @author Jörn Hameister 30 */ 31 public interface MyTreeTableModel<T extends MyTreeNode> extends TreeModel { 32 33 /** 34 * Get the visible root of the JTree view. 35 * 36 * @param root 37 */ 38 public T getRoot(); 39 40 /** 41 * Set the visible root of the JTree view. 42 * 43 * @param root 44 */ 45 public void setRoot(T root); 46 47 /** 48 * Removes an item from the TreeView. This method just removed the given node. Of course, it detaches all 49 * descendants of this node, if any. <BR/> 50 * There is no impact on the parent, out of the 'detaching' of this node. 51 * 52 * @param item 53 * @see #removeAndClean(MyTreeNode) 54 */ 55 public void remove(T item); 56 57 /** 58 * Removes an item from the TreeView, detach its descendant, and remove the parent node if this node was the only 59 * child of the parent. If this item has children, then all children are also removed. 60 * 61 * @param item 62 */ 63 public void removeAndClean(T item); 64 65 /** 66 * This method removes all empty folders. Empty folder can typically be created, when adding files. For instance, if 67 * there is subfolder1/subolfer2, and subfolder2 is empty. Then it is not created. But avoiding to create also 68 * subfolder1 needs to parse the whole hierarchy for each folder. Too long. It's quicker to clean afterwards. 69 * 70 * @return true if at least one node is removed. The {@link #reload()} method is called. 71 */ 72 public boolean cleanHierarchy(); 73 74 /** 75 * <p> 76 * Invoke this method if you've modified the TreeNodes upon which this model depends. The model will notify all of 77 * its listeners that the model has changed. It will fire the events, necessary to update the layout caches and 78 * repaint the tree. The tree will <i>not</i> be properly refreshed if you call the JTree.repaint instead. 79 * </p> 80 * <p> 81 * This method will refresh the information about whole tree from the root. If only part of the tree should be 82 * refreshed, it is more effective to call {@link #reload(TreeNode)}. 83 * </p> 84 * <p> 85 * Note: code taken from the JDK DefaultTreeModel. 86 * </p> 87 */ 88 public void reload(); 89 90 /** 91 * Invoke this method if you've modified the TreeNodes upon which this model depends. The model will notify all of 92 * its listeners that the model has changed. It will fire the events, necessary to update the layout caches and 93 * repaint the tree. The tree will <i>not</i> be properly refreshed if you call the JTree.repaint instead. 94 * 95 * @param node - the tree node, from which the tree nodes have changed (inclusive). If you do not know this node, 96 * call {@link #reload()} instead. 97 */ 98 public void reload(T node); 99 100 /** 101 * Returns the number of available columns. 102 * 103 * @return Number of Columns 104 */ 105 public int getColumnCount(); 106 107 /** 108 * Returns the column name. 109 * 110 * @param column Column number 111 * @return Column name 112 */ 113 public String getColumnName(int column); 114 115 /** 116 * Retrieves the default colum percentage size of a column, that is: its percentage of the available width. 117 * 118 * @param col The index of the column to query. 119 * @return the default size of the requested column. 120 */ 121 public int getColumnSizePercentage(int col); 122 123 /** 124 * Returns the type (class) of a column. 125 * 126 * @param column Column number 127 * @return Class 128 */ 129 public Class<?> getColumnClass(int column); 130 131 /** 132 * Attach a new Object into the current hierarchy. This method is not mandatory. If not implemented, it should raise 133 * an {@link IllegalStateException]. 134 * 135 * @param o 136 * @return The total number of object added. It may be more than one, for instance, when adding a folder (including 137 * its descendants) in a file hierarchy 138 * @throws IllegalStateException If this method is called on an instance, which doesn't possess this capability 139 * @throws IllegalArgumentException If the given Object is not of the right type, to allow an attachment into the 140 * current hierarchy 141 * @throws JUploadExceptionStopAddingFiles When an error occurs, indicating that not all files were added 142 */ 143 public int attachObject(Object o) throws IllegalStateException, IllegalArgumentException, 144 JUploadExceptionStopAddingFiles; 145 146 // TODO Replace Object by a new parameterized type 147 148 /** 149 * Returns the value of a node in a column. 150 * 151 * @param node Node 152 * @param column Column number 153 * @return Value of the node in the column 154 */ 155 public Object getValueAt(T node, int column); 156 157 /** 158 * Check if a cell of a node in one column is editable. 159 * 160 * @param node Node 161 * @param column Column number 162 * @return true/false 163 */ 164 public boolean isCellEditable(T node, int column); 165 166 /** 167 * Sets a value for a node in one column. 168 * 169 * @param aValue New value 170 * @param node Node 171 * @param column Column number 172 */ 173 public void setValueAt(Object aValue, T node, int column); 174 175 /** 176 * @param tree the tree to set 177 */ 178 // FIXME rename this method to setTableCellRenderer 179 public void setTree(MyTreeTableCellRenderer tree); 180 181 /** 182 * This method must be called when one or more nodes are changed. 183 * 184 * @param source - the Object responsible for generating the event (typically the creator of the event object passes 185 * this for its value) 186 * @param path - a TreePath object that identifies the path to the parent of the modified item(s) 187 * @param childIndices - an array of int that specifies the index values of the modified items 188 * @param children - an array of Object containing the inserted, removed, or changed objects 189 * @see TreeModelListener 190 */ 191 public void fireTreeNodesChanged(Object source, TreePath path, int[] childIndices, T[] children); 192 193 /** 194 * This method must be called when one or more nodes are changed. 195 * 196 * @param source - the Object responsible for generating the event (typically the creator of the event object passes 197 * this for its value) 198 * @param path - a TreePath object that identifies the path to the parent of the modified item(s) 199 * @param childIndices - an array of int that specifies the index values of the modified items 200 * @param children - an array of Object containing the inserted, removed, or changed objects 201 * @see TreeModelListener 202 */ 203 public void fireTreeNodesInserted(Object source, TreePath path, int[] childIndices, T[] children); 204 205 /** 206 * This method must be called when one or more nodes are changed. 207 * 208 * @param source - the Object responsible for generating the event (typically the creator of the event object passes 209 * this for its value) 210 * @param path - a TreePath object that identifies the path to the parent of the modified item(s) 211 * @param childIndices - an array of int that specifies the index values of the modified items 212 * @param children - an array of Object containing the inserted, removed, or changed objects 213 * @see TreeModelListener 214 */ 215 public void fireTreeNodesRemoved(Object source, TreePath path, int[] childIndices, T[] children); 216 217 /** 218 * This method must be called when one or more nodes are changed. 219 * 220 * @param source - the Object responsible for generating the event (typically the creator of the event object passes 221 * this for its value) 222 * @param path - a TreePath object that identifies the path to the parent of the modified item(s) 223 * @param childIndices - an array of int that specifies the index values of the modified items 224 * @param children - an array of Object containing the inserted, removed, or changed objects 225 * @see TreeModelListener 226 */ 227 public void fireTreeStructureChanged(Object source, TreePath path, int[] childIndices, T[] children); 228 229 /** 230 * This method build a {@link TreePath} for the given item. This item should be in the current tree. 231 * 232 * @param item 233 * @return The build TreePath 234 * @throws IllegalArgumentException If item is not in the tree represented by this model. 235 */ 236 public TreePath getTreePath(T item); 237 238 /** 239 * Get the {@link TreePath} for the given object. This method is optional. It is typically only applicable if this 240 * hierarchy maps on another hierarchy (for instance a hierarchy of files, based on a local file system) 241 * 242 * @param o 243 * @return The TreePath, with the last component being the node linked to the o object. It returns null, if this 244 * object is not in the hierarchy. 245 * @throws IllegalStateException If this method is called on an instance, which doesn't possess this capability 246 * @throws IllegalArgumentException If the given Object is not of the right type, to allow an attachment into the 247 * current hierarchy 248 */ 249 public TreePath getTreePathForObject(Object o); 250 // TODO Replace Object by a new parameterized type 251 }