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;
043:
044: import java.awt.Component;
045: import java.awt.Dialog;
046: import java.awt.Frame;
047: import java.awt.Window;
048: import org.netbeans.modules.visualweb.gravy.actions.HelpAction;
049: import org.netbeans.jemmy.ComponentChooser;
050: import org.netbeans.jemmy.TestOut;
051: import org.netbeans.jemmy.operators.JButtonOperator;
052: import org.netbeans.jemmy.operators.JEditorPaneOperator;
053: import org.netbeans.jemmy.operators.JSplitPaneOperator;
054: import org.netbeans.jemmy.operators.JTabbedPaneOperator;
055: import org.netbeans.jemmy.operators.JTextFieldOperator;
056: import org.netbeans.jemmy.operators.JTreeOperator;
057: import org.netbeans.jemmy.operators.WindowOperator;
058:
059: /** Class implementing all necessary methods for handling "IDE Help" Frame.
060: * Normally the Help window is a JFrame.
061: * But the Help window can be transformed to a JDialog
062: * when another modal dialog is shown. This operator can handle both states.
063: *
064: * @see HelpAction
065: */
066: public class HelpOperator extends WindowOperator {
067:
068: /** Creates new HelpOperator that can handle it. It tries to find a window
069: * which contains some javax.help.JHelp* sub component.
070: * It throws TimeoutExpiredException when window is not found
071: */
072: public HelpOperator() {
073: super (WindowOperator.waitWindow(new HelpWindowChooser()));
074: }
075:
076: /** Creates new HelpOperator that can handle it. It tries to find a window
077: * which contains some javax.help.JHelp* sub component and the window
078: * has given title.
079: * It throws TimeoutExpiredException when JFrame not found
080: * @param title String help frame title
081: */
082: public HelpOperator(String title) {
083: super (WindowOperator.waitWindow(new HelpWindowChooser(title)));
084: }
085:
086: private static final HelpAction helpAction = new HelpAction();
087:
088: private JButtonOperator _btBack;
089: private JButtonOperator _btNext;
090: private JButtonOperator _btPrint;
091: private JButtonOperator _btPageSetup;
092: private JSplitPaneOperator _splpHelpSplitPane;
093: private JTabbedPaneOperator _tbpHelpTabPane;
094: private JTreeOperator _treeContents;
095: private JTreeOperator _treeIndex;
096: private JTextFieldOperator _txtIndexFind;
097: private JTreeOperator _treeSearch;
098: private JTextFieldOperator _txtSearchFind;
099: private JEditorPaneOperator _txtContentViewer;
100:
101: /** Returns title of help window. The help window can be either JFrame
102: * or JDialog.
103: * @return title of help window
104: */
105: public String getTitle() {
106: if (getSource() instanceof Frame) {
107: return ((Frame) getSource()).getTitle();
108: } else {
109: return ((Dialog) getSource()).getTitle();
110: }
111: }
112:
113: /** invokes default help
114: * @return HelpOperator for invoked help */
115: public static HelpOperator invoke() {
116: helpAction.perform();
117: return new HelpOperator();
118: }
119:
120: /** invokes help with defined help set
121: * @param helpSet String help set name
122: * @return HelpOperator for invoked help */
123: public static HelpOperator invoke(String helpSet) {
124: new HelpAction(helpSet).perform();
125: return new HelpOperator(helpSet);
126: }
127:
128: /** Tries to find "" JButton in this dialog.
129: * It throws TimeoutExpiredException when component not found
130: * @return JButtonOperator
131: */
132: public JButtonOperator btBack() {
133: if (_btBack == null) {
134: _btBack = new JButtonOperator(this , helpButtonChooser, 0);
135: }
136: return _btBack;
137: }
138:
139: /** Tries to find "" JButton in this dialog.
140: * It throws TimeoutExpiredException when component not found
141: * @return JButtonOperator
142: */
143: public JButtonOperator btNext() {
144: if (_btNext == null) {
145: _btNext = new JButtonOperator(this , helpButtonChooser, 1);
146: }
147: return _btNext;
148: }
149:
150: /** Tries to find "" JButton in this dialog.
151: * It throws TimeoutExpiredException when component not found
152: * @return JButtonOperator
153: */
154: public JButtonOperator btPrint() {
155: if (_btPrint == null) {
156: _btPrint = new JButtonOperator(this , helpButtonChooser, 3);
157: }
158: return _btPrint;
159: }
160:
161: /** Tries to find "" JSplitPaneOperator in this dialog.
162: * It throws TimeoutExpiredException when component not found
163: * @return JButtonOperator
164: */
165: public JSplitPaneOperator splpHelpSplitPane() {
166: if (_splpHelpSplitPane == null) {
167: _splpHelpSplitPane = new JSplitPaneOperator(this );
168: }
169: return _splpHelpSplitPane;
170: }
171:
172: /** Tries to find "" JTabbedPane in this dialog.
173: * It throws TimeoutExpiredException when component not found
174: * @return JButtonOperator
175: */
176: public JTabbedPaneOperator tbpHelpTabPane() {
177: if (_tbpHelpTabPane == null) {
178: _tbpHelpTabPane = new JTabbedPaneOperator(
179: splpHelpSplitPane());
180: }
181: return _tbpHelpTabPane;
182: }
183:
184: /** Tries to find "" JButton in this dialog.
185: * It throws TimeoutExpiredException when component not found
186: * @return JButtonOperator
187: */
188: public JButtonOperator btPageSetup() {
189: if (_btPageSetup == null) {
190: _btPageSetup = new JButtonOperator(this , helpButtonChooser,
191: 4);
192: }
193: return _btPageSetup;
194: }
195:
196: /** Tries to find JTree in Contents tab of this dialog.
197: * It throws TimeoutExpiredException when component not found
198: * @return JTreeOperator
199: */
200: public JTreeOperator treeContents() {
201: selectPageContents();
202: if (_treeContents == null) {
203: _treeContents = new JTreeOperator(tbpHelpTabPane(), 0);
204: }
205: return _treeContents;
206: }
207:
208: /** Tries to find JTree in Index tab of this dialog.
209: * It throws TimeoutExpiredException when component not found
210: * @return JTreeOperator
211: */
212: public JTreeOperator treeIndex() {
213: selectPageIndex();
214: if (_treeIndex == null) {
215: _treeIndex = new JTreeOperator(tbpHelpTabPane(), 0);
216: }
217: return _treeIndex;
218: }
219:
220: /** Tries to find JTextField Find in Index tab of this dialog.
221: * It throws TimeoutExpiredException when component not found
222: * @return JTextFieldOperator
223: */
224: public JTextFieldOperator txtIndexFind() {
225: selectPageIndex();
226: if (_txtIndexFind == null) {
227: _txtIndexFind = new JTextFieldOperator(tbpHelpTabPane(), 0);
228: }
229: return _txtIndexFind;
230: }
231:
232: /** Tries to find JTree in Search tab of this dialog.
233: * It throws TimeoutExpiredException when component not found
234: * @return JTreeOperator
235: */
236: public JTreeOperator treeSearch() {
237: selectPageSearch();
238: if (_treeSearch == null) {
239: _treeSearch = new JTreeOperator(tbpHelpTabPane(), 0);
240: }
241: return _treeSearch;
242: }
243:
244: /** Tries to find JTextField Find in Search tab of this dialog.
245: * It throws TimeoutExpiredException when component not found
246: * @return JTextFieldOperator
247: */
248: public JTextFieldOperator txtSearchFind() {
249: selectPageSearch();
250: if (_txtSearchFind == null) {
251: _txtSearchFind = new JTextFieldOperator(tbpHelpTabPane(), 0);
252: }
253: return _txtSearchFind;
254: }
255:
256: /** Tries to find null BasicContentViewerUI$JHEditorPane in this dialog.
257: * It throws TimeoutExpiredException when component not found
258: * @return JEditorPaneOperator
259: */
260: public JEditorPaneOperator txtContentViewer() {
261: if (_txtContentViewer == null) {
262: _txtContentViewer = new JEditorPaneOperator(
263: splpHelpSplitPane(), 0);
264: }
265: return _txtContentViewer;
266: }
267:
268: /** clicks on "Back" JButton
269: * It throws TimeoutExpiredException when MetalSplitPaneDivider$1 not found
270: */
271: public void back() {
272: btBack().push();
273: }
274:
275: /** clicks on "Next" JButton
276: * It throws TimeoutExpiredException when JButton not found
277: */
278: public void next() {
279: btNext().push();
280: }
281:
282: /** clicks on "Print" JButton
283: * It throws TimeoutExpiredException when JButton not found
284: */
285: public void print() {
286: btPrint().push();
287: }
288:
289: /** clicks on "Page Setup" JButton
290: * It throws TimeoutExpiredException when JButton not found
291: */
292: public void pageSetup() {
293: btPageSetup().pushNoBlock();
294: }
295:
296: /** selects page Contents */
297: public void selectPageContents() {
298: tbpHelpTabPane().selectPage(0);
299: }
300:
301: /** selects page Index */
302: public void selectPageIndex() {
303: tbpHelpTabPane().selectPage(1);
304: }
305:
306: /** selects page Search */
307: public void selectPageSearch() {
308: tbpHelpTabPane().selectPage(2);
309: }
310:
311: /** tries to find and set text of txtIndexFind
312: * @param text String text
313: */
314: public void indexFind(String text) {
315: txtIndexFind().enterText(text);
316: }
317:
318: /** tries to find and set text of txtSearchFind
319: * @param text String text
320: */
321: public void searchFind(String text) {
322: txtSearchFind().enterText(text);
323: }
324:
325: /** returns help content in plain text form
326: * @return String text of help
327: */
328: public String getContentText() {
329: return txtContentViewer().getText();
330: }
331:
332: /** Performs verification by accessing all sub-components */
333: public void verify() {
334: btBack();
335: btNext();
336: btPageSetup();
337: btPrint();
338: treeContents();
339: txtContentViewer();
340: treeIndex();
341: txtIndexFind();
342: treeSearch();
343: txtSearchFind();
344: }
345:
346: /** Implementation of ComponentChooser to choose component which
347: * is instance of javax.help.JHelp*. */
348: private static final ComponentChooser jHelpChooser = new ComponentChooser() {
349: public boolean checkComponent(Component comp) {
350: return comp.getClass().getName().startsWith(
351: "javax.help.JHelp");
352: }
353:
354: public String getDescription() {
355: return ("any javax.help");
356: }
357: };
358:
359: /** Compare title of window with given pattern. */
360: private static boolean compareTitle(WindowOperator oper,
361: String expectedTitle) {
362: String title;
363: if (oper.getSource() instanceof Frame) {
364: title = ((Frame) oper.getSource()).getTitle();
365: } else {
366: title = ((Dialog) oper.getSource()).getTitle();
367: }
368: return oper.getComparator().equals(title, expectedTitle);
369: }
370:
371: /** SubChooser to determine Window which contains some
372: * javax.help.JHelp* sub component.
373: */
374: private static final class HelpWindowChooser implements
375: ComponentChooser {
376:
377: private String title;
378:
379: public HelpWindowChooser() {
380: }
381:
382: public HelpWindowChooser(String title) {
383: this .title = title;
384: }
385:
386: public boolean checkComponent(Component comp) {
387: WindowOperator winOper = new WindowOperator((Window) comp);
388: winOper.setOutput(TestOut.getNullOutput());
389: if (winOper.findSubComponent(jHelpChooser) != null) {
390: if (title != null) {
391: return compareTitle(winOper, title);
392: } else {
393: return true;
394: }
395: }
396: return false;
397: }
398:
399: public String getDescription() {
400: return "containing any javax.help.JHelp component"
401: + (title == null ? "" : " and with title '" + title
402: + "'");
403: }
404: }
405:
406: /** SubChooser to find HelpButton in help window. */
407: private static final ComponentChooser helpButtonChooser = new ComponentChooser() {
408:
409: public boolean checkComponent(Component comp) {
410: return comp.getClass().getName().endsWith("HelpButton");
411: }
412:
413: public String getDescription() {
414: return "HelpButton";
415: }
416: };
417: }
|