001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.store;
020:
021: import java.io.Externalizable;
022: import java.io.IOException;
023: import java.util.HashMap;
024:
025: import com.jeta.forms.logger.FormsLogger;
026: import com.jeta.forms.store.memento.FormMemento;
027: import com.jeta.open.registry.JETARegistry;
028: import com.jeta.swingbuilder.common.ComponentNames;
029: import com.jeta.swingbuilder.interfaces.app.ObjectStore;
030: import com.jeta.swingbuilder.interfaces.userprops.TSUserPropertiesUtils;
031:
032: /**
033: * Model for code generation options for a form.
034: *
035: * @author Jeff Tassin
036: */
037: public class CodeModel implements Externalizable {
038: static final long serialVersionUID = 5327987415154522473L;
039:
040: public static final String ID_CODEGEN_OPTIONS_CACHE = "code.options.cache";
041: public static final String ID_MEMBER_PREFIX = "code.member.prefix";
042: public static final String ID_INCLUDE_MAIN = "code.include.main";
043: public static final String ID_INCLUDE_NONSTANDARD = "code.include.nonstandard.components";
044:
045: private String m_form_id;
046:
047: /**
048: * verion of this class
049: */
050: public static final int VERSION = 1;
051:
052: /**
053: * attibutes local to a form
054: */
055: private String m_package;
056: private String m_classname = "MyForm";
057:
058: /**
059: * global attibutes
060: */
061: private transient String m_member_prefix = "m_";
062: private transient boolean m_include_main = true;
063: private transient boolean m_include_nonstandard = true;
064:
065: public CodeModel() {
066: loadGlobals();
067: }
068:
069: private CodeModel(FormMemento fm) {
070: m_form_id = fm.getId();
071: loadGlobals();
072: }
073:
074: public static CodeModel createInstance(FormMemento fm) {
075: try {
076: ObjectStore os = (ObjectStore) JETARegistry
077: .lookup(ComponentNames.APPLICATION_STATE_STORE);
078: HashMap cgen_options = (HashMap) os
079: .load(ID_CODEGEN_OPTIONS_CACHE);
080:
081: CodeModel model = null;
082: if (cgen_options != null) {
083: model = (CodeModel) cgen_options.get(fm.getId());
084: }
085:
086: if (model == null)
087: model = new CodeModel(fm);
088: model.loadGlobals();
089: return model;
090: } catch (Exception e) {
091: FormsLogger.severe(e);
092: return new CodeModel(fm);
093: }
094: }
095:
096: public static void save(CodeModel model) {
097: TSUserPropertiesUtils.setString(ID_MEMBER_PREFIX, model
098: .getMemberPrefix());
099: TSUserPropertiesUtils.setBoolean(ID_INCLUDE_MAIN, model
100: .isIncludeMain());
101: TSUserPropertiesUtils.setBoolean(ID_INCLUDE_NONSTANDARD, model
102: .isIncludeNonStandard());
103: try {
104: ObjectStore os = (ObjectStore) JETARegistry
105: .lookup(ComponentNames.APPLICATION_STATE_STORE);
106: HashMap cgen_options = (HashMap) os
107: .load(ID_CODEGEN_OPTIONS_CACHE);
108: if (cgen_options == null) {
109: cgen_options = new HashMap();
110: }
111: cgen_options.put(model.getFormId(), model);
112: os.store(ID_CODEGEN_OPTIONS_CACHE, cgen_options);
113: } catch (Exception e) {
114: FormsLogger.severe(e);
115: }
116: }
117:
118: public String getFormId() {
119: return m_form_id;
120: }
121:
122: public String getPackage() {
123: return m_package;
124: }
125:
126: public String getClassName() {
127: return m_classname;
128: }
129:
130: public String getMemberPrefix() {
131: return m_member_prefix;
132: }
133:
134: public boolean isIncludeMain() {
135: return m_include_main;
136: }
137:
138: public boolean isIncludeNonStandard() {
139: return m_include_nonstandard;
140: }
141:
142: private void loadGlobals() {
143: m_member_prefix = TSUserPropertiesUtils.getString(
144: ID_MEMBER_PREFIX, "m_");
145: m_include_main = TSUserPropertiesUtils.getBoolean(
146: ID_INCLUDE_MAIN, true);
147: m_include_nonstandard = TSUserPropertiesUtils.getBoolean(
148: ID_INCLUDE_NONSTANDARD, true);
149: }
150:
151: public void setPackage(String pkg) {
152: m_package = pkg;
153: }
154:
155: public void setClassName(String cname) {
156: m_classname = cname;
157: }
158:
159: public void setMemberPrefix(String prefix) {
160: m_member_prefix = prefix;
161: }
162:
163: public void setIncludeMain(boolean inc) {
164: m_include_main = inc;
165: }
166:
167: public void setIncludeNonStandard(boolean inc) {
168: m_include_nonstandard = inc;
169: }
170:
171: /**
172: * Externalizable Implementation
173: */
174: public void readExternal(java.io.ObjectInput in)
175: throws ClassNotFoundException, IOException {
176: int version = in.readInt();
177: m_package = (String) in.readObject();
178: m_classname = (String) in.readObject();
179: m_form_id = (String) in.readObject();
180: }
181:
182: /**
183: * Externalizable Implementation
184: */
185: public void writeExternal(java.io.ObjectOutput out)
186: throws IOException {
187: out.writeInt(VERSION);
188: out.writeObject(m_package);
189: out.writeObject(m_classname);
190: out.writeObject(m_form_id);
191: }
192:
193: }
|