Subscript indices in PropertySheetPanel

- add subscriptIndices to StringTools
- add tests
This commit is contained in:
Fabian Becker 2015-12-15 11:02:21 +01:00
parent b58a589b24
commit 965049c4cb
3 changed files with 48 additions and 6 deletions

View File

@ -300,10 +300,7 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
continue; continue;
} // end try } // end try
// Add some specific display for some greeks here propertyTableModel.addRow(new Object[]{prepareLabel(name), newView});
name = StringTools.translateGreek(name);
name = StringTools.humaniseCamelCase(name);
propertyTableModel.addRow(new Object[]{name, newView});
} }
propertyTable.setToolTips(toolTips); propertyTable.setToolTips(toolTips);
@ -327,6 +324,15 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
setVisible(true); setVisible(true);
} }
private String prepareLabel(String label) {
// Add some specific display for some greeks here
label = StringTools.translateGreek(label);
label = StringTools.humaniseCamelCase(label);
label = StringTools.subscriptIndices(label);
label = "<html>" + label + "</html>";
return label;
}
private static JPanel buildTitledSeperator(String title) { private static JPanel buildTitledSeperator(String title) {
JPanel titledSeperator = new JPanel(new GridBagLayout()); JPanel titledSeperator = new JPanel(new GridBagLayout());
@ -558,8 +564,6 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
return infoPanel; return infoPanel;
} }
/** /**
* Get the html help file name. * Get the html help file name.
* *

View File

@ -479,6 +479,9 @@ public final class StringTools {
} }
/** /**
* Takes a full package name and returns the string
* after the last period (usually the class name).
*
* @param value The string to cut * @param value The string to cut
* @return Returns the class Name without package. * @return Returns the class Name without package.
*/ */
@ -491,6 +494,13 @@ public final class StringTools {
return className; // now is shortName return className; // now is shortName
} }
/**
* Takes a string and looks for greek letter names. If found,
* replaces them by their greek unicode counterparts.
*
* @param name A string
* @return String with greek letter names replaced
*/
public static String translateGreek(String name) { public static String translateGreek(String name) {
// Add some specific display for some greeks here // Add some specific display for some greeks here
final String[][] mapping = { final String[][] mapping = {
@ -549,5 +559,21 @@ public final class StringTools {
return name; return name;
} }
/**
* Takes a string and looks for trailing numbers. If present those numbers will
* be replaced by a HTML subscript. Make sure to wrap inside a html block when
* displaying as part of a JLabel.
*
* @param label
* @return
*/
public static String subscriptIndices(String label) {
// Trailing numbers
Pattern p = Pattern.compile("(\\d+)$");
Matcher m = p.matcher(label);
return m.replaceFirst("<sub>$1</sub>");
}
} }

View File

@ -98,4 +98,16 @@ public class StringToolsTest {
assertEquals("taur", StringTools.translateGreek("taur")); assertEquals("taur", StringTools.translateGreek("taur"));
assertEquals("taur12", StringTools.translateGreek("taur12")); assertEquals("taur12", StringTools.translateGreek("taur12"));
} }
@Test
public void testSubscriptIndices() throws Exception {
// Doesn't alter numbers at the start or mid string
assertEquals("12derp", StringTools.subscriptIndices("12derp"));
assertEquals("der12p", StringTools.subscriptIndices("der12p"));
// Replaces trailing numbers with a sub html tag
assertEquals("derp<sub>12</sub>", StringTools.subscriptIndices("derp12"));
assertEquals("This is a string <sub>12</sub>", StringTools.subscriptIndices("This is a string 12"));
assertEquals("ϕ<sub>10</sub>", StringTools.subscriptIndices("ϕ10"));
}
} }