001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: package de.uka.ilkd.key.casetool.patternimplementor.patterns;
012:
013: import de.uka.ilkd.key.casetool.patternimplementor.*;
014:
015: public class AbstractFactory implements AbstractPatternImplementor {
016: private PIParameterGroup abstractFactory;
017: private ConstraintMechanism constraintMechanism;
018:
019: public PIParameterGroup getParameters() {
020: if (abstractFactory == null) {
021: createParameters();
022: }
023:
024: return abstractFactory;
025: }
026:
027: /*public AbstractFactory()
028: {
029:
030: createParameters();
031: }*/
032: protected void createParameters() {
033: abstractFactory = new PIParameterGroup(
034: "AbstractFactoryPattern", "Abstract Factory Pattern");
035:
036: PIParameterString af = new PIParameterString("AbstractFactory",
037: "Abstract factory", "AbstractFactory");
038: PIParameterMultiString ap = new PIParameterMultiString(
039: "AbstractProduct", "Abstract products",
040: "AbstractProduct");
041: PIParameterMultiString cf = new PIParameterMultiString(
042: "ConcreteFactory", "Concrete factories",
043: "ConcreteFactory1 ConcreteFactory2");
044: PIParameterDependent cp = new PIParameterDependent(
045: "ConcreteProduct", "Concrete products", cf);
046:
047: abstractFactory.add(af);
048: abstractFactory.add(ap);
049: abstractFactory.add(cf);
050: abstractFactory.add(cp);
051: constraintMechanism = new ConstraintMechanism(
052: "AbstractFactory.constraints", abstractFactory, this );
053:
054: /*System.out.println("abstractFactory {");
055: System.out.println(abstractFactory);
056: System.out.println("}");*/
057: }
058:
059: /*public void createAbstractFactoryDialog() {
060: JDialog jd = new JDialog();
061: jd.setContentPane(new PIParameterGUIGroup(abstractFactory));
062: jd.pack();
063: jd.setVisible(true);
064: }*/
065: private SourceCode createConcreteFactory() {
066: String af = abstractFactory.get("AbstractFactory").getValue();
067: String[] cf = ((PIParameterMultiString) abstractFactory
068: .get("ConcreteFactory")).getValues();
069: String[] ap = ((PIParameterMultiString) abstractFactory
070: .get("AbstractProduct")).getValues();
071: String[][] cps = ((PIParameterGroup) abstractFactory
072: .get("ConcreteProduct")).getValues();
073:
074: SourceCode source = new SourceCode();
075:
076: for (int i = 0; i < cf.length; i++) {
077: source.beginClass(cf[i]);
078: source.add("/**");
079: source.add(constraintMechanism.getConstraints(" * ",
080: "ConcreteFactory", cf[i]));
081: source.add(" */");
082: source.add("public class " + cf[i] + " extends " + af
083: + " {");
084:
085: // fulhack?
086: for (int j = 0; j < Math.min(ap.length, cps[i].length); j++) {
087: source.add("\t/**");
088: source.add(constraintMechanism.getConstraints("\t * ",
089: "ConcreteFactory::createProduct", cf[i] + "::"
090: + cps[i][j]));
091: source.add("\t */");
092: source.add("\tpublic " + ap[j] + " Create" + ap[j]
093: + "() {");
094: source.add("\t\treturn new " + cps[i][j] + "();");
095: source.add("\t}");
096: }
097:
098: source.add("}");
099: }
100:
101: return source;
102: }
103:
104: private SourceCode createConcreteProducts() {
105: String[] ap = ((PIParameterMultiString) abstractFactory
106: .get("AbstractProduct")).getValues();
107: String[][] cps = ((PIParameterGroup) abstractFactory
108: .get("ConcreteProduct")).getValues();
109:
110: //SourceCode classes = new SourceCode();
111: SourceCode source = new SourceCode();
112:
113: for (int j = 0; j < cps.length; j++) {
114: //Property cp = (Property)concreteProducts.elementAt(j);
115: for (int i = 0; i < Math.min(ap.length, cps[j].length); i++) {
116: //SourceCode source = new SourceCode();
117: source.beginClass(cps[j][i]);
118: source.add("/**");
119: source.add(constraintMechanism.getConstraints(" * ",
120: "ConcreteProduct", cps[j][i]));
121: source.add(" */");
122:
123: source.add("public class " + cps[j][i] + " extends "
124: + ap[i] + " {");
125: source.add("}");
126:
127: //classes.add(source);
128: }
129: }
130:
131: return source; //classes;
132: }
133:
134: private SourceCode createAbstractProducts() {
135: String[] ap = ((PIParameterMultiString) abstractFactory
136: .get("AbstractProduct")).getValues();
137:
138: //SourceCode classes = new SourceCode();
139: SourceCode source = new SourceCode();
140:
141: for (int i = 0; i < ap.length; i++) {
142: //SourceCode source = new SourceCode();
143: source.beginClass(ap[i]);
144: source.add("/**");
145: source.add(constraintMechanism.getConstraints(" * ",
146: "AbstractProduct", ap[i]));
147: source.add(" */");
148: source.add("public class " + ap[i] + " {");
149: source.add("}");
150:
151: //classes.add(source);
152: }
153:
154: return source;
155:
156: //return classes;
157: }
158:
159: private SourceCode createAbstractFactory() {
160: String af = abstractFactory.get("AbstractFactory").getValue();
161: String[] ap = ((PIParameterMultiString) abstractFactory
162: .get("AbstractProduct")).getValues();
163:
164: SourceCode source = new SourceCode();
165: source.beginClass(af);
166: source.add("/**");
167: source.add(constraintMechanism.getConstraints(" * ",
168: "AbstractFactory", af));
169: source.add(" */");
170: source.add("public abstract class " + af + " {");
171:
172: for (int i = 0; i < ap.length; i++) {
173: String tmp = ap[i];
174: source.add("\t/**");
175: source.add(constraintMechanism
176: .getConstraints("\t * ",
177: "AbstractFactory::createProduct", af + "::"
178: + ap[i]));
179: source.add("\t */");
180: source.add("\tpublic abstract " + ap[i] + " Create" + tmp
181: + "();");
182: }
183:
184: source.add("}");
185:
186: return source;
187: }
188:
189: public SourceCode getImplementation() {
190: if (abstractFactory == null) {
191: System.err
192: .println("ERROR - AbstractFactory,getImplementor : abstractFactory == null");
193:
194: return null;
195: }
196:
197: SourceCode code = new SourceCode();
198: code.add(createConcreteFactory());
199: code.add(createConcreteProducts());
200: code.add(createAbstractProducts());
201: code.add(createAbstractFactory());
202:
203: return code;
204: }
205:
206: public String getName() {
207: return "Creational Patterns:Abstract Factory";
208: }
209: }
|