JAL-1640 more properties moved to style bean and rationalisation of layout update...
[jalview.git] / src / jalview / viewmodel / styles / ViewStyle.java
index c7430ec..1e97af4 100644 (file)
@@ -3,6 +3,8 @@ package jalview.viewmodel.styles;
 import jalview.api.ViewStyleI;
 
 import java.awt.Color;
+import java.lang.reflect.Method;
+import java.util.HashMap;
 
 /**
  * A container for holding alignment view properties. View properties are
@@ -22,12 +24,25 @@ public class ViewStyle implements ViewStyleI
 
   int charWidth;
 
+  int idWidth = -1;
+
   /**
    * gui state - changes to colour scheme propagated to all groups
    */
   private boolean colourAppliesToAllGroups;
 
   /**
+   * centre columnar annotation labels in displayed alignment annotation
+   */
+  boolean centreColumnLabels = false;
+
+  private boolean showdbrefs;
+
+  private boolean shownpfeats;
+
+  // --------END Structure Conservation
+
+  /**
    * colour according to the reference sequence defined on the alignment
    */
   private boolean colourByReferenceSeq = false;
@@ -121,6 +136,113 @@ public class ViewStyle implements ViewStyleI
   boolean upperCasebold = false;
 
   /**
+   * name of base font for view
+   */
+  private String fontName;
+  /**
+   * size for base font
+   */
+  private int fontSize;
+  
+  public ViewStyle(ViewStyleI viewStyle)
+  {
+    ViewStyle.configureFrom(this, viewStyle);
+  }
+
+  public ViewStyle()
+  {
+  }
+
+  private static HashMap<String, Method> getters, isErs, setters;
+  static
+  {
+    getters = new HashMap<String, Method>();
+    isErs = new HashMap<String, Method>();
+    setters = new HashMap<String, Method>();
+    // Match Getters and Setters
+    for (Method m : ViewStyleI.class.getMethods())
+    {
+      if (m.getDeclaringClass() == ViewStyleI.class)
+      {
+        if (m.getName().startsWith("get"))
+        {
+          getters.put(m.getName().substring(3), m);
+        }
+        if (m.getName().startsWith("is"))
+        {
+          isErs.put(m.getName().substring(2), m);
+        }
+        if (m.getName().startsWith("set"))
+        {
+          setters.put(m.getName().substring(3), m);
+        }
+      }
+    }
+  }
+
+  private static void configureFrom(ViewStyle us, ViewStyleI viewStyle)
+  {
+      // try and do the set thing
+      for (String prop : setters.keySet())
+      {
+        Method getter = getters.get(prop);
+        Method setter = setters.get(prop);
+        if (getter == null)
+        {
+          getter = isErs.get(prop);
+        }
+      if (getter != null && setter != null)
+      {
+        try
+        {
+          setter.invoke(us, getter.invoke(viewStyle));
+        } catch (Exception q)
+        {
+          System.err.println("Unexpected exception setting view property "
+                  + prop + " by reflection");
+          q.printStackTrace();
+        }
+
+      }
+      }
+  }
+
+  private static boolean equivalent(ViewStyle us, ViewStyleI them)
+  {
+    // look for properties we can set
+    for (String prop : setters.keySet())
+    {
+      Method getter = getters.get(prop);
+      if (getter == null)
+      {
+        getter = isErs.get(prop);
+      }
+      if (getter != null)
+      {
+        try
+        {
+          if (!getter.invoke(them).equals(getter.invoke(us)))
+          {
+            return false;
+          }
+        } catch (Exception q)
+        {
+          System.err.println("Unexpected exception testing equivalence of property "
+                  + prop + " by reflection");
+          q.printStackTrace();
+        }
+      }
+    }
+
+    return true;
+  }
+
+  public boolean equals(ViewStyleI other)
+  {
+    return other == null ? false : equivalent(this, other);
+  }
+
+  /**
    * @return the upperCasebold
    */
   @Override
@@ -149,6 +271,8 @@ public class ViewStyle implements ViewStyleI
    */
   int wrappedWidth;
 
+  private int fontStyle;
+
   /**
    * GUI state
    * 
@@ -459,6 +583,7 @@ public class ViewStyle implements ViewStyleI
     abovePIDThreshold = b;
   }
 
+  
   /**
    * DOCUMENT ME!
    * 
@@ -763,4 +888,120 @@ public class ViewStyle implements ViewStyleI
   {
     this.wrappedWidth = w;
   }
+
+  @Override
+  public boolean sameStyle(ViewStyleI them)
+  {
+    return equivalent(this, them);
+  }
+
+  @Override
+  public String getFontName()
+  {
+    return fontName;
+  }
+
+  @Override
+  public int getFontSize()
+  {
+    return fontSize;
+  }
+
+  @Override
+  public int getFontStyle()
+  {
+    return fontStyle;
+  }
+
+  @Override
+  public void setFontName(String name)
+  {
+    fontName = name;
+  }
+
+  @Override
+  public void setFontSize(int size)
+  {
+    fontSize = size;
+
+  }
+
+  @Override
+  public void setFontStyle(int style)
+  {
+    fontStyle = style;
+  }
+
+  @Override
+  public int getIdWidth()
+  {
+    return idWidth;
+  }
+
+  /**
+   * @param idWidth
+   *          the idWidth to set
+   */
+  @Override
+  public void setIdWidth(int idWidth)
+  {
+    this.idWidth = idWidth;
+  }
+
+  /**
+   * @return the centreColumnLabels
+   */
+  @Override
+  public boolean isCentreColumnLabels()
+  {
+    return centreColumnLabels;
+  }
+
+  /**
+   * @param centreColumnLabels
+   *          the centreColumnLabels to set
+   */
+  @Override
+  public void setCentreColumnLabels(boolean centreColumnLabels)
+  {
+    this.centreColumnLabels = centreColumnLabels;
+  }
+
+  /**
+   * @return the showdbrefs
+   */
+  @Override
+  public boolean isShowDBRefs()
+  {
+    return showdbrefs;
+  }
+
+  /**
+   * @param showdbrefs
+   *          the showdbrefs to set
+   */
+  @Override
+  public void setShowDBRefs(boolean showdbrefs)
+  {
+    this.showdbrefs = showdbrefs;
+  }
+
+  /**
+   * @return the shownpfeats
+   */
+  @Override
+  public boolean isShowNPFeats()
+  {
+    return shownpfeats;
+  }
+
+  /**
+   * @param shownpfeats
+   *          the shownpfeats to set
+   */
+  @Override
+  public void setShowNPFeats(boolean shownpfeats)
+  {
+    this.shownpfeats = shownpfeats;
+  }
 }