001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.gravy.model.project.components;
043:
044: import org.netbeans.modules.visualweb.gravy.designer.DesignerPaneOperator;
045: import org.netbeans.modules.visualweb.gravy.DocumentOutlineOperator;
046: import org.netbeans.modules.visualweb.gravy.RaveWindowOperator;
047: import org.netbeans.modules.visualweb.gravy.TestUtils;
048: import org.netbeans.modules.visualweb.gravy.Bundle;
049: import org.netbeans.modules.visualweb.gravy.Util;
050: import org.netbeans.modules.visualweb.gravy.model.components.*;
051: import org.netbeans.modules.visualweb.gravy.model.project.JavaFile;
052:
053: import org.netbeans.jemmy.operators.JPopupMenuOperator;
054: import org.netbeans.jemmy.operators.JTreeOperator;
055: import org.netbeans.jemmy.JemmyException;
056:
057: import java.awt.event.KeyEvent;
058:
059: /**
060: * Common class for all components on web page.
061: */
062:
063: public class VisualEventHandlingComponent extends VisualComponent {
064:
065: private final static String bundle = "org.netbeans.modules.visualweb.gravy.model.project.components.Bundle";
066: private final static String viewOutline = Bundle.getStringTrimmed(
067: Bundle.getStringTrimmed(bundle, "OutlineBundle"), Bundle
068: .getStringTrimmed(bundle, "ViewOutline"));
069: private final static String popupEventHandler = Bundle
070: .getStringTrimmed(Bundle.getStringTrimmed(bundle,
071: "InsyncBundle"), Bundle.getStringTrimmed(bundle,
072: "EditEventHandler"));
073: private final static String[] action = { "action" };
074: private final static String popupActionEventHandler = Bundle
075: .getStringTrimmed(Bundle.getStringTrimmed(bundle,
076: "InsyncBundle"), Bundle.getStringTrimmed(bundle,
077: "EditDefEventHandler"), action);
078: private final static String openOutline = "Window|" + viewOutline;
079:
080: /**
081: * Create visual component on web page using web component from palette.
082: * @param webcomponent Web component from palette.
083: * @param name Name of component.
084: */
085: public VisualEventHandlingComponent(WebComponent webcomponent,
086: String name) {
087: super (webcomponent, name);
088: }
089:
090: /**
091: * Set event handler for component.
092: * @param handlerText Text of event handler.
093: */
094: public void setEventHandler(String handlerText) {
095: setEventHandler("", handlerText);
096: }
097:
098: /**
099: * Set event handler for component.
100: * @param eventType Type of event.
101: * @param handlerText Text of event handler.
102: */
103: public void setEventHandler(String eventType, String handlerText) {
104: Page.open();
105: try {
106: Util.getMainMenu().pushMenu(openOutline);
107: } catch (Exception e) {
108: throw new JemmyException(openOutline
109: + " menu can't be found!", e);
110: }
111: DocumentOutlineOperator outline = new DocumentOutlineOperator(
112: RaveWindowOperator.getDefaultRave());
113: TestUtils.wait(1000);
114: try {
115: JTreeOperator aotree = outline.getStructTreeOperator();
116: aotree.callPopupOnPath(aotree.findPath(Page.getName()
117: + "|page1|html1|body1|form1|" + getName()));
118: TestUtils.wait(1000);
119: if (!eventType.equals(""))
120: new JPopupMenuOperator().pushMenu(popupEventHandler
121: + "|" + eventType);
122: else
123: new JPopupMenuOperator()
124: .pushMenu(popupActionEventHandler);
125: TestUtils.wait(1000);
126: } catch (Exception e) {
127: throw new JemmyException(
128: "Item for action handler in popup menu can't be found!",
129: e);
130: }
131: try {
132: JavaFile jf = Page.getJavaFile();
133: jf.insert(handlerText);
134: TestUtils.wait(1000);
135: jf.pushKey(KeyEvent.VK_DOWN);
136: TestUtils.wait(500);
137: jf.pushKey(KeyEvent.VK_END);
138: TestUtils.wait(500);
139: if (!jf.getText().substring(jf.getCaretPosition() - 1,
140: jf.getCaretPosition()).equals("}")) {
141: jf.pushKey(KeyEvent.VK_HOME, KeyEvent.SHIFT_MASK);
142: TestUtils.wait(500);
143: jf.pushKey(KeyEvent.VK_DELETE);
144: }
145:
146: //Workaround: switch to design gives an error
147: //Workaround start
148: TestUtils.wait(500);
149: jf.pushKey(KeyEvent.VK_LEFT);
150: TestUtils.wait(500);
151: jf.pushKey(KeyEvent.VK_SPACE);
152: TestUtils.wait(500);
153: jf.pushKey(KeyEvent.VK_BACK_SPACE);
154: //Workaround end
155:
156: TestUtils.wait(1000);
157: jf.reformatCode();
158: } catch (Exception e) {
159: throw new JemmyException("Event Handler can't be added!", e);
160: }
161: DesignerPaneOperator.switchToDesignerPane();
162: TestUtils.wait(1000);
163: }
164: }
|