JAL-1640 copy constructor test and implementation by reflection
authorJim Procter <jprocter@dundee.ac.uk>
Mon, 2 Feb 2015 17:40:41 +0000 (17:40 +0000)
committerJim Procter <jprocter@dundee.ac.uk>
Mon, 2 Feb 2015 17:40:41 +0000 (17:40 +0000)
src/jalview/viewmodel/styles/ViewStyle.java
test/jalview/viewmodel/styles/TestviewStyle.java [new file with mode: 0644]

index c7430ec..ff19573 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
@@ -120,6 +122,63 @@ public class ViewStyle implements ViewStyleI
    */
   boolean upperCasebold = false;
 
+  public ViewStyle(ViewStyleI viewStyle)
+  {
+    configureFrom(viewStyle);
+  }
+
+  public ViewStyle()
+  {
+  }
+
+  private void configureFrom(ViewStyleI viewStyle)
+  {
+      HashMap<String, Method> getters = new HashMap<String, Method>(), isErs = new HashMap<String, Method>();
+      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);
+          }
+        }
+      }
+      // 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(this, getter.invoke(viewStyle));
+        } catch (Exception q)
+        {
+          System.err.println("Unexpected exception setting view property "
+                  + prop + " by reflection");
+          q.printStackTrace();
+        }
+
+      }
+      }
+  }
+
   /**
    * @return the upperCasebold
    */
diff --git a/test/jalview/viewmodel/styles/TestviewStyle.java b/test/jalview/viewmodel/styles/TestviewStyle.java
new file mode 100644 (file)
index 0000000..88ea82e
--- /dev/null
@@ -0,0 +1,23 @@
+package jalview.viewmodel.styles;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestviewStyle
+{
+
+  @Test
+  public void testSetterGetter()
+  {
+    ViewStyle orig = new ViewStyle();
+    orig.setCharHeight(23);
+    orig.setWrappedWidth(253);
+    orig.setWrapAlignment(true);
+    ViewStyle copy = new ViewStyle(orig);
+    orig.setWrapAlignment(false);
+    Assert.assertEquals(copy.getCharHeight(), orig.getCharHeight());
+    Assert.assertEquals(copy.getWrappedWidth(), orig.getWrappedWidth());
+    Assert.assertEquals(copy.getWrapAlignment(), !orig.getWrapAlignment());
+  }
+
+}