use of java.util.Comparator in project processing by processing.
the class Toolkit method setMenuMnemonics.
/**
* Removes all mnemonics, then sets a mnemonic for each menu and menu item
* recursively by these rules:
* <ol>
* <li> It tries to assign one of <a href="http://techbase.kde.org/Projects/Usability/HIG/Keyboard_Accelerators">
* KDE's defaults</a>.</li>
* <li> Failing that, it loops through the first letter of each word, where a word
* is a block of Unicode "alphabetical" chars, looking for an upper-case ASCII mnemonic
* that is not taken. This is to try to be relevant, by using a letter well-associated
* with the command. (MS guidelines) </li>
* <li> Ditto, but with lowercase. </li>
* <li> Next, it tries the second ASCII character, if its width >= half the width of
* 'A'. </li>
* <li> If the first letters are all taken/non-ASCII, then it loops through the
* ASCII letters in the item, widest to narrowest, seeing if any of them is not taken.
* To improve readability, it discriminates against decenders (qypgj), imagining they
* have 2/3 their actual width. (MS guidelines: avoid decenders). It also discriminates
* against vowels, imagining they have 2/3 their actual width. (MS and Gnome guidelines:
* avoid vowels.) </li>
* <li>Failing that, it will loop left-to-right for an available digit. This is a last
* resort because the normal setMnemonic dislikes them.</li>
* <li> If that doesn't work, it doesn't assign a mnemonic. </li>
* </ol>
*
* As a special case, strings starting "sketchbook → " have that bit ignored
* because otherwise the Recent menu looks awful. However, the name <tt>"sketchbook →
* Sketch"</tt>, for example, will have the 'S' of "Sketch" chosen, but the 's' of 'sketchbook
* will get underlined.
* No letter by an underscore will be assigned.
* Disabled on Mac, per Apple guidelines.
* <tt>menu</tt> may contain nulls.
*
* Author: George Bateman. Initial work Myer Nore.
* @param menu
* A menu, a list of menus or an array of menu items to set mnemonics for.
*/
public static void setMenuMnemonics(JMenuItem... menu) {
if (Platform.isMacOS())
return;
if (menu.length == 0)
return;
// The English is http://techbase.kde.org/Projects/Usability/HIG/Keyboard_Accelerators,
// made lowercase.
// Nothing but [a-z] except for '&' before mnemonics and regexes for changable text.
final String[] kdePreDefStrs = { "&file", "&new", "&open", "open&recent", "&save", "save&as", "saveacop&y", "saveas&template", "savea&ll", "reloa&d", "&print", "printpre&view", "&import", "e&xport", "&closefile", "clos&eallfiles", "&quit", "&edit", "&undo", "re&do", "cu&t", "©", "&paste", "&delete", "select&all", "dese&lect", "&find", "find&next", "findpre&vious", "&replace", "&gotoline", "&view", "&newview", "close&allviews", "&splitview", "&removeview", "splitter&orientation", "&horizontal", "&vertical", "view&mode", "&fullscreenmode", "&zoom", "zoom&in", "zoom&out", "zoomtopage&width", "zoomwhole&page", "zoom&factor", "&insert", "&format", "&go", "&up", "&back", "&forward", "&home", "&go", "&previouspage", "&nextpage", "&firstpage", "&lastpage", "read&updocument", "read&downdocument", "&back", "&forward", "&gotopage", "&bookmarks", "&addbookmark", "bookmark&tabsasfolder", "&editbookmarks", "&newbookmarksfolder", "&tools", "&settings", "&toolbars", "configure&shortcuts", "configuretool&bars", "&configure.*", "&help", ".+&handbook", "&whatsthis", "report&bug", "&aboutprocessing", "about&kde", // de
"&beenden", // de
"&suchen", // Preferências; pt
"&preferncias", // Preferências; pt
"&sair", // fr
"&rechercher" };
Pattern[] kdePreDefPats = new Pattern[kdePreDefStrs.length];
for (int i = 0; i < kdePreDefStrs.length; i++) {
kdePreDefPats[i] = Pattern.compile(kdePreDefStrs[i].replace("&", ""));
}
final Pattern nonAAlpha = Pattern.compile("[^A-Za-z]");
FontMetrics fmTmp = null;
for (JMenuItem m : menu) {
if (m != null) {
fmTmp = m.getFontMetrics(m.getFont());
break;
}
}
// All null menuitems; would fail.
if (fmTmp == null)
return;
// Hack for accessing variable in comparator.
final FontMetrics fm = fmTmp;
final Comparator<Character> charComparator = new Comparator<Character>() {
char[] baddies = "qypgjaeiouQAEIOU".toCharArray();
public int compare(Character ch1, Character ch2) {
// Discriminates against descenders for readability, per MS
// Human Interface Guide, and vowels per MS and Gnome.
float w1 = fm.charWidth(ch1), w2 = fm.charWidth(ch2);
for (char bad : baddies) {
if (bad == ch1)
w1 *= 0.66f;
if (bad == ch2)
w2 *= 0.66f;
}
return (int) Math.signum(w2 - w1);
}
};
// Holds only [0-9a-z], not uppercase.
// Prevents X != x, so "Save" and "Save As" aren't both given 'a'.
final List<Character> taken = new ArrayList<Character>(menu.length);
char firstChar;
char[] cleanChars;
Character[] cleanCharas;
// METHOD 1: attempt to assign KDE defaults.
for (JMenuItem jmi : menu) {
if (jmi == null)
continue;
if (jmi.getText() == null)
continue;
// Reset all mnemonics.
jmi.setMnemonic(0);
String asciiName = nonAAlpha.matcher(jmi.getText()).replaceAll("");
String lAsciiName = asciiName.toLowerCase();
for (int i = 0; i < kdePreDefStrs.length; i++) {
if (kdePreDefPats[i].matcher(lAsciiName).matches()) {
char mnem = asciiName.charAt(kdePreDefStrs[i].indexOf("&"));
jmi.setMnemonic(mnem);
jmi.setDisplayedMnemonicIndex(jmi.getText().indexOf(mnem));
// to lowercase
taken.add((char) (mnem | 32));
break;
}
}
}
// Where KDE defaults fail, use an algorithm.
algorithmicAssignment: for (JMenuItem jmi : menu) {
if (jmi == null)
continue;
if (jmi.getText() == null)
continue;
// Already assigned.
if (jmi.getMnemonic() != 0)
continue;
// The string can't be made lower-case as that would spoil
// the width comparison.
String cleanString = jmi.getText();
if (cleanString.startsWith("sketchbook → "))
cleanString = cleanString.substring(13);
if (cleanString.length() == 0)
continue;
// First, ban letters by underscores.
final List<Character> banned = new ArrayList<Character>();
for (int i = 0; i < cleanString.length(); i++) {
if (cleanString.charAt(i) == '_') {
if (i > 0)
banned.add(Character.toLowerCase(cleanString.charAt(i - 1)));
if (i + 1 < cleanString.length())
banned.add(Character.toLowerCase(cleanString.charAt(i + 1)));
}
}
// because there could be non-ASCII letters in a word.
for (String wd : cleanString.split("[^\\p{IsAlphabetic}]")) {
if (wd.length() == 0)
continue;
firstChar = wd.charAt(0);
if (taken.contains(Character.toLowerCase(firstChar)))
continue;
if (banned.contains(Character.toLowerCase(firstChar)))
continue;
if ('A' <= firstChar && firstChar <= 'Z') {
jmi.setMnemonic(firstChar);
jmi.setDisplayedMnemonicIndex(jmi.getText().indexOf(firstChar));
// tolowercase
taken.add((char) (firstChar | 32));
continue algorithmicAssignment;
}
}
// METHOD 3: Lowercase starts of words.
for (String wd : cleanString.split("[^\\p{IsAlphabetic}]")) {
if (wd.length() == 0)
continue;
firstChar = wd.charAt(0);
if (taken.contains(Character.toLowerCase(firstChar)))
continue;
if (banned.contains(Character.toLowerCase(firstChar)))
continue;
if ('a' <= firstChar && firstChar <= 'z') {
jmi.setMnemonic(firstChar);
jmi.setDisplayedMnemonicIndex(jmi.getText().indexOf(firstChar));
// is lowercase
taken.add(firstChar);
continue algorithmicAssignment;
}
}
// METHOD 4: Second wide-enough ASCII letter.
cleanString = nonAAlpha.matcher(jmi.getText()).replaceAll("");
if (cleanString.length() >= 2) {
char ascii2nd = cleanString.charAt(1);
if (!taken.contains((char) (ascii2nd | 32)) && !banned.contains((char) (ascii2nd | 32)) && fm.charWidth('A') <= 2 * fm.charWidth(ascii2nd)) {
jmi.setMnemonic(ascii2nd);
jmi.setDisplayedMnemonicIndex(jmi.getText().indexOf(ascii2nd));
taken.add((char) (ascii2nd | 32));
continue algorithmicAssignment;
}
}
// METHOD 5: charComparator over all ASCII letters.
cleanChars = cleanString.toCharArray();
cleanCharas = new Character[cleanChars.length];
for (int i = 0; i < cleanChars.length; i++) {
cleanCharas[i] = new Character(cleanChars[i]);
}
// sorts in increasing order
Arrays.sort(cleanCharas, charComparator);
for (char mnem : cleanCharas) {
if (taken.contains(Character.toLowerCase(mnem)))
continue;
if (banned.contains(Character.toLowerCase(mnem)))
continue;
// NB: setMnemonic(char) doesn't want [^A-Za-z]
jmi.setMnemonic(mnem);
jmi.setDisplayedMnemonicIndex(jmi.getText().indexOf(mnem));
taken.add(Character.toLowerCase(mnem));
continue algorithmicAssignment;
}
// METHOD 6: Digits as last resort.
for (char digit : jmi.getText().replaceAll("[^0-9]", "").toCharArray()) {
if (taken.contains(digit))
continue;
if (banned.contains(digit))
continue;
jmi.setMnemonic(KeyEvent.VK_0 + digit - '0');
// setDisplayedMnemonicIndex() unneeded: no case issues.
taken.add(digit);
continue algorithmicAssignment;
}
}
// Finally, RECURSION.
for (JMenuItem jmi : menu) {
if (jmi instanceof JMenu)
setMenuMnemsInside((JMenu) jmi);
}
}
use of java.util.Comparator in project robovm by robovm.
the class OldTreeMapTest method test_ConstructorLjava_util_Comparator.
public void test_ConstructorLjava_util_Comparator() {
// Test for method java.util.TreeMap(java.util.Comparator)
Comparator comp = new ReversedComparator();
TreeMap reversedTreeMap = new TreeMap(comp);
assertTrue("TreeMap answered incorrect comparator", reversedTreeMap.comparator() == comp);
reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
assertTrue("TreeMap does not use comparator (firstKey was incorrect)", reversedTreeMap.firstKey().equals(new Integer(2).toString()));
assertTrue("TreeMap does not use comparator (lastKey was incorrect)", reversedTreeMap.lastKey().equals(new Integer(1).toString()));
}
use of java.util.Comparator in project robovm by robovm.
the class OldTreeMapTest method test_ConstructorLjava_util_SortedMap.
public void test_ConstructorLjava_util_SortedMap() {
// Test for method java.util.TreeMap(java.util.SortedMap)
Comparator comp = new ReversedComparator();
TreeMap reversedTreeMap = new TreeMap(comp);
reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
TreeMap anotherTreeMap = new TreeMap(reversedTreeMap);
assertTrue("New tree map does not answer correct comparator", anotherTreeMap.comparator() == comp);
assertTrue("TreeMap does not use comparator (firstKey was incorrect)", anotherTreeMap.firstKey().equals(new Integer(2).toString()));
assertTrue("TreeMap does not use comparator (lastKey was incorrect)", anotherTreeMap.lastKey().equals(new Integer(1).toString()));
try {
new TreeMap((SortedMap) null);
fail("NullPointerException expected");
} catch (NullPointerException e) {
//expected
}
}
use of java.util.Comparator in project robovm by robovm.
the class CollectionsTest method test_reverseOrder.
/**
* java.util.Collections#reverseOrder()
*/
public void test_reverseOrder() {
// Test for method java.util.Comparator
// java.util.Collections.reverseOrder()
// assumes no duplicates in ll
Comparator comp = Collections.reverseOrder();
LinkedList list2 = new LinkedList(ll);
Collections.sort(list2, comp);
final int llSize = ll.size();
for (int counter = 0; counter < llSize; counter++) assertTrue("New comparator does not reverse sorting order", ll.get(counter) == list2.get(llSize - counter - 1));
}
use of java.util.Comparator in project robovm by robovm.
the class CollectionsTest method test_sortLjava_util_ListLjava_util_Comparator.
/**
* java.util.Collections#sort(java.util.List, java.util.Comparator)
*/
public void test_sortLjava_util_ListLjava_util_Comparator() {
// Test for method void java.util.Collections.sort(java.util.List,
// java.util.Comparator)
Comparator comp = new ReversedMyIntComparator();
try {
Collections.sort(null, comp);
fail("Expected NullPointerException for null list parameter");
} catch (NullPointerException e) {
}
Collections.shuffle(myll);
Collections.sort(myll, comp);
final int llSize = myll.size();
for (int counter = 0; counter < llSize - 1; counter++) {
assertTrue("Sorting shuffled list with custom comparator resulted in unsorted list", ((MyInt) myll.get(counter)).compareTo((MyInt) myll.get(counter + 1)) >= 0);
}
ArrayList al = new ArrayList();
al.add("String");
al.add(new Integer(1));
al.add(new Double(3.14));
try {
Collections.sort(al, comp);
fail("ClassCastException expected");
} catch (ClassCastException e) {
//expected
}
Mock_ArrayList mal = new Mock_ArrayList();
mal.add(new MyInt(1));
mal.add(new MyInt(2));
try {
Collections.sort(mal, comp);
fail("UnsupportedOperationException expected");
} catch (UnsupportedOperationException e) {
//expected
}
}
Aggregations