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: /*
043: * JPopupChooser.java
044: *
045: * Created on October 31, 2006, 4:41 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.test.umllib.util;
052:
053: import java.awt.Component;
054: import java.io.FileNotFoundException;
055: import javax.swing.JPopupMenu;
056: import org.netbeans.jemmy.ComponentChooser;
057: import org.netbeans.jemmy.util.Dumper;
058: import org.netbeans.test.umllib.exceptions.UMLCommonException;
059:
060: /**
061: *
062: * @author sp153251
063: */
064: public class JPopupByPointChooser implements ComponentChooser {
065:
066: private int x, y;
067: private int index = 0, counter = 0;
068: private Component component;
069: String res = "";
070:
071: /**
072: *
073: * Creates a new instance of JPopupChooser
074: * @param x - point to be within Popup area - in scrreen axes
075: * @param y - point to be within Popup area - in scrreen axes
076: */
077: public JPopupByPointChooser(int x, int y) {
078: this (x, y, 0);
079: }
080:
081: /**
082: *
083: * Creates a new instance of JPopupChooser
084: * @param p - point to be within Popup area - in scrreen axes
085: */
086: public JPopupByPointChooser(java.awt.Point p) {
087: this (p, 0);
088: }
089:
090: /**
091: *
092: * Creates a new instance of JPopupChooser
093: * @param index
094: * @param x - point to be within Popup area - in scrreen axes
095: * @param y - point to be within Popup area - in scrreen axes
096: */
097: public JPopupByPointChooser(int x, int y, int index) {
098: this (x, y, null, index);
099: }
100:
101: /**
102: *
103: * Creates a new instance of JPopupChooser
104: * @param index
105: * @param x - point to be within Popup area - relative to cmp
106: * @param y - point to be within Popup area - relative to cmp
107: * @param cmp - coordinates relative to this component
108: */
109: public JPopupByPointChooser(int x, int y, Component cmp, int index) {
110: if (index < 0)
111: throw new IndexOutOfBoundsException(
112: "Index shouldn't be negative");
113: this .x = x;
114: this .y = y;
115: if (cmp != null) {
116: this .x += cmp.getLocationOnScreen().x;
117: this .y += cmp.getLocationOnScreen().y;
118: }
119: counter = 0;
120: this .index = index;
121: this .component = cmp;
122: }
123:
124: /**
125: *
126: * Creates a new instance of JPopupChooser
127: * @param index
128: * @param p - point to be within Popup area - in scrreen axes
129: */
130: public JPopupByPointChooser(java.awt.Point p, int index) {
131: this (p.x, p.y, index);
132: }
133:
134: /**
135: *
136: * Creates a new instance of JPopupChooser
137: * @param cmp
138: * @param index
139: * @param p - point to be within Popup area - in scrreen axes
140: */
141: public JPopupByPointChooser(java.awt.Point p, Component cmp,
142: int index) {
143: this (p.x, p.y, cmp, index);
144: }
145:
146: /**
147: *
148: * @param component
149: * @return
150: */
151: public boolean checkComponent(Component component) {
152: if (component instanceof JPopupMenu) {
153: JPopupMenu pop = (JPopupMenu) component;
154: java.awt.Rectangle bnd = pop.getParent().getBounds();
155: //make a bit wider to cover possible shifts
156: bnd.width += 20;
157: bnd.height += 20;
158: bnd.x -= 10;
159: bnd.y -= 10;
160: if (res.length() < 2048)
161: res += pop + ":::" + bnd + ":::" + bnd.contains(x, y)
162: + "\n";
163: counter += bnd.contains(x, y) ? 1 : 0;
164: if (counter > index && bnd.contains(x, y))
165: return true;
166: }
167: return false;
168: }
169:
170: /**
171: *
172: * @return
173: */
174: public String getDescription() {
175: return "Find popup overlapping certain point: "
176: + x
177: + "::"
178: + y
179: + ";"
180: + res
181: + ((component != null) ? ("::" + component + "/"
182: + (x - component.getLocationOnScreen().x) + ":" + (y - component
183: .getLocationOnScreen().y))
184: : "");
185: }
186:
187: }
|