in progress
authorcmzmasek <cmzmasek@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Wed, 30 Jul 2014 19:29:37 +0000 (19:29 +0000)
committercmzmasek <cmzmasek@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Wed, 30 Jul 2014 19:29:37 +0000 (19:29 +0000)
forester/java/src/org/forester/archaeopteryx/MainFrame.java
forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java
forester/java/src/org/forester/archaeopteryx/TreePanel.java

index 377c999..8bbedf1 100644 (file)
@@ -32,6 +32,8 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;\r
 import java.io.File;\r
 import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.HashSet;\r
 import java.util.LinkedList;\r
 import java.util.List;\r
 import java.util.Locale;\r
@@ -67,6 +69,7 @@ import org.forester.phylogeny.PhylogenyNode.NH_CONVERSION_SUPPORT_VALUE_STYLE;
 import org.forester.phylogeny.data.Annotation;\r
 import org.forester.phylogeny.data.NodeVisualData.NodeFill;\r
 import org.forester.phylogeny.data.NodeVisualData.NodeShape;\r
+import org.forester.phylogeny.iterators.PhylogenyNodeIterator;\r
 import org.forester.sdi.GSDI;\r
 import org.forester.sdi.GSDIR;\r
 import org.forester.sdi.SDIException;\r
@@ -162,6 +165,8 @@ public abstract class MainFrame extends JFrame implements ActionListener {
     JMenuItem                   _annotate_item;\r
     JMenuItem                   _remove_branch_color_item;\r
     JMenuItem                   _remove_visual_styles_item;\r
+    JMenuItem                   _deleted_selected_nodes_item;\r
+    JMenuItem                   _deleted_not_selected_nodes_item;\r
     // font size menu:\r
     JMenuItem                   _super_tiny_fonts_item;\r
     JMenuItem                   _tiny_fonts_item;\r
@@ -320,6 +325,18 @@ public abstract class MainFrame extends JFrame implements ActionListener {
             }\r
             midpointRoot();\r
         }\r
+        else if ( o == _deleted_selected_nodes_item ) {\r
+            if ( isSubtreeDisplayed() ) {\r
+                return;\r
+            }\r
+            deleteSelectedNodes( true );\r
+        }\r
+        else if ( o == _deleted_not_selected_nodes_item ) {\r
+            if ( isSubtreeDisplayed() ) {\r
+                return;\r
+            }\r
+            deleteSelectedNodes( false );\r
+        }\r
         else if ( o == _annotate_item ) {\r
             annotateSequences();\r
         }\r
@@ -526,6 +543,88 @@ public abstract class MainFrame extends JFrame implements ActionListener {
         _contentpane.repaint();\r
     }\r
 \r
+    private void deleteSelectedNodes( boolean delete ) {\r
+        final Phylogeny phy = getMainPanel().getCurrentPhylogeny();\r
+        if ( phy == null || phy.getNumberOfExternalNodes() < 2 ) {\r
+            return;\r
+        }\r
+        List<PhylogenyNode> nodes = null;\r
+        if ( ( getCurrentTreePanel().getFoundNodes0() != null ) || ( getCurrentTreePanel().getFoundNodes1() != null ) ) {\r
+            nodes = getCurrentTreePanel().getFoundNodesAsListOfPhylogenyNodes();\r
+        }\r
+        String function = "Retain";\r
+        if ( delete ) {\r
+            function = "Delete";\r
+        }\r
+        if ( ( nodes == null ) || nodes.isEmpty() ) {\r
+            JOptionPane\r
+                    .showMessageDialog( this,\r
+                                        "Need to select external nodes, either via direct selection or via the \"Search\" function",\r
+                                        "No external nodes selected to " + function.toLowerCase(),\r
+                                        JOptionPane.ERROR_MESSAGE );\r
+            return;\r
+        }\r
+        final int todo = nodes.size();\r
+        final int ext = phy.getNumberOfExternalNodes();\r
+        int res = todo;\r
+        if ( delete ) {\r
+            res = ext - todo;\r
+        }\r
+        \r
+        if ( res < 1 ) {\r
+            JOptionPane.showMessageDialog( this,\r
+                                           "Cannot delete all nodes",\r
+                                           "Attempt to delete all nodes ",\r
+                                           JOptionPane.ERROR_MESSAGE );\r
+            return;\r
+        }\r
+        final int result = JOptionPane.showConfirmDialog( null, "OK to " + function.toLowerCase() + " " + todo\r
+                + " external node(s), from a total of " + ext + " external nodes," + "\nresulting in tree with " + res\r
+                + " nodes", function + " external nodes", JOptionPane.OK_CANCEL_OPTION );\r
+        if ( result == JOptionPane.OK_OPTION ) {\r
+            if ( !delete ) {\r
+                final List<PhylogenyNode> to_delete = new ArrayList<PhylogenyNode>();\r
+                for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {\r
+                    final PhylogenyNode n = it.next();\r
+                    if ( !nodes.contains( n ) ) {\r
+                        to_delete.add( n );\r
+                    }\r
+                }\r
+                for( final PhylogenyNode n : to_delete ) {\r
+                    phy.deleteSubtree( n, true );\r
+                }\r
+            }\r
+            else {\r
+                for( final PhylogenyNode n : nodes ) {\r
+                    phy.deleteSubtree( n, true );\r
+                }\r
+            }\r
+            resetSearch();\r
+            getCurrentTreePanel().setNodeInPreorderToNull();\r
+            phy.externalNodesHaveChanged();\r
+            phy.clearHashIdToNodeMap();\r
+            phy.recalculateNumberOfExternalDescendants( true );\r
+            getCurrentTreePanel().resetNodeIdToDistToLeafMap();\r
+            getCurrentTreePanel().setEdited( true );\r
+            repaint();\r
+        }\r
+    }\r
+\r
+    void resetSearch() {\r
+        getMainPanel().getCurrentTreePanel().setFoundNodes0( null );\r
+        getMainPanel().getCurrentTreePanel().setFoundNodes1( null );\r
+        getMainPanel().getControlPanel().setSearchFoundCountsOnLabel0( 0 );\r
+        getMainPanel().getControlPanel().getSearchFoundCountsLabel0().setVisible( false );\r
+        getMainPanel().getControlPanel().getSearchTextField0().setText( "" );\r
+        getMainPanel().getControlPanel().getSearchResetButton0().setEnabled( false );\r
+        getMainPanel().getControlPanel().getSearchResetButton0().setVisible( false );\r
+        getMainPanel().getControlPanel().setSearchFoundCountsOnLabel1( 0 );\r
+        getMainPanel().getControlPanel().getSearchFoundCountsLabel1().setVisible( false );\r
+        getMainPanel().getControlPanel().getSearchTextField1().setText( "" );\r
+        getMainPanel().getControlPanel().getSearchResetButton1().setEnabled( false );\r
+        getMainPanel().getControlPanel().getSearchResetButton1().setVisible( false );\r
+    }\r
+\r
     public Configuration getConfiguration() {\r
         return _configuration;\r
     }\r
