* panel in web service preferences</li>
* </ul>
*
- * @author $author$
- * @version $Revision$
*/
public class Cache implements ApplicationSingletonI
{
}
/**
- * These methods are used when checking if the saved preference is different
- * to the default setting
+ * Returns the boolean property value for the given property name. If the
+ * value is absent then the default value is returned instead.
+ *
+ * @param property
+ * @param def
+ * @return
*/
-
- public static boolean getDefault(String property, boolean def)
+ public static boolean getDefault(String property, final boolean def)
{
String string = getProperty(property);
- if (string != null)
- {
- def = Boolean.valueOf(string).booleanValue();
- }
-
- return def;
+ return string == null ? def : Boolean.parseBoolean(string);
}
- public static int getDefault(String property, int def)
+ /**
+ * Returns the integer property value for the given property name. If the
+ * value is absent, or malformed (not an integer), then the default value is
+ * returned instead.
+ *
+ * @param property
+ * @param def
+ * @return
+ */
+ public static int getDefault(String property, final int def)
{
String string = getProperty(property);
if (string != null)
{
try
{
- def = Integer.parseInt(string);
+ return Integer.parseInt(string);
} catch (NumberFormatException e)
{
System.out.println("Error parsing int property '" + property
}
/**
- * Answers the value of the given property, or the supplied default value if
+ * Returns the value of the given property, or the supplied default value if
* the property is not set
+ *
+ * @param property
+ * @param def
+ * @return
*/
- public static String getDefault(String property, String def)
+ public static String getDefault(String property, final String def)
{
String value = getProperty(property);
return value == null ? def : value;
return getInstance().setPropertyImpl(key, obj, true);
}
+ /**
+ * Sets a property value for the running application, without saving it to the
+ * properties file
+ *
+ * @param key
+ * @param obj
+ */
public static void setPropertyNoSave(String key, String obj)
{
getInstance().setPropertyImpl(key, obj, false);
}
+ /**
+ * Sets a property value, and optionally also saves the current properties to
+ * file
+ *
+ * @param key
+ * @param obj
+ * @param andSave
+ * @return
+ */
private Object setPropertyImpl(String key, String obj, boolean andSave)
{
Object oldValue = null;
}
/**
- * remove the specified property from the jalview properties file
+ * Removes the specified property from the jalview properties file
*
* @param key
*/
getInstance().removePropertyImpl(key, true);
}
+ /**
+ * Removes the named property for the running application, without saving the
+ * properties file
+ *
+ * @param key
+ */
public static void removePropertyNoSave(String key)
{
getInstance().removePropertyImpl(key, false);
}
+ /**
+ * Removes the named property, and optionally saves the current properties to
+ * file
+ *
+ * @param key
+ * @param andSave
+ */
private void removePropertyImpl(String key, boolean andSave)
{
applicationProperties.remove(key);
}
}
+ /**
+ * Saves the current properties to file
+ */
public static void saveProperties()
{
getInstance().savePropertiesImpl();
}
/**
- * get the user's default colour if available
+ * Returns the Color value of the given property's value, or the supplied
+ * default colour if no property is found, or it cannot be parsed to a colour.
+ * Colours are normally saved as hex rgb values, though other formats may be
+ * parseable.
*
* @param property
- * @param defcolour
+ * @param defaultColour
* @return
+ * @see Cache#setColourPropertyNoSave(String, Color)
+ * @see ColorUtils#parseColourString(String)
*/
- public static Color getDefaultColour(String property, Color defcolour)
+ public static Color getDefaultColour(String property,
+ final Color defaultColour)
{
String colprop = getProperty(property);
if (colprop == null)
{
- return defcolour;
+ return defaultColour;
}
Color col = ColorUtils.parseColourString(colprop);
if (col == null)
log.warn("Couldn't parse '" + colprop + "' as a colour for "
+ property);
}
- return (col == null) ? defcolour : col;
+ return (col == null) ? defaultColour : col;
}
/**
- * store a colour as a Jalview user default property
+ * Stores a colour as a Jalview property, converted to hex values for rgb.
+ * Properties are not saved to file.
*
* @param property
* @param colour
*/
- public static void setColourProperty(String property, Color colour)
+ public static void setColourPropertyNoSave(String property, Color colour)
{
setProperty(property, jalview.util.Format.getHexString(colour));
}
/**
* Stores a formatted date in a jalview property, using a fixed locale.
+ * Updated properties are written out to the properties file.
*
* @param propertyName
* @param date
}
else
{
- Cache.removePropertyNoSave(Preferences.USER_DEFINED_COLOURS);
+ Cache.removeProperty(Preferences.USER_DEFINED_COLOURS);
}
}
}
}
else
{
- Cache.removePropertyNoSave(preferencesKey);
+ Cache.removeProperty(preferencesKey);
}
}
protColour.getSelectedItem().toString());
Cache.setPropertyNoSave(DEFAULT_COLOUR_NUC,
nucColour.getSelectedItem().toString());
- Cache.setColourProperty(ANNOTATIONCOLOUR_MIN,
+ Cache.setColourPropertyNoSave(ANNOTATIONCOLOUR_MIN,
minColour.getBackground());
- Cache.setColourProperty(ANNOTATIONCOLOUR_MAX,
+ Cache.setColourPropertyNoSave(ANNOTATIONCOLOUR_MAX,
maxColour.getBackground());
/*
* Save Overview settings
*/
- Cache.setColourProperty(GAP_COLOUR, gapColour.getBackground());
- Cache.setColourProperty(HIDDEN_COLOUR, hiddenColour.getBackground());
+ Cache.setColourPropertyNoSave(GAP_COLOUR, gapColour.getBackground());
+ Cache.setColourPropertyNoSave(HIDDEN_COLOUR,
+ hiddenColour.getBackground());
Cache.setPropertyNoSave(USE_LEGACY_GAP,
Boolean.toString(useLegacyGap.isSelected()));
Cache.setPropertyNoSave(SHOW_OV_HIDDEN_AT_START,
}
/**
- * DOCUMENT ME!
+ * Opens a file browser, and if a file is chosen, sets its path as the text of
+ * the 'startup file' text field
*/
@Override
public void startupFileTextfield_mouseClicked()
FileFormatI format = chooser.getSelectedFormat();
if (format != null)
{
- Cache.setPropertyNoSave("DEFAULT_FILE_FORMAT",
- format.getName());
+ /*
+ * saving properties to file is deferred to the 'OK' action
+ */
+ Cache.setPropertyNoSave("DEFAULT_FILE_FORMAT", format.getName());
}
startupFileTextfield
.setText(chooser.getSelectedFile().getAbsolutePath());
import java.util.Arrays;
/**
- * DOCUMENT ME!
+ * A utility class that provides a variety of formatting methods.
*
- * @author $author$
- * @version $Revision$
+ * Principle method {@code format} formats the number following printf
+ * conventions. Main limitation: Can only handle one format parameter at a time
+ * Use multiple Format objects to format more than one number
+ *
+ * @author unknown
*/
public class Format
{
private final String formatString;
/**
- * Creates a new Format object.
+ * Creates a new Format object given a format descriptor, which follows printf
+ * conventions The string has a prefix, a format code and a suffix. The prefix
+ * and suffix become part of the formatted output. The format code directs the
+ * formatting of the (single) parameter to be formatted. The code has the
+ * following structure
+ * <ul>
+ * <li>a % (required)
+ * <li>a modifier (optional)
+ * <dl>
+ * <dt>+
+ * <dd>forces display of + for positive numbers
+ * <dt>0
+ * <dd>show leading zeroes
+ * <dt>-
+ * <dd>align left in the field
+ * <dt>space
+ * <dd>prepend a space in front of positive numbers
+ * <dt>#
+ * <dd>use "alternate" format. Add 0 or 0x for octal or hexadecimal numbers.
+ * Don't suppress trailing zeroes in general floating point format.
+ * </dl>
+ * <li>an integer denoting field width (optional)
+ * <li>a period followed by an integer denoting precision (optional)
+ * <li>a format descriptor (required)
+ * <dl>
+ * <dt>f
+ * <dd>floating point number in fixed format
+ * <dt>e, E
+ * <dd>floating point number in exponential notation (scientific format). The
+ * E format results in an uppercase E for the exponent (1.14130E+003), the e
+ * format in a lowercase e.
+ * <dt>g, G
+ * <dd>floating point number in general format (fixed format for small
+ * numbers, exponential format for large numbers). Trailing zeroes are
+ * suppressed. The G format results in an uppercase E for the exponent (if
+ * any), the g format in a lowercase e.
+ * <dt>d, i
+ * <dd>integer in decimal
+ * <dt>x
+ * <dd>integer in hexadecimal
+ * <dt>o
+ * <dd>integer in octal
+ * <dt>s
+ * <dd>string
+ * <dt>c
+ * <dd>character
+ * </dl>
+ * </ul>
*
* @param s
- * DOCUMENT ME!
+ * the format descriptor
*/
- public Format(String s)
+ public Format(final String s)
{
formatString = s;
width = 0;
}
/**
- * Formats the number following printf conventions. Main limitation: Can only
- * handle one format parameter at a time Use multiple Format objects to format
- * more than one number
- *
- * @param s
- * the format string following printf conventions The string has a
- * prefix, a format code and a suffix. The prefix and suffix become
- * part of the formatted output. The format code directs the
- * formatting of the (single) parameter to be formatted. The code has
- * the following structure
- * <ul>
- * <li>a % (required)
- * <li>a modifier (optional)
- * <dl>
- * <dt>+
- * <dd>forces display of + for positive numbers
- * <dt>0
- * <dd>show leading zeroes
- * <dt>-
- * <dd>align left in the field
- * <dt>space
- * <dd>prepend a space in front of positive numbers
- * <dt>#
- * <dd>use "alternate" format. Add 0 or 0x for octal or hexadecimal
- * numbers. Don't suppress trailing zeroes in general floating point
- * format.
- * </dl>
- * <li>an integer denoting field width (optional)
- * <li>a period followed by an integer denoting precision (optional)
- * <li>a format descriptor (required)
- * <dl>
- * <dt>f
- * <dd>floating point number in fixed format
- * <dt>e, E
- * <dd>floating point number in exponential notation (scientific
- * format). The E format results in an uppercase E for the exponent
- * (1.14130E+003), the e format in a lowercase e.
- * <dt>g, G
- * <dd>floating point number in general format (fixed format for
- * small numbers, exponential format for large numbers). Trailing
- * zeroes are suppressed. The G format results in an uppercase E for
- * the exponent (if any), the g format in a lowercase e.
- * <dt>d, i
- * <dd>integer in decimal
- * <dt>x
- * <dd>integer in hexadecimal
- * <dt>o
- * <dd>integer in octal
- * <dt>s
- * <dd>string
- * <dt>c
- * <dd>character
- * </dl>
- * </ul>
- * @exception IllegalArgumentException
- * if bad format
+ * Returns a 6 character string consisting of the hex values of the colour's
+ * rgb values (left padded with zeroes if necessary)
*
+ * @param color
+ * @return
*/
public static String getHexString(java.awt.Color color)
{
import jalview.gui.JvOptionPane;
+import java.awt.Color;
+
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
+ "ms");
assertTrue(elapsed2 > elapsed1);
}
+
+ @Test(groups = "Functional")
+ public void testGetHexString()
+ {
+ /*
+ * r = 13 = d base 16, gets padded to 0d
+ * g = 103 = (16x6) + 7 = 67 base 16
+ * b = 219 = (16x13) + 11 = db base 16
+ */
+ Color c = new Color(13, 103, 219);
+ assertEquals(Format.getHexString(c), "0d67db");
+
+ assertEquals(Format.getHexString(Color.black), "000000");
+
+ assertEquals(Format.getHexString(Color.white), "ffffff");
+ }
}