From c5e9e6987402a15dc3a58e9d5e4d54be6760ef79 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Mon, 2 Feb 2015 18:16:44 +0000 Subject: [PATCH] JAL-1640 setStyle method and associated tests (required a sameStyle method to be added to interface to test ViewStyleI equivalence) --- src/jalview/api/ViewStyleI.java | 2 + src/jalview/viewmodel/AlignmentViewport.java | 6 ++ src/jalview/viewmodel/styles/ViewStyle.java | 87 ++++++++++++++++++++------ test/jalview/io/Jalview2xmlTests.java | 35 +++++++++++ 4 files changed, 110 insertions(+), 20 deletions(-) diff --git a/src/jalview/api/ViewStyleI.java b/src/jalview/api/ViewStyleI.java index baebd97..e022f4c 100644 --- a/src/jalview/api/ViewStyleI.java +++ b/src/jalview/api/ViewStyleI.java @@ -134,5 +134,7 @@ public interface ViewStyleI void setUpperCasebold(boolean upperCasebold); boolean isUpperCasebold(); + + boolean sameStyle(ViewStyleI them); } diff --git a/src/jalview/viewmodel/AlignmentViewport.java b/src/jalview/viewmodel/AlignmentViewport.java index 5e17e68..d94fa5d 100644 --- a/src/jalview/viewmodel/AlignmentViewport.java +++ b/src/jalview/viewmodel/AlignmentViewport.java @@ -2093,4 +2093,10 @@ public abstract class AlignmentViewport implements AlignViewportI, { viewStyle = new ViewStyle(settingsForView); } + + @Override + public boolean sameStyle(ViewStyleI them) + { + return viewStyle.sameStyle(them); + } } diff --git a/src/jalview/viewmodel/styles/ViewStyle.java b/src/jalview/viewmodel/styles/ViewStyle.java index ff19573..9f845e4 100644 --- a/src/jalview/viewmodel/styles/ViewStyle.java +++ b/src/jalview/viewmodel/styles/ViewStyle.java @@ -124,36 +124,42 @@ public class ViewStyle implements ViewStyleI public ViewStyle(ViewStyleI viewStyle) { - configureFrom(viewStyle); + ViewStyle.configureFrom(this, viewStyle); } public ViewStyle() { } - private void configureFrom(ViewStyleI viewStyle) + private static HashMap getters, isErs, setters; + static { - HashMap getters = new HashMap(), isErs = new HashMap(); - HashMap setters = new HashMap(); - // Match Getters and Setters - for (Method m : ViewStyleI.class.getMethods()) + getters = new HashMap(); + isErs = new HashMap(); + setters = new HashMap(); + // Match Getters and Setters + for (Method m : ViewStyleI.class.getMethods()) + { + if (m.getDeclaringClass() == ViewStyleI.class) { - if (m.getDeclaringClass() == ViewStyleI.class) + if (m.getName().startsWith("get")) { - 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); - } + 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()) { @@ -167,7 +173,7 @@ public class ViewStyle implements ViewStyleI { try { - setter.invoke(this, getter.invoke(viewStyle)); + setter.invoke(us, getter.invoke(viewStyle)); } catch (Exception q) { System.err.println("Unexpected exception setting view property " @@ -179,6 +185,41 @@ public class ViewStyle implements ViewStyleI } } + 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 */ @@ -822,4 +863,10 @@ public class ViewStyle implements ViewStyleI { this.wrappedWidth = w; } + + @Override + public boolean sameStyle(ViewStyleI them) + { + return equivalent(this, them); + } } diff --git a/test/jalview/io/Jalview2xmlTests.java b/test/jalview/io/Jalview2xmlTests.java index 75626e9..be9fcdc 100644 --- a/test/jalview/io/Jalview2xmlTests.java +++ b/test/jalview/io/Jalview2xmlTests.java @@ -23,6 +23,7 @@ package jalview.io; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import jalview.api.AlignmentViewPanel; +import jalview.api.ViewStyleI; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; @@ -34,6 +35,7 @@ import jalview.schemes.ColourSchemeI; import java.io.File; import org.junit.AfterClass; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -306,4 +308,37 @@ public class Jalview2xmlTests } } + + @Test + public void testCopyViewSettings() throws Exception + { + AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded( + "examples/exampleFile_2_7.jar", FormatAdapter.FILE); + Assert.assertTrue("Didn't read in the example file correctly.", af != null); + AlignmentViewPanel sps = null, groups = null; + for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels()) + { + if ("Spinach Feredoxin Structure".equals(ap.getViewName())) + { + sps = ap; + } + if (ap.getViewName().contains("MAFFT")) + { + groups = ap; + } + } + assertTrue("Couldn't find the structure view", sps != null); + assertTrue("Couldn't find the MAFFT view", groups != null); + + ViewStyleI structureStyle = sps.getAlignViewport().getViewStyle(); + ViewStyleI groupStyle = groups.getAlignViewport().getViewStyle(); + Assert.assertFalse(structureStyle.sameStyle(groupStyle)); + + groups.getAlignViewport().setViewStyle(structureStyle); + Assert.assertFalse(groupStyle.sameStyle(groups.getAlignViewport() + .getViewStyle())); + Assert.assertTrue(structureStyle.sameStyle(groups.getAlignViewport() + .getViewStyle())); + + } } -- 1.7.10.2