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): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * 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: * $Id$ $Revision$ $Date$
044: *
045: */
046:
047: package org.netbeans.jemmy.util;
048:
049: import java.awt.Color;
050:
051: import javax.swing.text.Document;
052: import javax.swing.text.Element;
053: import javax.swing.text.StyleConstants;
054: import javax.swing.text.StyledDocument;
055:
056: import org.netbeans.jemmy.operators.JTextComponentOperator.TextChooser;
057:
058: /**
059: * Defines searching criteria for <code>javax.swing.text.StyledDocument</code>
060: * <a href="JTextComponentOperator.java">JTextComponentOperator.getPositionByText(String, JTextComponentOperator.TextChooser, int)</a>.
061: *
062: * @author Alexandre Iline (alexandre.iline@sun.com)
063: */
064: public class TextStyleChooser extends AbstractTextStyleChooser {
065: Boolean bold = null;
066: Boolean italic = null;
067: Boolean strike = null;
068: Boolean understrike = null;
069: Integer fontSize = null;
070: String fontFamily = null;
071: Integer alignment = null;
072: Color background = null;
073: Color foreground = null;
074:
075: /**
076: * Constructor.
077: */
078: public TextStyleChooser() {
079: super ();
080: }
081:
082: /**
083: * Adds boldness checking to the criteria.
084: * @param bold Specifies if font needs to be bold.
085: */
086: public void setBold(boolean bold) {
087: this .bold = bold ? Boolean.TRUE : Boolean.FALSE;
088: }
089:
090: /**
091: * Removes boldness checking from the criteria.
092: */
093: public void unsetBold() {
094: this .bold = null;
095: }
096:
097: /**
098: * Adds italic style checking to the criteria.
099: * @param italic Specifies if font needs to be italic.
100: */
101: public void setItalic(boolean italic) {
102: this .italic = italic ? Boolean.TRUE : Boolean.FALSE;
103: }
104:
105: /**
106: * Removes italic style checking from the criteria.
107: */
108: public void unsetItalic() {
109: this .italic = null;
110: }
111:
112: /**
113: * Adds strikeness checking to the criteria.
114: * @param strike Specifies if font needs to be striked.
115: */
116: public void setStrike(boolean strike) {
117: this .strike = strike ? Boolean.TRUE : Boolean.FALSE;
118: }
119:
120: /**
121: * Removes strikeness checking from the criteria.
122: */
123: public void unsetStrike() {
124: this .strike = null;
125: }
126:
127: /**
128: * Adds understrikeness checking to the criteria.
129: * @param understrike Specifies if font needs to be understriked.
130: */
131: public void setUnderstrike(boolean understrike) {
132: this .understrike = understrike ? Boolean.TRUE : Boolean.FALSE;
133: }
134:
135: /**
136: * Removes understrikeness checking from the criteria.
137: */
138: public void unsetUnderstrike() {
139: this .understrike = null;
140: }
141:
142: /**
143: * Adds font size checking to the criteria.
144: * @param fontSize Specifies a font size.
145: */
146: public void setFontSize(int fontSize) {
147: this .fontSize = new Integer(fontSize);
148: }
149:
150: /**
151: * Removes font size checking from the criteria.
152: */
153: public void unsetFontSize() {
154: this .fontSize = null;
155: }
156:
157: /**
158: * Adds alignment checking to the criteria.
159: * @param alignment Specifies a text alignment.
160: */
161: public void setAlignment(int alignment) {
162: this .alignment = new Integer(alignment);
163: }
164:
165: /**
166: * Removes alignment checking from the criteria.
167: */
168: public void unsetAlignment() {
169: this .alignment = null;
170: }
171:
172: /**
173: * Adds font family checking to the criteria.
174: * @param fontFamily Specifies a font family.
175: */
176: public void setFontFamily(String fontFamily) {
177: this .fontFamily = fontFamily;
178: }
179:
180: /**
181: * Removes font family checking from the criteria.
182: */
183: public void unsetFontFamily() {
184: this .fontFamily = null;
185: }
186:
187: /**
188: * Adds backgroung color checking to the criteria.
189: * @param background Specifies a background color.
190: */
191: public void setBackground(Color background) {
192: this .background = background;
193: }
194:
195: /**
196: * Removes backgroung color checking from the criteria.
197: */
198: public void unsetBackground() {
199: this .background = null;
200: }
201:
202: /**
203: * Adds foregroung color checking to the criteria.
204: * @param foreground Specifies a foreground color.
205: */
206: public void setForeground(Color foreground) {
207: this .foreground = foreground;
208: }
209:
210: /**
211: * Removes foregroung color checking from the criteria.
212: */
213: public void unsetForeground() {
214: this .foreground = null;
215: }
216:
217: public boolean checkElement(StyledDocument doc, Element element,
218: int offset) {
219: if (bold != null) {
220: if (StyleConstants.isBold(element.getAttributes()) != bold
221: .booleanValue()) {
222: return (false);
223: }
224: }
225: if (italic != null) {
226: if (StyleConstants.isItalic(element.getAttributes()) != italic
227: .booleanValue()) {
228: return (false);
229: }
230: }
231: if (strike != null) {
232: if (StyleConstants.isStrikeThrough(element.getAttributes()) != strike
233: .booleanValue()) {
234: return (false);
235: }
236: }
237: if (understrike != null) {
238: if (StyleConstants.isUnderline(element.getAttributes()) != understrike
239: .booleanValue()) {
240: return (false);
241: }
242: }
243: if (fontSize != null) {
244: if (StyleConstants.getFontSize(element.getAttributes()) != fontSize
245: .intValue()) {
246: return (false);
247: }
248: }
249: if (alignment != null) {
250: if (StyleConstants.getAlignment(element.getAttributes()) != alignment
251: .intValue()) {
252: return (false);
253: }
254: }
255: if (fontFamily != null) {
256: if (!StyleConstants.getFontFamily(element.getAttributes())
257: .equals(fontFamily)) {
258: return (false);
259: }
260: }
261: if (background != null) {
262: if (!StyleConstants.getBackground(element.getAttributes())
263: .equals(background)) {
264: return (false);
265: }
266: }
267: if (foreground != null) {
268: if (!StyleConstants.getForeground(element.getAttributes())
269: .equals(foreground)) {
270: return (false);
271: }
272: }
273: return (true);
274: }
275:
276: public String getDescription() {
277: String result = "";
278: if (bold != null) {
279: result = result + (bold.booleanValue() ? "" : "not ")
280: + "bold, ";
281: }
282: if (italic != null) {
283: result = result + (italic.booleanValue() ? "" : "not ")
284: + "italic, ";
285: }
286: if (strike != null) {
287: result = result + (strike.booleanValue() ? "" : "not ")
288: + "strike, ";
289: }
290: if (understrike != null) {
291: result = result
292: + (understrike.booleanValue() ? "" : "not ")
293: + "understrike, ";
294: }
295: if (fontSize != null) {
296: result = result + fontSize.toString() + " size, ";
297: }
298: if (alignment != null) {
299: result = result + alignment.toString() + " alignment, ";
300: }
301: if (fontFamily != null) {
302: result = result + "\"" + fontFamily + "\" font family, ";
303: }
304: if (background != null) {
305: result = result + background.toString() + " background, ";
306: }
307: if (foreground != null) {
308: result = result + foreground.toString() + " foreground, ";
309: }
310: if (result.equals("")) {
311: result = "any, ";
312: }
313: return (result.substring(0, result.length() - 2) + " font");
314: }
315: }
|