@@ -1426,7 +1525,11 @@ public abstract class MainFrame extends JFrame implements ActionListener {
 \r
     private void annotateSequences() {\r
         if ( getCurrentTreePanel() != null ) {\r
-            final Set<Long> nodes = getCurrentTreePanel().getFoundNodes0();\r
+            List<PhylogenyNode> nodes = null;\r
+            if ( ( getCurrentTreePanel().getFoundNodes0() != null )\r
+                    || ( getCurrentTreePanel().getFoundNodes1() != null ) ) {\r
+                nodes = getCurrentTreePanel().getFoundNodesAsListOfPhylogenyNodes();\r
+            }\r
             if ( ( nodes == null ) || nodes.isEmpty() ) {\r
                 JOptionPane\r
                         .showMessageDialog( this,\r
@@ -1475,8 +1578,7 @@ public abstract class MainFrame extends JFrame implements ActionListener {
                         desc = desc.replaceAll( "\\s+", " " );\r
                     }\r
                     if ( !ForesterUtil.isEmpty( ref ) || !ForesterUtil.isEmpty( desc ) ) {\r
-                        for( final Long id : nodes ) {\r
-                            final PhylogenyNode n = phy.getNode( id );\r
+                        for( final PhylogenyNode n : nodes ) {\r
                             ForesterUtil.ensurePresenceOfSequence( n );\r
                             final Annotation ann = ForesterUtil.isEmpty( ref ) ? new Annotation()\r
                                     : new Annotation( ref );\r
index f37cb7c..b688722 100644 (file)
@@ -1077,6 +1077,11 @@ public final class MainFrameApplication extends MainFrame {
         _tools_menu.add( _midpoint_root_item = new JMenuItem( "Midpoint-Root" ) );\r
         customizeJMenuItem( _midpoint_root_item );\r
         _tools_menu.addSeparator();\r
+        _tools_menu.add( _deleted_selected_nodes_item = new JMenuItem( "Delete Selected Nodes" ) );\r
+        customizeJMenuItem( _deleted_selected_nodes_item );\r
+        _tools_menu.add( _deleted_not_selected_nodes_item = new JMenuItem( "Retain Selected Nodes" ) );\r
+        customizeJMenuItem( _deleted_not_selected_nodes_item );\r
+        _tools_menu.addSeparator();\r
         _tools_menu.add( _collapse_species_specific_subtrees = new JMenuItem( "Collapse Species-Specific Subtrees" ) );\r
         customizeJMenuItem( _collapse_species_specific_subtrees );\r
         _tools_menu\r
index d3a1c38..3b0abfe 100644 (file)
@@ -2820,7 +2820,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         return getTreeFontSet().getFontMetricsLarge();
     }
 
-    private List<PhylogenyNode> getFoundNodesAsListOfPhylogenyNodes() {
+    List<PhylogenyNode> getFoundNodesAsListOfPhylogenyNodes() {
         final List<PhylogenyNode> additional_nodes = new ArrayList<PhylogenyNode>();
         if ( getFoundNodes0() != null ) {
             for( final Long id : getFoundNodes0() ) {