Commit d541e988 authored by Fabian Becker's avatar Fabian Becker

Subscript indices in PropertySheetPanel

- add subscriptIndices to StringTools
- add tests
parent ebe2e819
Pipeline #50 passed with stage
......@@ -300,10 +300,7 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
continue;
} // end try
// Add some specific display for some greeks here
name = StringTools.translateGreek(name);
name = StringTools.humaniseCamelCase(name);
propertyTableModel.addRow(new Object[]{name, newView});
propertyTableModel.addRow(new Object[]{prepareLabel(name), newView});
}
propertyTable.setToolTips(toolTips);
......@@ -327,6 +324,15 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
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) {
JPanel titledSeperator = new JPanel(new GridBagLayout());
......@@ -558,8 +564,6 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
return infoPanel;
}
/**
* Get the html help file name.
*
......
......@@ -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
* @return Returns the class Name without package.
*/
......@@ -491,6 +494,13 @@ public final class StringTools {
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) {
// Add some specific display for some greeks here
final String[][] mapping = {
......@@ -549,5 +559,21 @@ public final class StringTools {
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>");
}
}
......@@ -98,4 +98,16 @@ public class StringToolsTest {
assertEquals("taur", StringTools.translateGreek("taur"));
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"));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment