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-2006 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: package org.netbeans.jellytools;
042:
043: import java.lang.reflect.Field;
044: import javax.swing.JDialog;
045: import org.netbeans.jemmy.*;
046: import org.netbeans.jemmy.operators.JButtonOperator;
047: import org.netbeans.jemmy.operators.JListOperator;
048:
049: /** Enable to manipulate NetBeans wizards. You can access list of steps on the
050: * left side, all buttons at the bottom (Back, Next, Finish, Cancel, Help).
051: * Each step of a particular wizard is represented by an ancestor of
052: * WizardOperator, i.e. all components are described there.
053: * Timeout "WizardOperator.WaitWizardStepTimeout" is defined during waiting for
054: * exact step (methods stepsWaitSelectedIndex(int) and
055: * stepsWaitSelectedValue(String)). */
056: public class WizardOperator extends NbDialogOperator {
057:
058: private JButtonOperator _btNext;
059: private JButtonOperator _btBack;
060: private JButtonOperator _btFinish;
061: private JListOperator _lstSteps;
062:
063: private static int WAIT_TIME = 60000;
064:
065: static {
066: Timeouts.initDefault("WizardOperator.WaitWizardStepTimeout",
067: WAIT_TIME);
068: }
069:
070: /** Generic constructor
071: * @param dialog instance of NbDialog
072: */
073: public WizardOperator(JDialog dialog) {
074: super (dialog);
075: }
076:
077: /** Creates a new instance of WizardOperator.
078: * It waits for a dialog with given title.
079: * @param title title of a wizard window
080: */
081: public WizardOperator(String title) {
082: super (title);
083: }
084:
085: /** Returns operator of "Next >" button.
086: * @return JButtonOperator instance of "Next >" button
087: */
088: public JButtonOperator btNext() {
089: if (_btNext == null) {
090: String nextCaption = Bundle.getStringTrimmed(
091: "org.openide.Bundle", "CTL_NEXT");
092: _btNext = new JButtonOperator(this , nextCaption);
093: }
094: return _btNext;
095: }
096:
097: /** Returns operator of "< Back" button.
098: * @return JButtonOperator instance of "< Back" button
099: */
100: public JButtonOperator btBack() {
101: if (_btBack == null) {
102: String backCaption = Bundle.getStringTrimmed(
103: "org.openide.Bundle", "CTL_PREVIOUS");
104: _btBack = new JButtonOperator(this , backCaption);
105: }
106: return _btBack;
107: }
108:
109: /** Returns operator of "Finish" button.
110: * @return JButtonOperator instance of "Finish" button
111: */
112: public JButtonOperator btFinish() {
113: if (_btFinish == null) {
114: String finishCaption = Bundle.getStringTrimmed(
115: "org.openide.Bundle", "CTL_FINISH");
116: _btFinish = new JButtonOperator(this , finishCaption);
117: }
118: return _btFinish;
119: }
120:
121: /** Returns operator of the list of steps on the left side in wizard dialog.
122: * @return JListOperator instance of list of steps
123: */
124: public JListOperator lstSteps() {
125: if (_lstSteps == null) {
126: _lstSteps = new JListOperator(this );
127: }
128: return _lstSteps;
129: }
130:
131: /** Pushes "Next >" button. */
132: public void next() {
133: btNext().push();
134: }
135:
136: /** Pushes "< Back" button. */
137: public void back() {
138: btBack().push();
139: }
140:
141: /** Pushes "Finish" button. */
142: public void finish() {
143: btFinish().push();
144: }
145:
146: /** Returns index of currently selected step which is bold faced.
147: * @return index of currently selected step (starts at 0)
148: */
149: public int stepsGetSelectedIndex() {
150: int selectedIndex = -1;
151: try {
152: Field field = lstSteps().getCellRenderer().getClass()
153: .getDeclaredField("selected");
154: field.setAccessible(true);
155: selectedIndex = field.getInt(lstSteps().getCellRenderer());
156: } catch (NoSuchFieldException e1) {
157: throw new JemmyException(
158: "Field selected not found in CellRenderer.", e1);
159: } catch (IllegalAccessException e2) {
160: throw new JemmyException(
161: "Illegal access to field selected.", e2);
162: }
163: return selectedIndex;
164: }
165:
166: /** Waits for panel with given index.
167: * Timeout is declared as "WizardOperator.WaitWizardStepTimeout".
168: * @param index int index of requested panel */
169: public void stepsWaitSelectedIndex(final int index) {
170: try {
171: Waiter waiter = new Waiter(new Waitable() {
172: public Object actionProduced(Object obj) {
173: return index == stepsGetSelectedIndex() ? new Object()
174: : null;
175: }
176:
177: public String getDescription() {
178: return ("Wait WizardOperator step");
179: }
180: });
181: Timeouts times = getTimeouts().cloneThis();
182: times
183: .setTimeout(
184: "Waiter.WaitingTime",
185: times
186: .getTimeout("WizardOperator.WaitWizardStepTimeout"));
187: waiter.setTimeouts(times);
188: waiter.setOutput(getOutput());
189: waiter.waitAction(null);
190: } catch (InterruptedException e) {
191: }
192: }
193:
194: /** Returns currently selected step which is bold faced.
195: * @return value of currently selected step without leading number
196: */
197: public String stepsGetSelectedValue() {
198: return lstSteps().getModel().getElementAt(
199: stepsGetSelectedIndex()).toString();
200: }
201:
202: /** Waits for panel with given name.
203: * Timeout is declared as "WizardOperator.WaitWizardStepTimeout".
204: * @param panelName String requested panel name */
205: public void stepsWaitSelectedValue(String panelName) {
206: int index = lstSteps().findItemIndex(panelName);
207: if (index < 0)
208: throw new JemmyException("Panel with name \"" + panelName
209: + "\" not found.");
210: stepsWaitSelectedIndex(index);
211: }
212:
213: /** Checks if given panel name is currently selected/shown in wizard. It
214: * compares name for exact match.
215: * @param panelName name of panel
216: */
217: protected void checkPanel(String panelName) {
218: if (!stepsGetSelectedValue().equals(panelName)) {
219: throw new JemmyException("Wrong panel! Found \""
220: + stepsGetSelectedValue() + "\" instead of \""
221: + panelName + "\".");
222: }
223: }
224:
225: /** Performs verification by accessing all sub-components */
226: public void verify() {
227: btBack();
228: btNext();
229: btFinish();
230: btCancel();
231: btHelp();
232: lstSteps();
233: }
234:
235: }
|