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.test.umllib;
043:
044: import java.awt.Component;
045: import java.awt.Container;
046: import javax.swing.JList;
047: import javax.swing.ListModel;
048: import org.netbeans.jellytools.TopComponentOperator;
049: import org.netbeans.jemmy.ComponentChooser;
050: import org.netbeans.jemmy.EventTool;
051: import org.netbeans.jemmy.TimeoutExpiredException;
052: import org.netbeans.jemmy.operators.ContainerOperator;
053: import org.netbeans.jemmy.operators.JListOperator;
054: import org.netbeans.jemmy.operators.JToggleButtonOperator;
055: import org.netbeans.test.umllib.exceptions.NotFoundException;
056: import org.netbeans.test.umllib.exceptions.UMLCommonException;
057: import org.netbeans.test.umllib.util.LibProperties;
058:
059: public class UMLPaletteOperator extends TopComponentOperator {
060:
061: public final static String PALETTE_NAME = "Palette";
062:
063: private static String debug_info;
064:
065: private EventTool eventTool = new EventTool();
066:
067: public UMLPaletteOperator() {
068: super (PALETTE_NAME);
069: waitComponentVisible(true);
070: }
071:
072: /**
073: *
074: * @param toolName
075: * @throws qa.uml.exceptions.NotFoundException
076: */
077: public void selectTool(String toolName) throws NotFoundException {
078: debug_info = "Select Tool Starts;\n";
079: makeComponentVisible();
080: waitComponentVisible(true);
081: //
082: try {
083: Thread.sleep(100);
084: } catch (Exception ex) {
085: }
086: //
087: ComponentChooser listChooser = new JListByItemChooser(toolName);
088: Component comp = waitSubComponent(listChooser);
089: if (comp == null) {
090: throw new NotFoundException("The element with name "
091: + toolName + " was not found on UML palette");
092: }
093: Container cont = comp.getParent();
094: JToggleButtonOperator btn = new JToggleButtonOperator(
095: new ContainerOperator(cont));
096: JListOperator listOperator = new JListOperator((JList) comp);
097: eventTool.waitNoEvent(500);
098: if (!btn.isSelected()) {
099: btn.pushNoBlock();
100: btn.waitSelected(true);
101: }
102: try {
103: Thread.sleep(100);
104: } catch (Exception ex) {
105: }
106: //refresh
107: listOperator = new JListOperator((JList) comp);
108: int itemIndex = listOperator
109: .findItemIndex(new PaletteListItemChooser(toolName));
110: try {
111: listOperator.scrollToItem(itemIndex);
112: } catch (TimeoutExpiredException ex) {
113: //sometimes scrolling generate exceptions without visible reason
114: //may be thhid check will help
115: listOperator = new JListOperator((JList) comp);//refresh list operator
116: if (((JList) (listOperator.getSource()))
117: .getLastVisibleIndex() < itemIndex
118: || ((JList) (listOperator.getSource()))
119: .getFirstVisibleIndex() > itemIndex)
120: throw new UMLCommonException(
121: "Scrolling, current index: "
122: + itemIndex
123: + "; min: "
124: + ((JList) (listOperator.getSource()))
125: .getFirstVisibleIndex()
126: + "; max: "
127: + ((JList) (listOperator.getSource()))
128: .getLastVisibleIndex());
129: //else all is good and we can click
130: }
131: listOperator.getTimeouts().setTimeout(
132: "JScrollBarOperator.WholeScrollTimeout", 500);
133: try {
134: Thread.sleep(100);
135: } catch (Exception ex) {
136: }
137: if (listOperator.getSelectedIndex() != itemIndex) {
138: listOperator.clickOnItem(itemIndex, 1);
139: listOperator.waitItemSelection(itemIndex, true);
140: try {
141: Thread.sleep(100);
142: } catch (Exception ex) {
143: }
144: }
145: listOperator.getTimeouts().setTimeout(
146: "JScrollBarOperator.WholeScrollTimeout", 30000);
147: //wait again with waiter
148: waitSelection(toolName, true);
149: }
150:
151: /**
152: *
153: * @param type
154: * @throws qa.uml.exceptions.NotFoundException
155: */
156: public void selectToolByType(Enum type) throws NotFoundException {
157: selectTool(getToolNameByElementType(type));
158: }
159:
160: /**
161: * @return
162: */
163: public String getSelectedItemname() {
164: JList lst = null;
165: JListWithSelectionChooser tmp = new JListWithSelectionChooser();
166: lst = (JList) findSubComponent(tmp);
167: Object sel = null;
168: if (lst != null)
169: sel = lst.getSelectedValue();
170: return (sel != null) ? (sel.toString()) : ("null");
171: }
172:
173: /**
174: *
175: * @param elementType
176: * @return
177: */
178: public String getToolNameByElementType(Enum elementType) {
179: return LibProperties.getCurrentToolName(elementType);
180: }
181:
182: /**
183: *
184: * @param groupName
185: */
186: public void expandGroup(String groupName) {
187: makeComponentVisible();
188: JToggleButtonOperator groupBtn = new JToggleButtonOperator(
189: this , groupName);
190: if (!groupBtn.isSelected()) {
191: groupBtn.pushNoBlock();
192: groupBtn.waitSelected(true);
193: }
194: }
195:
196: /**
197: *
198: * @param groupName
199: */
200: public void collapseGroup(String groupName) {
201: makeComponentVisible();
202: JToggleButtonOperator groupBtn = new JToggleButtonOperator(
203: this , groupName);
204: if (groupBtn.isSelected()) {
205: groupBtn.pushNoBlock();
206: groupBtn.waitSelected(false);
207: }
208: }
209:
210: private class PaletteListItemChooser implements
211: JListOperator.ListItemChooser {
212: String itemLabel;
213:
214: public PaletteListItemChooser(String itemLabel) {
215: this .itemLabel = itemLabel;
216: }
217:
218: public boolean checkItem(JListOperator oper, int index) {
219: Object obj = oper.getModel().getElementAt(index);
220: return itemLabel.equals(obj.toString());
221:
222: //FilterNode node = (FilterNode)obj;
223: //getOutput().printError("chooser:"+node.getDisplayName());
224: //return (node.getDisplayName().equals(itemLabel));
225: }
226:
227: public String getDescription() {
228: return ("Item equal to \"" + itemLabel + "\" string");
229: }
230: }
231:
232: class JListByItemChooser implements ComponentChooser {
233: String itemName = null;
234:
235: /**
236: *
237: * @param itemName
238: */
239: JListByItemChooser(String itemName) {
240: this .itemName = itemName.trim();
241: System.out.println(" Item Name = \"" + itemName + "\"");
242: }
243:
244: /*
245: * (non-Javadoc)
246: *
247: * @see org.netbeans.jemmy.ComponentChooser#checkComponent(java.awt.Component)
248: */
249: /**
250: *
251: * @param comp
252: * @return
253: */
254: public boolean checkComponent(Component comp) {
255:
256: if (comp instanceof JList) {
257: JList list = (JList) comp;
258: ListModel model = list.getModel();
259: for (int i = 0; i < model.getSize(); i++) {
260: Object obj = model.getElementAt(i);
261:
262: System.out.println(" Elem = \"" + obj.toString()
263: + "\"");
264: if (itemName.equals(obj.toString())) {
265: return true;
266: }
267: /*
268: if (obj instanceof FilterNode){
269: FilterNode node = (FilterNode)obj;
270: //getOutput().printError("display:|"+node.getDisplayName()+ "| "+obj+" index"+i+"search:"+itemName);
271: if (node.getDisplayName().trim().equals(itemName)){
272: return true;
273: }
274: }
275: */
276: }
277: }
278: return false;
279: }
280:
281: /**
282: *
283: * @return
284: */
285: public String getDescription() {
286: return "Tool list by tool with name \"" + itemName
287: + "\" chooser";
288: }
289: }
290:
291: class JListWithSelectionChooser implements ComponentChooser {
292: /**
293: */
294: JListWithSelectionChooser() {
295: }
296:
297: /*
298: * (non-Javadoc)
299: *
300: * @see org.netbeans.jemmy.ComponentChooser#checkComponent(java.awt.Component)
301: */
302: /**
303: *
304: * @param comp
305: * @return
306: */
307: public boolean checkComponent(Component comp) {
308:
309: if (comp instanceof JList) {
310: JList list = (JList) comp;
311: if (list.getSelectedIndices().length > 0)
312: return true;
313: }
314:
315: return false;
316: }
317:
318: /**
319: *
320: * @return
321: */
322: public String getDescription() {
323: return "Tool list which have selected elements";
324: }
325: }
326:
327: /**
328: * wait tool to be selected/deselected
329: * do not open/close, scroll lists or make palette visible
330: * @param toolName
331: * @param selected
332: */
333: public void waitSelection(String toolName, boolean selected) {
334: ComponentChooser listChooser = new JListByItemChooser(toolName);
335: Component comp = waitSubComponent(listChooser);
336: if (comp == null) {
337: throw new NotFoundException("The element with name "
338: + toolName + " was not found on UML palette");
339: }
340: Container cont = comp.getParent();
341: JToggleButtonOperator btn = new JToggleButtonOperator(
342: new ContainerOperator(cont));
343: JListOperator listOperator = new JListOperator((JList) comp);
344: eventTool.waitNoEvent(500);
345: int itemIndex = listOperator
346: .findItemIndex(new PaletteListItemChooser(toolName));
347: listOperator.waitItemSelection(itemIndex, selected);
348: }
349:
350: /**
351: *
352: * @param type
353: * @param selected
354: */
355: public void waitSelection(Enum type, boolean selected) {
356: waitSelection(getToolNameByElementType(type), selected);
357: }
358:
359: public static String getDebugInfo() {
360: return debug_info;
361: }
362: }
|