KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > util > HTMLPrinter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
19  * Utility class for html-related functions.
20  *
21  * @since 3.2
22  */

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 JavaDoc() {
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 JavaDoc buffer, String JavaDoc face, int height, String JavaDoc bullet) {
45         if (bullet != null) {
46             buffer.append("<li>"); //$NON-NLS-1$
47
buffer.append("<span style= \"font-size:"); //$NON-NLS-1$
48
buffer.append(height);
49             buffer.append(".0pt;font-family:"); //$NON-NLS-1$
50
buffer.append(face);
51             buffer.append("\">"); //$NON-NLS-1$
52
buffer.append(bullet);
53             buffer.append("</span>"); //$NON-NLS-1$
54
buffer.append("</li>"); //$NON-NLS-1$
55
}
56     }
57
58     public static void addPageEpilog(StringBuffer JavaDoc buffer) {
59         buffer.append("</font></body></html>"); //$NON-NLS-1$
60
}
61
62     public static void addParagraph(StringBuffer JavaDoc buffer, String JavaDoc face, int height, String JavaDoc paragraph) {
63         if (paragraph != null) {
64             buffer.append("<p>"); //$NON-NLS-1$
65
buffer.append("<span style= \"font-size:"); //$NON-NLS-1$
66
buffer.append(height);
67             buffer.append(".0pt;font-family:"); //$NON-NLS-1$
68
buffer.append(face);
69             buffer.append("\">"); //$NON-NLS-1$
70
buffer.append(paragraph);
71             buffer.append("</span>"); //$NON-NLS-1$
72
}
73     }
74
75     public static void addSmallHeader(StringBuffer JavaDoc buffer, String JavaDoc face, int height, String JavaDoc header) {
76         if (header != null) {
77             buffer.append("<h5>"); //$NON-NLS-1$
78
buffer.append("<span style= \"font-size:"); //$NON-NLS-1$
79
buffer.append(height);
80             buffer.append(".0pt;font-family:"); //$NON-NLS-1$
81
buffer.append(face);
82             buffer.append("\">"); //$NON-NLS-1$
83
buffer.append(header);
84             buffer.append("</span>"); //$NON-NLS-1$
85
buffer.append("</h5>"); //$NON-NLS-1$
86
}
87     }
88
89     private static void appendColor(StringBuffer JavaDoc 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 JavaDoc convertToHTMLContent(String JavaDoc content) {
97         content= replace(content, '&', "&amp;"); //$NON-NLS-1$
98
content= replace(content, '"', "&quot;"); //$NON-NLS-1$
99
content= replace(content, '<', "&lt;"); //$NON-NLS-1$
100
return replace(content, '>', "&gt;"); //$NON-NLS-1$
101
}
102
103     public static void endBulletList(StringBuffer JavaDoc buffer) {
104         buffer.append("</ul>"); //$NON-NLS-1$
105
}
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 JavaDoc buffer, String JavaDoc font, int height, int position, RGB bgRGB) {
116         if (bgRGB == null)
117             insertPageProlog(buffer, font, height, position, getBgColor());
118         else {
119             StringBuffer JavaDoc pageProlog= new StringBuffer JavaDoc(60);
120             pageProlog.append("<html><body text=\"#000000\" bgcolor=\""); //$NON-NLS-1$
121
appendColor(pageProlog, bgRGB);
122             pageProlog.append("\"><font face=\"" + font + "\" size=\"" + height + ".0pt\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
123
buffer.insert(position, pageProlog.toString());
124         }
125     }
126
127     public static void insertStyles(StringBuffer JavaDoc buffer, String JavaDoc[] styles) {
128         if (styles == null || styles.length == 0)
129             return;
130         StringBuffer JavaDoc styleBuf= new StringBuffer JavaDoc(10 * styles.length);
131         for (int i= 0; i < styles.length; i++) {
132             styleBuf.append(" style=\""); //$NON-NLS-1$
133
styleBuf.append(styles[i]);
134             styleBuf.append('"');
135         }
136         int index= buffer.indexOf("<body "); //$NON-NLS-1$
137
if (index == -1)
138             return;
139         buffer.insert(index + 5, styleBuf);
140     }
141
142     private static String JavaDoc replace(String JavaDoc text, char c, String JavaDoc s) {
143         int previous= 0;
144         int current= text.indexOf(c, previous);
145         if (current == -1)
146             return text;
147         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
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 JavaDoc buffer) {
159         buffer.append("<ul>"); //$NON-NLS-1$
160
}
161
162     private HTMLPrinter() {
163         // Not for instantiation
164
}
165 }
166
Free Books   Free Magazines  
Popular Tags