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;
043:
044: import org.netbeans.modules.visualweb.gravy.TestUtils;
045: import org.netbeans.modules.visualweb.gravy.ProjectNavigatorOperator;
046:
047: import org.netbeans.jemmy.TimeoutExpiredException;
048: import org.netbeans.jemmy.JemmyException;
049: import org.netbeans.jemmy.operators.Operator;
050: import org.netbeans.jemmy.operators.JButtonOperator;
051: import org.netbeans.jemmy.operators.JDialogOperator;
052: import org.netbeans.jemmy.operators.JTextFieldOperator;
053:
054: import javax.swing.tree.TreePath;
055:
056: /**
057: * Class for source folders included in Project.
058: */
059:
060: public class SourceFolder extends Folder {
061:
062: private final String popupNewPackage = "New|Java Package...";
063: private final String dlgNewPackage = "New Java Package";
064: private final String popupNewClass = "New|Java Class...";
065: private final String dlgNewClass = "New Java Class";
066: private final String btnFinish = "Finish";
067: private final String btnCancel = "Cancel";
068:
069: SourceFolder(TreePath path, String name, ProjectEntry parent) {
070: super (path, name, parent);
071: }
072:
073: /**
074: * Add new source subfolder to current folder.
075: * @param name Name of new source subfolder.
076: * @return Added source subfolder.
077: */
078: public SourceFolder addSourceSubFolder(String name) {
079: if (getSourceSubFolder(name) == null) {
080: String path_part1 = "", path_part2 = "", full_path = "", path_to_find = "";
081: ProjectNavigatorOperator prjNav = ProjectNavigatorOperator
082: .showProjectNavigator();
083: TestUtils.wait(1000);
084: full_path = this .path.toString().substring(1,
085: this .path.toString().length() - 1)
086: + "|" + this .name;
087: path_to_find = full_path + "|" + name;
088: if (full_path.indexOf("|", full_path.indexOf("|") + 1) != -1) {
089: path_part1 = full_path.substring(0, full_path.indexOf(
090: "|", full_path.indexOf("|") + 1) + 1);
091: path_part2 = full_path.substring(
092: full_path.indexOf("|",
093: full_path.indexOf("|") + 1) + 1,
094: full_path.length()).replace('|', '.');
095: full_path = path_part1.concat(path_part2);
096: path_to_find = full_path + "." + name;
097: }
098: try {
099: prjNav.tree().findPath(path_to_find);
100: } catch (TimeoutExpiredException tee) {
101: try {
102: prjNav.pressPopupItemOnNode(full_path,
103: popupNewPackage,
104: new Operator.DefaultStringComparator(true,
105: true));
106: } catch (Exception e) {
107: throw new JemmyException(popupNewPackage
108: + " item in popup menu of " + full_path
109: + " node can't be found!", e);
110: }
111: TestUtils.wait(1000);
112: try {
113: JDialogOperator jdo = new JDialogOperator(
114: dlgNewPackage);
115: if (!path_part2.equals(""))
116: path_part2 = path_part2 + ".";
117: new JTextFieldOperator(jdo, 0).setText(path_part2
118: + name);
119: TestUtils.wait(500);
120: new JButtonOperator(jdo, btnFinish).pushNoBlock();
121: } catch (Exception e) {
122: throw new JemmyException("Error occurs in "
123: + dlgNewPackage + " dialog!", e);
124: }
125: TestUtils.wait(1000);
126: }
127: SourceFolder newSourceFolder = new SourceFolder(
128: new TreePath(full_path), name, this );
129: childList.add(newSourceFolder);
130: return newSourceFolder;
131: } else
132: return getSourceSubFolder(name);
133: }
134:
135: /**
136: * Get source subfolder with specified name.
137: * @param name Name of source subfolder.
138: * @return Source folder with specified name.
139: */
140: public SourceFolder getSourceSubFolder(String name) {
141: for (int i = 0; i < childList.size(); i++) {
142: if (childList.get(i) instanceof SourceFolder
143: && ((SourceFolder) childList.get(i)).getName()
144: .equals(name))
145: return ((SourceFolder) childList.get(i));
146: }
147: return null;
148: }
149:
150: /**
151: * Add Java file to folder.
152: * @param name Name of Java file.
153: * @return Added Java file.
154: */
155: public JavaFile addJavaFile(String name) {
156: if (getJavaFile(name) == null) {
157: String path_part1 = "", path_part2 = "", full_path = "";
158: ProjectNavigatorOperator prjNav = ProjectNavigatorOperator
159: .showProjectNavigator();
160: TestUtils.wait(1000);
161: full_path = this .path.toString().substring(1,
162: this .path.toString().length() - 1)
163: + "|" + this .name;
164: if (full_path.indexOf("|", full_path.indexOf("|") + 1) != -1) {
165: path_part1 = full_path.substring(0, full_path.indexOf(
166: "|", full_path.indexOf("|") + 1) + 1);
167: path_part2 = full_path.substring(
168: full_path.indexOf("|",
169: full_path.indexOf("|") + 1) + 1,
170: full_path.length()).replace('|', '.');
171: full_path = path_part1.concat(path_part2);
172: }
173: try {
174: prjNav.tree()
175: .findPath(full_path + "|" + name + ".java");
176: } catch (TimeoutExpiredException tee) {
177: try {
178: prjNav.pressPopupItemOnNode(full_path,
179: popupNewClass,
180: new Operator.DefaultStringComparator(true,
181: true));
182: } catch (Exception e) {
183: throw new JemmyException(popupNewClass
184: + " item in popup menu of " + full_path
185: + " node can't be found!", e);
186: }
187: TestUtils.wait(1000);
188: try {
189: JDialogOperator jdo = new JDialogOperator(
190: dlgNewClass);
191: new JTextFieldOperator(jdo, 0).setText(name);
192: TestUtils.wait(500);
193: new JButtonOperator(jdo, btnFinish).pushNoBlock();
194: } catch (Exception e) {
195: throw new JemmyException("Error occurs in "
196: + dlgNewClass + " dialog!", e);
197: }
198: TestUtils.wait(1000);
199: }
200: JavaFile newJavaFile;
201: if (getParent() instanceof RootEntry)
202: newJavaFile = new JavaFile(new TreePath(full_path
203: + "|<default package>"), name, this );
204: else
205: newJavaFile = new JavaFile(new TreePath(full_path),
206: name, this );
207: childList.add(newJavaFile);
208: return newJavaFile;
209: } else
210: return getJavaFile(name);
211: }
212:
213: /**
214: * Get Java file with specified name.
215: * @param name Name of Java file.
216: * @return Java file with specified name.
217: */
218: public JavaFile getJavaFile(String name) {
219: for (int i = 0; i < childList.size(); i++) {
220: if (childList.get(i) instanceof JavaFile
221: && ((JavaFile) childList.get(i)).getName().equals(
222: name))
223: return ((JavaFile) childList.get(i));
224: }
225: if ((getParent().getParent() instanceof RootEntry)
226: && (name.equals("ApplicationBean1")
227: || name.equals("RequestBean1") || name
228: .equals("SessionBean1"))) {
229: String str_path = this .path.toString().substring(1,
230: this .path.toString().length() - 1);
231: JavaFile newJavaFile = new JavaFile(new TreePath(str_path
232: + "|" + this.name), name, this);
233: childList.add(newJavaFile);
234: return newJavaFile;
235: }
236: return null;
237: }
238: }
|