All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.bio.phylogeny.TreeDisplaySettings Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.bio.phylogeny;


import java.awt.*;

import com.hfg.exception.InvalidValueException;
import com.hfg.exception.ProgrammingException;
import com.hfg.graphics.units.GfxSize;
import com.hfg.graphics.units.GfxUnits;
import com.hfg.graphics.units.Pixels;
import com.hfg.setting.BooleanSetting;
import com.hfg.setting.ComplexSetting;
import com.hfg.setting.FloatSetting;
import com.hfg.setting.IntSetting;
import com.hfg.setting.Settings;
import com.hfg.setting.StringSetting;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;

//------------------------------------------------------------------------------
/**
 * Phylogenetic tree display options for use with NewickTree.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class TreeDisplaySettings extends Settings
{

   private static final String CLADOGRAM_STYLE                    = "CladogramStyle";
   private static final String TREE_WIDTH                         = "TreeWidth";
   private static final String ASSIGN_GROUPS_TO_SINGLETONS        = "AssignGroupsToSingletons";
   private static final String ALIGN_LEAF_NODE_LABELS             = "AlignLeaftNodeLabels";
   private static final String SHOW_SCALE                         = "ShowScale";
   private static final String ENABLE_DYNAMIC_GROUPING            = "EnableDynamicGrouping";
   private static final String BRANCH_LENGTH_TRAVERSAL_LIMIT      = "BranchLengthTraversalLimit";
   private static final String SHOW_BRANCH_LENGTH_TRAVERSAL_LIMIT = "ShowBranchLengthTraversalLimit";
   private static final String BLTL_ONCHANGE_CALLBACK             = "BLTL_OnChangeCallback";
   private static final String BLTL_SELECTION_COMPLETE_CALLBACK   = "BLTL_SelectionCompleteCallback";
   private static final String FONT                               = "Font";
   private static final String LEAF_LABEL_LEFT_PADDING            = "LeafLabelLeftPadding";
   private static final String SCALE_TICK_HEIGHT                  = "ScaleTickHeight";
   private static final String SCALE_PADDING_TOP                  = "ScalePaddingTop";

   private static CladogramStyle sDefaultCladogramStyle      = CladogramStyle.Rectangular;
   private static int     sDefaultTreeWidth                  = 500;
   private static boolean sDefaultAssignGroupsToSingletons   = false;
   private static boolean sDefaultAlignLeaftNodeLabels       = true;
   private static boolean sDefaultShowScale                  = true;
   private static boolean sDefaultShowBranchLengthTraversalLimit = false;
   private static boolean sDefaultEnableDynamicGrouping      = false;
   private static Font    sDefaultFont                       = Font.decode("Arial-PLAIN-10");
   private static int     sDefaultScaleTickHeight            = 10;
   private static GfxSize sDefaultLeafLabelLeftPadding       = new Pixels(10);
   private static GfxSize sDefaultScalePaddingTop            = new Pixels(40);

   //**************************************************************************
   // CONSTRUCTORS
   //**************************************************************************

   //---------------------------------------------------------------------------
   public TreeDisplaySettings()
   {

   }

   //---------------------------------------------------------------------------
   @Override
   protected void init()
   {
      super.init();

      add(new ComplexSetting(CLADOGRAM_STYLE, sDefaultCladogramStyle));
      add(new IntSetting(TREE_WIDTH, sDefaultTreeWidth));
      add(new BooleanSetting(ASSIGN_GROUPS_TO_SINGLETONS, sDefaultAssignGroupsToSingletons));
      add(new BooleanSetting(ALIGN_LEAF_NODE_LABELS, sDefaultAlignLeaftNodeLabels));
      add(new BooleanSetting(SHOW_SCALE, sDefaultShowScale));
      add(new BooleanSetting(ENABLE_DYNAMIC_GROUPING, sDefaultEnableDynamicGrouping));
      add(new FloatSetting(BRANCH_LENGTH_TRAVERSAL_LIMIT));
      add(new BooleanSetting(SHOW_BRANCH_LENGTH_TRAVERSAL_LIMIT, sDefaultShowBranchLengthTraversalLimit));
      add(new StringSetting(BLTL_ONCHANGE_CALLBACK));
      add(new StringSetting(BLTL_SELECTION_COMPLETE_CALLBACK));
      add(new StringSetting(FONT));
      add(new IntSetting(LEAF_LABEL_LEFT_PADDING));
      add(new IntSetting(SCALE_TICK_HEIGHT, sDefaultScaleTickHeight));
      add(new IntSetting(SCALE_PADDING_TOP));

      setFont(sDefaultFont);
      setLeafLabelLeftPadding(sDefaultLeafLabelLeftPadding);
      setScalePaddingTop(sDefaultScalePaddingTop);
   }

   //###########################################################################
   // PUBLIC METHODS
   //###########################################################################

   //---------------------------------------------------------------------------
   public TreeDisplaySettings setCladogramStyle(CladogramStyle inValue)
   {
      get(CLADOGRAM_STYLE).setValue(inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public CladogramStyle getCladogramStyle()
   {
      return (CladogramStyle) get(CLADOGRAM_STYLE).getValue();
   }


   //---------------------------------------------------------------------------
   public TreeDisplaySettings setTreeWidth(int inValue)
   {
      get(TREE_WIDTH).setValue(inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public Integer getTreeWidth()
   {
      return (Integer) get(TREE_WIDTH).getValue();
   }


   //---------------------------------------------------------------------------
   public TreeDisplaySettings setAssignGroupsToSingletons(boolean inValue)
   {
      get(ASSIGN_GROUPS_TO_SINGLETONS).setValue(inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public Boolean getAssignGroupsToSingletons()
   {
      return (Boolean) get(ASSIGN_GROUPS_TO_SINGLETONS).getValue();
   }


   //---------------------------------------------------------------------------
   public TreeDisplaySettings setAlignLeaftNodeLabels(boolean inValue)
   {
      get(ALIGN_LEAF_NODE_LABELS).setValue(inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public Boolean getAlignLeaftNodeLabels()
   {
      return (Boolean) get(ALIGN_LEAF_NODE_LABELS).getValue();
   }


   //---------------------------------------------------------------------------
   public TreeDisplaySettings setShowScale(boolean inValue)
   {
      get(SHOW_SCALE).setValue(inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public Boolean getShowScale()
   {
      return (Boolean) get(SHOW_SCALE).getValue();
   }


   //---------------------------------------------------------------------------
   /**
    When outputting the tree in SVG, this option allows for dynamically adjusting
    the branch length traversal limit to group closely related leaf nodes.
    * @param inValue whether or not to enable the dynamic grouping option
    * @return this Settings object to allow method chaining
    */
   public TreeDisplaySettings setEnableDynamicGrouping(boolean inValue)
   {
      get(ENABLE_DYNAMIC_GROUPING).setValue(inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public Boolean getEnableDynamicGrouping()
   {
      return (Boolean) get(ENABLE_DYNAMIC_GROUPING).getValue();
   }


   //---------------------------------------------------------------------------
   public TreeDisplaySettings setBranchLengthTraversalLimit(Float inValue)
   {
      get(BRANCH_LENGTH_TRAVERSAL_LIMIT).setValue(inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public Float getBranchLengthTraversalLimit()
   {
      return (Float) get(BRANCH_LENGTH_TRAVERSAL_LIMIT).getValue();
   }

   //--------------------------------------------------------------------------
   public void boundsCheckBranchLengthTraversalLimit(PhylogeneticTree inTree)
   {
      Float branchLengthTraversalLimit = getBranchLengthTraversalLimit();
      if (branchLengthTraversalLimit != null)
      {
         // Bounds check
         if (branchLengthTraversalLimit < 0)
         {
            branchLengthTraversalLimit = 0f;
         }
         else if (branchLengthTraversalLimit > 2 * inTree.getRootedTreeDistance())
         {
            branchLengthTraversalLimit = 2 * inTree.getRootedTreeDistance();
         }

         setBranchLengthTraversalLimit(branchLengthTraversalLimit);
      }
   }


   //---------------------------------------------------------------------------
   public TreeDisplaySettings setShowBranchLengthTraversalLimit(boolean inValue)
   {
      get(SHOW_BRANCH_LENGTH_TRAVERSAL_LIMIT).setValue(inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public Boolean getShowBranchLengthTraversalLimit()
   {
      return (Boolean) get(SHOW_BRANCH_LENGTH_TRAVERSAL_LIMIT).getValue();
   }


   //---------------------------------------------------------------------------
   /**
    While dynamically adjusting the branch length traversal limit to group closely
    related leaf nodes, this callback, if defined, is invoked so the changing BLTL
    value can be displayed.
    * @param inValue the javascript method to invoke (with the new BLTL value, and
    *                the drag handle object as arguments)
    * @return this Settings object to allow method chaining
    */
   public TreeDisplaySettings setBLTL_OnChangeCallback(String inValue)
   {
      get(BLTL_ONCHANGE_CALLBACK).setValue(inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public String getBLTL_OnChangeCallback()
   {
      return (String) get(BLTL_ONCHANGE_CALLBACK).getValue();
   }


   //---------------------------------------------------------------------------
   /**
    When the dynamic adjustment of the branch length traversal limit to group closely
    related leaf nodes is completed (end of drag event), this callback, if defined, is invoked.
    * @param inValue the javascript method to invoke (with the new BLTL value, and
    *                the drag handle object as arguments)
    * @return this Settings object to allow method chaining
    */
   public TreeDisplaySettings setBLTL_SelectionCompleteCallback(String inValue)
   {
      get(BLTL_SELECTION_COMPLETE_CALLBACK).setValue(inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public String getBLTL_SelectionCompleteCallback()
   {
      return (String) get(BLTL_SELECTION_COMPLETE_CALLBACK).getValue();
   }


   //---------------------------------------------------------------------------
   public TreeDisplaySettings setFont(Font inValue)
   {
      CharSequence value = null;

      if (inValue != null)
      {
         // TODO: This logic should be put in some utility function like FontUtil.encode()
         String style;
         switch (inValue.getStyle())
         {
            case Font.PLAIN:
               style = "PLAIN";
               break;
            case Font.BOLD:
               style = "BOLD";
               break;
            case Font.ITALIC:
               style = "ITALIC";
               break;
            case 3:
               style = "BOLDITALIC";
               break;
            default:
               throw new ProgrammingException("Unexpected Font style: " + inValue.getStyle() + "!");
         }

         StringBuilderPlus stringValue = new StringBuilderPlus().setDelimiter("-");

         stringValue.append(inValue.getFontName())
               .delimitedAppend(style)
               .delimitedAppend(inValue.getSize());
         value = stringValue;
      }

      get(FONT).setValue(value != null ? value.toString() : null);
      return this;
   }

   //---------------------------------------------------------------------------
   public Font getFont()
   {
      Font value = null;

      String stringValue = (String) get(FONT).getValue();
      if (StringUtil.isSet(stringValue))
      {
         value = Font.decode(stringValue);
      }

      return value;
   }


   //---------------------------------------------------------------------------
   public TreeDisplaySettings setLeafLabelLeftPadding(GfxSize inValue)
   {
      if (null == inValue)
      {
         throw new InvalidValueException("The leaf label left padding cannot be set to null!");
      }

      get(LEAF_LABEL_LEFT_PADDING).setValue(inValue != null ? inValue.toInt(GfxUnits.pixels) : null);
      return this;
   }

   //---------------------------------------------------------------------------
   public GfxSize getLeafLabelLeftPadding()
   {
      GfxSize padding = null;

      Integer pixelValue = (Integer) get(LEAF_LABEL_LEFT_PADDING).getValue();
      if (pixelValue != null)
      {
         padding = GfxSize.allocate(pixelValue, GfxUnits.pixels);
      }

      return padding;
   }


   //---------------------------------------------------------------------------
   public TreeDisplaySettings setScaleTickHeight(int inValue)
   {
      get(SCALE_TICK_HEIGHT).setValue(inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public Integer getScaleTickHeight()
   {
      return (Integer) get(SCALE_TICK_HEIGHT).getValue();
   }


   //---------------------------------------------------------------------------
   public TreeDisplaySettings setScalePaddingTop(GfxSize inValue)
   {
      if (null == inValue)
      {
         throw new InvalidValueException("The scale padding top cannot be set to null!");
      }

      get(SCALE_PADDING_TOP).setValue(inValue != null ? inValue.toInt(GfxUnits.pixels) : null);
      return this;
   }

   //---------------------------------------------------------------------------
   public GfxSize getScalePaddingTop()
   {
      GfxSize padding = null;

      Integer pixelValue = (Integer) get(SCALE_PADDING_TOP).getValue();
      if (pixelValue != null)
      {
         padding = GfxSize.allocate(pixelValue, GfxUnits.pixels);
      }

      return padding;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy