001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.admin;
007:
008: import java.util.*;
009: import java.io.*;
010:
011: public class AdminMenu {
012:
013: class MenuPair {
014: String name = null;
015: String handle = null;
016:
017: public MenuPair() {
018: }
019:
020: public MenuPair(String text) {
021: int ndx = text.indexOf('|');
022: if (ndx > 0) {
023: name = text.substring(0, ndx);
024: handle = text.substring(ndx + 1).trim();
025: } else {
026: name = text.trim();
027: handle = text.trim();
028: }
029: }
030: }
031:
032: static private HashMap menus = new HashMap();
033: public static String default_menutext = "Server|Server,Robot|Control,Database|ImportAgents,Categories|Reindex,Reports|RobotAdvancedReport";
034: public static String default_submenus[] = {
035: "Settings|Server, Advanced|AdvancedLog",
036: "Control, Sites|Sites, Filters|Filters, Crawling|Crawling, Indexing|Indexing, Simulator|Simulator, Site Probe|SiteProbe, Schedule|RobotSchedule",
037: "Import|ImportAgents, Edit|RDEditor, Schema|SchemaEditor, Partition|Partition, Reindex|Reindex, Purge|Purge, Analysis|DbStat, Schedule|ImportSchedule",
038: "Category Editor|TaxonomyEditor, Classification Rules Editor|RobotCnRules, Reindex|Reindex",
039: "Starting Points|StartPoints, Excluded URLs|ExcludedURLs, Robot Advanced Reports|RobotAdvancedReport, View Log Files|Logs" };
040: private MenuPair[] main_mps = null;
041: private Hashtable sub_menus = null;
042: private Hashtable sub2main = null;
043:
044: static public void main(String[] args) {
045: AdminMenu m = getMenu(java.util.Locale.getDefault());
046: for (int i = 0; i < m.main_mps.length; i++) {
047: System.out.println("menu:" + m.main_mps[i].name);
048: }
049: }
050:
051: static public AdminMenu getMenu(Locale locale) {
052: AdminMenu menu = (AdminMenu) menus.get(locale);
053: if (menu == null) {
054: menu = new AdminMenu(locale);
055: menus.put(locale, menu);
056: }
057: return menu;
058: }
059:
060: public AdminMenu(Locale locale) {
061:
062: ResourceBundle adminMenu;
063: try {
064: adminMenu = ResourceBundle.getBundle(
065: CompassAdminConstants.ADMIN_MENU_FILE, locale);
066: } catch (Exception e) {
067: return;
068: }
069:
070: sub_menus = new Hashtable();
071: sub2main = new Hashtable();
072: String ms = adminMenu.getString("csmenu.list");
073: if (ms != null) {
074: main_mps = (MenuPair[]) menuTexts2MenuPairArray(ms);
075: for (int i = 0; i < main_mps.length; i++) {
076: try {
077: ms = adminMenu.getString("submenu."
078: + main_mps[i].handle);
079: if (ms != null) {
080: MenuPair[] sub_mps = (MenuPair[]) menuTexts2MenuPairArray(ms);
081: sub_menus.put(main_mps[i].handle, sub_mps);
082: for (int j = 0; j < sub_mps.length; j++) {
083: sub2main.put(sub_mps[j].handle,
084: main_mps[i].handle);
085: }
086: }
087: } catch (Exception e) {
088: //no subMenu
089: }
090: }
091: } else {
092: main_mps = (MenuPair[]) menuTexts2MenuPairArray(default_menutext);
093: for (int i = 0; i < main_mps.length; i++) {
094: MenuPair[] sub_mps = (MenuPair[]) menuTexts2MenuPairArray(default_submenus[i]);
095: sub_menus.put(main_mps[i].handle, sub_mps);
096: for (int j = 0; j < sub_mps.length; j++) {
097: sub2main.put(sub_mps[j].handle, main_mps[i].handle);
098: }
099: }
100: }
101: }
102:
103: protected MenuPair[] menuTexts2MenuPairArray(String ms) {
104: if (ms != null && ms.length() > 0) {
105: ArrayList menu_list = new ArrayList();
106: StringTokenizer st = new StringTokenizer(ms, ",");
107: while (st.hasMoreTokens()) {
108: String m = st.nextToken();
109: MenuPair mp = new MenuPair(m);
110: menu_list.add(mp);
111: }
112: MenuPair b[] = new MenuPair[1];
113: return ((MenuPair[]) menu_list.toArray(b));
114: }
115: return null;
116: }
117:
118: public int numOfMainMenu() {
119: if (main_mps != null) {
120: return main_mps.length;
121: }
122: return 0;
123: }
124:
125: public int numOfSubMenu(String mainKey) {
126: MenuPair[] sub_mps = (MenuPair[]) sub_menus.get(mainKey);
127: if (sub_mps != null) {
128: return sub_mps.length;
129: }
130: return 0;
131: }
132:
133: public String getFirstSubHandle(String mainKey) {
134: MenuPair[] sub_mps = (MenuPair[]) sub_menus.get(mainKey);
135: if (sub_mps != null) {
136: if (sub_mps.length > 0) {
137: return sub_mps[0].handle;
138: }
139: }
140: return mainKey;
141: }
142:
143: public String getMainMenuTextByIndex(int index) {
144: return main_mps[index].name;
145: }
146:
147: public String getSubMenuTextByIndex(String mainKey, int index) {
148: MenuPair[] sub_mps = (MenuPair[]) sub_menus.get(mainKey);
149: return sub_mps[index].name;
150: }
151:
152: public String getSubMenuHandleByIndex(String mainKey, int index) {
153: MenuPair[] sub_mps = (MenuPair[]) sub_menus.get(mainKey);
154: if (sub_mps != null) {
155: return sub_mps[index].handle;
156: }
157: return mainKey;
158: }
159:
160: public String getMainMenuHandleByIndex(int index) {
161: return main_mps[index].handle;
162: }
163:
164: private Object[] getSubList(String id) {
165: if (sub_menus == null) {
166: return null;
167: }
168: return (Object[]) sub_menus.get(id);
169: }
170:
171: public String getMainHandleBySubHandle(String sub) {
172: String handle = (String) sub2main.get(sub);
173: if (handle == null) {
174: handle = sub;
175: }
176: return handle;
177: }
178:
179: public static String menuURL(String text) {
180: int ndx = text.indexOf('|');
181: if (ndx > 0) {
182: return text.substring(ndx + 1);
183: } else {
184: return text;
185: }
186: }
187:
188: public static String menuText(String text) {
189: int ndx = text.indexOf('|');
190: if (ndx > 0) {
191: return text.substring(0, ndx);
192: } else {
193: return text;
194: }
195: }
196: }
|