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 javax.swing.JDialog;
044: import org.netbeans.jellytools.actions.NewFileAction;
045: import org.netbeans.jellytools.nodes.Node;
046: import org.netbeans.jemmy.JemmyException;
047: import org.netbeans.jemmy.TimeoutExpiredException;
048: import org.netbeans.jemmy.Waitable;
049: import org.netbeans.jemmy.Waiter;
050: import org.netbeans.jemmy.operators.*;
051: import javax.swing.tree.TreePath;
052:
053: /**
054: * Handle NetBeans New File wizard.
055: * It is invoked either from main menu File -> New File...
056: * <code>NewFileAction.performMenu();</code>
057: or from popup menu on folder <code>NewFileAction.performPopup();</code><br>
058: * Usage:
059: *
060: * <pre>
061: * NewFileWizardOperator op = NewFileWizardOperator.invoke();
062: * op.selectCategory("Java Classes");
063: * op.selectFileType("Java Class");
064: * </pre>
065: *
066: * @author tb115823
067: */
068: public class NewFileWizardOperator extends WizardOperator {
069:
070: private JLabelOperator _lblProject;
071: private JLabelOperator _lblCategories;
072: private JLabelOperator _lblFileTypes;
073: private JTreeOperator _treeCategories;
074: private JListOperator _lstFileTypes;
075: private JLabelOperator _lblDescription;
076: private JEditorPaneOperator _txtDescription;
077: private JComboBoxOperator _cboProject;
078:
079: /** Creates new NewFileWizardOperator that can handle it.
080: */
081: public NewFileWizardOperator() {
082: super (Bundle.getString(
083: "org.netbeans.modules.project.ui.Bundle",
084: "LBL_NewFileWizard_Subtitle"));
085: }
086:
087: /** Waits for wizard with given title.
088: * @param title title of wizard
089: */
090: public NewFileWizardOperator(String title) {
091: super (title);
092: }
093:
094: /** Invokes new wizard and returns instance of NewFileWizardOperator.
095: * @return instance of NewFileWizardOperator
096: */
097: public static NewFileWizardOperator invoke() {
098: new NewFileAction().perform();
099: return new NewFileWizardOperator();
100: }
101:
102: /** Invokes new wizard and returns instance of NewFileWizardOperator.
103: * @param title initial wizard title
104: * @return instance of NewFileWizardOperator
105: */
106: public static NewFileWizardOperator invoke(String title) {
107: new NewFileAction().perform();
108: return new NewFileWizardOperator(title);
109: }
110:
111: /** Selects specified node and invokes new file wizard by default action.
112: * In "Choose File Type" wizard's page it selects given category and filetype.
113: * It returns instance of NewFileWizardOperator representing "Name and Location"
114: * page of the wizard.
115: * @param node node which should be selected before new file wizard is invoked
116: * @param category category to be selected
117: * @param filetype file type to be selected (exact name - not substring)
118: * @return instance of NewFileWizardOperator
119: */
120: public static NewFileWizardOperator invoke(Node node,
121: String category, String filetype) {
122: new NewFileAction().perform(node);
123: String wizardTitle = Bundle.getString(
124: "org.netbeans.modules.project.ui.Bundle",
125: "LBL_NewFileWizard_Title");
126: NewFileWizardOperator nfwo = new NewFileWizardOperator(
127: wizardTitle);
128: nfwo.selectCategory(category);
129: nfwo.selectFileType(filetype);
130: nfwo.next();
131: return new NewFileWizardOperator();
132: }
133:
134: /** Creates a new object from template. It invokes new file wizard,
135: * sets given project, category and file type. On the next panel it
136: * sets package and object name. If package name is null or empty, it lets
137: * the default one.
138: * @param projectName name of project in which new object should be created
139: * @param category category to be selected
140: * @param fileType file type to be selected
141: * @param packageName package name of new object
142: * @param name name of created object
143: */
144: public static void create(String projectName, String category,
145: String fileType, String packageName, String name) {
146: String wizardTitle = Bundle.getString(
147: "org.netbeans.modules.project.ui.Bundle",
148: "LBL_NewFileWizard_Title");
149: NewFileWizardOperator nfwo = invoke(wizardTitle);
150: nfwo.selectProject(projectName);
151: nfwo.selectCategory(category);
152: nfwo.selectFileType(fileType);
153: nfwo.next();
154: NewFileNameLocationStepOperator nfnlso = new NewFileNameLocationStepOperator();
155: nfnlso.setObjectName(name);
156: if (packageName != null && !"".equals(packageName)) {
157: nfnlso.setPackage(packageName);
158: }
159: nfnlso.finish();
160: }
161:
162: /** Select given project in combobox of projects
163: * @param project name of project
164: */
165: public void selectProject(String project) {
166: cboProject().selectItem(project);
167: }
168:
169: /** Selects given project category
170: * @param category name of the category to select
171: */
172: public void selectCategory(String category) {
173: // we need to wait until some node is selected because 'please, wait' node
174: // is shown before tree is initialized. Then we can change selection.
175: try {
176: new Waiter(new Waitable() {
177: public Object actionProduced(Object param) {
178: return treeCategories().isSelectionEmpty() ? null
179: : Boolean.TRUE;
180: }
181:
182: public String getDescription() {
183: return ("Wait node is selected");
184: }
185: }).waitAction(null);
186: } catch (InterruptedException e) {
187: throw new JemmyException("Interrupted.", e);
188: } catch (TimeoutExpiredException tee) {
189: // ignore it because sometimes can happen that no category is selected by default
190: }
191: new Node(treeCategories(), category).select();
192: }
193:
194: /** Selects given file type
195: * @param filetype name of file type to select (exact name - not substring)
196: */
197: public void selectFileType(String filetype) {
198: lstFileTypes().selectItem(filetype);
199: }
200:
201: /** Tries to find "Project:"
202: * @return JLabelOperator
203: */
204: public JLabelOperator lblProject() {
205: if (_lblProject == null) {
206: _lblProject = new JLabelOperator(this , Bundle.getString(
207: "org.netbeans.modules.project.ui.Bundle",
208: "CTL_Project"));
209: }
210: return _lblCategories;
211: }
212:
213: /** Tries to find "Categories:" JLabel in this dialog.
214: * @return JLabelOperator
215: */
216: public JLabelOperator lblCategories() {
217: if (_lblCategories == null) {
218: _lblCategories = new JLabelOperator(this , Bundle.getString(
219: "org.netbeans.modules.project.ui.Bundle",
220: "CTL_Categories"));
221: }
222: return _lblCategories;
223: }
224:
225: /** Tries to find "Projects:" JLabel in this dialog.
226: * @return JLabelOperator
227: */
228: public JLabelOperator lblFileTypes() {
229: if (_lblFileTypes == null) {
230: _lblFileTypes = new JLabelOperator(this , Bundle.getString(
231: "org.netbeans.modules.project.ui.Bundle",
232: "CTL_FileTypes"));
233: }
234: return _lblFileTypes;
235: }
236:
237: /** Tries to find JComboBox Project
238: * @return JComboBoxOperator
239: */
240: public JComboBoxOperator cboProject() {
241: if (_cboProject == null) {
242: _cboProject = new JComboBoxOperator(this );
243: }
244: return _cboProject;
245: }
246:
247: /** returns selected item for cboProject
248: * @return selected project
249: */
250: public String getSelectedProject() {
251: return cboProject().getSelectedItem().toString();
252: }
253:
254: /** Tries to find null TreeView$ExplorerTree in this dialog.
255: * @return JTreeOperator
256: */
257: public JTreeOperator treeCategories() {
258: if (_treeCategories == null) {
259: _treeCategories = new JTreeOperator(this );
260: }
261: return _treeCategories;
262: }
263:
264: /** returns selected path in treeCategories
265: * @return TreePath
266: */
267: public TreePath getSelectedCategory() {
268: return treeCategories().getSelectionPath();
269: }
270:
271: /** Tries to find FileTypes ListView in this dialog.
272: * @return JListOperator
273: */
274: public JListOperator lstFileTypes() {
275: if (_lstFileTypes == null) {
276: _lstFileTypes = new JListOperator(this , 1);
277: // set exact comparator because Java Classes has types 'Java Package Info'
278: // and 'Java Package'.
279: _lstFileTypes
280: .setComparator(new Operator.DefaultStringComparator(
281: true, true));
282: }
283: return _lstFileTypes;
284: }
285:
286: /** returns selected item in lstFileType
287: * @return String selected file type
288: */
289: public String getSelectedFileType() {
290: return lstFileTypes().getSelectedValue().toString();
291: }
292:
293: /** Tries to find "Description:" JLabel in this dialog.
294: * @return JLabelOperator
295: */
296: public JLabelOperator lblDescription() {
297: if (_lblDescription == null) {
298: _lblDescription = new JLabelOperator(this , Bundle
299: .getString(
300: "org.netbeans.modules.project.ui.Bundle",
301: "CTL_Description"));
302: }
303: return _lblDescription;
304: }
305:
306: /** Tries to find null JEditorPane in this dialog.
307: * @return JEditorPaneOperator
308: */
309: public JEditorPaneOperator txtDescription() {
310: if (_txtDescription == null) {
311: _txtDescription = new JEditorPaneOperator(this );
312: }
313: return _txtDescription;
314: }
315:
316: /** gets text for txtDescription
317: * @return String text
318: */
319: public String getDescription() {
320: return txtDescription().getText();
321: }
322:
323: /** Performs verification of NewFileWizardOperator by accessing all its components.
324: */
325: public void verify() {
326: lblCategories();
327: lblFileTypes();
328: cboProject();
329: treeCategories();
330: lstFileTypes();
331: lblDescription();
332: txtDescription();
333: }
334: }
|