IndexBensUtils.java
/*
* "COM.bensoft.base" by Michael Benson - 7/27/97
*
* "BensUtils" contains various static utilities.
*/
package COM.bensoft.base;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.net.*;
/**
* BensUtils implements simple utilities that are used in various
* places. The methods are all static.
*
* @author Michael Benson
*/
public class BensUtils
{
/**
* Capitalize all words in a phrase. The input string can
* have any number of words separated by spaces. All words
* will be capitalized.
*
* @param str the string to be capitalized.
*/
public static String capitalizePhrase (String str)
{
StringBuffer buf = new StringBuffer();
Character c;
// Capitalize All Words In A Phrase:
boolean prevSpace = true;
for (int i = 0; i < str.length(); i++) {
if (prevSpace) {
c = new Character(Character.toUpperCase(str.charAt(i)));
buf.append(c);
prevSpace = false;
} else {
buf.append(str.charAt(i));
}
if (str.charAt(i) == ' ') {
prevSpace = true;
}
}
return buf.toString();
}
/**
* Get the indefinite article that should precede a word. Words that
* begin with a vowel should return "an". Words that begin with a
* consonant should return "a".
*
* @param str the word to be preceded by indefinite article.
* @param capitalize true if article is to be capitalized,
* false if not.
*/
public static String getIndefiniteArticle (String str, boolean capitalize)
{
String aoran = null;
char c = str.charAt(0);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c== 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c== 'U') {
aoran = new String("an");
} else {
aoran = new String("a");
}
if (capitalize) {
aoran = capitalizePhrase(aoran);
}
return aoran;
}
/**
* Create a menu bar. The input is an array of strings. Each string
* contains the text of the menu or menu item. If the string begins
* with ">", it is a menu item to be added to the last menu. If it
* begins with "-", a separator is to be added. For example, suppose
* the input strings are as follows:
*
* String menuStrs[] = {"File", ">Open", "-", ">Quit", "Edit", ">Clear"};
*
* This will create two menus, "File" and "Edit". The "File" menu will
* contain 3 items, "Open", a separator, and "Quit". The "Edit" menu
* will contain 1 item, "Clear".
*
* Eventually, you should be able to add command keys (accelerators) by
* using the following form: ">Quit#Q". This will add a menu item "Quit"
* with the command key equivalent "Q". This is not implemented yet,
* however you can use the command key notation and nothing will happen
* for now.
*
* @param menuStrs an array of menu and menu item strings.
* @return the newly constructed menu bar.
*/
public static MenuBar createMenuMar (String menuStrs[])
{
MenuBar mbar = new MenuBar();
Menu menu = null;
StringBuffer item;
char c;
int i = 0;
while (menuStrs[i] != null) {
if (menuStrs[i].startsWith(">")) {
// Add a menu item:
if (menu != null) {
item = new StringBuffer();
if (menuStrs[i].charAt(0) == '-') {
menu.addSeparator();
} else {
for (int j = 1; j < menuStrs[i].length(); j++) {
// Don't handle command keys yet:
c = menuStrs[i].charAt(j);
if (c == '#') break;
item.append(c);
}
menu.add(item.toString());
}
}
}
else {
// Add a menu:
menu = new Menu(menuStrs[i]);
mbar.add(menu);
}
i++; // Next menu string.
}
return mbar;
}
}