parent
0002b5a797
commit
07066578d5
@ -5,30 +5,26 @@ package eva2.gui;
|
|||||||
*/
|
*/
|
||||||
class Mnemonic {
|
class Mnemonic {
|
||||||
|
|
||||||
private char mnemonic;
|
private final char mnemonic;
|
||||||
private String text;
|
private final String text;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public Mnemonic(String s) {
|
public Mnemonic(String s) {
|
||||||
setString(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public void setString(String s) {
|
|
||||||
StringBuilder buf = new StringBuilder(s);
|
StringBuilder buf = new StringBuilder(s);
|
||||||
|
char c = Character.MIN_VALUE;
|
||||||
|
|
||||||
for (int i = 0; i < buf.length(); i++) {
|
for (int i = 0; i < buf.length(); i++) {
|
||||||
if (buf.charAt(i) == '&') {
|
if (buf.charAt(i) == '&') {
|
||||||
buf.deleteCharAt(i);
|
buf.deleteCharAt(i);
|
||||||
i++;
|
i++;
|
||||||
if (i < buf.length() && buf.charAt(i) != '&') {
|
if (i < buf.length() && buf.charAt(i) != '&') {
|
||||||
mnemonic = buf.charAt(i - 1);
|
c = buf.charAt(i - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
mnemonic = c;
|
||||||
text = buf.toString();
|
text = buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
44
src/test/java/eva2/gui/MnemonicTest.java
Normal file
44
src/test/java/eva2/gui/MnemonicTest.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package eva2.gui;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class MnemonicTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMnemonic() throws Exception {
|
||||||
|
Mnemonic m = new Mnemonic("No mnemonic");
|
||||||
|
|
||||||
|
// String without ampersand does not have a mnemonic
|
||||||
|
assertEquals(0, m.getMnemonic());
|
||||||
|
|
||||||
|
// Detects ampersand at beginning
|
||||||
|
m = new Mnemonic("&Fancy");
|
||||||
|
assertEquals('F', m.getMnemonic());
|
||||||
|
|
||||||
|
// Detects ampersand in the middle
|
||||||
|
m = new Mnemonic("Super&fancy");
|
||||||
|
assertEquals('f', m.getMnemonic());
|
||||||
|
|
||||||
|
// Handles ampersand at the end
|
||||||
|
m = new Mnemonic("Hellyea&");
|
||||||
|
assertEquals(0, m.getMnemonic());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetMnemonic() throws Exception {
|
||||||
|
Mnemonic m = new Mnemonic("Super &great");
|
||||||
|
assertEquals('g', m.getMnemonic());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetText() throws Exception {
|
||||||
|
Mnemonic m1 = new Mnemonic("The text");
|
||||||
|
assertEquals("The text", m1.getText());
|
||||||
|
|
||||||
|
Mnemonic m2 = new Mnemonic("&Mnemonic");
|
||||||
|
assertEquals("Mnemonic", m2.getText());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user