Skip to content

Commit

Permalink
Refactor to reduce code duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpietzsch committed Oct 15, 2023
1 parent b5b5324 commit 8f74436
Showing 1 changed file with 61 additions and 82 deletions.
143 changes: 61 additions & 82 deletions src/main/java/bdv/ui/settings/SettingsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

Expand Down Expand Up @@ -101,32 +103,7 @@ public class SettingsPanel extends JPanel
public void addPage( final SettingsPage page )
{
final String path = page.getTreePath();
final String[] parts = path.split( ">" );
DefaultMutableTreeNode current = root;
for ( final String part : parts )
{
final String text = part.trim();
DefaultMutableTreeNode next = null;
for ( int i = 0; i < current.getChildCount(); ++i )
{
final DefaultMutableTreeNode child = ( DefaultMutableTreeNode ) current.getChildAt( i );
final SettingsNodeData data = ( SettingsNodeData ) child.getUserObject();
if ( text.equals( data.name ) )
{
next = child;
break;
}
}

if ( next == null )
{
final SettingsNodeData data = new SettingsNodeData( text, null );
next = new DefaultMutableTreeNode( data );
model.insertNodeInto( next, current, current.getChildCount() );
}

current = next;
}
final DefaultMutableTreeNode current = getSettingsPageNode( path, true );

page.modificationListeners().add( modificationListener );

Expand All @@ -137,7 +114,7 @@ public void addPage( final SettingsPage page )
if ( pages.getComponents().length == 0 )
tree.getSelectionModel().setSelectionPath( new TreePath( model.getPathToRoot( current ) ) );

pages.add( data.page.getTreePath(), data.page.getJPanel() );
pages.add( page.getTreePath(), page.getJPanel() );
pages.revalidate();
pages.repaint();
}
Expand All @@ -152,25 +129,8 @@ public void addPage( final SettingsPage page )
*/
public void removePage( final String path )
{
final String[] parts = path.split( ">" );
DefaultMutableTreeNode current = root;
for ( final String part : parts )
{
final String text = part.trim();
DefaultMutableTreeNode next = null;
for ( int i = 0; i < current.getChildCount(); ++i )
{
final DefaultMutableTreeNode child = ( DefaultMutableTreeNode ) current.getChildAt( i );
final SettingsNodeData data = ( SettingsNodeData ) child.getUserObject();
if ( text.equals( data.name ) )
{
next = child;
break;
}
}
current = next;
}
if ( null == current )
final MutableTreeNode current = getSettingsPageNode( path, false );
if ( current == null )
return; // Path not found in the tree.

model.removeNodeFromParent( current );
Expand All @@ -183,6 +143,25 @@ public void removePage( final String path )
pages.repaint();
}

public void showPage( final String path )
{
final TreeNode current = getSettingsPageNode( path, false );
if ( current == null )
return; // Path not found in the tree.

for ( final SettingsPage page : getPages() )
{
if ( page.getTreePath().equals( path ) )
{
final TreePath tp = new TreePath( model.getPathToRoot( current ) );
tree.setSelectionPath( tp );
break;
}
}
pages.revalidate();
pages.repaint();
}

public SettingsPanel()
{
root = new DefaultMutableTreeNode( new SettingsNodeData( "root", null ) );
Expand Down Expand Up @@ -366,6 +345,42 @@ public void mouseClicked( final MouseEvent e )
breadcrumbs.repaint();
}

private DefaultMutableTreeNode getSettingsPageNode( final String path, final boolean createIfNotExists )
{
final String[] parts = path.split( ">" );
DefaultMutableTreeNode current = root;
for ( final String part : parts )
{
final String text = part.trim();
DefaultMutableTreeNode next = null;
for ( int i = 0; i < current.getChildCount(); ++i )
{
final DefaultMutableTreeNode child = ( DefaultMutableTreeNode ) current.getChildAt( i );
final SettingsNodeData data = ( SettingsNodeData ) child.getUserObject();
if ( text.equals( data.name ) )
{
next = child;
break;
}
}

if ( next == null )
{
if ( createIfNotExists )
{
final SettingsNodeData data = new SettingsNodeData( text, null );
next = new DefaultMutableTreeNode( data );
model.insertNodeInto( next, current, current.getChildCount() );
}
else
return null;
}

current = next;
}
return current;
}

private JLabel semiboldLabel( final String text )
{
return new JLabel( text )
Expand Down Expand Up @@ -434,40 +449,4 @@ public void paintComponent( final Graphics g )
super.paintComponent( g );
}
}

public void showPage( final String path )
{
final String[] parts = path.split( ">" );
DefaultMutableTreeNode current = root;
for ( final String part : parts )
{
final String text = part.trim();
DefaultMutableTreeNode next = null;
for ( int i = 0; i < current.getChildCount(); ++i )
{
final DefaultMutableTreeNode child = ( DefaultMutableTreeNode ) current.getChildAt( i );
final SettingsNodeData data = ( SettingsNodeData ) child.getUserObject();
if ( text.equals( data.name ) )
{
next = child;
break;
}
}
current = next;
}
if ( null == current )
return; // Path not found in the tree.

for ( final SettingsPage page : getPages() )
{
if ( page.getTreePath().equals( path ) )
{
final TreePath tp = new TreePath( model.getPathToRoot( current ) );
tree.setSelectionPath( tp );
break;
}
}
pages.revalidate();
pages.repaint();
}
}

0 comments on commit 8f74436

Please sign in to comment.