1 14 package org.wings.plaf.css; 15 16 import org.wings.*; 17 import org.wings.io.Device; 18 import org.wings.io.NullDevice; 19 import org.wings.plaf.CGManager; 20 import org.wings.script.ScriptListener; 21 22 import java.awt.*; 23 import java.io.BufferedReader ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 37 public final class Utils { 38 41 public static boolean PRINT_DEBUG = true; 42 43 protected final static char[] hexDigits = { 44 '0', '1', '2', '3', '4', '5', 45 '6', '7', '8', '9', 'a', 'b', 46 'c', 'd', 'e', 'f'}; 47 48 protected Utils() { 49 } 50 51 54 public static void renderContainer(Device d, SContainer c) 55 throws IOException { 56 SLayoutManager layout = c.getLayout(); 57 58 if (layout == null) { 59 final CGManager manager = c.getSession().getCGManager(); 61 Object value; 62 value = manager.getObject("SContainer.defaultLayoutBehaviour", String .class); 63 if (value != null && value instanceof String ) { 64 if ("classic".equals(((String ) value).toLowerCase())) { 65 layout = new SBoxLayout(SBoxLayout.VERTICAL); 66 } else if ("standard".equals(((String ) value).toLowerCase())) { 67 layout = new SFlowLayout(SFlowLayout.LEFT); 68 } else if ("none".equals(((String ) value).toLowerCase())) { 69 for (int i = 0; i < c.getComponentCount(); i++) { 71 c.getComponent(i).write(d); 72 } 73 return; 74 } else { layout = new SFlowLayout(SFlowLayout.LEFT); 76 } 77 } else { 78 layout = new SFlowLayout(SFlowLayout.LEFT); 80 } 81 c.setLayout(layout); 82 } 83 layout.write(d); 84 } 85 86 public static void writeEvents(Device d, SComponent c) 87 throws IOException { 88 ScriptListener[] listeners = c.getScriptListeners(); 89 if (listeners.length > 0) { 90 Map eventScripts = new HashMap (); 91 for (int i = 0; i < listeners.length; i++) { 92 final ScriptListener script = listeners[i]; 93 final String event = script.getEvent(); 94 String eventScriptCode = script.getCode(); 95 96 if (event == null 97 || event.length() == 0 98 || eventScriptCode == null 99 || eventScriptCode.length() == 0) { 100 continue; 101 } 102 103 if (eventScripts.containsKey(event)) { 104 String savedEventScriptCode = (String ) eventScripts.get(event); 105 eventScriptCode = savedEventScriptCode 106 + (savedEventScriptCode.trim().endsWith(";") ? "" : ";") 107 + eventScriptCode; 108 } 109 eventScripts.put(event, eventScriptCode); 110 } 111 112 Iterator it = eventScripts.keySet().iterator(); 113 while (it.hasNext()) { 114 String event = (String ) it.next(); 115 String code = (String ) eventScripts.get(event); 116 d.print(" "); 117 d.print(event); 118 d.print("=\""); 119 d.print(code); 120 d.print("\""); 121 } 122 } 123 } 124 125 136 137 143 public static String event(SComponent component) { 144 return component.getEncodedLowLevelEventId(); 145 } 147 148 155 public static void printDivHorizontalAlignment(Device d, int align) throws IOException { 156 printTableHorizontalAlignment(d, align); 157 } 158 159 162 private static void printTableHorizontalAlignment(Device d, int align) 163 throws IOException { 164 if (align == SConstants.NO_ALIGN || align == SConstants.LEFT) { 165 d.print(" align=\"left\""); 166 } else if (align == SConstants.CENTER) { 167 d.print(" align=\"center\""); 168 } else if (align == SConstants.RIGHT) { 169 d.print(" align=\"right\""); 170 } else if (align == SConstants.JUSTIFY) { 171 d.print(" align=\"justify\""); 172 } 173 } 174 175 178 private static void printTableVerticalAlignment(Device d, int align) 179 throws IOException { 180 if (align == SConstants.NO_ALIGN || align == SConstants.CENTER) { 181 } else if (align == SConstants.TOP) { 182 d.print(" valign=\"top\""); 183 } else if (align == SConstants.BOTTOM) { 184 d.print(" valign=\"bottom\""); 185 } else if (align == SConstants.BASELINE) { 186 d.print(" valign=\"baseline\""); 187 } 188 } 189 190 public static void printTableCellAlignment(Device d, SComponent c) 191 throws IOException { 192 if (c != null) { 193 printTableHorizontalAlignment(d, c.getHorizontalAlignment()); 194 printTableVerticalAlignment(d, c.getVerticalAlignment()); 195 } 196 } 197 198 public static String toColorString(int rgb) { 199 char[] buf = new char[6]; 200 int digits = 6; 201 do { 202 buf[--digits] = hexDigits[rgb & 15]; 203 rgb >>>= 4; 204 } while (digits != 0); 205 206 return new String (buf); 207 } 208 209 public static String toColorString(java.awt.Color c) { 210 return toColorString(c.getRGB()); 211 } 212 213 226 public static void printCSSInlineStyleAttributes(Device d, SComponent component) throws IOException { 227 if (component == null) 228 return; 229 StringBuffer styleString = generateCSSComponentInlineStyle(component); 230 231 Utils.optAttribute(d, "style", styleString.toString()); 232 } 233 234 public static StringBuffer generateCSSComponentInlineStyle(SComponent component) { 235 StringBuffer styleString = new StringBuffer (); 236 if (component != null) { 237 final Color fgColor = component.getForeground(); 238 final Color bgcolor = component.getBackground(); 239 final SFont font = component.getFont(); 240 final SDimension dim = component.getPreferredSize(); 241 242 if (bgcolor != null) 243 styleString.append("background-color:#").append(toColorString(bgcolor)).append(";"); 244 245 if (fgColor != null) { 246 styleString.append("color:#").append(toColorString(fgColor)).append(";"); 247 } 248 249 if (font != null) { 250 int style = font.getStyle(); 251 styleString.append("font-size:").append(font.getSize()).append("pt;"); 252 styleString.append("font-style:").append((style & Font.ITALIC) > 0 ? "italic;" : "normal;"); 253 styleString.append("font-weight:").append((style & Font.BOLD) > 0 ? "bold;" : "normal;"); 254 styleString.append("font-family:").append(font.getFace()).append(";"); 255 } 256 257 if (dim != null) { 258 if (dim.isWidthDefined()) styleString.append("width:").append(dim.getWidth()).append(";"); 259 if (dim.isHeightDefined()) styleString.append("height:").append(dim.getHeight()).append(";"); 260 } 261 } 262 return styleString; 263 } 264 265 273 public static void printCSSInlinePreferredSize(Device device, SDimension preferredSize) throws IOException { 274 if (preferredSize != null && (preferredSize.isWidthDefined() || preferredSize.isHeightDefined())) { 275 device.print(" style=\""); 276 device.print(generateCSSInlinePreferredSize(preferredSize)); 277 device.print("\""); 278 } 279 } 280 281 288 public static StringBuffer generateCSSInlinePreferredSize(SDimension preferredSize) { 289 StringBuffer styleString = new StringBuffer (); 290 if (preferredSize != null && (preferredSize.isWidthDefined() || preferredSize.isHeightDefined())) { 291 if (preferredSize.isWidthDefined()) 292 styleString.append("width:").append(preferredSize.getWidth()).append(";"); 293 if (preferredSize.isHeightDefined()) 294 styleString.append("height:").append(preferredSize.getHeight()).append(";"); 295 } 296 return styleString; 297 } 298 299 public static StringBuffer generateCSSInlineBorder(int borderSize) { 300 StringBuffer styleString = new StringBuffer (); 301 if (borderSize > 0) 302 styleString.append("border:").append(borderSize).append("px solid black;"); 303 else 304 ; return styleString; 306 } 307 308 314 public static void printCSSInlineFullSize(Device device, SDimension preferredSize) throws IOException { 315 if (preferredSize != null && (preferredSize.isWidthDefined() || preferredSize.isHeightDefined())) { 316 device.print(" style=\"width:100%;height:100%\""); 317 } 318 } 319 320 324 private static void quote(Device d, String s, boolean quoteNewline) throws IOException { 326 if (s == null) return; 327 char[] chars = s.toCharArray(); 328 char c; 329 int last = 0; 330 for (int pos = 0; pos < chars.length; ++pos) { 331 c = chars[pos]; 332 if (c < 32 || c > 127) { 334 d.print(chars, last, (pos - last)); 335 if (c == '\n' && quoteNewline) { 336 d.print("<br>"); 337 } else { 338 d.print("&#"); 339 d.print((int) c); 340 d.print(";"); 341 } last = pos + 1; 343 } else 344 switch (c) { 345 case '&': 346 d.print(chars, last, (pos - last)); 347 d.print("&"); 348 last = pos + 1; 349 break; 350 case '"': 351 d.print(chars, last, (pos - last)); 352 d.print("""); 353 last = pos + 1; 354 break; 355 case '<': 356 d.print(chars, last, (pos - last)); 357 d.print("<"); 358 last = pos + 1; 359 break; 360 case '>': 361 d.print(chars, last, (pos - last)); 362 d.print(">"); 363 last = pos + 1; 364 break; 365 378 } 379 } 380 d.print(chars, last, chars.length - last); 381 } 382 383 public static void writeRaw(Device d, String s) throws IOException { 384 if (s == null) return; 385 d.print(s); 386 } 387 388 395 public static void write(Device d, String s) throws IOException { 396 if (s == null) return; 397 if ((s.length() > 5) && (s.startsWith("<html>"))) { 398 writeRaw(d, s.substring(6)); 399 } else { 400 quote(d, s, false); 401 } 402 } 403 404 409 public static void optAttribute(Device d, String attr, String value) 410 throws IOException { 411 if (value != null && value.trim().length() > 0) { 412 d.print(" ").print(attr).print("=\""); 413 quote(d, value, true); 414 d.print("\""); 415 } 416 } 417 418 423 public static void optAttribute(Device d, String attr, Color value) 424 throws IOException { 425 if (value != null) { 426 d.print(" "); 427 d.print(attr); 428 d.print("=\""); 429 write(d, value); 430 d.print("\""); 431 } 432 } 433 434 437 public static void optAttribute(Device d, String attr, Renderable r) 438 throws IOException { 439 if (r != null) { 440 d.print(" "); 441 d.print(attr); 442 d.print("=\""); 443 r.write(d); 444 d.print("\""); 445 } 446 } 447 448 452 public static void optAttribute(Device d, String attr, int value) 453 throws IOException { 454 if (value > 0) { 455 d.print(" "); 456 d.print(attr); 457 d.print("=\""); 458 d.print(String.valueOf(value)); 459 d.print("\""); 460 } 461 } 462 463 467 public static void optAttribute(Device d, String attr, SDimension value) 468 throws IOException { 469 if (value != null) { 470 d.print(" "); 471 d.print(attr); 472 d.print("=\""); 473 write(d, value.toString()); 474 d.print("\""); 475 } 476 } 477 478 482 public static void write(Device d, Color c) throws IOException { 483 d.print("#"); 484 int rgb = (c == null) ? 0 : c.getRGB(); 485 int mask = 0xf00000; 486 for (int bitPos = 20; bitPos >= 0; bitPos -= 4) { 487 d.print(hexDigits[(rgb & mask) >>> bitPos]); 488 mask >>>= 4; 489 } 490 } 491 492 495 public static void write(Device d, Renderable r) throws IOException { 496 if (r == null) return; 497 r.write(d); 498 } 499 500 503 public static void main(String argv[]) throws Exception { 504 Color c = new Color(255, 254, 7); 505 Device d = new org.wings.io.StringBufferDevice(); 506 write(d, c); 507 quote(d, "\nThis is a <abc> string \"; foo & sons\nmoin", true); 508 d.print(String.valueOf(-42)); 509 d.print(String.valueOf(Integer.MIN_VALUE)); 510 511 write(d, "hello test \n"); 512 write(d, "<html>hallo test \n"); 513 514 d = new org.wings.io.NullDevice(); 515 long start = System.currentTimeMillis(); 516 for (int i = 0; i < 1000000; ++i) { 517 quote(d, "this is a little & foo", true); 518 } 519 System.err.println("took: " + (System.currentTimeMillis() - start) 520 + "ms"); 521 } 522 523 531 public static Device printDebug(Device d, String s) throws IOException { 532 if (PRINT_DEBUG) 533 return d.print(s); 534 else 535 return NullDevice.DEFAULT; 536 } 537 538 542 public static Device printDebugNewline(Device d, SComponent currentComponent) throws IOException { 543 if (PRINT_DEBUG) 544 return printNewline(d, currentComponent); 545 else 546 return d; 547 } 548 549 552 public static Device printNewline(Device d, SComponent currentComponent) throws IOException { 553 if (currentComponent == null || PRINT_DEBUG == false) return d; 555 d.print("\n"); 556 while (currentComponent.getParent() != null && currentComponent.getParent().getParent() != null) { 557 d.print("\t"); 558 currentComponent = currentComponent.getParent(); 559 } 560 return d; 561 } 562 563 569 public static String loadScript(String path) { 570 InputStream in = null; 571 BufferedReader reader = null; 572 573 try { 574 in = Utils.class.getClassLoader().getResourceAsStream(path); 575 reader = new BufferedReader (new InputStreamReader (in)); 576 StringBuffer buffer = new StringBuffer (); 577 String line; 578 while ((line = reader.readLine()) != null) { 579 buffer.append(line).append("\n"); 580 } 581 buffer.append("\n"); 582 583 return buffer.toString(); 584 } catch (Exception e) { 585 e.printStackTrace(); 586 return ""; 587 } finally { 588 try { 589 in.close(); 590 } catch (Exception ign) { 591 } 592 try { 593 reader.close(); 594 } catch (Exception ign1) { 595 } 596 } 597 } 598 599 604 public static String nonBreakingSpaces(String text) { 605 return text.replace(' ', '\u00A0'); 606 } 607 608 } 609 | Popular Tags |