001: package com.xoetrope.carousel.builder;
002:
003: import com.xoetrope.builder.w3c.html.XHtmlBuilder;
004: import com.xoetrope.builder.w3c.html.tags.XHtmlTagHandler;
005: import com.xoetrope.carousel.build.BuildProperties;
006: import java.io.Reader;
007: import javax.swing.JComponent;
008: import javax.swing.JEditorPane;
009: import javax.swing.SwingUtilities;
010: import javax.swing.text.html.HTML;
011: import net.xoetrope.debug.DebugLogger;
012: import net.xoetrope.editor.project.EditorXuiBuilder;
013: import net.xoetrope.xui.PageSupport;
014: import net.xoetrope.editor.project.XEditorProject;
015: import net.xoetrope.editor.project.pages.IEditorUtility;
016: import net.xoetrope.editor.project.pages.PageDesigner;
017: import net.xoetrope.editor.project.pages.XPageResource;
018: import net.xoetrope.xui.XPage;
019: import net.xoetrope.xui.style.XStyleFactory;
020:
021: /**
022: * A build for W3C HTML forms/pages for use within the XuiPro IDE. The builder
023: * can create XUI pages on-the-fly or it can be used to convert and save an html
024: * page to a xui format.
025: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
026: * the GNU Public License (GPL), please see license.txt for more details. If
027: * you make commercial use of this software you must purchase a commercial
028: * license from Xoetrope.</p>
029: * <p> $Revision: 1.2 $</p>
030: */
031: public class XEditorHtmlBuilder extends XHtmlBuilder implements
032: EditorXuiBuilder {
033: protected PageDesigner pageDesigner;
034: protected XEditorProject currentProject;
035: protected XPageResource currentResource;
036: protected boolean sourceExists;
037:
038: protected String pageName, pageExtension;
039:
040: private static IEditorUtility editorUtility;
041:
042: private static String[][] handlerTypes = {
043: { "onblur", "mouseHandler" },
044: { "onchange", "mouseHandler" },
045: { "onclick", "mouseHandler" },
046: { "ondblclick", "mouseHandler" },
047: { "onfocus", "focusHandler" },
048: { "onkeydown", "keyHandler" },
049: { "onkeypress", "keyHandler" },
050: { "onkeyup", "keyHandler" }, { "onload", "mouseHandler" },
051: { "onmousedown", "mouseHandler" },
052: { "onmousemove", "mouseMotionHandler" },
053: { "onmouseout", "mouseMotionHandler" },
054: { "onmouseover", "mouseMotionHandler" },
055: { "onmouseup", "mouseHandler" },
056: { "onreset", "mouseHandler" },
057: { "onselect", "actionHandler" },
058: { "onsubmit", "mouseHandler" },
059: { "onunload", "mouseHandler" }, { "href", "mouseHandler" } };
060:
061: /**
062: * Creates a new instance of XEditorHtmlBuilder
063: * @param editorProject the editor project
064: * @param factory the component factory
065: */
066: public XEditorHtmlBuilder(XEditorProject editorProject,
067: XStyleFactory factory) {
068: super (editorProject);
069:
070: currentProject = editorProject;
071: componentFactory = new XEditorHtmlComponentFactory(
072: editorProject, XPage.XUI_SWING_PACKAGE);
073: pageDesigner = null;
074: evaluator = null;
075: sourceExists = false;
076:
077: /** @todo remove this method and set the utility directly in interested classes */
078: editorUtility = PageDesigner.getEditorUtility();
079: }
080:
081: /**
082: * Check the current resources that control the interaction with the editor
083: * and the insertion of source code
084: */
085: protected void checkResources() {
086: if (currentResource == null) {
087: currentResource = currentProject.getPageResource(pageName,
088: pageExtension);
089: sourceExists = currentResource.sourceFileExists();
090: }
091: }
092:
093: /**
094: * Loads an XPage via a reader obtained from the XResourceManager (searches
095: * the classpath). The pageName is assumed to be the name of an XML file. For
096: * example if the pageName is 'welcome' then the 'welcome.xml' file is read as
097: * a UTF8 encoded XML file.
098: * @param defPackageName the package or path to the page
099: * @param page the page name or the name of the class implementing the page
100: * @param include true if the page to be loaded is being included in another
101: * page in which case any class attribute of the included page is ignored
102: * @param fullPathProvided the full path to the page name is provided
103: * @return the page
104: */
105: public PageSupport loadPage(String defPackageName, String name,
106: boolean include) {
107: packageName = defPackageName;
108: pageName = name;
109:
110: Reader r = null;
111: String exts[] = { ".html", ".htm" };
112: for (String ext : exts) {
113: try {
114: r = currentProject.getBufferedReader(pageName + ext,
115: null);
116: } catch (Exception ex) {
117: if (BuildProperties.DEBUG)
118: DebugLogger.logError("File NOT found: " + pageName);
119: }
120: if (r == null)
121: return null;
122:
123: documentUrl = currentProject.findResource(pageName + ext);
124:
125: PageSupport ps = readPage(r, pageName, ext, include);
126: if (ps instanceof JComponent)
127: ((JComponent) ps).setOpaque(true);
128:
129: pageExtension = ext;
130: return ps;
131: }
132:
133: return null;
134: }
135:
136: // Start script handling -----------------------------------------------------
137: /**
138: * Add a new script function to the current class, if and only if a Java file of
139: * the same name does not already exist for this page.
140: * @param componentName the name of the element being processed, or null if a page level element is being processed
141: * @param eventName the name of the html event or null if a page event is being processed
142: * @param methodName the name of the new method
143: * @param script the contents of the method - the original javascript (assuming it is javascript)
144: */
145: public void addScript(String componentName, String eventName,
146: String methodName, String script) {
147: checkResources();
148: if (!sourceExists) {
149: currentResource.openSourceFile(methodName, true);
150:
151: final String theMethod = methodName;
152: final String theScript = script;
153: SwingUtilities.invokeLater(new Runnable() {
154: public void run() {
155: insertMethod(theMethod, theScript);
156: }
157: });
158: }
159: }
160:
161: /**
162: * Process the event specification, specified as an attribute of an HTML element
163: * @param handler the tag handler
164: * @param attribName the attribute name
165: * @param attribValue the attribute value, containing the script
166: */
167: public void processEvent(XHtmlTagHandler handler,
168: String attribName, String attribValue) {
169: checkResources();
170: if (!sourceExists) {
171: final String name = handler.getComponentName();
172: final String theMethod = name + "_" + attribName;
173: final String theScript = attribValue;
174:
175: currentResource.openSourceFile("pageCreated", true);
176:
177: SwingUtilities.invokeLater(new Runnable() {
178: public void run() {
179: insertMethod(theMethod, theScript);
180: }
181: });
182:
183: // Add the event handler
184: String handlerType = null;
185: String jsEventType = attribName.toLowerCase();
186: for (int i = 0; i < handlerTypes.length; i++) {
187: if (handlerTypes[i][0].equals(jsEventType)) {
188: handlerType = handlerTypes[i][0];
189: break;
190: }
191: }
192: }
193: }
194:
195: /**
196: * Inserts a new method in the class
197: * @param methodName The name of the method we are looking for.
198: * @param addNewMethod If true create the method if not found.
199: * @param pageRes the page resources that owns the method or null
200: * @return true if a new method was added
201: */
202: public void insertMethod(String methodName, String comment) {
203: String fileName = currentResource.getJavaSourceFileName();
204: editorUtility.setSourceCodeEditor(currentResource, fileName);
205: Object sourceCodeEditor = currentResource.getSourceCodeEditor();
206: if (sourceCodeEditor == null) {
207: currentResource.openSourceFile(methodName, true);
208: sourceCodeEditor = currentResource.getSourceCodeEditor();
209: }
210:
211: String searchText = "public void " + methodName + "()";
212:
213: // int pos = sourceCodeEditor.getText().indexOf( searchText );
214: // sourceCodeEditor.setCaretPosition( 0 );
215: // String source = sourceCodeEditor.getText();
216: // pos = source.lastIndexOf( '}' ) - 1;
217: String newMethod = "\n\n\t" + searchText + "\n\t{"
218: + "\n\t\t/* Original JavaScript code: " + "\n\t\t"
219: + comment + "\n\t\t*/" + "\n\t}\n";
220:
221: // if ( pos < 0 )
222: // pos = 0;
223:
224: editorUtility.insertTextAtCaret(sourceCodeEditor, newMethod);
225: // sourceCodeEditor.setText( source.substring( 0, pos ) + newMethod + source.substring( pos ) );
226: // sourceCodeEditor.setCaretPosition( pos + 4 );
227: // currentResource.setJavaSource( sourceCodeEditor.getText() );
228: }
229:
230: /**
231: * Set the page designer
232: * @param designer
233: */
234: public void setPageDesigner(PageDesigner designer) {
235: pageDesigner = designer;
236: }
237: // End script handling -------------------------------------------------------
238: }
|