1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package org.openoffice.xmerge.converter.xml.sxc; 25 26 import java.awt.Color; 27 28 /** 29 * This class specifies the format for a given spreadsheet cell. 30 * 31 * @author Mark Murnane 32 * @author Martin Maher (Extended Style Support) 33 */ 34 public class Format implements Cloneable { 35 36 /** Horizontal Alignment Constants. */ 37 final public static int RIGHT_ALIGN = 0x01; 38 final public static int CENTER_ALIGN = 0x02; 39 final public static int LEFT_ALIGN = 0x03; 40 final public static int JUST_ALIGN = 0x04; 41 42 /** Vertical Alignment Constants. */ 43 final public static int TOP_ALIGN = 0x01; 44 final public static int MIDDLE_ALIGN = 0x02; 45 final public static int BOTTOM_ALIGN = 0x03; 46 47 /** Indicates <i>bold</i> text. */ 48 final public static int BOLD = 0x01; 49 /** Indicates <i>italic</i> text. */ 50 final public static int ITALIC = 0x02; 51 /** Indicates <i>underlined</i> text. */ 52 final public static int UNDERLINE = 0x04; 53 /** Indicates <i>strike-through</i> in the text. */ 54 final public static int STRIKETHRU = 0x08; 55 /** Indicates <i>superscripted</i> text. */ 56 final public static int SUPERSCRIPT = 0x10; 57 /** Indicates <i>subscripted</i> text. */ 58 final public static int SUBSCRIPT = 0x20; 59 60 final public static int LEFT_BORDER = 0x40; 61 final public static int RIGHT_BORDER = 0x80; 62 final public static int TOP_BORDER = 0x100; 63 final public static int BOTTOM_BORDER = 0x200; 64 65 final public static int WORD_WRAP = 0x400; 66 67 private int align; 68 private int vertAlign; 69 private String category; 70 private String value; 71 private String formatSpecifier; 72 private int decimalPlaces; 73 74 /** Font name. */ 75 private String fontName; 76 /** Font size in points. */ 77 protected int sizeInPoints; 78 79 private Color foreground, background; 80 81 /** Values of text attributes. */ 82 protected int attributes = 0; 83 /** Bitwise mask of text attributes. */ 84 protected int mask = 0; 85 86 /** 87 * Constructor for creating a new <code>Format</code>. 88 */ Format()89 public Format() { 90 clearFormatting(); 91 } 92 93 /** 94 * Constructor that creates a new <code>Format</code> object 95 * by setting all the format attributes. 96 * 97 */ Format(int attributes, int fontSize, String fontName)98 public Format(int attributes, int fontSize, String fontName) { 99 100 this.attributes = attributes; 101 sizeInPoints = fontSize; 102 this.fontName = fontName; 103 } 104 105 /** 106 * Constructor for creating a new <code>Format</code> object 107 * based on an existing one. 108 * 109 * @param fmt <code>Format</code> to copy. 110 */ Format(Format fmt)111 public Format(Format fmt) { 112 category = fmt.getCategory(); 113 value = fmt.getValue(); 114 formatSpecifier = fmt.getFormatSpecifier(); 115 decimalPlaces = fmt.getDecimalPlaces(); 116 117 attributes = fmt.attributes; 118 mask = fmt.mask; 119 120 fontName = fmt.getFontName(); 121 align = fmt.getAlign(); 122 vertAlign = fmt.getVertAlign(); 123 foreground = fmt.getForeground(); 124 background = fmt.getBackground(); 125 sizeInPoints = fmt.sizeInPoints; 126 } 127 128 129 /** 130 * Reset this <code>Format</code> description. 131 */ clearFormatting()132 public void clearFormatting() { 133 category = ""; 134 value = ""; 135 formatSpecifier = ""; 136 decimalPlaces = 0; 137 attributes = 0; 138 mask = 0; 139 sizeInPoints = 10; 140 align = LEFT_ALIGN; 141 vertAlign = BOTTOM_ALIGN; 142 fontName = ""; 143 foreground = null; 144 background = null; 145 } 146 147 /** 148 * Set one or more text attributes to <i>on</i>. 149 * 150 * @param flags Flag attributes to set <i>on</i>. 151 */ setAttribute(int flags, boolean toggle)152 public void setAttribute(int flags, boolean toggle) { 153 mask |= flags; 154 if(toggle) { 155 attributes |= flags; 156 } else { 157 attributes &= ~flags; 158 } 159 } 160 161 /** 162 * Return true if the <code>attribute</code> is set to <i>on</i> 163 * 164 * @param attribute Attribute to check ({@link #BOLD}, 165 * {@link #ITALIC}, etc.) 166 * 167 * @return true if <code>attribute</code> is set to <i>on</i>, 168 * otherwise false. 169 */ getAttribute(int attribute)170 public boolean getAttribute(int attribute) { 171 if ((mask & attribute) == 0) 172 return false; 173 return (!((attributes & attribute) == 0)); 174 } 175 176 /** 177 * Return true if text <code>attribute</code> is set in this 178 * <code>Style</code>.An attribute that is set may have a 179 * value of <i>on</i> or <i>off</i>. 180 * 181 * @param attribute The attribute to check ({@link #BOLD}, 182 * {@link #ITALIC}, etc.). 183 * 184 * @return true if text <code>attribute</code> is set in this 185 * <code>Style</code>, false otherwise. 186 */ isSet(int attribute)187 public boolean isSet(int attribute) { 188 return (!((mask & attribute) == 0)); 189 } 190 191 192 /** 193 * Set the formatting category of this object, ie number, date, 194 * currency.The <code>OfficeConstants</code> class contains string 195 * constants for the category types. 196 * 197 * @see org.openoffice.xmerge.converter.xml.OfficeConstants 198 * 199 * @param newCategory The name of the category to be set. 200 */ setCategory(String newCategory)201 public void setCategory(String newCategory) { 202 category = newCategory; 203 } 204 205 /** 206 * Return the formatting category of the object. 207 * 208 * @see org.openoffice.xmerge.converter.xml.OfficeConstants 209 * 210 * @return The formatting category of the object. 211 */ getCategory()212 public String getCategory() { 213 return category; 214 } 215 216 /** 217 * In the case of Formula returns the value of the formula. 218 * 219 * @return The value of the formula 220 */ getValue()221 public String getValue() { 222 return value; 223 } 224 225 /** 226 * In the case of formula the contents are set as the formula string and 227 * the value of the formula is a formatting attribute. 228 * 229 * @param newValue the formuala value 230 */ setValue(String newValue)231 public void setValue(String newValue) { 232 value = newValue; 233 } 234 235 236 /** 237 * Set the <code>Format</code> specifier for this category. 238 * 239 * @param formatString The new <code>Format</code> specifier. 240 */ setFormatSpecifier(String formatString)241 public void setFormatSpecifier(String formatString) { 242 formatSpecifier = formatString; 243 } 244 245 246 /** 247 * Get the <code>Format</code> specifier for this category. 248 * 249 * @return <code>Format</code> specifier for this category. 250 */ getFormatSpecifier()251 public String getFormatSpecifier() { 252 return formatSpecifier; 253 } 254 255 256 /** 257 * Set the precision of the number to be displayed. 258 * 259 * @param precision The number of decimal places to display. 260 */ setDecimalPlaces(int precision)261 public void setDecimalPlaces(int precision) { 262 decimalPlaces = precision; 263 } 264 265 266 /** 267 * Get the number of decimal places displayed. 268 * 269 * @return Number of decimal places. 270 */ getDecimalPlaces()271 public int getDecimalPlaces() { 272 return decimalPlaces; 273 } 274 275 276 /** 277 * Set the font used for this cell. 278 * 279 * @param fontName The name of the font. 280 */ setFontName(String fontName)281 public void setFontName(String fontName) { 282 this.fontName = fontName; 283 } 284 285 286 /** 287 * Get the font used for this cell. 288 * 289 * @return The font name. 290 */ getFontName()291 public String getFontName() { 292 return fontName; 293 } 294 295 /** 296 * Set the font used for this cell. 297 * 298 * @param fontSize 299 */ setFontSize(int fontSize)300 public void setFontSize(int fontSize) { 301 sizeInPoints = fontSize; 302 } 303 304 305 /** 306 * Get the font used for this cell. 307 * 308 * @return The font name. 309 */ getFontSize()310 public int getFontSize() { 311 return sizeInPoints; 312 } 313 314 /** 315 * Set the alignment used for this cell. 316 * 317 * @param vertAlign 318 */ setVertAlign(int vertAlign)319 public void setVertAlign(int vertAlign) { 320 this.vertAlign = vertAlign; 321 } 322 323 324 /** 325 * Get the alignment used for this cell. 326 * 327 * @return The font name. 328 */ getVertAlign()329 public int getVertAlign() { 330 return vertAlign; 331 } 332 333 /** 334 * Set the alignment used for this cell. 335 * 336 * @param align 337 */ setAlign(int align)338 public void setAlign(int align) { 339 this.align = align; 340 } 341 342 343 /** 344 * Get the alignment used for this cell. 345 * 346 * @return The font name. 347 */ getAlign()348 public int getAlign() { 349 return align; 350 } 351 /** 352 * Set the Foreground <code>Color</code> for this cell. 353 * 354 * @param c A <code>Color</code> object representing the 355 * foreground color. 356 */ setForeground(Color c)357 public void setForeground(Color c) { 358 if(c!=null) 359 foreground = new Color(c.getRGB()); 360 } 361 362 363 /** 364 * Get the Foreground <code>Color</code> for this cell. 365 * 366 * @return Foreground <code>Color</code> value. 367 */ getForeground()368 public Color getForeground() { 369 return foreground; 370 } 371 372 373 /** 374 * Set the Background <code>Color</code> for this cell 375 * 376 * @param c A <code>Color</code> object representing 377 * the background color. 378 */ setBackground(Color c)379 public void setBackground(Color c) { 380 if(c!=null) 381 background = new Color(c.getRGB()); 382 } 383 384 385 /** 386 * Get the Foreground <code>Color</code> for this cell 387 * 388 * @return Background <code>Color</code> value 389 */ getBackground()390 public Color getBackground() { 391 return background; 392 } 393 394 /** 395 * Get the Foreground <code>Color</code> for this cell 396 * 397 * @return Background <code>Color</code> value 398 */ toString()399 public String toString() { 400 return new String("Value : " + getValue() + " Category : " + getCategory()); 401 } 402 403 /** 404 * Tests if the current <code>Format</code> object has default attribute 405 * values. 406 * 407 * @return true if it contains default value 408 */ isDefault()409 public boolean isDefault() { 410 411 Format rhs = new Format(); 412 413 if (rhs.attributes!= attributes) 414 return false; 415 416 if (foreground!=rhs.foreground) 417 return false; 418 419 if (background!=rhs.background) 420 return false; 421 422 if (rhs.align!= align) 423 return false; 424 425 if (rhs.vertAlign!= vertAlign) 426 return false; 427 428 return true; 429 } 430 431 /** 432 * Return true if <code>style</code> specifies as much or less 433 * than this <code>Style</code>, and nothing it specifies 434 * contradicts this <code>Style</code>. 435 * 436 * @param rhs The <code>Style</code> to check. 437 * 438 * @return true if <code>style</code> is a subset, false 439 * otherwise. 440 */ isSubset(Format rhs)441 public boolean isSubset(Format rhs) { 442 if (rhs.getClass() != this.getClass()) 443 return false; 444 445 if (rhs.attributes!= attributes) 446 return false; 447 448 if (rhs.sizeInPoints != 0) { 449 if (sizeInPoints != rhs.sizeInPoints) 450 return false; 451 } 452 453 if (fontName!=rhs.fontName) 454 return false; 455 456 if (foreground!=rhs.foreground) 457 return false; 458 459 if (background!=rhs.background) 460 return false; 461 462 if (rhs.align!= align) 463 return false; 464 465 if (rhs.vertAlign!= vertAlign) 466 return false; 467 468 return true; 469 } 470 } 471 472