Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 56e94401e4298e1378bed17089882657084ff2e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.texteditor;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.jface.internal.text.html.BrowserInformationControl;
import org.eclipse.jface.internal.text.html.HTMLPrinter;
import org.eclipse.jface.resource.JFaceResources;

import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlExtension4;

import org.eclipse.ui.editors.text.EditorsUI;


/**
 * The revision information control creator that can show the tool tip affordance.
 *
 * @since 3.3
 */
class RevisionHoverInformationControlCreator extends AbstractReusableInformationControlCreator {

	private static final String fgStyleSheet= "/* Font definitions */\n" + //$NON-NLS-1$
	"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$
	"pre				{ font-family: monospace; font-size: 9pt }\n" + //$NON-NLS-1$
	"\n" + //$NON-NLS-1$
	"/* Margins */\n" + //$NON-NLS-1$
	"body	     { overflow: auto; margin-top: 0; margin-bottom: 4; margin-left: 3; margin-right: 0 }\n" + //$NON-NLS-1$
	"h1           { margin-top: 5; margin-bottom: 1 }	\n" + //$NON-NLS-1$
	"h2           { margin-top: 25; margin-bottom: 3 }\n" + //$NON-NLS-1$
	"h3           { margin-top: 20; margin-bottom: 3 }\n" + //$NON-NLS-1$
	"h4           { margin-top: 20; margin-bottom: 3 }\n" + //$NON-NLS-1$
	"h5           { margin-top: 0; margin-bottom: 0 }\n" + //$NON-NLS-1$
	"p            { margin-top: 10px; margin-bottom: 10px }\n" + //$NON-NLS-1$
	"pre	         { margin-left: 6 }\n" + //$NON-NLS-1$
	"ul	         { margin-top: 0; margin-bottom: 10 }\n" + //$NON-NLS-1$
	"li	         { margin-top: 0; margin-bottom: 0 } \n" + //$NON-NLS-1$
	"li p	     { margin-top: 0; margin-bottom: 0 } \n" + //$NON-NLS-1$
	"ol	         { margin-top: 0; margin-bottom: 10 }\n" + //$NON-NLS-1$
	"dl	         { margin-top: 0; margin-bottom: 10 }\n" + //$NON-NLS-1$
	"dt	         { margin-top: 0; margin-bottom: 0; font-weight: bold }\n" + //$NON-NLS-1$
	"dd	         { margin-top: 0; margin-bottom: 0 }\n" + //$NON-NLS-1$
	"\n" + //$NON-NLS-1$
	"/* Styles and colors */\n" + //$NON-NLS-1$
	"a:link	     { color: hyperlinkColor }\n" + //$NON-NLS-1$
	"a:hover	     { color: activeHyperlinkColor }\n" + //$NON-NLS-1$
	"a:visited    { text-decoration: underline }\n" + //$NON-NLS-1$
	"h4           { font-style: italic }\n" + //$NON-NLS-1$
	"strong	     { font-weight: bold }\n" + //$NON-NLS-1$
	"em	         { font-style: italic }\n" + //$NON-NLS-1$
	"var	         { font-style: italic }\n" + //$NON-NLS-1$
	"th	         { font-weight: bold }\n" + //$NON-NLS-1$
	""; //$NON-NLS-1$


	private static final class RevisionInformationControl extends BrowserInformationControl {

		public RevisionInformationControl(Shell parent, String statusFieldText) {
			super(parent, JFaceResources.DIALOG_FONT, statusFieldText);
		}

		public RevisionInformationControl(Shell parent) {
			super(parent, JFaceResources.DIALOG_FONT, true);
		}

		/**
		 * {@inheritDoc}
		 *
		 * @deprecated use {@link #setInput(Object)}
		 */
		@Deprecated
		@Override
		public void setInformation(String content) {
			content= addCSSToHTMLFragment(content);
			super.setInformation(content);
		}

		/**
		 * Adds a HTML header and CSS info if <code>html</code> is only an HTML fragment (has no
		 * &lt;html&gt; section).
		 *
		 * @param html the html / text produced by a revision
		 * @return modified html
		 */
		private String addCSSToHTMLFragment(String html) {
			int max= Math.min(100, html.length());
			if (html.substring(0, max).indexOf("<html>") != -1) //$NON-NLS-1$
				// there is already a header
				return html;

			StringBuilder info= new StringBuilder(512 + html.length());
			HTMLPrinter.insertPageProlog(info, 0, fgStyleSheet);
			info.append(html);
			HTMLPrinter.addPageEpilog(info);
			return info.toString();
		}
	}


	private boolean fIsFocusable;


	public RevisionHoverInformationControlCreator(boolean isFocusable) {
		fIsFocusable= isFocusable;
	}

	@Override
	public boolean canReuse(IInformationControl control) {
		if (!super.canReuse(control))
			return false;

		if (control instanceof IInformationControlExtension4)
			((IInformationControlExtension4)control).setStatusText(EditorsUI.getTooltipAffordanceString());

		return true;
	}

	@Override
	protected IInformationControl doCreateInformationControl(Shell parent) {
		if (BrowserInformationControl.isAvailable(parent)) {
			if (fIsFocusable)
				return new RevisionInformationControl(parent);
			return new RevisionInformationControl(parent, EditorsUI.getTooltipAffordanceString());
		}

		if (fIsFocusable)
			return new DefaultInformationControl(parent, true);
		return new DefaultInformationControl(parent, EditorsUI.getTooltipAffordanceString());
	}

}

Back to the top