Local variable proposals (Ctrl+SPACE) added
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / HTMLPrinter.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
12
13
14 import java.io.IOException;
15 import java.io.Reader;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.widgets.Display;
20
21 import org.eclipse.ui.IWorkbenchWindow;
22 import org.eclipse.ui.PlatformUI;
23
24
25 /**
26  * Provides a set of convenience methods for creating HTML pages.
27  */
28 public class HTMLPrinter {
29                         
30         private HTMLPrinter() {
31         }
32         
33         private static String replace(String text, char c, String s) {
34                                 
35                 int previous= 0;
36                 int current= text.indexOf(c, previous);
37                 
38                 if (current == -1)
39                         return text;
40                 
41                 StringBuffer buffer= new StringBuffer();        
42                 while (current > -1) {
43                         buffer.append(text.substring(previous, current));
44                         buffer.append(s);
45                         previous= current + 1;
46                         current= text.indexOf(c, previous);
47                 }
48                 buffer.append(text.substring(previous));
49                 
50                 return buffer.toString();
51         }
52         
53         public static String convertToHTMLContent(String content) {
54                 content= replace(content, '<', "&lt;"); //$NON-NLS-1$
55                 return replace(content, '>', "&gt;"); //$NON-NLS-1$
56         }
57         
58         public static String read(Reader rd) {
59                 
60                 StringBuffer buffer= new StringBuffer();
61                 char[] readBuffer= new char[2048];
62                 
63                 try {
64                         int n= rd.read(readBuffer);
65                         while (n > 0) {
66                                 buffer.append(readBuffer, 0, n);
67                                 n= rd.read(readBuffer);
68                         }
69                         return buffer.toString();
70                 } catch (IOException x) {
71                 }
72                 
73                 return null;
74         }
75
76         public static void insertPageProlog(StringBuffer buffer, int position, RGB bgRGB) {
77                 if (bgRGB == null)
78                         insertPageProlog(buffer, position);
79                 else {
80                         StringBuffer pageProlog= new StringBuffer(60);
81                         pageProlog.append("<html><body text=\"#000000\" bgcolor=\""); //$NON-NLS-1$
82                         appendColor(pageProlog, bgRGB);
83                         pageProlog.append("\"><font size=-1>"); //$NON-NLS-1$
84                         buffer.insert(position,  pageProlog.toString());
85                 }
86         }
87         
88         private static void appendColor(StringBuffer buffer, RGB rgb) {
89                 buffer.append('#');
90                 buffer.append(Integer.toHexString(rgb.red));
91                 buffer.append(Integer.toHexString(rgb.green));
92                 buffer.append(Integer.toHexString(rgb.blue));
93         }
94
95         public static void insertPageProlog(StringBuffer buffer, int position) {
96                 RGB bgColor= null;
97                 IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
98                 if (window != null) {
99                         Display display= window.getShell().getDisplay();
100                         if (display != null && !display.isDisposed())
101                                 bgColor= display.getSystemColor(SWT.COLOR_INFO_BACKGROUND).getRGB();
102                 }
103                 if (bgColor == null)
104                         bgColor= new RGB(255,255, 225); // RGB value of info bg color on WindowsXP
105                         
106                 insertPageProlog(buffer, position, bgColor); //$NON-NLS-1$
107         }
108         
109         public static void addPageProlog(StringBuffer buffer) {
110                 insertPageProlog(buffer, buffer.length());
111         }
112         
113         public static void addPageEpilog(StringBuffer buffer) {
114                 buffer.append("</font></body></html>"); //$NON-NLS-1$
115         }
116         
117         public static void startBulletList(StringBuffer buffer) {
118                 buffer.append("<ul>"); //$NON-NLS-1$
119         }
120         
121         public static void endBulletList(StringBuffer buffer) {
122                 buffer.append("</ul>"); //$NON-NLS-1$
123         }
124         
125         public static void addBullet(StringBuffer buffer, String bullet) {
126                 if (bullet != null) {
127                         buffer.append("<li>"); //$NON-NLS-1$
128                         buffer.append(bullet);
129                         buffer.append("</li>"); //$NON-NLS-1$
130                 }
131         }
132         
133         public static void addSmallHeader(StringBuffer buffer, String header) {
134                 if (header != null) {
135                         buffer.append("<h5>"); //$NON-NLS-1$
136                         buffer.append(header);
137                         buffer.append("</h5>"); //$NON-NLS-1$
138                 }
139         }
140         
141         public static void addParagraph(StringBuffer buffer, String paragraph) {
142                 if (paragraph != null) {
143                         buffer.append("<p>"); //$NON-NLS-1$
144                         buffer.append(paragraph);
145                 }
146         }
147         
148         public static void addParagraph(StringBuffer buffer, Reader paragraphReader) {
149                 if (paragraphReader != null)
150                         addParagraph(buffer, read(paragraphReader));
151         }
152 }