X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fviewmodel%2Fstyles%2FViewStyle.java;h=c15dba720906373bb68380b1af81fae58cbf8a49;hb=c19d2a91ca05e052e3408bf5852d88eb5d0608f1;hp=adc2bafeda69b470f5b767696eb71bf98209a024;hpb=e0c4027fce496e56101696f10559eb3ea94c7e7e;p=jalview.git diff --git a/src/jalview/viewmodel/styles/ViewStyle.java b/src/jalview/viewmodel/styles/ViewStyle.java index adc2baf..c15dba7 100644 --- a/src/jalview/viewmodel/styles/ViewStyle.java +++ b/src/jalview/viewmodel/styles/ViewStyle.java @@ -1,11 +1,29 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2) + * Copyright (C) 2015 The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.viewmodel.styles; -import java.awt.Color; -import java.lang.reflect.Method; -import java.util.HashMap; - import jalview.api.ViewStyleI; +import java.awt.Color; + /** * A container for holding alignment view properties. View properties are * data-independent, which means they can be safely copied between views @@ -17,7 +35,6 @@ import jalview.api.ViewStyleI; */ public class ViewStyle implements ViewStyleI { - private boolean abovePIDThreshold = false; int charHeight; @@ -151,102 +168,165 @@ public class ViewStyle implements ViewStyleI */ private boolean scaleProteinAsCdna = true; - public ViewStyle(ViewStyleI viewStyle) - { - ViewStyle.configureFrom(this, viewStyle); + /** + * Copy constructor + * + * @param vs + */ + public ViewStyle(ViewStyleI vs) + { + setAbovePIDThreshold(vs.getAbovePIDThreshold()); + setCentreColumnLabels(vs.isCentreColumnLabels()); + setCharHeight(vs.getCharHeight()); + setCharWidth(vs.getCharWidth()); + setColourAppliesToAllGroups(vs.getColourAppliesToAllGroups()); + setColourByReferenceSeq(vs.isColourByReferenceSeq()); + setColourText(vs.getColourText()); + setConservationColourSelected(vs.isConservationColourSelected()); + setConservationSelected(vs.getConservationSelected()); + setDisplayReferenceSeq(vs.isDisplayReferenceSeq()); + setFontName(vs.getFontName()); + setFontSize(vs.getFontSize()); + setFontStyle(vs.getFontStyle()); + setIdWidth(vs.getIdWidth()); + setIncrement(vs.getIncrement()); + setRenderGaps(vs.isRenderGaps()); + setRightAlignIds(vs.isRightAlignIds()); + setScaleAboveWrapped(vs.getScaleAboveWrapped()); + setScaleLeftWrapped(vs.getScaleLeftWrapped()); + setScaleProteinAsCdna(vs.isScaleProteinAsCdna()); + setScaleRightWrapped(vs.getScaleRightWrapped()); + setSeqNameItalics(vs.isSeqNameItalics()); + setShowAnnotation(vs.isShowAnnotation()); + setShowBoxes(vs.getShowBoxes()); + setShowColourText(vs.isShowColourText()); + setShowDBRefs(vs.isShowDBRefs()); + setShowHiddenMarkers(vs.getShowHiddenMarkers()); + setShowJVSuffix(vs.getShowJVSuffix()); + setShowNPFeats(vs.isShowNPFeats()); + setShowSequenceFeaturesHeight(vs.isShowSequenceFeaturesHeight()); + setShowSequenceFeatures(vs.isShowSequenceFeatures()); + setShowText(vs.getShowText()); + setShowUnconserved(vs.getShowUnconserved()); + setTextColour(vs.getTextColour()); + setTextColour2(vs.getTextColour2()); + setThreshold(vs.getThreshold()); + setThresholdTextColour(vs.getThresholdTextColour()); + setUpperCasebold(vs.isUpperCasebold()); + setWrapAlignment(vs.getWrapAlignment()); + setWrappedWidth(vs.getWrappedWidth()); + // ViewStyle.configureFrom(this, viewStyle); } public ViewStyle() { } - private static HashMap getters, isErs, setters; - static + /** + * Returns true if all attributes of the ViewStyles have the same value + */ + @Override + public boolean equals(Object other) { - 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.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()) + if (other == null || !(other instanceof ViewStyle)) { - 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 false; } - - return true; - } - - public boolean equals(ViewStyleI other) - { - return other == null ? false : equivalent(this, other); + ViewStyle vs = (ViewStyle) other; + + boolean match = (getAbovePIDThreshold() == vs.getAbovePIDThreshold() + && isCentreColumnLabels() == vs.isCentreColumnLabels() + && getCharHeight() == vs.getCharHeight() + && getCharWidth() == vs.getCharWidth() + && getColourAppliesToAllGroups() == vs + .getColourAppliesToAllGroups() + && isColourByReferenceSeq() == vs.isColourByReferenceSeq() + && getColourText() == vs.getColourText() + && isConservationColourSelected() == vs + .isConservationColourSelected() + && getConservationSelected() == vs.getConservationSelected() + && isDisplayReferenceSeq() == vs.isDisplayReferenceSeq() + && getFontSize() == vs.getFontSize() + && getFontStyle() == vs.getFontStyle() + && getIdWidth() == vs.getIdWidth() + && getIncrement() == vs.getIncrement() + && isRenderGaps() == vs.isRenderGaps() + && isRightAlignIds() == vs.isRightAlignIds() + && getScaleAboveWrapped() == vs.getScaleAboveWrapped() + && getScaleLeftWrapped() == vs.getScaleLeftWrapped() + && isScaleProteinAsCdna() == vs.isScaleProteinAsCdna() + && getScaleRightWrapped() == vs.getScaleRightWrapped() + && isSeqNameItalics() == vs.isSeqNameItalics() + && isShowAnnotation() == vs.isShowAnnotation() + && getShowBoxes() == vs.getShowBoxes() + && isShowColourText() == vs.isShowColourText() + && isShowDBRefs() == vs.isShowDBRefs() + && getShowHiddenMarkers() == vs.getShowHiddenMarkers() + && getShowJVSuffix() == vs.getShowJVSuffix() + && isShowNPFeats() == vs.isShowNPFeats() + && isShowSequenceFeaturesHeight() == vs + .isShowSequenceFeaturesHeight() + && isShowSequenceFeatures() == vs.isShowSequenceFeatures() + && getShowText() == vs.getShowText() + && getShowUnconserved() == vs.getShowUnconserved() + && getThreshold() == vs.getThreshold() + && getThresholdTextColour() == vs.getThresholdTextColour() + && isUpperCasebold() == vs.isUpperCasebold() + && getWrapAlignment() == vs.getWrapAlignment() && getWrappedWidth() == vs + .getWrappedWidth()); + /* + * and compare non-primitive types; syntax below will match null with null + * values + */ + match = match + && String.valueOf(getFontName()).equals( + String.valueOf(vs.getFontName())); + match = match + && String.valueOf(getTextColour()).equals( + String.valueOf(vs.getTextColour())); + match = match + && String.valueOf(getTextColour2()).equals( + String.valueOf(vs.getTextColour2())); + return match; + // return equivalent(this, (ViewStyle) other); + } + + /** + * Overridden to ensure that whenever vs1.equals(vs2) then vs1.hashCode() == + * vs2.hashCode() + */ + @Override + public int hashCode() + { + /* + * No need to include all properties, just a selection... + */ + int hash = 0; + int m = 1; + // Boolean.hashCode returns 1231 or 1237 + hash += m++ * Boolean.valueOf(this.abovePIDThreshold).hashCode(); + hash += m++ * Boolean.valueOf(this.centreColumnLabels).hashCode(); + hash += m++ * Boolean.valueOf(this.colourAppliesToAllGroups).hashCode(); + hash += m++ * Boolean.valueOf(this.displayReferenceSeq).hashCode(); + hash += m++ * Boolean.valueOf(this.renderGaps).hashCode(); + hash += m++ * Boolean.valueOf(this.rightAlignIds).hashCode(); + hash += m++ * Boolean.valueOf(this.scaleProteinAsCdna).hashCode(); + hash += m++ * Boolean.valueOf(this.scaleRightWrapped).hashCode(); + hash += m++ * Boolean.valueOf(this.seqNameItalics).hashCode(); + hash += m++ * Boolean.valueOf(this.showAnnotation).hashCode(); + hash += m++ * Boolean.valueOf(this.showBoxes).hashCode(); + hash += m++ * Boolean.valueOf(this.showdbrefs).hashCode(); + hash += m++ * Boolean.valueOf(this.showJVSuffix).hashCode(); + hash += m++ * Boolean.valueOf(this.showSequenceFeatures).hashCode(); + hash += m++ * Boolean.valueOf(this.showUnconserved).hashCode(); + hash += m++ * Boolean.valueOf(this.wrapAlignment).hashCode(); + hash += m++ * this.charHeight; + hash += m++ * this.charWidth; + hash += m++ * fontSize; + hash += m++ * fontStyle; + hash += m++ * idWidth; + hash += String.valueOf(this.fontName).hashCode(); + return hash; } /** @@ -559,7 +639,7 @@ public class ViewStyle implements ViewStyleI * @return the showSeqFeaturesHeight */ @Override - public boolean isShowSeqFeaturesHeight() + public boolean isShowSequenceFeaturesHeight() { return showSeqFeaturesHeight; } @@ -570,13 +650,6 @@ public class ViewStyle implements ViewStyleI return showSequenceFeatures; } - @Override - public boolean isShowSequenceFeaturesHeight() - { - - return showSeqFeaturesHeight; - } - /** * GUI state * @@ -590,7 +663,6 @@ public class ViewStyle implements ViewStyleI abovePIDThreshold = b; } - /** * DOCUMENT ME! * @@ -615,7 +687,6 @@ public class ViewStyle implements ViewStyleI this.charWidth = w; } - /** * @param value * indicating if subsequent colourscheme changes will be propagated @@ -794,7 +865,7 @@ public class ViewStyle implements ViewStyleI } @Override - public void setShowSeqFeaturesHeight(boolean selected) + public void setShowSequenceFeaturesHeight(boolean selected) { showSeqFeaturesHeight = selected; @@ -897,9 +968,9 @@ public class ViewStyle implements ViewStyleI } @Override - public boolean sameStyle(ViewStyleI them) + public boolean sameStyle(ViewStyleI that) { - return equivalent(this, them); + return this.equals(that); } @Override