KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > RevisionHoverInformationControlCreator


1 /*******************************************************************************
2  * Copyright (c) 2007 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.ui.texteditor;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.widgets.Shell;
15
16 import org.eclipse.jface.internal.text.html.BrowserInformationControl;
17 import org.eclipse.jface.internal.text.html.HTMLPrinter;
18 import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
19
20 import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
21 import org.eclipse.jface.text.DefaultInformationControl;
22 import org.eclipse.jface.text.IInformationControl;
23 import org.eclipse.jface.text.IInformationControlExtension4;
24
25 import org.eclipse.ui.editors.text.EditorsUI;
26
27
28 /**
29  * The revision information control creator
30  * that can show the tool tip affordance.
31  *
32  * @since 3.3
33  */

34 class RevisionHoverInformationControlCreator extends AbstractReusableInformationControlCreator {
35
36     private static final String JavaDoc fgStyleSheet= "/* Font definitions */\n" + //$NON-NLS-1$
37
"body, h1, h2, h3, h4, h5, h6, p, table, td, caption, th, ul, ol, dl, li, dd, dt {font-family: sans-serif; font-size: 9pt }\n" + //$NON-NLS-1$
38
"pre { font-family: monospace; font-size: 9pt }\n" + //$NON-NLS-1$
39
"\n" + //$NON-NLS-1$
40
"/* Margins */\n" + //$NON-NLS-1$
41
"body { overflow: auto; margin-top: 0; margin-bottom: 4; margin-left: 3; margin-right: 0 }\n" + //$NON-NLS-1$
42
"h1 { margin-top: 5; margin-bottom: 1 } \n" + //$NON-NLS-1$
43
"h2 { margin-top: 25; margin-bottom: 3 }\n" + //$NON-NLS-1$
44
"h3 { margin-top: 20; margin-bottom: 3 }\n" + //$NON-NLS-1$
45
"h4 { margin-top: 20; margin-bottom: 3 }\n" + //$NON-NLS-1$
46
"h5 { margin-top: 0; margin-bottom: 0 }\n" + //$NON-NLS-1$
47
"p { margin-top: 10px; margin-bottom: 10px }\n" + //$NON-NLS-1$
48
"pre { margin-left: 6 }\n" + //$NON-NLS-1$
49
"ul { margin-top: 0; margin-bottom: 10 }\n" + //$NON-NLS-1$
50
"li { margin-top: 0; margin-bottom: 0 } \n" + //$NON-NLS-1$
51
"li p { margin-top: 0; margin-bottom: 0 } \n" + //$NON-NLS-1$
52
"ol { margin-top: 0; margin-bottom: 10 }\n" + //$NON-NLS-1$
53
"dl { margin-top: 0; margin-bottom: 10 }\n" + //$NON-NLS-1$
54
"dt { margin-top: 0; margin-bottom: 0; font-weight: bold }\n" + //$NON-NLS-1$
55
"dd { margin-top: 0; margin-bottom: 0 }\n" + //$NON-NLS-1$
56
"\n" + //$NON-NLS-1$
57
"/* Styles and colors */\n" + //$NON-NLS-1$
58
"a:link { color: #0000FF }\n" + //$NON-NLS-1$
59
"a:hover { color: #000080 }\n" + //$NON-NLS-1$
60
"a:visited { text-decoration: underline }\n" + //$NON-NLS-1$
61
"h4 { font-style: italic }\n" + //$NON-NLS-1$
62
"strong { font-weight: bold }\n" + //$NON-NLS-1$
63
"em { font-style: italic }\n" + //$NON-NLS-1$
64
"var { font-style: italic }\n" + //$NON-NLS-1$
65
"th { font-weight: bold }\n" + //$NON-NLS-1$
66
""; //$NON-NLS-1$
67

68     private boolean fIsFocusable;
69     
70     
71     public RevisionHoverInformationControlCreator(boolean isFocusable) {
72         fIsFocusable= isFocusable;
73     }
74     
75     /*
76      * @see org.eclipse.ui.texteditor.AbstractReusableInformationControlCreator#canReuse(org.eclipse.jface.text.IInformationControl)
77      */

78     public boolean canReuse(IInformationControl control) {
79         if (!super.canReuse(control))
80             return false;
81         
82         if (control instanceof IInformationControlExtension4)
83             ((IInformationControlExtension4)control).setStatusText(EditorsUI.getTooltipAffordanceString());
84         
85         return true;
86     }
87
88     /*
89      * @see org.eclipse.jface.internal.text.revisions.AbstractReusableInformationControlCreator#doCreateInformationControl(org.eclipse.swt.widgets.Shell)
90      */

91     protected IInformationControl doCreateInformationControl(Shell parent) {
92         int style= fIsFocusable ? SWT.V_SCROLL | SWT.H_SCROLL : SWT.NONE;
93         
94         if (BrowserInformationControl.isAvailable(parent)) {
95             final int shellStyle= SWT.TOOL | (fIsFocusable ? SWT.RESIZE : SWT.NO_TRIM);
96             return new BrowserInformationControl(parent, shellStyle, style, EditorsUI.getTooltipAffordanceString()) {
97
98                 /*
99                  * @see org.eclipse.jface.internal.text.html.BrowserInformationControl#setInformation(java.lang.String)
100                  */

101                 public void setInformation(String JavaDoc content) {
102                     content= addCSSToHTMLFragment(content);
103                     super.setInformation(content);
104                 }
105                 
106                 /**
107                  * Adds a HTML header and CSS info if <code>html</code> is only an HTML fragment (has no
108                  * &lt;html&gt; section).
109                  *
110                  * @param html the html / text produced by a revision
111                  * @return modified html
112                  */

113                 private String JavaDoc addCSSToHTMLFragment(String JavaDoc html) {
114                     int max= Math.min(100, html.length());
115                     if (html.substring(0, max).indexOf("<html>") != -1) //$NON-NLS-1$
116
// there is already a header
117
return html;
118                     
119                     StringBuffer JavaDoc info= new StringBuffer JavaDoc(512 + html.length());
120                     HTMLPrinter.insertPageProlog(info, 0, fgStyleSheet);
121                     info.append(html);
122                     HTMLPrinter.addPageEpilog(info);
123                     return info.toString();
124                 }
125             };
126         }
127         return new DefaultInformationControl(parent, style, new HTMLTextPresenter(), EditorsUI.getTooltipAffordanceString());
128     }
129 }
Popular Tags