1 11 package org.eclipse.ltk.internal.ui.refactoring.util; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.SWTError; 15 import org.eclipse.swt.graphics.RGB; 16 import org.eclipse.swt.widgets.Display; 17 18 23 public final class HTMLPrinter { 24 25 private static RGB BG_COLOR_RGB= null; 26 27 static { 28 final Display display= Display.getDefault(); 29 if (display != null && !display.isDisposed()) { 30 try { 31 display.asyncExec(new Runnable () { 32 33 public void run() { 34 BG_COLOR_RGB= display.getSystemColor(SWT.COLOR_INFO_BACKGROUND).getRGB(); 35 } 36 }); 37 } catch (SWTError error) { 38 if (error.code != SWT.ERROR_DEVICE_DISPOSED) 39 throw error; 40 } 41 } 42 } 43 44 public static void addBullet(StringBuffer buffer, String face, int height, String bullet) { 45 if (bullet != null) { 46 buffer.append("<li>"); buffer.append("<span style= \"font-size:"); buffer.append(height); 49 buffer.append(".0pt;font-family:"); buffer.append(face); 51 buffer.append("\">"); buffer.append(bullet); 53 buffer.append("</span>"); buffer.append("</li>"); } 56 } 57 58 public static void addPageEpilog(StringBuffer buffer) { 59 buffer.append("</font></body></html>"); } 61 62 public static void addParagraph(StringBuffer buffer, String face, int height, String paragraph) { 63 if (paragraph != null) { 64 buffer.append("<p>"); buffer.append("<span style= \"font-size:"); buffer.append(height); 67 buffer.append(".0pt;font-family:"); buffer.append(face); 69 buffer.append("\">"); buffer.append(paragraph); 71 buffer.append("</span>"); } 73 } 74 75 public static void addSmallHeader(StringBuffer buffer, String face, int height, String header) { 76 if (header != null) { 77 buffer.append("<h5>"); buffer.append("<span style= \"font-size:"); buffer.append(height); 80 buffer.append(".0pt;font-family:"); buffer.append(face); 82 buffer.append("\">"); buffer.append(header); 84 buffer.append("</span>"); buffer.append("</h5>"); } 87 } 88 89 private static void appendColor(StringBuffer buffer, RGB rgb) { 90 buffer.append('#'); 91 buffer.append(Integer.toHexString(rgb.red)); 92 buffer.append(Integer.toHexString(rgb.green)); 93 buffer.append(Integer.toHexString(rgb.blue)); 94 } 95 96 public static String convertToHTMLContent(String content) { 97 content= replace(content, '&', "&"); content= replace(content, '"', """); content= replace(content, '<', "<"); return replace(content, '>', ">"); } 102 103 public static void endBulletList(StringBuffer buffer) { 104 buffer.append("</ul>"); } 106 107 private static RGB getBgColor() { 108 if (BG_COLOR_RGB != null) 109 return BG_COLOR_RGB; 110 else 111 return new RGB(255, 255, 225); 112 113 } 114 115 public static void insertPageProlog(StringBuffer buffer, String font, int height, int position, RGB bgRGB) { 116 if (bgRGB == null) 117 insertPageProlog(buffer, font, height, position, getBgColor()); 118 else { 119 StringBuffer pageProlog= new StringBuffer (60); 120 pageProlog.append("<html><body text=\"#000000\" bgcolor=\""); appendColor(pageProlog, bgRGB); 122 pageProlog.append("\"><font face=\"" + font + "\" size=\"" + height + ".0pt\">"); buffer.insert(position, pageProlog.toString()); 124 } 125 } 126 127 public static void insertStyles(StringBuffer buffer, String [] styles) { 128 if (styles == null || styles.length == 0) 129 return; 130 StringBuffer styleBuf= new StringBuffer (10 * styles.length); 131 for (int i= 0; i < styles.length; i++) { 132 styleBuf.append(" style=\""); styleBuf.append(styles[i]); 134 styleBuf.append('"'); 135 } 136 int index= buffer.indexOf("<body "); if (index == -1) 138 return; 139 buffer.insert(index + 5, styleBuf); 140 } 141 142 private static String replace(String text, char c, String s) { 143 int previous= 0; 144 int current= text.indexOf(c, previous); 145 if (current == -1) 146 return text; 147 StringBuffer buffer= new StringBuffer (); 148 while (current > -1) { 149 buffer.append(text.substring(previous, current)); 150 buffer.append(s); 151 previous= current + 1; 152 current= text.indexOf(c, previous); 153 } 154 buffer.append(text.substring(previous)); 155 return buffer.toString(); 156 } 157 158 public static void startBulletList(StringBuffer buffer) { 159 buffer.append("<ul>"); } 161 162 private HTMLPrinter() { 163 } 165 } 166
| Popular Tags
|