Blackberry - SubMenu


class SubMenu extends PopupScreen implements ListFieldCallback {
 private static final int MAX_WIDTH = Font.getDefault().getAdvance(
   "max menu item text");
 XYRect mRectangle = new XYRect();
 Vector mSubMenuItems = new Vector();
 ListField mListField;

 public SubMenu() {
  super(new SubMenuItemManager(), DEFAULT_CLOSE);
  int rowHeight = getFont().getHeight() + 2;
  mListField = new ListField() {
   protected boolean navigationClick(int status, int time) {
    runMenuItem(getSelectedIndex());
    return super.navigationClick(status, time);
   }
  };
  mListField.setRowHeight(rowHeight);
  add(mListField);
  mListField.setCallback(this);
  updateMenuItems();
 }

 public void add(SubMenuItem subMenuItem) {
  mSubMenuItems.addElement(subMenuItem);
  subMenuItem.mMenu = this;
  updateMenuItems();
 }

 private void updateMenuItems() {
  int rowCounts = mSubMenuItems.size();
  mRectangle.width = getMaxObjectToStringWidth(mSubMenuItems);
  mRectangle.height = mListField.getRowHeight() * rowCounts;
  mListField.setSize(rowCounts);
 }

 private void runMenuItem(int index) {
  SubMenuItem item = (SubMenuItem) get(mListField, index);
  if (null != item.mSubMenu) {
   item.mSubMenu.setSubMenuPosition(getSubMenuRect(index));
   Ui.getUiEngine().pushScreen(item.mSubMenu);
  } else {
   item.run();
  }
 }

 private XYRect getSubMenuRect(int index) {
  SubMenuItem item = (SubMenuItem) get(mListField, index);
  XYRect result = item.mSubMenu.mRectangle;
  result.x = mRectangle.x + mRectangle.width;
  result.y = mRectangle.y + mListField.getRowHeight() * index;
  int testWidth = Display.getWidth() - (mRectangle.width + mRectangle.x);
  if (testWidth < result.width) {
   result.width = testWidth;
  }

  int testHeight = Display.getHeight()
    - (mRectangle.height + mRectangle.y);
  if (testHeight < result.height)
   result.height = testHeight;

  return result;
 }

 public void setSubMenuPosition(XYRect rect) {
  mRectangle = rect;
 }

 protected void sublayout(int width, int height) {
  super.sublayout(mRectangle.width, mRectangle.height);
  setPosition(mRectangle.x, mRectangle.y);
  setExtent(mRectangle.width, mRectangle.height);
 }

 private int getMaxObjectToStringWidth(Vector objects) {
  int result = 0;
  for (int i = 0; i < objects.size(); i++) {
   int width = getFont().getAdvance(objects.elementAt(i).toString());
   if (width > result) {
    if (width > MAX_WIDTH) {
     result = MAX_WIDTH;
     break;
    } else {
     result = width;
    }
   }
  }
  return result;
 }

 public void drawListRow(ListField field, Graphics g, int i, int y, int w) {
  // Draw the text.
  String text = get(field, i).toString();
  g.setColor(Color.WHITE);
  g.drawText(text, 0, y, DrawStyle.ELLIPSIS, w);
 }

 public Object get(ListField listField, int index) {
  return mSubMenuItems.elementAt(index);
 }

 public int getPreferredWidth(ListField listField) {
  return mRectangle.width;
 }

 public int indexOfList(ListField listField, String prefix, int start) {
  return 0;
 }
}

class SubMenuItemManager extends VerticalFieldManager {
 public SubMenuItemManager() {
  super(USE_ALL_HEIGHT | USE_ALL_WIDTH);
 }

 public int getPreferredWidth() {
  return ((SubMenu) getScreen()).mRectangle.width;
 }

 public int getPreferredHeight() {
  return ((SubMenu) getScreen()).mRectangle.height;
 }

 protected void sublayout(int maxWidth, int maxHeight) {
  maxWidth = getPreferredWidth();
  maxHeight = getPreferredHeight();
  super.sublayout(maxWidth, maxHeight);
  setExtent(maxWidth, maxHeight);
 }

}

// Sub Menu Item Class
class SubMenuItem implements Runnable {
 String mName;
 SubMenu mMenu;
 SubMenu mSubMenu;

 public SubMenuItem(String name) {
  this(name, null);
 }

 public SubMenuItem(String name, SubMenu subMenu) {
  mName = name;
  mSubMenu = subMenu;
 }

 public String toString() {
  return mName;
 }

 public void run() {
  if (mName.equals("Menu1")) {
   subMenu.close();
   Dialog.alert("Menu1");
  } else if (mName.equals("Menu2")) {
   subMenu.close();
   Dialog.alert("Menu2");
  }
 }
}

Comments

Popular posts from this blog

Input filter (space not allowed) for EditText

Android Versions details

English - Formal and Informal words