KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > label > LabelManager


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.snip.label;
27
28 import org.radeox.util.logging.Logger;
29 import org.radeox.util.Service;
30 import org.snipsnap.container.Components;
31
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36
37 /**
38  * Manages the creation and finding of labels, e.g. by type.
39  * Delivers a plugin structure to easily add labels.
40  * @author Stephan J. Schmidt
41  * @version $Id: LabelManager.java 1609 2004-05-18 13:28:38Z stephan $
42  */

43 public class LabelManager {
44   private Map JavaDoc typeMap;
45   private String JavaDoc defaultName;
46   private static LabelManager instance = null;
47   private static String JavaDoc labelClassName = "org.snipsnap.snip.label.Label";
48
49   public LabelManager() {
50     typeMap = new HashMap JavaDoc();
51     defaultName = "DefaultLabel";
52     try {
53       Iterator JavaDoc labelTypes = Service.providers(Class.forName(labelClassName));
54       while (labelTypes.hasNext()) {
55         Label label = (Label) labelTypes.next();
56         addLabelType(label.getType(), label.getClass());
57       }
58     } catch (ClassNotFoundException JavaDoc e) {
59       System.err.println("LabelManager: base label type " + labelClassName + " not found. Label types have not been registered."+e);
60     }
61   }
62
63    private void addLabelType(String JavaDoc name, String JavaDoc className) {
64     // TODO: check if labeltype with same name exists
65
// additional parameter 'overwrite' or exception or return value?
66
// (decision should to be made by user)
67
try {
68       Class JavaDoc labelClass = Class.forName(className);
69       addLabelType(name, labelClass);
70     } catch (ClassNotFoundException JavaDoc e) {
71       Logger.warn("LabelManager: label class " + className + " not found and therefore not registered.", e);
72     }
73   }
74
75   private void addLabelType(String JavaDoc name, Class JavaDoc labelClass) {
76     // TODO: check if labeltype with same name exists
77
// additional parameter 'overwrite' or exception or return value?
78
// (decision should to be made by user)
79
typeMap.put(name, labelClass);
80   }
81
82   public Label getLabel(String JavaDoc type) {
83     if (null == type) {
84       return null;
85     }
86     Class JavaDoc labelClass = (Class JavaDoc) typeMap.get(type);
87     if (null == labelClass) {
88       return null;
89     }
90     Label label = null;
91     try {
92       label = (Label) labelClass.newInstance();
93       label.create();
94     } catch (Exception JavaDoc e) {
95       Logger.warn("LabelManager: label class of type " + type + " could not be instantiated.", e);
96     }
97     return label;
98   }
99
100   public Label getDefaultLabel() {
101     return getLabel(defaultName);
102   }
103
104   public Set JavaDoc getTypes() {
105     return typeMap.keySet();
106   }
107 }
108
Popular Tags