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.RaveWindowOperator;
045: import org.netbeans.modules.visualweb.gravy.EditorOperator;
046: import org.netbeans.modules.visualweb.gravy.TestUtils;
047:
048: import javax.swing.tree.TreePath;
049: import java.util.ArrayList;
050: import java.util.List;
051:
052: import org.netbeans.jemmy.operators.JPopupMenuOperator;
053: import org.netbeans.jemmy.TimeoutExpiredException;
054:
055: /**
056: * Class for Java files.
057: */
058:
059: public class JavaFile extends SourceFile implements ProjectEntry {
060:
061: private final static String reformatCode = "Reformat Code";
062:
063: WebPage webpage = null;
064:
065: private EditorOperator editor = null;
066:
067: /**
068: * Parent of the java file.
069: */
070: private ProjectEntry parent;
071:
072: /**
073: * Child project entries of the java file.
074: */
075: private List childList = new ArrayList();
076:
077: /**
078: * Creates a new instance of web page.
079: * @param path Path to web page in project.
080: * @param name Name of web page.
081: */
082: JavaFile(TreePath path, String name, ProjectEntry parent) {
083: super (path, name);
084: setParent(parent);
085: }
086:
087: /**
088: * Save java file.
089: */
090: public void save() {
091: }
092:
093: /**
094: * Save java file as java file with specified name.
095: */
096: public void saveAs(String name) {
097: }
098:
099: /**
100: * @return Name of the java file.
101: */
102: public String getName() {
103: return name;
104: }
105:
106: /**
107: * @return TreePath of the java file.
108: */
109: public TreePath getTreePath() {
110: return path;
111: }
112:
113: /**
114: * @return Parent of the java file.
115: */
116: public ProjectEntry getParent() {
117: return parent;
118: }
119:
120: /**
121: * Set parent of the xml file.
122: */
123: private void setParent(ProjectEntry parent) {
124: this .parent = parent;
125: }
126:
127: /**
128: * @return Child project entries of the java file.
129: */
130: public ProjectEntry[] getChildren() {
131: return ((ProjectEntry[]) childList
132: .toArray(new ProjectEntry[childList.size()]));
133: }
134:
135: /**
136: * Remove java file.
137: */
138: public void delete() {
139: }
140:
141: /**
142: * Open java file editor.
143: */
144: private void doOpenJavaEditor() {
145: webpage.open();
146: editor = webpage.page.getJavaEditor();
147: editor.setVerification(false);
148: editor.requestFocus();
149: }
150:
151: /**
152: * Open java file of web page.
153: */
154: private EditorOperator openJava() {
155: long tmpTimeout = 0;
156: try {
157: tmpTimeout = RaveWindowOperator.getDefaultRave()
158: .getTimeouts().getTimeout(
159: "ComponentOperator.WaitComponentTimeout");
160: RaveWindowOperator.getDefaultRave().getTimeouts()
161: .setTimeout(
162: "ComponentOperator.WaitComponentTimeout",
163: 5000);
164: if (editor == null) {
165: doOpenJavaEditor();
166: } else {
167: try {
168: editor.makeComponentVisible();
169: } catch (Exception e) {
170: throw new TimeoutExpiredException("");
171: }
172: }
173: } catch (TimeoutExpiredException e) {
174: doOpenJavaEditor();
175: } finally {
176: RaveWindowOperator.getDefaultRave().getTimeouts()
177: .setTimeout(
178: "ComponentOperator.WaitComponentTimeout",
179: tmpTimeout);
180: }
181: return editor;
182: }
183:
184: /**
185: * Gets text from the currently opened java file.
186: * @return String representing whole content of the java file.
187: * (including new line characters)
188: */
189: public String getText() {
190: editor = openJava();
191: return editor.getText();
192: }
193:
194: /**
195: * Gets text from specified line.
196: * @param lineNumber Number of line.
197: * @return String representing content of the line.
198: */
199: public String getText(int lineNumber) {
200: editor = openJava();
201: return editor.getText(lineNumber);
202: }
203:
204: /**
205: * Checks if java file contains text specified as parameter text.
206: * @param text Text to compare to.
207: * @return true if text was found, false otherwise.
208: */
209: public boolean contains(String text) {
210: editor = openJava();
211: return editor.contains(text);
212: }
213:
214: /**
215: * Replaces first occurence of oldText by newText.
216: * @param oldText Text to be replaced.
217: * @param newText Text to write instead.
218: */
219: public void replace(String oldText, String newText) {
220: editor = openJava();
221: editor.replace(oldText, newText);
222: }
223:
224: /**
225: * Replaced index-th occurence of oldText by newText.
226: * @param oldText Text to be replaced
227: * @param newText Text to write instead
228: * @param index Tndex of oldText occurence.
229: */
230: public void replace(String oldText, String newText, int index) {
231: editor = openJava();
232: editor.replace(oldText, newText, index);
233: }
234:
235: /**
236: * Insert text to current position.
237: * @param text String to be inserted.
238: */
239: public void insert(String text) {
240: editor = openJava();
241: editor.insert(text);
242: }
243:
244: /**
245: * Inserts text to position specified by line number and column.
246: * @param text String to be inserted.
247: * @param lineNumber Number of line.
248: * @param column Column position.
249: */
250: public void insert(String text, int lineNumber, int column) {
251: editor = openJava();
252: editor.insert(text, lineNumber, column);
253: }
254:
255: /**
256: * Deletes given number of characters from specified possition.
257: * @param offset Position inside document.
258: * @param length Number of characters to be deleted.
259: */
260: public void delete(int offset, int length) {
261: editor = openJava();
262: editor.delete(offset, length);
263: }
264:
265: /**
266: * Deletes given number of characters from current caret possition.
267: * @param length Number of characters to be deleted.
268: */
269: public void delete(int length) {
270: editor = openJava();
271: editor.delete(length);
272: }
273:
274: /**
275: * Delete specified line.
276: * @param line Number of line.
277: */
278: public void deleteLine(int line) {
279: editor = openJava();
280: editor.deleteLine(line);
281: }
282:
283: /**
284: * Deletes characters between column1 and column2 on the specified line.
285: * @param lineNumber Number of line.
286: * @param column1 Column position where to start deleting.
287: * @param column2 Column position where to stop deleting.
288: */
289: public void delete(int lineNumber, int column1, int column2) {
290: editor = openJava();
291: editor.delete(lineNumber, column1, column2);
292: }
293:
294: /**
295: * Pushs key.
296: * @param keyCode Key code (KeyEvent.VK_* value)
297: * @param modifiers Modifiers (combination of InputEvent.*_MASK fields)
298: */
299: public void pushKey(int keyCode, int modifiers) {
300: editor = openJava();
301: editor.pushKey(keyCode, modifiers);
302: }
303:
304: /**
305: * Pushs key.
306: * @param keyCode Key code (KeyEvent.VK_* value)
307: */
308: public void pushKey(int keyCode) {
309: editor = openJava();
310: editor.pushKey(keyCode);
311: }
312:
313: /**
314: * Get position of caret.
315: * @return int Position of caret.
316: */
317: public int getCaretPosition() {
318: editor = openJava();
319: return editor.txtEditorPane().getCaretPosition();
320: }
321:
322: /**
323: * Reformat code in Java editor accordingly to specified rules.
324: */
325: public void reformatCode() {
326: editor = openJava();
327: editor.txtEditorPane().clickForPopup();
328: TestUtils.wait(1000);
329: new JPopupMenuOperator().pushMenu(reformatCode);
330: TestUtils.wait(1000);
331: }
332: }
|