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 org.netbeans.jemmy.Waitable;
045: import org.netbeans.modules.visualweb.gravy.MainWindowOperator;
046:
047: /**
048: * This class is used for observation of status of some component.
049: */
050: public class StatusWaitable implements Waitable {
051: private String[] variants;
052: private String valid;
053: private boolean exactMatch;
054: private boolean thread;
055:
056: /**
057: * Creates an instance of this class.
058: * @param variants an array of Strings, which could appear in an observable component.
059: */
060: public StatusWaitable(String[] variants) {
061: this .variants = variants;
062: this .exactMatch = true;
063: this .thread = false;
064: }
065:
066: /**
067: * Creates an instance of this class.
068: * @param variants an array of Strings, which could appear in an observable component.
069: * @param isExact a boolean value, which defines a mode of String comparison:
070: * true means using of the method equals(), false means using of the method indexOf().
071: */
072: public StatusWaitable(String[] variants, boolean isExact) {
073: this .variants = variants;
074: this .exactMatch = isExact;
075: this .thread = false;
076: }
077:
078: /**
079: * Creates an instance of this class.
080: * @param variant1 a string, which could appear in an observable component.
081: * @param variant2 a string, which could appear in an observable component.
082: */
083: public StatusWaitable(String variant1, String variant2) {
084: this .variants = new String[] { variant1, variant2 };
085: this .exactMatch = false;
086: this .thread = true;
087: }
088:
089: /**
090: * Creates an instance of this class.
091: * @param variant1 a string, which could appear in an observable component.
092: * @param variant2 a string, which could appear in an observable component.
093: * @param variant3 a string, which could appear in an observable component.
094: */
095: public StatusWaitable(String variant1, String variant2,
096: String variant3) {
097: this .variants = new String[] { variant1, variant2, variant3 };
098: this .exactMatch = false;
099: this .thread = true;
100: }
101:
102: /**
103: * Checks if wait criteria have been met.
104: * @param o optional waiting parameter.
105: * @return null if criteria has not been met.
106: */
107: public Object actionProduced(Object o) {
108: if (exactMatch) {
109: for (int i = 0; i < variants.length; i++) {
110: if (variants[i].equals(getStatusText())) {
111: valid = variants[i];
112: return Boolean.TRUE;
113: }
114: }
115: } else {
116: if (thread) {
117: String status = getStatusText();
118: if (status.startsWith(variants[0])) {
119: int index = status.lastIndexOf(variants[1]);
120: int index1 = 0;
121: if (variants.length == 3)
122: index1 = status.lastIndexOf(variants[2]);
123: if (index + variants[1].length() == status.length()) {
124: return Boolean.TRUE;
125: }
126: if (variants.length == 3)
127: if (index1 + variants[2].length() == status
128: .length()) {
129: return Boolean.TRUE;
130: }
131: }
132: } else {
133: for (int i = 0; i < variants.length; i++) {
134: if (getStatusText().indexOf(variants[i]) != -1) {
135: valid = variants[i];
136: return Boolean.TRUE;
137: }
138: }
139: }
140: }
141: return null;
142: }
143:
144: /**
145: * Returns description.
146: * @return a text description of the wait criteria.
147: */
148: public String getDescription() {
149: String dsc = "";
150: for (int i = 0; i < variants.length; i++) {
151: dsc += variants[i] + "|";
152: }
153: return "Variants = " + dsc;
154: }
155:
156: /**
157: * Returns current status of an observable component.
158: * @return a text corresponding to current status.
159: */
160: public String getStatus() {
161: return valid;
162: }
163:
164: /**
165: * Returns current text from status line of the IDE's main window.
166: * @return a text from IDE's status line.
167: */
168: private String getStatusText() {
169: String s = (Util.getMainWindow()).getStatusText();
170: return (s == null) ? "" : s;
171: }
172: }
|