PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17package com.google.marvin.shell;
 18
 19import java.util.ArrayList;
 20import java.util.Collections;
 21import java.util.HashMap;
 22import java.util.Iterator;
 23
 24/**
 25 * A class for representing menus as a HashMap mapping gesture codes to
 26 * MenuItems.
 27 *
 28 * @author credo@google.com (Tim Credo)
 29 */
 30public final class Menu extends HashMap<Integer, MenuItem> {
 31
 32    private static String XML_MENU_TAG = "<menu label='%s' wallpaper='%s'>\n";
 33
 34    private static String XML_MENU_TAG_WITH_ID = "<menu label='%s' wallpaper='%s' id='%s'>\n";
 35
 36    private static String XML_MENU_CLOSE_TAG = "</menu>\n";
 37
 38    private String mName;
 39
 40    private String mID = null;
 41
 42    private String mWallpaper;
 43
 44    /**
 45     * A menu is just a HashMap with an extra name String.
 46     */
 47    public Menu(String name) {
 48        super();
 49        mName = name;
 50        mID = name;
 51        mWallpaper = "";
 52    }
 53
 54    public Menu(String name, HashMap<Integer, MenuItem> items, String wallpaper) {
 55        super(items);
 56        mName = name;
 57        mID = name;
 58        mWallpaper = wallpaper;
 59    }
 60
 61    public String getName() {
 62        return mName;
 63    }
 64
 65    public void setName(String name) {
 66        mName = name;
 67    }
 68
 69    public String getID() {
 70        return mID;
 71    }
 72
 73    public void setID(String id) {
 74        mID = id;
 75    }
 76
 77    public String getWallpaper() {
 78        return mWallpaper;
 79    }
 80
 81    public void setWallpaper(String wallpaper) {
 82        mWallpaper = wallpaper;
 83    }
 84
 85    /**
 86     * Write out this menu to an XML string.
 87     */
 88    public String toXml() {
 89        StringBuilder xmlBuilder = new StringBuilder();
 90        // don't use the id if not necessary
 91        if (mID == null || mID == mName) {
 92            xmlBuilder.append(
 93                    String.format(XML_MENU_TAG, MenuManager.escapeEntities(mName),
 94                            MenuManager.escapeEntities(mWallpaper)));
 95        } else {
 96            xmlBuilder.append(String.format(XML_MENU_TAG_WITH_ID, MenuManager.escapeEntities(mName),
 97                    MenuManager.escapeEntities(mWallpaper), MenuManager.escapeEntities(mID)));
 98        }
 99        ArrayList<Integer> keyList = new ArrayList<Integer>(keySet());
100        Collections.sort(keyList);
101        Iterator<Integer> it = keyList.iterator();
102        while (it.hasNext()) {
103            int gesture = it.next();
104            MenuItem item = get(gesture);
105            xmlBuilder.append(item.toXml(gesture));
106        }
107        xmlBuilder.append(XML_MENU_CLOSE_TAG);
108        return xmlBuilder.toString();
109    }
110}