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.properties;
043:
044: import java.awt.Component;
045: import java.awt.Container;
046: import java.lang.reflect.Method;
047: import javax.swing.JComponent;
048: import org.netbeans.jemmy.Action;
049: import org.netbeans.jemmy.ActionProducer;
050: import org.netbeans.jemmy.ComponentChooser;
051: import org.netbeans.jemmy.JemmyException;
052: import org.netbeans.jemmy.operators.ContainerOperator;
053: import org.netbeans.jemmy.operators.JComponentOperator;
054: import org.netbeans.jemmy.operators.Operator;
055: import org.netbeans.jemmy.util.MouseVisualizer;
056: import org.openide.explorer.propertysheet.PropertyPanel;
057:
058: /**
059: * Handle sheet button in IDE property sheets. SheetButton extends JPanel
060: * and it can show property name, property value or customizer button ("..." button).
061: * It is not recommended to use it directly but to use Property class and its
062: * descendants.
063: *
064: * @deprecated JTable used instead of array of SheetButtons.
065: * Use {@link Property} to change value of property.
066: */
067: public class SheetButtonOperator extends JComponentOperator {
068:
069: /** Index of name button (number of row) */
070: private int nameButtonIndex;
071: /** Temporary storage */
072: private static int temporaryNameButtonIndex;
073:
074: /** Create new instance. Use getters below to construct a new instance.
075: * @deprecated JTable used instead of array of SheetButtons.
076: * Use {@link Property} to change value of property.
077: */
078: private SheetButtonOperator(Component button) {
079: super ((JComponent) button);
080: }
081:
082: /** Waits for sheet button representing specified property name. It sets
083: * index of button in sheet.
084: * @param contOper where to find button
085: * @param propertyName name of property
086: * @return SheetButtonOperator instance
087: * @see #getNameButtonIndex()
088: * @deprecated JTable used instead of array of SheetButtons.
089: * Use {@link Property} to change value of property.
090: */
091: public static synchronized SheetButtonOperator nameButton(
092: ContainerOperator contOper, String propertyName) {
093: SheetButtonOperator sbo = new SheetButtonOperator(
094: waitNameButton(contOper, propertyName, 0));
095: sbo.nameButtonIndex = temporaryNameButtonIndex;
096: return sbo;
097: }
098:
099: /** Waits for index-th sheet button representing property name. It sets
100: * index of button in sheet.
101: * @param contOper where to find button
102: * @param index index of button (row in sheet - starts at 0)
103: * @return SheetButtonOperator instance
104: * @see #getNameButtonIndex()
105: * @deprecated JTable used instead of array of SheetButtons.
106: * Use {@link Property} to change value of property.
107: */
108: public static SheetButtonOperator nameButton(
109: ContainerOperator contOper, int index) {
110: SheetButtonOperator sbo = new SheetButtonOperator(
111: waitNameButton(contOper, null, index));
112: sbo.nameButtonIndex = index;
113: return sbo;
114: }
115:
116: /** Waits for sheet button representing value of property. It is identified
117: * by index which can be found by name button.
118: * @param contOper where to find
119: * @param nameButtonIndex index of property in sheet
120: * @return SheetButtonOperator instance
121: * @deprecated JTable used instead of array of SheetButtons.
122: * Use {@link Property} to change value of property.
123: */
124: public static SheetButtonOperator valueButton(
125: ContainerOperator contOper, int nameButtonIndex) {
126: return new SheetButtonOperator(waitValueButton(contOper,
127: nameButtonIndex));
128: }
129:
130: /** Waits for customizer button ("..." button) in given ContainerOperator.
131: * @param contOper where to find button
132: * @return SheetButtonOperator instance
133: * @deprecated JTable used instead of array of SheetButtons.
134: * Use {@link Property} to change value of property.
135: */
136: public static SheetButtonOperator customizerButton(
137: ContainerOperator contOper) {
138: return new SheetButtonOperator(waitCustomizerButton(contOper));
139: }
140:
141: /** Waits LazyToolTipSheetButton by its name and index. Sets index of
142: * property in sheet. */
143: private static Component waitNameButton(ContainerOperator contOper,
144: String name, int index) {
145: throw new JemmyException(
146: "Don't use this! SheetButton no more used in property sheet.");
147: /*SheetButtonChooser chooser = new SheetButtonChooser(name, contOper.getComparator());
148: Component comp = contOper.waitComponent((Container)contOper.getSource(), chooser, index);
149: temporaryNameButtonIndex = chooser.getIndex();
150: return comp;*/
151: }
152:
153: /** Waits "..." button which is SheetButton class. */
154: private static Component waitCustomizerButton(
155: ContainerOperator contOper) {
156: throw new JemmyException(
157: "Don't use this! SheetButton no more used in property sheet.");
158: /*ComponentChooser chooser = new ComponentChooser() {
159: public boolean checkComponent(Component comp) {
160: return comp.getClass().getName().indexOf("propertysheet.SheetButton") != -1;
161: }
162:
163: public String getDescription() {
164: return "SheetButton \"...\"";
165: }
166: };
167: return contOper.waitComponent((Container)contOper.getSource(), chooser);*/
168: }
169:
170: /** Waits value button which is instance of PropertySheetButton. First
171: * it finds nameButtonIndex-th PropertyPanel and inside it it tries to find
172: * PropertySheetButton.
173: */
174: private static Component waitValueButton(
175: ContainerOperator contOper, int nameButtonIndex) {
176: throw new JemmyException(
177: "Don't use this! SheetButton no more used in property sheet.");
178: /*ComponentChooser chooser = new ComponentChooser() {
179: public boolean checkComponent(Component comp) {
180: return comp.getClass().getName().indexOf("PropertySheetButton") != -1;
181: }
182:
183: public String getDescription() {
184: return "PropertySheetButton";
185: }
186: };
187: Component propertyPanel = waitPropertyPanel(contOper, nameButtonIndex);
188: return contOper.waitComponent((Container)propertyPanel, chooser);*/
189: }
190:
191: /** Waits org.openide.explorer.propertysheet.PropertyPanel with given index.
192: * PropertyPanel represents container for a value both in editable and
193: * non editable states.
194: */
195: static Component waitPropertyPanel(ContainerOperator contOper,
196: int nameButtonIndex) {
197: throw new JemmyException(
198: "Don't use this! SheetButton no more used in property sheet.");
199: /*ComponentChooser chooser = new ComponentChooser() {
200: public boolean checkComponent(Component comp) {
201: return comp instanceof PropertyPanel;
202: }
203:
204: public String getDescription() {
205: return "org.openide.explorer.propertysheet.PropertyPanel";
206: }
207: };
208: return contOper.waitComponent((Container)contOper.getSource(), chooser, nameButtonIndex);*/
209: }
210:
211: /** Chooser to find instance LazyToolTipSheetButton by property name.
212: * getIndex() method can be used to get index of found button.
213: * It is also used by PropertySheetTabOperator. */
214: static class SheetButtonChooser implements ComponentChooser {
215: private String propertyName;
216: private int index = -1;
217: private StringComparator comparator;
218:
219: public SheetButtonChooser(String propertyName,
220: StringComparator comparator) {
221: this .propertyName = propertyName;
222: this .comparator = comparator;
223: }
224:
225: public boolean checkComponent(Component comp) {
226: if (comp.getClass().getName().indexOf(
227: "LazyToolTipSheetButton") != -1) {
228: index++;
229: return comparator.equals(getButtonLabel(comp),
230: propertyName);
231: }
232: return false;
233: }
234:
235: public String getDescription() {
236: return "\""
237: + (propertyName == null ? "index-th" : propertyName)
238: + "\" SheetButton";
239: }
240:
241: public int getIndex() {
242: return index;
243: }
244: }
245:
246: /** Gets label of given sheet button by reflection call. */
247: private static String getButtonLabel(Component sheetButton) {
248: try {
249: Class clazz = Class
250: .forName("org.openide.explorer.propertysheet.SheetButton");
251: Method getLabelMethod = clazz.getDeclaredMethod("getLabel",
252: null);
253: getLabelMethod.setAccessible(true);
254: return getLabelMethod.invoke(sheetButton, null).toString();
255: } catch (Exception e) {
256: throw new JemmyException(
257: "SheetButton.getLabel() by reflection failed.", e);
258: }
259: }
260:
261: /** Gets label of this sheet button.
262: * @return label of this button
263: */
264: public String getLabel() {
265: return getButtonLabel(getSource());
266: }
267:
268: /** Gets index (row) of name button in property sheet. Each row represents
269: * one property.
270: * @return index (row) of name button
271: */
272: public int getNameButtonIndex() {
273: return nameButtonIndex;
274: }
275:
276: /** If needed, it scrolls to be this button visible, and clicks on
277: * this button by mouse.
278: */
279: public void push() {
280: // Possibly we don't need to scroll and makeVisible, if we use only
281: // setText, setSelectedItem (not typeText, selectItem) in Property
282: // and its descendants
283: MouseVisualizer mv = new MouseVisualizer(MouseVisualizer.TOP,
284: 0.5, 10, false);
285: mv.scroll(true);
286: mv.makeVisible(this );
287: clickMouse();
288: }
289:
290: /** If needed, it scrolls to be this button visible, and clicks
291: * on this button by mouse and no block further execution. */
292: public void pushNoBlock() {
293: try {
294: new ActionProducer(new Action() {
295: public Object launch(Object obj) {
296: push();
297: return null;
298: }
299:
300: public String getDescription() {
301: return ("Push \"" + getLabel() + "\" button - don't wait");
302: }
303: }, false).produceAction(null);
304: } catch (Exception e) {
305: throw new JemmyException("Push \"" + getLabel()
306: + "\" button action failed.", e);
307: }
308: }
309:
310: /** If needed, it scrolls to be this button visible, and clicks for popup
311: * on this button.
312: */
313: public void clickForPopup() {
314: MouseVisualizer mv = new MouseVisualizer(MouseVisualizer.TOP,
315: 0.5, 10, false);
316: mv.scroll(true);
317: mv.makeVisible(this);
318: super.clickForPopup();
319: }
320:
321: }
|