[Schmitzm-commits] r2152 - trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Wed Dec 5 10:35:16 CET 2012
Author: mojays
Date: 2012-12-05 10:35:16 +0100 (Wed, 05 Dec 2012)
New Revision: 2152
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree/DirectoryTree.java
Log:
DirectoryTree: update of tree structure modified
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree/DirectoryTree.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree/DirectoryTree.java 2012-11-30 00:24:20 UTC (rev 2151)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree/DirectoryTree.java 2012-12-05 09:35:16 UTC (rev 2152)
@@ -116,15 +116,20 @@
});
}
-// TODO: Ueberdenken und testen
-// /**
-// * Invalidates the tree, so that the directory structure will be
-// * reorganized on next update.
-// */
-// public void updateTreeStructure() {
-// ((DirectoryTreeModel)getModel()).invalidate();
-// repaint();
-// }
+ /**
+ * Reorganizes the tree structure according to file system.
+ */
+ public void updateTreeStructure() {
+ // Remember selected paths
+ File[] leafDirs = getLeafDirectories(); // open directories
+ File selectedDir = getSelectedDirectory(); // selected Directory
+ // Initialize tree model
+ ((DirectoryTreeModel)getModel()).initialize();
+ // Try to reset previously selected directories
+ for ( File dir : leafDirs )
+ expandPath( ((DirectoryTreeModel)getModel()).findPath(dir) );
+ setSelectedDirectory(selectedDir);
+ }
/**
* Returns the description used if root node text is empty
@@ -166,18 +171,39 @@
* enabled.
*/
public File[] getSelectedDirectories() {
+ return getFilesFromPaths( getSelectionPaths() );
+ }
+
+ /**
+ * Returns all directories which are currently opened in tree.
+ */
+ public File[] getLeafDirectories() {
+ Enumeration<TreePath> paths = getExpandedDescendants( new TreePath(getModel().getRoot()) );
+
List<File> dirList = new ArrayList<File>();
- if ( getSelectionPaths() != null )
- for (TreePath path : getSelectionPaths() ) {
+ if ( paths != null )
+ for (TreePath path = paths.nextElement(); paths.hasMoreElements(); path = paths.nextElement() ) {
File dir = convertTreePathLeafToFile(path);
if ( path != null )
dirList.add(dir);
}
return (File[])dirList.toArray(new File[0]);
-
}
-
+
/**
+ * Returns the corresponding directories to a list of tree paths.
+ */
+ protected File[] getFilesFromPaths(TreePath[] paths) {
+ List<File> dirList = new ArrayList<File>();
+ if ( paths != null )
+ for (TreePath path : paths ) {
+ File dir = convertTreePathLeafToFile(path);
+ if ( path != null )
+ dirList.add(dir);
+ }
+ return (File[])dirList.toArray(new File[0]);
+ }
+ /**
* Returns the directory represented by a tree path.
* @return {@code null} if empty path is given
*/
@@ -214,14 +240,11 @@
super( new DirectoryTreeNode(rootDir), true );
}
-// /**
-// * Invalidates the tree, so that the directory structure will be
-// * reorganized on next update.
-// */
-// public void invalidate() {
-// ((DirectoryTreeNode)getRoot()).revalidate();
-// }
-
+ public void initialize() {
+ File rootDir = (File)((DirectoryTreeNode)getRoot()).getUserObject();
+ setRoot( new DirectoryTreeNode(rootDir) );
+ }
+
/**
* Creates a {@link TreePath} for the specified directory.
*/
@@ -342,19 +365,6 @@
this.initialized = false;
}
-// /**
-// * If the node is already valid, its children are regenerated.
-// */
-// public void revalidate() {
-// if ( isValid() ) {
-// for (Object child : children) {
-// DirectoryTreeNode childNode = (DirectoryTreeNode)child;
-// childNode.revalidate();
-// }
-// generateChildren();
-// }
-// }
-
/**
* Returns the simple file name as representation of the node.
*/
More information about the Schmitzm-commits
mailing list