TextStyleChooser.java revision 13978:1993af50385d
1/* 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23package org.netbeans.jemmy.util; 24 25import java.awt.Color; 26 27import javax.swing.text.Element; 28import javax.swing.text.StyleConstants; 29import javax.swing.text.StyledDocument; 30 31/** 32 * Defines searching criteria for {@code javax.swing.text.StyledDocument} 33 * <a href="JTextComponentOperator.java">JTextComponentOperator.getPositionByText(String, 34 * JTextComponentOperator.TextChooser, int)</a>. 35 * 36 * @author Alexandre Iline (alexandre.iline@oracle.com) 37 */ 38public class TextStyleChooser extends AbstractTextStyleChooser { 39 40 Boolean bold = null; 41 Boolean italic = null; 42 Boolean strike = null; 43 Boolean understrike = null; 44 Integer fontSize = null; 45 String fontFamily = null; 46 Integer alignment = null; 47 Color background = null; 48 Color foreground = null; 49 50 /** 51 * Constructor. 52 */ 53 public TextStyleChooser() { 54 super(); 55 } 56 57 /** 58 * Adds boldness checking to the criteria. 59 * 60 * @param bold Specifies if font needs to be bold. 61 */ 62 public void setBold(boolean bold) { 63 this.bold = bold ? Boolean.TRUE : Boolean.FALSE; 64 } 65 66 /** 67 * Removes boldness checking from the criteria. 68 */ 69 public void unsetBold() { 70 this.bold = null; 71 } 72 73 /** 74 * Adds italic style checking to the criteria. 75 * 76 * @param italic Specifies if font needs to be italic. 77 */ 78 public void setItalic(boolean italic) { 79 this.italic = italic ? Boolean.TRUE : Boolean.FALSE; 80 } 81 82 /** 83 * Removes italic style checking from the criteria. 84 */ 85 public void unsetItalic() { 86 this.italic = null; 87 } 88 89 /** 90 * Adds strikeness checking to the criteria. 91 * 92 * @param strike Specifies if font needs to be striked. 93 */ 94 public void setStrike(boolean strike) { 95 this.strike = strike ? Boolean.TRUE : Boolean.FALSE; 96 } 97 98 /** 99 * Removes strikeness checking from the criteria. 100 */ 101 public void unsetStrike() { 102 this.strike = null; 103 } 104 105 /** 106 * Adds understrikeness checking to the criteria. 107 * 108 * @param understrike Specifies if font needs to be understriked. 109 */ 110 public void setUnderstrike(boolean understrike) { 111 this.understrike = understrike ? Boolean.TRUE : Boolean.FALSE; 112 } 113 114 /** 115 * Removes understrikeness checking from the criteria. 116 */ 117 public void unsetUnderstrike() { 118 this.understrike = null; 119 } 120 121 /** 122 * Adds font size checking to the criteria. 123 * 124 * @param fontSize Specifies a font size. 125 */ 126 public void setFontSize(int fontSize) { 127 this.fontSize = Integer.valueOf(fontSize); 128 } 129 130 /** 131 * Removes font size checking from the criteria. 132 */ 133 public void unsetFontSize() { 134 this.fontSize = null; 135 } 136 137 /** 138 * Adds alignment checking to the criteria. 139 * 140 * @param alignment Specifies a text alignment. 141 */ 142 public void setAlignment(int alignment) { 143 this.alignment = Integer.valueOf(alignment); 144 } 145 146 /** 147 * Removes alignment checking from the criteria. 148 */ 149 public void unsetAlignment() { 150 this.alignment = null; 151 } 152 153 /** 154 * Adds font family checking to the criteria. 155 * 156 * @param fontFamily Specifies a font family. 157 */ 158 public void setFontFamily(String fontFamily) { 159 this.fontFamily = fontFamily; 160 } 161 162 /** 163 * Removes font family checking from the criteria. 164 */ 165 public void unsetFontFamily() { 166 this.fontFamily = null; 167 } 168 169 /** 170 * Adds backgroung color checking to the criteria. 171 * 172 * @param background Specifies a background color. 173 */ 174 public void setBackground(Color background) { 175 this.background = background; 176 } 177 178 /** 179 * Removes backgroung color checking from the criteria. 180 */ 181 public void unsetBackground() { 182 this.background = null; 183 } 184 185 /** 186 * Adds foregroung color checking to the criteria. 187 * 188 * @param foreground Specifies a foreground color. 189 */ 190 public void setForeground(Color foreground) { 191 this.foreground = foreground; 192 } 193 194 /** 195 * Removes foregroung color checking from the criteria. 196 */ 197 public void unsetForeground() { 198 this.foreground = null; 199 } 200 201 @Override 202 public boolean checkElement(StyledDocument doc, Element element, int offset) { 203 if (bold != null) { 204 if (StyleConstants.isBold(element.getAttributes()) != bold.booleanValue()) { 205 return false; 206 } 207 } 208 if (italic != null) { 209 if (StyleConstants.isItalic(element.getAttributes()) != italic.booleanValue()) { 210 return false; 211 } 212 } 213 if (strike != null) { 214 if (StyleConstants.isStrikeThrough(element.getAttributes()) != strike.booleanValue()) { 215 return false; 216 } 217 } 218 if (understrike != null) { 219 if (StyleConstants.isUnderline(element.getAttributes()) != understrike.booleanValue()) { 220 return false; 221 } 222 } 223 if (fontSize != null) { 224 if (StyleConstants.getFontSize(element.getAttributes()) != fontSize.intValue()) { 225 return false; 226 } 227 } 228 if (alignment != null) { 229 if (StyleConstants.getAlignment(element.getAttributes()) != alignment.intValue()) { 230 return false; 231 } 232 } 233 if (fontFamily != null) { 234 if (!StyleConstants.getFontFamily(element.getAttributes()).equals(fontFamily)) { 235 return false; 236 } 237 } 238 if (background != null) { 239 if (!StyleConstants.getBackground(element.getAttributes()).equals(background)) { 240 return false; 241 } 242 } 243 if (foreground != null) { 244 if (!StyleConstants.getForeground(element.getAttributes()).equals(foreground)) { 245 return false; 246 } 247 } 248 return true; 249 } 250 251 @Override 252 public String getDescription() { 253 String result = ""; 254 if (bold != null) { 255 result = result + (bold.booleanValue() ? "" : "not ") + "bold, "; 256 } 257 if (italic != null) { 258 result = result + (italic.booleanValue() ? "" : "not ") + "italic, "; 259 } 260 if (strike != null) { 261 result = result + (strike.booleanValue() ? "" : "not ") + "strike, "; 262 } 263 if (understrike != null) { 264 result = result + (understrike.booleanValue() ? "" : "not ") + "understrike, "; 265 } 266 if (fontSize != null) { 267 result = result + fontSize.toString() + " size, "; 268 } 269 if (alignment != null) { 270 result = result + alignment.toString() + " alignment, "; 271 } 272 if (fontFamily != null) { 273 result = result + "\"" + fontFamily + "\" font family, "; 274 } 275 if (background != null) { 276 result = result + background.toString() + " background, "; 277 } 278 if (foreground != null) { 279 result = result + foreground.toString() + " foreground, "; 280 } 281 if (result.equals("")) { 282 result = "any, "; 283 } 284 return result.substring(0, result.length() - 2) + " font"; 285 } 286 287 @Override 288 public String toString() { 289 return "TextStyleChooser{description = " + getDescription() + '}'; 290 } 291} 292