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: package org.netbeans.jellytools;
042:
043: import java.awt.Component;
044:
045: import java.lang.reflect.Method;
046:
047: import org.netbeans.jellytools.Bundle;
048: import org.netbeans.jellytools.TopComponentOperator;
049: import org.netbeans.jellytools.actions.PaletteViewAction;
050:
051: import org.netbeans.jemmy.ComponentChooser;
052: import org.netbeans.jemmy.JemmyException;
053: import org.netbeans.jemmy.operators.JCheckBoxOperator;
054: import org.netbeans.jemmy.operators.JListOperator;
055: import org.netbeans.jemmy.operators.JListOperator.ListItemChooser;
056:
057: /**
058: * Keeps methods to access component palette.
059: * <p>
060: * Usage:<br>
061: * <pre>
062: ComponentPaletteOperator cpo = new ComponentPaletteOperator();
063: cpo.expand("Some Category Name");
064: cpo.selectComponent("Some Component Name");
065: * </pre>
066: *
067: * @author Jiri.Skrivanek@sun.com, mmirilovic@netbeans.org
068: */
069: public class PaletteOperator extends TopComponentOperator {
070:
071: private static final PaletteViewAction paletteAction = new PaletteViewAction();
072:
073: // "Palette"
074: private static final String PALETTE_TITLE = Bundle.getString(
075: "org.netbeans.modules.palette.Bundle",
076: "CTL_Component_palette");
077:
078: /** Waits for the Component Palette appearence and creates operator for it. */
079: public PaletteOperator() {
080: super (waitTopComponent(null, PALETTE_TITLE, 0,
081: new PaletteTopComponentChooser()));
082: }
083:
084: /** invokes palette and returns new instance of PaletteOperator
085: * @return new instance of PaletteOperator */
086: public static PaletteOperator invoke() {
087: paletteAction.perform();
088: return new PaletteOperator();
089: }
090:
091: //subcomponents
092:
093: /** Getter for the component types list.
094: * List really looks like a toolbar here.
095: * @return JListOperator instance of a palette
096: */
097: public JListOperator lstComponents() {
098: int i = 0;
099: JListOperator jlo = new JListOperator(this , i++);
100: // find only list which has size greater then 0
101: while (jlo.getModel().getSize() == 0 && i < 10) {
102: jlo = new JListOperator(this , i++);
103: }
104: return jlo;
105: }
106:
107: //common
108:
109: /** Select a component in expanded category of components. Use one of
110: * expand methods before using this method.
111: * @param displayName display name of component to be selected (e.g. Button)
112: * @see #expand
113: */
114: public void selectComponent(final String displayName) {
115: int index = lstComponents().findItemIndex(
116: new ListItemChooser() {
117: public boolean checkItem(JListOperator oper,
118: int index) {
119: try {
120: // call method org.netbeans.modules.palette.DefaultItem#getDisplayName
121: Object item = oper.getModel().getElementAt(
122: index);
123: Method getDisplayNameMethod = item
124: .getClass().getMethod(
125: "getDisplayName",
126: new Class[] {}); // NOI18N
127: getDisplayNameMethod.setAccessible(true);
128: String indexDisplayName = (String) getDisplayNameMethod
129: .invoke(item, new Object[] {});
130: return oper.getComparator().equals(
131: indexDisplayName, displayName);
132: } catch (Exception e) {
133: throw new JemmyException(
134: "getDisplayName failed.", e); // NOI18N
135: }
136: }
137:
138: public String getDescription() {
139: return "display name equals " + displayName; // NOI18N
140: }
141: });
142: lstComponents().selectItem(index);
143: }
144:
145: /** Expands collapsed category.
146: * @param categoryOper JCheckBoxOperator of components category
147: * @param expand true to expand, false to collapse
148: */
149: public void expand(JCheckBoxOperator categoryOper, boolean expand) {
150: if (categoryOper.isSelected() != expand) {
151: categoryOper.push();
152: categoryOper.waitSelected(expand);
153: }
154: }
155:
156: private static class PaletteTopComponentChooser implements
157: ComponentChooser {
158: public boolean checkComponent(Component comp) {
159: return (comp.getClass().getName()
160: .equals("org.netbeans.spi.palette.PaletteTopComponent"));
161: }
162:
163: public String getDescription() {
164: return ("Any PaletteTopComponent");
165: }
166: }
167:
168: /** Performs verification by accessing all sub-components */
169: public void verify() {
170: lstComponents();
171: }
172: }
|