/shell/src/com/google/marvin/shell/Menu.java

http://eyes-free.googlecode.com/ · Java · 110 lines · 64 code · 18 blank · 28 comment · 6 complexity · de6765c591ef853d6e607e07c0fe2960 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.marvin.shell;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. /**
  22. * A class for representing menus as a HashMap mapping gesture codes to
  23. * MenuItems.
  24. *
  25. * @author credo@google.com (Tim Credo)
  26. */
  27. public final class Menu extends HashMap<Integer, MenuItem> {
  28. private static String XML_MENU_TAG = "<menu label='%s' wallpaper='%s'>\n";
  29. private static String XML_MENU_TAG_WITH_ID = "<menu label='%s' wallpaper='%s' id='%s'>\n";
  30. private static String XML_MENU_CLOSE_TAG = "</menu>\n";
  31. private String mName;
  32. private String mID = null;
  33. private String mWallpaper;
  34. /**
  35. * A menu is just a HashMap with an extra name String.
  36. */
  37. public Menu(String name) {
  38. super();
  39. mName = name;
  40. mID = name;
  41. mWallpaper = "";
  42. }
  43. public Menu(String name, HashMap<Integer, MenuItem> items, String wallpaper) {
  44. super(items);
  45. mName = name;
  46. mID = name;
  47. mWallpaper = wallpaper;
  48. }
  49. public String getName() {
  50. return mName;
  51. }
  52. public void setName(String name) {
  53. mName = name;
  54. }
  55. public String getID() {
  56. return mID;
  57. }
  58. public void setID(String id) {
  59. mID = id;
  60. }
  61. public String getWallpaper() {
  62. return mWallpaper;
  63. }
  64. public void setWallpaper(String wallpaper) {
  65. mWallpaper = wallpaper;
  66. }
  67. /**
  68. * Write out this menu to an XML string.
  69. */
  70. public String toXml() {
  71. StringBuilder xmlBuilder = new StringBuilder();
  72. // don't use the id if not necessary
  73. if (mID == null || mID == mName) {
  74. xmlBuilder.append(
  75. String.format(XML_MENU_TAG, MenuManager.escapeEntities(mName),
  76. MenuManager.escapeEntities(mWallpaper)));
  77. } else {
  78. xmlBuilder.append(String.format(XML_MENU_TAG_WITH_ID, MenuManager.escapeEntities(mName),
  79. MenuManager.escapeEntities(mWallpaper), MenuManager.escapeEntities(mID)));
  80. }
  81. ArrayList<Integer> keyList = new ArrayList<Integer>(keySet());
  82. Collections.sort(keyList);
  83. Iterator<Integer> it = keyList.iterator();
  84. while (it.hasNext()) {
  85. int gesture = it.next();
  86. MenuItem item = get(gesture);
  87. xmlBuilder.append(item.toXml(gesture));
  88. }
  89. xmlBuilder.append(XML_MENU_CLOSE_TAG);
  90. return xmlBuilder.toString();
  91. }
  92. }