Continuation of commit 71aabb6

This commit is contained in:
Vhati 2013-11-22 00:10:01 -05:00
parent 71aabb6205
commit 1a5bf935df
6 changed files with 95 additions and 21 deletions

View file

@ -31,7 +31,7 @@ public class ChecklistTreePanel extends JPanel {
public ChecklistTreePanel() { public ChecklistTreePanel() {
super( new BorderLayout() ); super( new BorderLayout() );
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode( "Root" ); DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode( "Root", true );
treeModel = new DefaultTreeModel( rootNode ); treeModel = new DefaultTreeModel( rootNode );
tree = new JTree( treeModel ); tree = new JTree( treeModel );
tree.setCellRenderer( new GroupTreeCellRenderer() ); tree.setCellRenderer( new GroupTreeCellRenderer() );

View file

@ -27,6 +27,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List;
import java.util.Stack; import java.util.Stack;
import javax.swing.tree.DefaultTreeSelectionModel; import javax.swing.tree.DefaultTreeSelectionModel;
import javax.swing.tree.TreeModel; import javax.swing.tree.TreeModel;
@ -122,7 +123,7 @@ public class ChecklistTreeSelectionModel extends DefaultTreeSelectionModel {
TreePath[] selectionPaths = getSelectionPaths(); TreePath[] selectionPaths = getSelectionPaths();
if ( selectionPaths == null ) break; if ( selectionPaths == null ) break;
ArrayList toBeRemoved = new ArrayList(); List<TreePath> toBeRemoved = new ArrayList<TreePath>();
for ( int j=0; j < selectionPaths.length; j++ ) { for ( int j=0; j < selectionPaths.length; j++ ) {
if ( isDescendant( selectionPaths[j], path ) ) { if ( isDescendant( selectionPaths[j], path ) ) {
toBeRemoved.add( selectionPaths[j] ); toBeRemoved.add( selectionPaths[j] );
@ -209,7 +210,7 @@ public class ChecklistTreeSelectionModel extends DefaultTreeSelectionModel {
* Otherwise, the given path will be unselected, and nothing else will change. * Otherwise, the given path will be unselected, and nothing else will change.
*/ */
private void toggleRemoveSelection( TreePath path ) { private void toggleRemoveSelection( TreePath path ) {
Stack stack = new Stack(); Stack<TreePath> stack = new Stack<TreePath>();
TreePath parent = path.getParentPath(); TreePath parent = path.getParentPath();
while ( parent != null && !isPathSelected( parent ) ) { while ( parent != null && !isPathSelected( parent ) ) {
@ -226,8 +227,8 @@ public class ChecklistTreeSelectionModel extends DefaultTreeSelectionModel {
} }
while ( !stack.isEmpty() ) { while ( !stack.isEmpty() ) {
TreePath temp = (TreePath)stack.pop(); TreePath temp = stack.pop();
TreePath peekPath = ( stack.isEmpty() ? path : (TreePath)stack.peek() ); TreePath peekPath = ( stack.isEmpty() ? path : stack.peek() );
Object node = temp.getLastPathComponent(); Object node = temp.getLastPathComponent();
Object peekNode = peekPath.getLastPathComponent(); Object peekNode = peekPath.getLastPathComponent();
@ -243,16 +244,22 @@ public class ChecklistTreeSelectionModel extends DefaultTreeSelectionModel {
} }
public Enumeration getAllSelectedPaths() { public Enumeration<TreePath> getAllSelectedPaths() {
Enumeration<TreePath> result = null;
TreePath[] treePaths = getSelectionPaths(); TreePath[] treePaths = getSelectionPaths();
if ( treePaths == null ) { if ( treePaths == null ) {
return Collections.enumeration( Collections.EMPTY_LIST ); List<TreePath> pathsList = Collections.emptyList();
result = Collections.enumeration( pathsList );
}
else {
List<TreePath> pathsList = Arrays.asList( treePaths );
result = Collections.enumeration( pathsList );
if ( dig ) {
result = new PreorderEnumeration( result, model );
}
} }
Enumeration enumer = Collections.enumeration( Arrays.asList( treePaths ) ); return result;
if ( dig ) {
enumer = new PreorderEnumeration( enumer, model );
}
return enumer;
} }
} }

View file

@ -29,7 +29,7 @@ import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath; import javax.swing.tree.TreePath;
public class ChildrenEnumeration implements Enumeration { public class ChildrenEnumeration implements Enumeration<TreePath> {
private TreePath path; private TreePath path;
private TreeModel model; private TreeModel model;
@ -49,7 +49,7 @@ public class ChildrenEnumeration implements Enumeration {
} }
@Override @Override
public Object nextElement() { public TreePath nextElement() {
if( !hasMoreElements() ) throw new NoSuchElementException(); if( !hasMoreElements() ) throw new NoSuchElementException();
return path.pathByAddingChild( model.getChild( path.getLastPathComponent(), position++ ) ); return path.pathByAddingChild( model.getChild( path.getLastPathComponent(), position++ ) );

View file

@ -25,22 +25,23 @@ package net.vhati.modmanager.ui.tree;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List;
import java.util.Stack; import java.util.Stack;
import javax.swing.tree.TreeModel; import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath; import javax.swing.tree.TreePath;
public class PreorderEnumeration implements Enumeration { public class PreorderEnumeration implements Enumeration<TreePath> {
private TreeModel model; private TreeModel model;
protected Stack<Enumeration> stack = new Stack<Enumeration>(); protected Stack<Enumeration<TreePath>> stack = new Stack<Enumeration<TreePath>>();
public PreorderEnumeration( TreePath path, TreeModel model ) { public PreorderEnumeration( TreePath path, TreeModel model ) {
this( Collections.enumeration( Collections.singletonList( path ) ), model ); this( Collections.enumeration( Collections.singletonList( path ) ), model );
} }
public PreorderEnumeration( Enumeration enumer, TreeModel model ){ public PreorderEnumeration( Enumeration<TreePath> enumer, TreeModel model ){
this.model = model; this.model = model;
stack.push( enumer ); stack.push( enumer );
} }
@ -52,9 +53,9 @@ public class PreorderEnumeration implements Enumeration {
} }
@Override @Override
public Object nextElement() { public TreePath nextElement() {
Enumeration enumer = stack.peek(); Enumeration<TreePath> enumer = stack.peek();
TreePath path = (TreePath)enumer.nextElement(); TreePath path = enumer.nextElement();
if ( !enumer.hasMoreElements() ) stack.pop(); if ( !enumer.hasMoreElements() ) stack.pop();

View file

@ -0,0 +1,66 @@
package net.vhati.modmanager.ui.tree;
import java.util.ArrayList;
import java.util.List;
/**
* An implementation-agnostic model to pass between the GUI thread and the
* (de)serializer.
*/
public class TreeState {
protected TreeNodeState rootNodeState;
public TreeState() {
}
public void setRootNodeState( TreeNodeState rootNodeState ) {
this.rootNodeState = rootNodeState;
}
public TreeNodeState getRootNodeState() {
return rootNodeState;
}
public static class TreeNodeState {
protected Object userObject = null;
protected boolean expand = false;
protected List<TreeNodeState> children = null;
public TreeNodeState() {
this( false, false );
}
public TreeNodeState( boolean allowsChildren, boolean expand ) {
if ( allowsChildren ) {
this.expand = expand;
children = new ArrayList<TreeNodeState>();
}
}
public boolean getAllowsChildren() {
return ( children != null );
}
public List<TreeNodeState> getChildren() {
return children;
}
public void setUserObject( Object userObject ) {
this.userObject = userObject;
}
public Object getUserObject() {
return userObject;
}
}
}

View file

@ -71,7 +71,7 @@ public class TreeTransferHandler extends TransferHandler {
// For each length (shortest-first), iterate its paths. // For each length (shortest-first), iterate its paths.
// For each of those paths, search longer lengths' lists, // For each of those paths, search longer lengths' lists,
// removing any paths that are descendants of those short ancestor nodes. // removing any paths that are descendants of those short ancestor nodes.
List<Integer> lengthsList = new ArrayList( pathsByLengthMap.keySet() ); List<Integer> lengthsList = new ArrayList<Integer>( pathsByLengthMap.keySet() );
for ( int i=0; i < lengthsList.size(); i++ ) { for ( int i=0; i < lengthsList.size(); i++ ) {
for ( TreePath ancestorPath : pathsByLengthMap.get( lengthsList.get( i ) ) ) { for ( TreePath ancestorPath : pathsByLengthMap.get( lengthsList.get( i ) ) ) {
for ( int j=i+1; j < lengthsList.size(); j++ ) { for ( int j=i+1; j < lengthsList.size(); j++ ) {