📄 defaultconfiguration.java
字号:
/*
* Configuration.java
*
* Created on 19. November 2002, 22:27
*/
package org.jconfig;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;
import javax.swing.event.EventListenerList;
import org.jconfig.event.CategoryChangedEvent;
import org.jconfig.event.CategoryListener;
import org.jconfig.event.ConfigurationChangedEvent;
import org.jconfig.event.ConfigurationChangedEventImpl;
import org.jconfig.event.ConfigurationListener;
import org.jconfig.event.PropertyChangedEvent;
import org.jconfig.event.PropertyListener;
import org.jconfig.utils.IncludeEntry;
/**
* This class is the configuration itself. The Configuration is
* useful if one wants to manage multiple configurations. A single
* instance of the Configuration may contain, for example, information
* for one application or user.
*
* @author Andreas Mecky andreas.mecky@xcom.de
* @author Terry Dye terry.dye@xcom.de
*/
public class DefaultConfiguration implements Serializable,Configuration {
private static boolean debug = false;
protected static final VariableManager vm = VariableManager.getInstance();
// the name of the configuration
protected String configName;
// a map containing all properties for each category
protected HashMap categories;
// the name of the default category
protected String mainCategory;
// The List of ConfigurationListeners
private EventListenerList configurationListenerList = new EventListenerList();
// flag to determine if this is a newly created configuration
private boolean created = true;
private String encoding;
protected String baseConfigName;
private boolean dirtyFlag = false;
private Vector includes = new Vector();
protected DefaultConfiguration() {
}
/**
* The constructor that creates a new configuration
* with one empty category called "general". This
* category is also the default category.
*
* @param configName the name of the configuration
*/
public DefaultConfiguration(String configName) {
categories = new HashMap();
this.configName = configName;
setCategory("general", true);
created = true;
}
/**
* This method sets a category but it does not set
* this category as default.
*
* @param name the name of the category
*/
public void setCategory(String name) {
setCategory(name, false);
}
public void renameCategory(String newName,String oldName) {
Category c = getCategory(oldName);
if ( mainCategory.equals(c.getCategoryName())) {
mainCategory = newName;
}
categories.remove(oldName);
c.renameCategory(newName);
categories.put(newName,c);
markDirty();
}
/**
* Besides setting the category, it will also set this
* category as default category if main is true. It will
* only set (ie create) the category if it does not exist. If you
* want delete a category then use @see #removeCategory(String)
*
* @param categoryName the name of the category
* @param main if true then this category is the default category
*/
public void setCategory(String categoryName, boolean main) {
if (categoryName != null) {
if (main) {
mainCategory = categoryName;
}
if (!categories.containsKey(categoryName)) {
// System.err.println("setCategory(): creating DefaultCategory " + categoryName + " in " + configName + " configuration");
Category category = new DefaultCategory(categoryName);
category.setConfigurationName(configName);
category.addCategoryListener(new MyCategoryListener());
categories.put(categoryName, category);
markDirty();
category.fireCategoryChangedEvent(
new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null ));
fireConfigurationChangedEvent(
new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null ));
}
}
}
/* (non-Javadoc)
* @see org.jconfig.Configuration#setCategory(org.jconfig.Category)
*/
public void setCategory(Category category) {
if (!categories.containsKey(category.getCategoryName())) {
category.setConfigurationName(configName);
category.addCategoryListener(new MyCategoryListener());
categories.put(category.getCategoryName(), category);
category.fireCategoryChangedEvent(
new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null ));
fireConfigurationChangedEvent(
new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null ));
}
}
/**
* This method returns the name of the
* default category
*
* @return name of the default category
*/
public String getMainCategoryName() {
return mainCategory;
}
/**
* This method returns a string array with all
* category names.
*
* @return a string array with all category names
*/
public String[] getCategoryNames() {
return getCategoryNames(true);
}
/**
* A convenience method that returns the key set in the
* form or a String Array.
*
* @param includeParent true will check for a base configuration
* and include those category names as well.
* @return the String Array as described
*/
protected String[] getCategoryNames(boolean includeParent) {
Set allCategories = categories.keySet();
Vector all = new Vector(allCategories);
if ( baseConfigName != null && includeParent ) {
Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
String[] parentCategories = cfg.getCategoryNames();
for ( int i = 0 ; i < parentCategories.length ; i++) {
if ( all.indexOf(parentCategories[i]) == -1 ) {
all.add(parentCategories[i]);
}
}
}
return (String[]) all.toArray(new String[0]);
}
/**
* This method returns the String value based on the given key.
*
* Implementation details: It calls getProperty(name,null,null).
* It searches inside the default category for the property.
*
* @param key the name of the property
* @return the value as String if it is found or null
*/
public String getProperty(String key) {
return getProperty(key, null, null);
}
/**
* This method is the same as getProperty(key) but it
* returns the defaultValue if the property cannot be found.
*
* Implementation details: It calls getProperty(key,defaultValue,null).
*
* @param key the name of the property
* @param defaultValue the defaultValue that will be returned if the property cannot be found
* @return the value as String
*/
public String getProperty(String key, String defaultValue) {
return getProperty(key, defaultValue, null);
}
/**
* This is the real implementation. It will return the value of the property
* with the specific name. First of all, it checks if the name of the category
* exists. If not, then it will use the name of the default category.
* The next step is that it will look for the property. If it is not found in
* the category, it will look inside the default category (inheritance). If
* it still cannot find the property, it will return the defaultValue
*
* @param key the name of the property
* @param defaultValue the default value
* @param categoryName the name of the category
* @return the value as String
*/
public String getProperty(String key,String defaultValue,String categoryName) {
if ( categoryName == null ) {
categoryName = mainCategory;
}
boolean isMainCat = false;
if (key == null) {
return defaultValue;
}
if (!categories.containsKey(categoryName)) {
isMainCat = true;
// categoryName = mainCategory;
}
Category category = getCategory(categoryName);
if ( category.getCategoryName().equals(mainCategory)) {
isMainCat = true;
}
String tmp = category.getProperty(key);
// property not found so look in mainCategory
// if it is not already the mainCategory
if ( tmp == null && !isMainCat) {
category = getCategory(mainCategory);
tmp = category.getProperty(key);
}
if ( tmp == null ) {
if ( baseConfigName != null ) {
Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
tmp = cfg.getProperty(key,defaultValue,categoryName);
}
else {
tmp = defaultValue;
}
}
return tmp;
}
/**
* This method sets a property with the name and the value
* in the default category. It calls setProperty(name.value,null).
*
* @param name the name of the property
* @param value the value as String
*/
public void setProperty(String name, String value) {
setProperty(name, value, null);
}
/**
* This method sets the value for a property for the given
* category. It also raises a PropertyEvent. If the category
* is null then it uses the default category.
*
* @param name the name of the property
* @param value the value as String
* @param categoryName the name of the category
*/
public void setProperty(String name, String value, String categoryName) {
// System.err.println("DefaultConfiguration.setProperty category=" + categoryName + ", propertyName="+name + ", propertyValue="+value);
if (name != null) {
if (categoryName == null) {
categoryName = mainCategory;
}
Category category = getCategory(categoryName);
category.setProperty(name, value);
markDirty();
fireConfigurationChangedEvent(
new ConfigurationChangedEventImpl(ConfigurationChangedEvent.PROPERTY_CHANGED, category, name, value,null ));
}
}
/**
* This method deletes a property from the default category.
* It calls removeProperty(name,null).
*
* @param name the name of the property
*/
public void removeProperty(String name) {
if (name != null) {
removeProperty(name, null);
}
}
/**
* This method deletes a property with the given name
* from the specific category. If the category is null
* then it will delete the property from the default category.
*
* @param name the name of the property
* @param category the name of the category
*/
public void removeProperty(String name, String category) {
if (category == null) {
category = mainCategory;
}
if (name != null) {
//if (categories.containsKey(category)) {
Category cat = getCategory(category);
String tmp = cat.getProperty(name,null);
cat.setProperty(name, null);
fireConfigurationChangedEvent(
new ConfigurationChangedEventImpl(ConfigurationChangedEvent.PROPERTY_REMOVED,cat, name, tmp,null ));
markDirty();
}
}
/**
* The method returns the number of categories inside this configuration.
*
* @return the number of categories
*/
public int getNumberOfCategories() {
return getCategoryNames().length;
}
/**
* This method deletes a category with all its properties
*
* @param category the name of the category
*/
public void removeCategory(String category) {
if (categories.containsKey(category)) {
Category cat = getCategory(category);
categories.remove(category);
cat.fireCategoryChangedEvent(
new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_REMOVED, cat, null, null, null));
fireConfigurationChangedEvent(
new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_REMOVED,cat, null, null,null ));
cat = null;
markDirty();
}
}
/* (non-Javadoc)
* @see org.jconfig.Configuration#getProperties(java.lang.String)
*/
public Properties getProperties(String category) {
return getProperties(category,true);
}
/* (non-Javadoc)
* @see org.jconfig.Configuration#getProperties()
*/
public Properties getProperties() {
return getProperties(mainCategory,true);
}
/**
* @param category
* @param includeParent
* @return
*/
protected Properties getProperties(String category,boolean includeParent) {
println("configname = " + configName + ", baseConfigname = " + baseConfigName+", category="+category);
Category cat = getCategory(category);
Properties props = new Properties();
if ( cat != null ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -