Skip to content

Commit

Permalink
adding Bare Percent label format option for issue rambaut#205
Browse files Browse the repository at this point in the history
Adding a new Bare Percent label format option, e.g. "95" instead of "95%". Haven't tested this and no idea if it works.
  • Loading branch information
zygoballus committed Apr 1, 2024
1 parent 3747295 commit 0e59c3c
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/figtree/treeviewer/painters/LabelPainterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package figtree.treeviewer.painters;

import figtree.ui.PercentFormat;
import figtree.ui.BarePercentFormat;
import figtree.ui.RomanFormat;
import jam.controlpalettes.AbstractController;
import jam.controlpalettes.ControllerListener;
Expand Down Expand Up @@ -173,7 +174,7 @@ public void stateChanged(ChangeEvent changeEvent) {
NumberFormat format = labelPainter.getNumberFormat();
int digits = format.getMaximumFractionDigits();

numericalFormatCombo = new JComboBox(new String[] { "Decimal", "Scientific", "Percent", "Roman"});
numericalFormatCombo = new JComboBox(new String[] { "Decimal", "Scientific", "Percent", "Bare Percent", "Roman"});
numericalFormatCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String formatType = (String) numericalFormatCombo.getSelectedItem();
Expand All @@ -185,6 +186,8 @@ public void actionPerformed(ActionEvent event) {
format = new DecimalFormat(SCIENTIFIC_NUMBER_FORMATTING);
} else if (formatType.equals("Percent")) {
format = new PercentFormat();
} else if (formatType.equals("Bare Percent")) {
format = new BarePercentFormat();
} else if (formatType.equals("Roman")) {
format = new RomanFormat();
}
Expand Down
5 changes: 4 additions & 1 deletion src/figtree/treeviewer/painters/LegendPainterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import figtree.treeviewer.TreeViewer;
import figtree.treeviewer.decorators.ColourDecorator;
import figtree.ui.PercentFormat;
import figtree.ui.BarePercentFormat;
import figtree.ui.RomanFormat;
import jam.controlpalettes.AbstractController;
import jam.controlpalettes.ControllerListener;
Expand Down Expand Up @@ -137,7 +138,7 @@ public void stateChanged(ChangeEvent changeEvent) {
NumberFormat format = legendPainter.getNumberFormat();
int digits = format.getMaximumFractionDigits();

numericalFormatCombo = new JComboBox(new String[] { "Decimal", "Scientific", "Percent", "Roman"});
numericalFormatCombo = new JComboBox(new String[] { "Decimal", "Scientific", "Percent", "Bare Percent", "Roman"});
numericalFormatCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String formatType = (String) numericalFormatCombo.getSelectedItem();
Expand All @@ -149,6 +150,8 @@ public void actionPerformed(ActionEvent event) {
format = new DecimalFormat(SCIENTIFIC_NUMBER_FORMATTING);
} else if (formatType.equals("Percent")) {
format = new PercentFormat();
} else if (formatType.equals("Bare Percent")) {
format = new BarePercentFormat();
} else if (formatType.equals("Roman")) {
format = new RomanFormat();
}
Expand Down
184 changes: 184 additions & 0 deletions src/figtree/ui/BarePercentFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* BarePercentFormat.java
*
* Copyright (C) 2006-2014 Andrew Rambaut
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package figtree.ui;

import java.text.*;

/**
* This NumberFormat converts numbers to and from percent notation.
* Once an instance has been created, the format and parse methods may be used
* as defined in java.text.NumberFormat.
*
* @version $Id$
*
* $HeadURL$
*
* $LastChangedBy$
* $LastChangedDate$
* $LastChangedRevision$
*/

public class BarePercentFormat extends NumberFormat
{
private final NumberFormat nf;

public BarePercentFormat() {
this.nf = new DecimalFormat();
}


/**
* Returns the maximum number of digits allowed in the fraction portion of a
* number.
*
* @see #setMaximumFractionDigits
*/
@Override
public int getMaximumFractionDigits() {
return nf.getMaximumFractionDigits();
}

/**
* Returns the maximum number of digits allowed in the integer portion of a
* number.
*
* @see #setMaximumIntegerDigits
*/
@Override
public int getMaximumIntegerDigits() {
return nf.getMaximumIntegerDigits();
}

/**
* Returns the minimum number of digits allowed in the fraction portion of a
* number.
*
* @see #setMinimumFractionDigits
*/
@Override
public int getMinimumFractionDigits() {
return nf.getMinimumFractionDigits();
}

/**
* Returns the minimum number of digits allowed in the integer portion of a
* number.
*
* @see #setMinimumIntegerDigits
*/
@Override
public int getMinimumIntegerDigits() {
return nf.getMinimumIntegerDigits();
}

/**
* Sets the maximum number of digits allowed in the fraction portion of a
* number. maximumFractionDigits must be >= minimumFractionDigits. If the
* new value for maximumFractionDigits is less than the current value
* of minimumFractionDigits, then minimumFractionDigits will also be set to
* the new value.
*
* @param newValue the maximum number of fraction digits to be shown; if
* less than zero, then zero is used. The concrete subclass may enforce an
* upper limit to this value appropriate to the numeric type being formatted.
* @see #getMaximumFractionDigits
*/
@Override
public void setMaximumFractionDigits(int newValue) {
nf.setMaximumFractionDigits(newValue);
}

/**
* Sets the minimum number of digits allowed in the integer portion of a
* number. minimumIntegerDigits must be <= maximumIntegerDigits. If the
* new value for minimumIntegerDigits exceeds the current value
* of maximumIntegerDigits, then maximumIntegerDigits will also be set to
* the new value
*
* @param newValue the minimum number of integer digits to be shown; if
* less than zero, then zero is used. The concrete subclass may enforce an
* upper limit to this value appropriate to the numeric type being formatted.
* @see #getMinimumIntegerDigits
*/
@Override
public void setMinimumIntegerDigits(int newValue) {
nf.setMinimumIntegerDigits(newValue);
}

/**
* Sets the minimum number of digits allowed in the fraction portion of a
* number. minimumFractionDigits must be <= maximumFractionDigits. If the
* new value for minimumFractionDigits exceeds the current value
* of maximumFractionDigits, then maximumIntegerDigits will also be set to
* the new value
*
* @param newValue the minimum number of fraction digits to be shown; if
* less than zero, then zero is used. The concrete subclass may enforce an
* upper limit to this value appropriate to the numeric type being formatted.
* @see #getMinimumFractionDigits
*/
@Override
public void setMinimumFractionDigits(int newValue) {
nf.setMinimumFractionDigits(newValue);
}

/**
* Sets the maximum number of digits allowed in the integer portion of a
* number. maximumIntegerDigits must be >= minimumIntegerDigits. If the
* new value for maximumIntegerDigits is less than the current value
* of minimumIntegerDigits, then minimumIntegerDigits will also be set to
* the new value.
*
* @param newValue the maximum number of integer digits to be shown; if
* less than zero, then zero is used. The concrete subclass may enforce an
* upper limit to this value appropriate to the numeric type being formatted.
* @see #getMaximumIntegerDigits
*/
@Override
public void setMaximumIntegerDigits(int newValue) {
nf.setMaximumIntegerDigits(newValue);
}

/**
* Specialization of format.
*
* @see java.text.Format#format
*/
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
return nf.format(number * 100.0, toAppendTo, pos);
}

/**
* Specialization of format.
*
* @see java.text.Format#format
*/
public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
return nf.format(number * 100, toAppendTo, pos);
}

/**
* @see java.text.Format#parseObject
*/
public Number parse(String source, ParsePosition parsePosition) {
return nf.parse(source, parsePosition).doubleValue() / 100.0;
}
}

0 comments on commit 0e59c3c

Please sign in to comment.