001: package org.romaframework.module.designer;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.lang.reflect.Field;
006: import java.util.Collections;
007: import java.util.HashSet;
008: import java.util.LinkedHashSet;
009: import java.util.LinkedList;
010: import java.util.List;
011: import java.util.Set;
012:
013: import org.apache.xmlbeans.XmlOptions;
014: import org.romaframework.aspect.view.ViewAspect;
015: import org.romaframework.aspect.view.ViewConstants;
016: import org.romaframework.aspect.view.echo2.area.AreaInstance;
017: import org.romaframework.aspect.view.feature.ViewBaseFeatures;
018: import org.romaframework.core.Utility;
019: import org.romaframework.core.domain.wrapper.SelectWrapper;
020: import org.romaframework.core.domain.wrapper.TextWrapper;
021: import org.romaframework.core.serializer.RomaSerializationException;
022: import org.romaframework.core.serializer.RomaSerializerComponent;
023: import org.romaframework.module.designer.domain.wrapper.LayoutSelectWrapper;
024: import org.romaframework.xml.config.XmlConfigScreenDocument;
025: import org.romaframework.xml.config.XmlConfigScreenDocument.Factory;
026:
027: public class DesignerHelper {
028:
029: public final static String CONSTANTS = "Constants";
030:
031: /**
032: *
033: * @param feature
034: * @return
035: */
036: private static Set<String> getAspectSelectValues(
037: Class constantClass, String feature) {
038: List<String> result = new LinkedList<String>();
039: for (Field field : constantClass.getFields()) {
040: if (field.getName().startsWith(feature.toUpperCase())) {
041: try {
042: result.add(field.get(constantClass.newInstance())
043: .toString());
044: } catch (IllegalArgumentException e) {
045: e.printStackTrace();
046: } catch (IllegalAccessException e) {
047: e.printStackTrace();
048: } catch (InstantiationException e) {
049: e.printStackTrace();
050: }
051: }
052: }
053: Collections.sort(result);
054: HashSet<String> orderedResult = new LinkedHashSet<String>();
055: orderedResult.addAll(result);
056: return orderedResult;
057: }
058:
059: /**
060: * Returns a Set of Boolean Object values
061: *
062: * @return
063: */
064: private static Set<Boolean> getBooleanSelectValues() {
065: Set<Boolean> result = new HashSet<Boolean>();
066: result.add(new Boolean(true));
067: result.add(new Boolean(false));
068: return result;
069: }
070:
071: /**
072: * Return a SelectWrapper with boolean values
073: *
074: * @param iInitial the initial values
075: * @return
076: */
077: public static SelectWrapper<Boolean> getBooleanWrapper(
078: Boolean iInitial) {
079: if (iInitial != null) {
080: return new SelectWrapper<Boolean>(DesignerHelper
081: .getBooleanSelectValues(), iInitial);
082: } else {
083: return new SelectWrapper<Boolean>(DesignerHelper
084: .getBooleanSelectValues());
085: }
086: }
087:
088: /**
089: * Return a SelectWrapper with the values of the given View feature
090: *
091: * @param iFeature the feature to get the wrapper
092: * @param iInitial the initial value
093: * @return
094: * @throws ClassNotFoundException
095: */
096:
097: public static SelectWrapper<String> getSelectableFeatureWrapper(
098: String aspectName, String iFeature, Object iInitial)
099: throws ClassNotFoundException {
100: Class clazz = Class.forName("org.romaframework.aspect."
101: + aspectName + "." + firstToUpperCase(aspectName)
102: + CONSTANTS);
103: if (iInitial != null) {
104: return new SelectWrapper<String>(DesignerHelper
105: .getAspectSelectValues(clazz, iFeature), iInitial
106: .toString());
107: } else {
108: return new SelectWrapper<String>(DesignerHelper
109: .getAspectSelectValues(clazz, iFeature));
110: }
111: }
112:
113: public static LayoutSelectWrapper<String> getLayoutOrderWrapper(
114: Object iInitial) {
115: if (iInitial != null) {
116: return new LayoutSelectWrapper<String>(DesignerHelper
117: .getAspectSelectValues(ViewConstants.class,
118: ViewBaseFeatures.LAYOUT), iInitial
119: .toString());
120: } else {
121: return new LayoutSelectWrapper<String>(DesignerHelper
122: .getAspectSelectValues(ViewConstants.class,
123: ViewBaseFeatures.LAYOUT));
124: }
125: }
126:
127: /**
128: * Return a Text Wrapper of the given Class
129: *
130: * @param iInitial
131: * @param clazz
132: * @return
133: */
134: public static TextWrapper getTextWrapper(Object iInitial,
135: Class clazz) {
136: return new TextWrapper(iInitial, clazz);
137: }
138:
139: public static File getScreenByXmlName(String xmlFile) {
140: String screenPackage = Utility
141: .getApplicationAspectPackage(ViewAspect.ASPECT_NAME)
142: + ".domain.screen";
143: String pathResource = "/"
144: + screenPackage.replaceAll("\\.", "/") + "/";
145:
146: return new File(Object.class
147: .getResource(pathResource + xmlFile).getFile());
148:
149: }
150:
151: public static File getScreenDir() {
152: String screenPackage = Utility
153: .getApplicationAspectPackage(ViewAspect.ASPECT_NAME)
154: + ".domain.screen";
155: String pathResource = "/"
156: + screenPackage.replaceAll("\\.", "/") + "/";
157: return new File(Object.class.getResource(pathResource)
158: .getFile());
159: }
160:
161: private static String firstToUpperCase(String s) {
162: return s.substring(0, 1).toUpperCase() + s.substring(1);
163: }
164:
165: public static void saveScreen(File fileToSave,
166: AreaInstance areaInstance)
167: throws RomaSerializationException, IOException {
168: XmlOptions options = new XmlOptions();
169: options.setCharacterEncoding("UTF-8");
170: options.setUseDefaultNamespace();
171: options.setSavePrettyPrint();
172: XmlConfigScreenDocument document = XmlConfigScreenDocument.Factory
173: .newInstance();
174: document.addNewScreen().addNewArea().set(
175: RomaSerializerComponent.getInstance().serialize(
176: areaInstance));
177: document.save(fileToSave, options);
178: }
179: }
|