001: /**
002: * Miroslav Popov, Dec 1, 2005 miroslav.popov@gmail.com
003: */package org.enhydra.jawe;
004:
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.Map;
008: import java.util.Properties;
009:
010: /**
011: * @author Miroslav Popov
012: */
013: public abstract class JaWEComponentSettings implements Settings {
014:
015: protected Map componentAction = new HashMap();
016:
017: protected Map componentSettings = new HashMap();
018:
019: protected PropertyMgr propertyMgr;
020:
021: protected AdditionalResourceManager arm;
022:
023: public String PROPERTYFILE_PATH = JaWEConstants.JAWE_BASIC_PROPERTYFILE_PATH;
024:
025: public String PROPERTYFILE_NAME = JaWEConstants.JAWE_BASIC_PROPERTYFILE_NAME;
026:
027: public void init(JaWEComponent comp) {
028: if (propertyMgr == null) {
029: if (Utils.checkFileExistence(PROPERTYFILE_NAME)
030: || Utils.checkResourceExistence(PROPERTYFILE_PATH,
031: PROPERTYFILE_NAME)) {
032: manageProperties(comp, PROPERTYFILE_PATH,
033: PROPERTYFILE_NAME);
034: } else {
035: manageProperties(comp,
036: JaWEConstants.JAWE_BASIC_PROPERTYFILE_PATH,
037: JaWEConstants.JAWE_BASIC_PROPERTYFILE_NAME);
038: }
039: } else {
040: if (Utils.checkFileExistence(PROPERTYFILE_NAME)
041: || Utils.checkResourceExistence(PROPERTYFILE_PATH,
042: PROPERTYFILE_NAME)) {
043: propertyMgr.manageProperties(comp, this ,
044: PROPERTYFILE_PATH, PROPERTYFILE_NAME);
045: } else {
046: propertyMgr.manageProperties(comp, this ,
047: JaWEConstants.JAWE_BASIC_PROPERTYFILE_PATH,
048: JaWEConstants.JAWE_BASIC_PROPERTYFILE_NAME);
049: }
050: }
051: }
052:
053: public abstract void loadDefault(JaWEComponent comp,
054: Properties properties);
055:
056: public void setPropertyMgr(PropertyMgr pm) {
057: this .propertyMgr = pm;
058: }
059:
060: public PropertyMgr getPropertyMgr() {
061: return propertyMgr;
062: }
063:
064: public void clear() {
065: componentAction.clear();
066: componentSettings.clear();
067: }
068:
069: public void addSetting(String key, Object setting) {
070: componentSettings.put(key, setting);
071: }
072:
073: public Object getSetting(String key) {
074: return componentSettings.get(key);
075: }
076:
077: public String getSettingString(String key) {
078: Object s = componentSettings.get(key);
079: if (s instanceof String) {
080: return (String) s;
081: } else if (s != null) {
082: return s.toString();
083: } else {
084: return null;
085: }
086: }
087:
088: public int getSettingInt(String key) {
089: Object s = componentSettings.get(key);
090: if (s instanceof Integer) {
091: return ((Integer) s).intValue();
092: } else if (s != null) {
093: try {
094: int i = Integer.parseInt(getSettingString(key));
095: return i;
096: } catch (Exception e) {
097: return -1;
098: }
099: } else {
100: return -1;
101: }
102: }
103:
104: public boolean getSettingBoolean(String key) {
105: Object s = componentSettings.get(key);
106: if (s instanceof Boolean) {
107: return ((Boolean) s).booleanValue();
108: } else if (s != null) {
109: return new Boolean(getSettingString(key)).booleanValue();
110: } else {
111: return false;
112: }
113: }
114:
115: public double getSettingDouble(String key) {
116: Object s = componentSettings.get(key);
117: if (s instanceof Double) {
118: return ((Double) s).doubleValue();
119: } else if (s != null) {
120: try {
121: double d = Double.parseDouble(getSettingString(key));
122: return d;
123: } catch (Exception e) {
124: return -1;
125: }
126: } else {
127: return -1;
128: }
129: }
130:
131: public Map getActions() {
132: return componentAction;
133: }
134:
135: /**
136: * Method to get component's action corresponding to the given string.
137: *
138: * @param actionName String representation of controller's action.
139: * @return action specified by the string cmd.
140: */
141: public JaWEAction getAction(String actionName) {
142: return (JaWEAction) componentAction.get(actionName);
143: }
144:
145: public void addAction(String actionName, JaWEAction action) {
146: componentAction.put(actionName, action);
147: }
148:
149: public void adjustActions() {
150: for (Iterator it = componentAction.values().iterator(); it
151: .hasNext();) {
152: ActionBase ab = ((JaWEAction) it.next()).getAction();
153: if (ab != null) {
154: ab.enableDisableAction();
155: }
156: }
157: }
158:
159: public void changeActionState(String actionName, boolean state) {
160: ActionBase ab = ((JaWEAction) componentAction.get(actionName))
161: .getAction();
162:
163: ab.setEnabled(state);
164: }
165:
166: public String getLanguageDependentString(String nm) {
167: return ResourceManager.getLanguageDependentString(arm, nm);
168: }
169:
170: public String getMainMenuActionOrder() {
171: return "";
172: }
173:
174: public String getMenuActionOrder(String menuName) {
175: return "";
176: }
177:
178: public String getToolbarActionOrder(String toolbarName) {
179: return "";
180: }
181:
182: public void manageProperties(JaWEComponent comp, String path,
183: String name) {
184: Properties properties = new Properties();
185:
186: try {
187: Utils.manageProperties(properties, path, name);
188: } catch (Exception e) {
189: }
190:
191: try {
192: loadDefault(comp, properties);
193: } catch (Exception e) {
194: try {
195: clear();
196: properties = new Properties();
197: Utils.copyPropertyFile(
198: JaWEConstants.JAWE_BASIC_PROPERTYFILE_PATH,
199: JaWEConstants.JAWE_BASIC_PROPERTYFILE_NAME,
200: true);
201: System.err
202: .println("Invalid configuration setting(s) found in "
203: + name
204: + ", the file has been overwritten by the default one!");
205: loadDefault(comp, properties);
206: } catch (Exception ex) {
207: ex.printStackTrace();
208: }
209: }
210: }
211:
212: }
|