KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > assistant > AssistantMessages


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.form.assistant;
20
21 import java.util.*;
22
23 import org.openide.util.NbBundle;
24
25 /**
26  * Repository of assistant messages.
27  *
28  * @author Jan Stola
29  */

30 public class AssistantMessages {
31     private static AssistantMessages defaultInstance = new AssistantMessages();
32     private boolean initialized = false;
33     private Map/*<String, String[]>*/ contextToMessages;
34
35     private AssistantMessages() {
36     }
37
38     public static AssistantMessages getDefault() {
39         return defaultInstance;
40     }
41
42     public String JavaDoc[] getMessages(String JavaDoc context) {
43         if (!initialized) {
44             initialize();
45         }
46         String JavaDoc[] messages = (String JavaDoc[])contextToMessages.get(context);
47         return messages;
48     }
49
50     private void initialize() {
51         Map contextToSet = new HashMap();
52         ResourceBundle bundle = NbBundle.getBundle(AssistantMessages.class);
53         Enumeration enumeration = bundle.getKeys();
54         while (enumeration.hasMoreElements()) {
55             String JavaDoc bundleKey = (String JavaDoc)enumeration.nextElement();
56             String JavaDoc context = getContext(bundleKey);
57             Set messages = (Set)contextToSet.get(context);
58             if (messages == null) {
59                 messages = new HashSet();
60                 contextToSet.put(context, messages);
61             }
62             messages.add(bundle.getString(bundleKey));
63         }
64
65         // Transform sets into arrays
66
contextToMessages = new HashMap();
67         Iterator iter = contextToSet.entrySet().iterator();
68         while (iter.hasNext()) {
69             Map.Entry entry = (Map.Entry)iter.next();
70             String JavaDoc key = (String JavaDoc)entry.getKey();
71             Set value = (Set)entry.getValue();
72             String JavaDoc[] messages = (String JavaDoc[])value.toArray(new String JavaDoc[value.size()]);
73             contextToMessages.put(key, messages);
74         }
75     }
76
77     private String JavaDoc getContext(String JavaDoc bundleKey) {
78         int index = bundleKey.indexOf('_');
79         if (index == -1) {
80             return bundleKey;
81         } else {
82             return bundleKey.substring(0, index);
83         }
84     }
85
86 }
87
Popular Tags