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.awt.Container;
044: import java.util.Iterator;
045: import org.netbeans.core.windows.ModeImpl;
046: import org.netbeans.core.windows.WindowManagerImpl;
047: import org.netbeans.jemmy.JemmyException;
048: import org.netbeans.jemmy.QueueTool;
049: import org.netbeans.jemmy.operators.ContainerOperator;
050: import org.netbeans.jemmy.operators.JButtonOperator;
051: import org.netbeans.jemmy.operators.JTableOperator;
052: import org.openide.windows.TopComponent;
053:
054: /**
055: * Handle documents area of NetBeans IDE. It holds editor top components
056: * which can be tested by {@link EditorOperator}. This operator only enables
057: * to switch between editors and to manipulate control buttons.
058: * <p>
059: * Usage:<br>
060: * <pre>
061: EditorWindowOperator ewo = new EditorWindowOperator();
062: String filename = "MyClass";
063: EditorOperator eo1 = ewo.selectPage(filename);
064: // gets currently selected editor
065: EditorOperator eo2 = ewo.getEditor();
066: // switches to requested editor and gets EditorOperator instance
067: EditorOperator eo3 = ewo.getEditor(filename);
068: // manipulate control buttons
069: ewo.moveTabsLeft();
070: ewo.moveTabsRight();
071: ewo.jumpLeft();
072: // selects document from the list
073: ewo.selectDocument(filename);
074: * </pre>
075: * @author Jiri.Skrivanek@sun.com
076: */
077: public class EditorWindowOperator {
078:
079: /** Components operators. */
080: private static JButtonOperator _btLeft;
081: private static JButtonOperator _btRight;
082: private static JButtonOperator _btDown;
083:
084: /** Creates new instance of EditorWindowOperator.
085: * @deprecated Use static methods instead.
086: */
087: public EditorWindowOperator() {
088: // useless now because all methods are static
089: }
090:
091: /** Returns operator of left arrow button in top right corner intended to
092: * move tabs to be visible left ones.
093: * @return JButtonOperator instance
094: */
095: public static JButtonOperator btLeft() {
096: if (_btLeft == null) {
097: _btLeft = new JButtonOperator(new ContainerOperator(
098: getEditor().findTabDisplayer()), 0);
099: }
100: return _btLeft;
101: }
102:
103: /** Returns operator of right arrow button in top right corner intended to
104: * move tabs to be visible right ones.
105: * @return JButtonOperator instance
106: */
107: public static JButtonOperator btRight() {
108: if (_btRight == null) {
109: _btRight = new JButtonOperator(new ContainerOperator(
110: getEditor().findTabDisplayer()), 1);
111: }
112: return _btRight;
113: }
114:
115: /** Returns operator of down arrow button in top right corner intended to
116: * show list of opened documents and selects a document in the list.
117: * @return JButtonOperator instance
118: */
119: public static JButtonOperator btDown() {
120: if (_btDown == null) {
121: _btDown = new JButtonOperator(new ContainerOperator(
122: getEditor().findTabDisplayer()), 2);
123: }
124: return _btDown;
125: }
126:
127: /** Close all opened documents and discard all changes.
128: * It works also if no file is modified, so it is a safe way how to close
129: * documents and no block further execution.
130: */
131: public static void closeDiscard() {
132: Iterator iter = findEditorMode().getOpenedTopComponents()
133: .iterator();
134: while (iter.hasNext()) {
135: EditorOperator.close((TopComponent) iter.next(), false);
136: }
137: }
138:
139: /** Selects page by its label. If only one file is open in Editor, it does
140: * nothing.
141: * @param label label of page to switch to
142: * @return instance of selected EditorOperator
143: */
144: public static EditorOperator selectPage(String label) {
145: return new EditorOperator(label);
146: }
147:
148: /** Selects page by its index. If only one file is open in Editor, it does
149: * nothing.
150: * @param index index of page to be selected
151: * @return instance of selected EditorOperator
152: * @see EditorOperator
153: */
154: public static EditorOperator selectPage(int index) {
155: try {
156: // finds and selects index-th editor
157: new TopComponentOperator((TopComponent) getEditor()
158: .findTabbedAdapter().getTopComponentAt(index));
159: } catch (IndexOutOfBoundsException e) {
160: throw new JemmyException("Index " + index
161: + " out of bounds.", e); //NOI18N
162: }
163: return getEditor();
164: }
165:
166: /** Returns EditorOperator instance of currently selected document.
167: * @return EditorOperator instance of the selected document
168: * @see EditorOperator
169: */
170: public static EditorOperator getEditor() {
171: final ModeImpl mode = findEditorMode();
172: // run in dispatch thread
173: String name = (String) new QueueTool()
174: .invokeSmoothly(new QueueTool.QueueAction(
175: "getSelectedTopComponent().getName()") { // NOI18N
176: public Object launch() {
177: return mode.getSelectedTopComponent().getName();
178: }
179: });
180: return new EditorOperator(name);
181: }
182:
183: /** Selects page with given label and returns EditorOperator instance of
184: * that page.
185: * @param label label of page to be selected
186: * @return EditorOperator instance of requested page
187: * @see EditorOperator
188: */
189: public static EditorOperator getEditor(String label) {
190: return selectPage(label);
191: }
192:
193: /** Selects page with given index and returns EditorOperator instance of
194: * that page.
195: * @param index index of page to be selected
196: * @return EditorOperator instance of requested page
197: * @see EditorOperator
198: */
199: public static EditorOperator getEditor(int index) {
200: return selectPage(index);
201: }
202:
203: /********************** Control buttons ********************************/
204:
205: /** If the leftmost visible tab is partially hidden, it clicks on it.
206: * Otherwise it does nothing.
207: * @return true if tabs were moved, false otherwise
208: */
209: public boolean jumpLeft() {
210: if (btLeft().isEnabled()) {
211: Container cont = getEditor().findTabDisplayer();
212: // click left corner
213: new ContainerOperator(cont).clickMouse(cont.getX() + 1,
214: cont.getY() + cont.getHeight() / 2, 1);
215: return true;
216: }
217: return false;
218: }
219:
220: /** Pushes rigth arrow control button in top right corner intended to
221: * move tabs to be visible right ones. If the button is not enabled,
222: * it does nothing.
223: */
224: public static void moveTabsRight() {
225: btRight().push();
226: }
227:
228: /** Pushes left arrow control button in top right corner intended to
229: * move tabs to be visible left ones. If the button is not enabled,
230: * it does nothing.
231: */
232: public static void moveTabsLeft() {
233: btLeft().push();
234: }
235:
236: /** Pushes down arrow control button in top right corner intended to
237: * show list of opened documents and selects index-th documents in the list.
238: */
239: public static void selectDocument(int index) {
240: btDown().push();
241: JTableOperator tableOper = new JTableOperator(
242: MainWindowOperator.getDefault());
243: tableOper.selectCell(index, 0);
244: }
245:
246: /** Pushes down arrow control button in top right corner intended to
247: * show list of opened documents and selects document with given name
248: * in the list.
249: */
250: public static void selectDocument(String name) {
251: btDown().push();
252: JTableOperator tableOper = new JTableOperator(
253: MainWindowOperator.getDefault());
254: int row = tableOper.findCellRow(name);
255: if (row > -1) {
256: tableOper.selectCell(row, 0);
257: } else {
258: throw new JemmyException("Cannot select document \"" + name
259: + "\".");
260: }
261: }
262:
263: /** Performs verification by accessing all sub-components */
264: public static void verify() {
265: getEditor().verify();
266: }
267:
268: /** Finds editor mode within IDE window system.
269: * @return editor mode instance
270: */
271: private static ModeImpl findEditorMode() {
272: // run in dispatch thread
273: return (ModeImpl) new QueueTool()
274: .invokeSmoothly(new QueueTool.QueueAction("findMode") { // NOI18N
275: public Object launch() {
276: return WindowManagerImpl.getInstance()
277: .findMode("editor"); //NOI18N
278: }
279: });
280: }
281: }
|