001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tasklist.trampoline;
043:
044: import java.awt.Image;
045: import java.util.ArrayList;
046: import java.util.HashMap;
047: import java.util.List;
048: import java.util.Map;
049: import java.util.ResourceBundle;
050: import org.openide.util.Lookup;
051: import org.openide.util.LookupEvent;
052: import org.openide.util.LookupListener;
053: import org.openide.util.NbBundle;
054: import org.openide.util.Utilities;
055: import org.openide.util.lookup.Lookups;
056:
057: /**
058: * Factory to create new instances of a TaskGroup.
059: * TaskGroups definitions are read from XML layers.
060: *
061: * @author S. Aubrecht
062: */
063: public final class TaskGroupFactory {
064: static final String ATTR_GROUP_NAME = "groupName"; //NOI18N
065: static final String ATTR_BUNDLE_NAME = "localizingBundle"; //NOI18N
066: static final String ATTR_DISPLAY_NAME_KEY = "diplayNameKey"; //NOI18N
067: static final String ATTR_DESCRIPTION_KEY = "descriptionKey"; //NOI18N
068: static final String ATTR_ICON_KEY = "iconKey"; //NOI18N
069:
070: private static final String GROUP_LIST_PATH = "TaskList/Groups"; //NOI18N
071:
072: private static TaskGroupFactory theInstance;
073:
074: private Lookup.Result<TaskGroup> lookupRes;
075:
076: private Map<String, TaskGroup> name2group;
077: private List<TaskGroup> groups;
078:
079: private static TaskGroup defaultGroup;
080:
081: /** Creates a new instance of TaskTypeFactory */
082: private TaskGroupFactory() {
083: }
084:
085: /**
086: * Creates a new TaskGroup
087: *
088: * @param attrs TaskGroup's attributes
089: */
090: public static TaskGroup create(Map<String, String> attrs) {
091: String groupName = attrs.get(ATTR_GROUP_NAME);
092: String bundleName = attrs.get(ATTR_BUNDLE_NAME);
093: String displayNameKey = attrs.get(ATTR_DISPLAY_NAME_KEY);
094: String descriptionKey = attrs.get(ATTR_DESCRIPTION_KEY);
095: String iconKey = attrs.get(ATTR_ICON_KEY);
096: return create(groupName, bundleName, displayNameKey,
097: descriptionKey, iconKey);
098: }
099:
100: /**
101: * Creates a new TaskGroup
102: *
103: * @param groupName Group's id
104: * @param bundleName Resource bundle name
105: * @param displayNameKey Bundle key for display name
106: * @param descriptionKey Bundle key for description
107: * @param iconKey Bundle key for group's icon
108: * @return New TaskGroup
109: */
110: public static TaskGroup create(String groupName, String bundleName,
111: String displayNameKey, String descriptionKey, String iconKey) {
112: ResourceBundle bundle = NbBundle.getBundle(bundleName);
113: String displayName = bundle.getString(displayNameKey);
114: String description = bundle.getString(descriptionKey);
115: String iconPath = bundle.getString(iconKey);
116: Image icon = Utilities.loadImage(iconPath);
117:
118: return new TaskGroup(groupName, displayName, description, icon);
119: }
120:
121: /**
122: * @return The one and only instance of this class.
123: */
124: public static TaskGroupFactory getDefault() {
125: if (null == theInstance) {
126: theInstance = new TaskGroupFactory();
127: }
128: return theInstance;
129: }
130:
131: /**
132: * @return The default group to be used for unknown group names.
133: */
134: public TaskGroup getDefaultGroup() {
135: if (null == defaultGroup) {
136: ResourceBundle bundle = NbBundle
137: .getBundle(TaskGroupFactory.class);
138: defaultGroup = new TaskGroup(
139: "nb-unknown-group", //NOI18N
140: bundle.getString("LBL_UnknownGroup"), //NOI18N
141: bundle.getString("HINT_UnknownGroup"), //NOI18N
142: Utilities
143: .loadImage("org/netbeans/modules/tasklist/trampoline/unknown.gif")); //NOI18N
144: }
145: return defaultGroup;
146: }
147:
148: private void initGroups() {
149: if (null == name2group) {
150: if (null == lookupRes) {
151: lookupRes = initLookup();
152: lookupRes.addLookupListener(new LookupListener() {
153: public void resultChanged(LookupEvent ev) {
154: name2group = null;
155: groups = null;
156: }
157: });
158: }
159: int index = 0;
160: groups = new ArrayList<TaskGroup>(lookupRes.allInstances());
161: name2group = new HashMap<String, TaskGroup>(groups.size());
162: for (TaskGroup tg : groups) {
163: name2group.put(tg.getName(), tg);
164: tg.setIndex(index++);
165: }
166: }
167: }
168:
169: /**
170: *
171: * @param groupName Group's unique name/id
172: * @return TaskGroup for the given name or null if such a group does not exist.
173: */
174: public TaskGroup getGroup(String groupName) {
175: assert null != groupName;
176: initGroups();
177: return name2group.get(groupName);
178: }
179:
180: /**
181: * @return List of all available groups
182: */
183: public List<? extends TaskGroup> getGroups() {
184: initGroups();
185: return groups;
186: }
187:
188: private Lookup.Result<TaskGroup> initLookup() {
189: Lookup lkp = Lookups.forPath(GROUP_LIST_PATH);
190: Lookup.Template<TaskGroup> template = new Lookup.Template<TaskGroup>(
191: TaskGroup.class);
192: Lookup.Result<TaskGroup> res = lkp.lookup(template);
193: return res;
194: }
195: }
|