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.navigation;
043:
044: import org.netbeans.modules.visualweb.gravy.Util;
045:
046: import org.netbeans.modules.visualweb.navigation.*; //import org.netbeans.modules.web.jsf.navigation.NavigationView;
047: import org.netbeans.modules.visualweb.xhtml.FormNamePanel;
048: import org.netbeans.modules.visualweb.gravy.DNDDriver;
049: import org.netbeans.modules.visualweb.gravy.*;
050: import java.awt.*;
051: import java.awt.event.*;
052: import java.util.List;
053: import org.netbeans.jemmy.*;
054: import org.netbeans.jemmy.operators.*;
055: import org.netbeans.jemmy.operators.JToggleButtonOperator;
056: import org.netbeans.jemmy.operators.JComponentOperator;
057: import org.netbeans.jellytools.TopComponentOperator;
058:
059: /**
060: * Operator for "Page Navigation" pane.
061: */
062: public class NavigatorOperator extends JComponentOperator {
063: private static final String DEFAULT_LINK_ID = "case1",
064: PAGE_NAVIGATION_TITLE = org.netbeans.modules.visualweb.gravy.Bundle
065: .getStringTrimmed(
066: "org.netbeans.modules.visualweb.navigation.Bundle",
067: "NavigationView"),
068: NAVIGATION_BUTTON = org.netbeans.modules.visualweb.gravy.Bundle
069: .getStringTrimmed(
070: "org.netbeans.modules.visualweb.navigation.Bundle",
071: "ViewNavigation");
072:
073: ComponentOperator desktop = null;
074: private JToggleButtonOperator _btSource;
075: private JToggleButtonOperator _btNavigation;
076:
077: public NavigatorOperator(ContainerOperator parent) {
078: super (parent, new NavigatorChooser());
079: this .setComparator(new Operator.DefaultStringComparator(true,
080: true));
081: }
082:
083: public NavigatorOperator() {
084: this (Util.getMainWindow());
085: }
086:
087: /**
088: * Get "XML" button.
089: */
090: public JToggleButtonOperator btSource() {
091: if (_btSource == null) {
092: TopComponentOperator topComponent = new TopComponentOperator(
093: Util.getMainWindow());
094: _btSource = new JToggleButtonOperator(topComponent, "XML");
095: }
096: return _btSource;
097: }
098:
099: /**
100: * Get "Navigation" button.
101: */
102: public JToggleButtonOperator btNavigation() {
103: if (_btNavigation == null) {
104: TopComponentOperator topComponent = new TopComponentOperator(
105: Util.getMainWindow());
106: _btNavigation = new JToggleButtonOperator(topComponent,
107: NAVIGATION_BUTTON);
108: }
109: return _btNavigation;
110: }
111:
112: /**
113: * Switch to source code of navigation rules.
114: */
115: public void switchToSource() {
116: btSource().pushNoBlock();
117: Util.wait(1000);
118: new QueueTool().waitEmpty();
119: }
120:
121: /**
122: * Switch to visual designer of navigation rules.
123: */
124: public void switchToNavigation() {
125: btNavigation().pushNoBlock();
126: Util.wait(1000);
127: new QueueTool().waitEmpty();
128: }
129:
130: /**
131: * Show "Page Navigation" pane.
132: */
133: public static NavigatorOperator show() {
134: Util.getMainTab().setSelectedIndex(
135: Util.getMainTab().findPage("Page Navigation"));
136: return (new NavigatorOperator());
137: }
138:
139: private ComponentOperator getDesktop() {
140: if (desktop == null) {
141: desktop = new NavigatorGraphFrameOperator();
142: }
143: return (desktop);
144: }
145:
146: /**
147: * Push given item of popup menu.
148: * @param menuText Name of given item.
149: */
150: public void pushPopup(String menuText) {
151: /* No need to handle Mac differently, comment it out
152: if (System.getProperty("os.name").equals("Mac OS X"))
153: getDesktop().typeKey(' ', InputEvent.CTRL_MASK);
154: else */
155:
156: getDesktop().clickForPopup(1, 1);
157: try {
158: Thread.sleep(500);
159: } catch (Exception e) {
160: }
161: new JPopupMenuOperator().pushMenuNoBlock(menuText);
162: try {
163: Thread.sleep(500);
164: } catch (Exception e) {
165: }
166: }
167:
168: /**
169: * Call popup menu in given position of "Page Navigation" pane.
170: * @param x x-coordinate of necessary position.
171: * @param y y-coordinate of necessary position.
172: */
173: public void clickForPopup(int x, int y) {
174: getDesktop().clickForPopup(x, y);
175: }
176:
177: /**
178: * Click mouse in given position of "Page Navigation" pane.
179: * @param x x-coordinate of necessary position.
180: * @param y y-coordinate of necessary position.
181: */
182: public void clickMouse(int x, int y) {
183: getDesktop().clickMouse(x, y, 1);
184: }
185:
186: /**
187: * Click mouse specified times in given position of "Page Navigation" pane.
188: * @param x x-coordinate of necessary position.
189: * @param y y-coordinate of necessary position.
190: * @param count Number of clicks.
191: */
192: public void clickMouse(int x, int y, int count) {
193: getDesktop().clickMouse(x, y, count);
194: }
195:
196: /**
197: * Select given page in "Page Navigation" pane.
198: * @param page Name of the page.
199: */
200: /*
201: public void select(String page) {
202: PageOperator pp = new PageOperator(this, page);
203: pp.select();
204: }
205: */
206:
207: /**
208: * Create link from one given page to another.
209: * @param page1 Name of the page where link will be created from.
210: * @param page2 Name of the page where link will be created to.
211: */
212: public void link(String page1, String page2) {
213:
214: linkUsingXmlSource(page1, page2);
215: /*
216: PageOperator p1 = new PageOperator(this, page1);
217: PageOperator p2 = new PageOperator(this, page2);
218: p1.select();
219: Util.wait(5000);
220: //Util.wait(500);
221:
222: getDesktop().pressMouse(p1.getX()+40, p1.getY()+40);
223: getDesktop().moveMouse(p1.getX()+41, p1.getY()+41);
224: getDesktop().moveMouse(p2.getX()+41, p2.getY()+41);
225: getDesktop().moveMouse(p2.getX()+40, p2.getY()+40);
226: getDesktop().releaseMouse(p2.getX()+40, p2.getY()+40);
227: Util.wait(1000);
228: */
229: //new DNDDriver().dnd(this, new Point(p1.getX()+40, p1.getY()+40), this, new Point(p2.getX()+40, p2.getY()+40));
230: }
231:
232: /**
233: * Create link with specified name from one given page to another.
234: * @param page1 Name of the page where link will be created from.
235: * @param page2 Name of the page where link will be created to.
236: * @param linkId Name of the link.
237: */
238: public void link(String page1, String page2, String linkId) {
239: linkUsingXmlSource(page1, page2, linkId);
240: /* TODO need to fix it
241: link(page1,page2);
242: Util.wait(1000);
243: for (int i = 0; i < linkId.length(); i++) {
244: typeKey(linkId.charAt(i));
245: Util.wait(500);
246: }
247: pushKey(KeyEvent.VK_ENTER);
248: Util.wait(1000);
249: */
250: }
251:
252: /**
253: * Create links from one given page to another.
254: * Even elements in the list are names of pages where links will be created from.
255: * Odd elements in the list are names of pages where links will be created to.
256: * @param webPageNameList List names of the pages.
257: */
258: public void linkUsingXmlSource(List webPageNameList) {
259: linkUsingXmlSource(webPageNameList, DEFAULT_LINK_ID);
260: }
261:
262: /**
263: * Create links with specified name from one given page to another.
264: * Even elements in the list are names of pages where links will be created from.
265: * Odd elements in the list are names of pages where links will be created to.
266: * @param webPageNameList List names of the pages.
267: * @param linkId Name of the link.
268: */
269: public void linkUsingXmlSource(List webPageNameList, String linkId) {
270: switchToSource();
271: Util.wait(1000);
272: new QueueTool().waitEmpty();
273:
274: EditorOperator editor = getEditorOperator();
275: for (int i = 0; i < webPageNameList.size() - 1; ++i) {
276: String masterWebPageName = (String) webPageNameList.get(i), slaveWebPageName = (String) webPageNameList
277: .get(i + 1);
278: appendLinkXmlTags(editor, masterWebPageName,
279: slaveWebPageName, linkId);
280: TestUtils.outMsg("+++ Web page link [" + linkId
281: + "] from [" + masterWebPageName + "] to ["
282: + slaveWebPageName + "] is created");
283: }
284: switchToNavigation();
285: Util.wait(1000);
286: new QueueTool().waitEmpty();
287: }
288:
289: /**
290: * Create link from one given page to another with source code editor.
291: * @param fromPageName Name of the page where link will be created from.
292: * @param toPageName Name of the page where link will be created to.
293: */
294: public void linkUsingXmlSource(String fromPageName,
295: String toPageName) {
296: linkUsingXmlSource(fromPageName, toPageName, DEFAULT_LINK_ID);
297: }
298:
299: /**
300: * Create link with specified name from one given page to another with source code editor.
301: * @param fromPageName Name of the page where link will be created from.
302: * @param toPageName Name of the page where link will be created to.
303: * @param linkId Name of the link.
304: */
305: public void linkUsingXmlSource(String fromPageName,
306: String toPageName, String linkId) {
307: switchToSource();
308: Util.wait(1000);
309: new QueueTool().waitEmpty();
310:
311: EditorOperator editor = getEditorOperator();
312: appendLinkXmlTags(editor, fromPageName, toPageName, linkId);
313:
314: switchToNavigation();
315: Util.wait(1000);
316: new QueueTool().waitEmpty();
317: }
318:
319: /**
320: * Add string for link with specified name from one given page to another into source code editor.
321: * @param editor Operator for navigation source code editor.
322: * @param fromPageName Name of the page where link will be created from.
323: * @param toPageName Name of the page where link will be created to.
324: * @param linkId Name of the link.
325: */
326: private void appendLinkXmlTags(EditorOperator editor,
327: String fromPageName, String toPageName, String linkId) {
328: // put editor's caret to the start position of the last line
329: putCaretToBeginOfLastLine(editor);
330: Util.wait(500);
331: new QueueTool().waitEmpty();
332:
333: String linkXmlTags = getLinkXmlTags(fromPageName, toPageName,
334: linkId);
335: TestUtils.outMsg("+++ xml-tags used for link [" + linkId
336: + "] between pages [" + fromPageName + "] and ["
337: + toPageName + "]: " + linkXmlTags);
338: editor.insert(linkXmlTags);
339: Util.wait(1000);
340: new QueueTool().waitEmpty();
341: }
342:
343: /**
344: * Put caret to begin of last line in source code editor.
345: * @param editor Operator for navigation source code editor.
346: */
347: private void putCaretToBeginOfLastLine(EditorOperator editor) {
348: if (System.getProperty("os.name").equalsIgnoreCase("Mac OS X"))
349: editor.pushKey(KeyEvent.VK_END, KeyEvent.META_DOWN_MASK);
350: else
351: editor.pushKey(KeyEvent.VK_END, KeyEvent.CTRL_DOWN_MASK);
352: Util.wait(1000);
353: new QueueTool().waitEmpty();
354: editor.pushUpArrowKey();
355: Util.wait(1000);
356: new QueueTool().waitEmpty();
357: }
358:
359: /**
360: * Return prepared tag for link with specified name from one given page to another.
361: * @param fromPageName Name of the page where link will be created from.
362: * @param toPageName Name of the page where link will be created to.
363: * @param linkId Name of the link.
364: * @return String Tag of navigation rule.
365: */
366: private String getLinkXmlTags(String fromPageName,
367: String toPageName, String linkId) {
368: String[] xmlTags = new String[] {
369: " <navigation-rule>",
370: " <from-view-id>/" + fromPageName
371: + "</from-view-id>",
372: " <navigation-case>",
373: " <from-outcome>" + linkId
374: + "</from-outcome>",
375: " <to-view-id>/" + toPageName
376: + "</to-view-id>",
377: " </navigation-case>", " </navigation-rule>" };
378: StringBuffer buffer = new StringBuffer();
379: for (int i = 0; i < xmlTags.length; ++i) {
380: buffer.append(xmlTags[i]);
381: buffer.append("\n");
382: }
383: return buffer.toString();
384: }
385:
386: /**
387: * Get navigation source code editor.
388: * @return Operator for navigation source code editor.
389: */
390: private EditorOperator getEditorOperator() {
391: //TopComponentOperator topComponent = new TopComponentOperator(PAGE_NAVIGATION_TITLE);
392: TopComponentOperator topComponent = new TopComponentOperator(
393: Util.getMainWindow());
394: EditorOperator editor = new EditorOperator(topComponent,
395: PAGE_NAVIGATION_TITLE);
396: Util.wait(1000);
397: new QueueTool().waitEmpty();
398: TestUtils.outMsg("+++ Source editor for ["
399: + PAGE_NAVIGATION_TITLE + "] found");
400: return editor;
401: }
402:
403: /**
404: * Create static link with specified name from one given page to another.
405: * @param page1 Name of the page where link will be created from.
406: * @param page2 Name of the page where link will be created to.
407: * @param linkId Name of the link.
408: */
409: /*
410: public void staticLink(String page1, String page2,String linkId) {
411: PageOperator p1 = new PageOperator(this, page1);
412: PageOperator p2 = new PageOperator(this, page2);
413:
414: p1.select();
415:
416: Util.wait(300);
417:
418: new DNDDriver().dnd(this, new Point(p1.getX()+10, p1.getY()+33), this, new Point(p2.getX()+5, p2.getY()+5));
419:
420: Util.wait(300);
421:
422: for (int i=0; i<linkId.length(); i++){
423: typeKey(linkId.charAt(i));
424: }
425: }*/
426:
427: public class NavigatorGraphFrameOperator extends ComponentOperator {
428: public NavigatorGraphFrameOperator() {
429: super (NavigatorOperator.this ,
430: new NavigatorGraphFrameChooser());
431: }
432: }
433:
434: public class NavigatorGraphFrameChooser implements ComponentChooser {
435: public boolean checkComponent(Component comp) {
436: //return(comp instanceof PageFlowGraph);
437: //return(comp instanceof org.netbeans.graph.JGraphView);
438: //Hotfix for sanity to pass. There should be no test for Navigation.
439: return false;
440: }
441:
442: public String getDescription() {
443: //return(FormNamePanel.class.getName());
444: return (FormNamePanel.class.getName());
445: }
446: }
447:
448: public static class NavigatorChooser implements ComponentChooser {
449: public NavigatorChooser() {
450: }
451:
452: public boolean checkComponent(Component comp) {
453: // TODO:
454: // boolean res = comp instanceof NavigationView;
455: // return res;
456: return false;
457: }
458:
459: public String getDescription() {
460: // return(NavigationView.class.getName());
461: return (null);
462: }
463: }
464: }
|