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;
043:
044: import java.awt.Component;
045: import java.io.IOException;
046: import org.netbeans.jemmy.ComponentChooser;
047:
048: import org.netbeans.jemmy.*;
049:
050: import org.netbeans.jemmy.operators.JComponentOperator;
051: import org.netbeans.jemmy.operators.ContainerOperator;
052: import org.netbeans.jemmy.operators.Operator;
053:
054: import org.openide.windows.*;
055: import org.openide.cookies.SaveCookie;
056: import org.openide.loaders.DataObject;
057:
058: import javax.swing.*;
059:
060: /**
061: * Class for TopComponents.
062: */
063: public class TopComponentOperator extends JComponentOperator {
064:
065: /**
066: * Create TopComponentOperator with specified name in given container.
067: * @param parent Container where TopComponent will be looking for.
068: * @param ID Name of the TopComponent.
069: */
070: public TopComponentOperator(ContainerOperator parent, String ID) {
071: super (parent, new TopComponentChooser(ID));
072: }
073:
074: /**
075: * Create TopComponentOperator with chooser in given container.
076: * @param parent Container where TopComponent will be looking for.
077: * @param chooser Chooser for finding.
078: */
079: public TopComponentOperator(ContainerOperator parent,
080: ComponentChooser chooser) {
081: super (parent, chooser);
082: }
083:
084: /**
085: * Create TopComponentOperator from given JComponent.
086: * @param component JComponent from which TopComponentOperator will be created.
087: */
088: public TopComponentOperator(JComponent component) {
089: super (component);
090: }
091:
092: public static class TopComponentChooser implements ComponentChooser {
093: String ID;
094:
095: public TopComponentChooser(String ID) {
096: this .ID = ID;
097: }
098:
099: public boolean checkComponent(Component comp) {
100: return (comp instanceof TopComponent && comp != null
101: && ((TopComponent) comp).getName() != null && ((TopComponent) comp)
102: .getName().indexOf(ID) != -1);
103: }
104:
105: public String getDescription() {
106: return ("A TopComponent with \"" + ID + "\" ID");
107: }
108: }
109:
110: /**
111: * Find TopComponent with specified name in given container with given subchooser.
112: * @param cont Container where TopComponent will be looking for.
113: * @param name Name of the TopComponent.
114: * @param subchooser Subchooser for finding.
115: * @return JComponent Found component.
116: */
117: protected static JComponent findTopComponent(
118: ContainerOperator cont, String name, int index,
119: ComponentChooser subchooser) {
120: Object tc[] = TopComponent.getRegistry().getOpened().toArray();
121: StringComparator comparator = cont == null ? Operator
122: .getDefaultStringComparator() : cont.getComparator();
123: TopComponent c;
124: for (int i = 0; i < tc.length; i++) {
125: c = (TopComponent) tc[i];
126: if (c.isShowing()
127: && comparator.equals(c.getName(), name)
128: && isUnder(cont, c)
129: && (subchooser == null || subchooser
130: .checkComponent(c))) {
131: index--;
132: if (index < 0)
133: return c;
134: }
135: }
136: for (int i = 0; i < tc.length; i++) {
137: c = (TopComponent) tc[i];
138: if ((!c.isShowing())
139: && isParentShowing(c)
140: && comparator.equals(c.getName(), name)
141: && isUnder(cont, c)
142: && (subchooser == null || subchooser
143: .checkComponent(c))) {
144: index--;
145: if (index < 0)
146: return c;
147: }
148: }
149: return null;
150: }
151:
152: /**
153: * Check if component's parent is showing.
154: * @param c Component for verification.
155: * @return True if component's parent is showing.
156: */
157: private static boolean isParentShowing(Component c) {
158: while (c != null) {
159: if (c.isShowing())
160: return true;
161: c = c.getParent();
162: }
163: return false;
164: }
165:
166: /**
167: * Check if component is under given container.
168: * @param cont Container.
169: * @param c Component for verification.
170: * @return True if component is under given container.
171: */
172: private static boolean isUnder(ContainerOperator cont, Component c) {
173: if (cont == null)
174: return true;
175: Component comp = cont.getSource();
176: while (comp != c && c != null)
177: c = c.getParent();
178: return (comp == c);
179: }
180:
181: /** Closes this TopComponent instance by IDE API call and wait until
182: * it is not closed. If this TopComponent is modified (e.g. editor top
183: * component), question dialog is shown and you have to close it. To close
184: * this TopComponent and discard possible changes use {@link #closeDiscard}
185: * method.
186: */
187: public void close() {
188: // run in a new thread because question may block further execution
189: new Thread(new Runnable() {
190: public void run() {
191: // run in dispatch thread
192: runMapping(new MapVoidAction("close") {
193: public void map() {
194: ((TopComponent) getSource()).close();
195: }
196: });
197: }
198: }, "thread to close TopComponent").start();
199: // wait to be away
200: waitComponentShowing(false);
201: }
202:
203: /**
204: * Waits the topcomponent to be closed.
205: */
206: public void waitClosed() {
207: getOutput().printLine(
208: "Wait topcomponent to be closed \n : "
209: + getSource().toString());
210: getOutput().printGolden("Wait topcomponent to be closed");
211: waitState(new ComponentChooser() {
212: public boolean checkComponent(Component comp) {
213: return (!comp.isVisible());
214: }
215:
216: public String getDescription() {
217: return ("Closed topcomponent");
218: }
219: });
220: }
221:
222: /** Saves content of this TopComponent. If it is not applicable or content
223: * of TopComponent is not modified, it does nothing.
224: */
225: public void save() {
226: // should be just one node
227: org.openide.nodes.Node[] nodes = ((TopComponent) getSource())
228: .getActivatedNodes();
229: // TopComponent like Execution doesn't have any nodes associated
230: if (nodes != null) {
231: for (int i = 0; i < nodes.length; i++) {
232: SaveCookie sc = (SaveCookie) nodes[i]
233: .getCookie(SaveCookie.class);
234: if (sc != null) {
235: try {
236: sc.save();
237: } catch (IOException e) {
238: throw new JemmyException(
239: "Exception while saving this TopComponent.",
240: e);
241: }
242: }
243: }
244: }
245: }
246:
247: /** Closes this TopComponent instance by IDE API call and wait until
248: * it is not closed. If this TopComponent is modified (e.g. editor top
249: * component), it discards possible changes.
250: * @see #close
251: */
252: public void closeDiscard() {
253: setUnmodified();
254: close();
255: }
256:
257: /** Finds DataObject for the content of this TopComponent and set it
258: * unmodified. Used in closeDiscard method.
259: */
260: private void setUnmodified() {
261: // should be just one node
262: org.openide.nodes.Node[] nodes = ((TopComponent) getSource())
263: .getActivatedNodes();
264: // TopComponent like Execution doesn't have any nodes associated
265: if (nodes != null) {
266: for (int i = 0; i < nodes.length; i++) {
267: DataObject dob = (DataObject) nodes[i]
268: .getCookie(DataObject.class);
269: dob.setModified(false);
270: }
271: }
272: }
273:
274: /** Waits for index-th TopComponent with given name in IDE registry.
275: * It throws JemmyException when TopComponent is not find until timeout
276: * expires.
277: * @param name name of TopComponent
278: * @param index index of TopComponent
279: * @return TopComponent instance or throws JemmyException if not found
280: * @see #findTopComponent
281: */
282: protected static JComponent waitTopComponent(final String name,
283: final int index) {
284: return waitTopComponent(null, name, index, null);
285: }
286:
287: /** Waits for index-th TopComponent with given name in IDE registry.
288: * It throws JemmyException when TopComponent is not find until timeout
289: * expires.
290: * @param cont container where to search
291: * @param name name of TopComponent
292: * @param index index of TopComponent
293: * @param subchooser ComponentChooser to determine exact TopComponent
294: * @return TopComponent instance or throws JemmyException if not found
295: * @see #findTopComponent
296: */
297: protected static JComponent waitTopComponent(
298: final ContainerOperator cont, final String name,
299: final int index, final ComponentChooser subchooser) {
300: try {
301: Waiter waiter = new Waiter(new Waitable() {
302: public Object actionProduced(Object obj) {
303: return findTopComponent(cont, name, index,
304: subchooser);
305: }
306:
307: public String getDescription() {
308: return ("Wait TopComponent with name="
309: + name
310: + " index="
311: + String.valueOf(index)
312: + (subchooser == null ? "" : " subchooser="
313: + subchooser.getDescription()) + " loaded");
314: }
315: });
316: Timeouts times = JemmyProperties.getCurrentTimeouts()
317: .cloneThis();
318: times
319: .setTimeout(
320: "Waiter.WaitingTime",
321: times
322: .getTimeout("ComponentOperator.WaitComponentTimeout"));
323: waiter.setTimeouts(times);
324: waiter.setOutput(JemmyProperties.getCurrentOutput());
325: return ((JComponent) waiter.waitAction(null));
326: } catch (InterruptedException e) {
327: return (null);
328: }
329: }
330:
331: }
|