001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.widgets.Shell;
014:
015: import org.eclipse.jface.internal.text.html.BrowserInformationControl;
016: import org.eclipse.jface.internal.text.html.HTMLPrinter;
017: import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
018:
019: import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
020: import org.eclipse.jface.text.DefaultInformationControl;
021: import org.eclipse.jface.text.IInformationControl;
022: import org.eclipse.jface.text.IInformationControlExtension4;
023:
024: import org.eclipse.ui.editors.text.EditorsUI;
025:
026: /**
027: * The revision information control creator
028: * that can show the tool tip affordance.
029: *
030: * @since 3.3
031: */
032: class RevisionHoverInformationControlCreator extends
033: AbstractReusableInformationControlCreator {
034:
035: private static final String fgStyleSheet = "/* Font definitions */\n" + //$NON-NLS-1$
036: "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"
037: + //$NON-NLS-1$
038: "pre { font-family: monospace; font-size: 9pt }\n"
039: + //$NON-NLS-1$
040: "\n"
041: + //$NON-NLS-1$
042: "/* Margins */\n"
043: + //$NON-NLS-1$
044: "body { overflow: auto; margin-top: 0; margin-bottom: 4; margin-left: 3; margin-right: 0 }\n"
045: + //$NON-NLS-1$
046: "h1 { margin-top: 5; margin-bottom: 1 } \n"
047: + //$NON-NLS-1$
048: "h2 { margin-top: 25; margin-bottom: 3 }\n"
049: + //$NON-NLS-1$
050: "h3 { margin-top: 20; margin-bottom: 3 }\n"
051: + //$NON-NLS-1$
052: "h4 { margin-top: 20; margin-bottom: 3 }\n"
053: + //$NON-NLS-1$
054: "h5 { margin-top: 0; margin-bottom: 0 }\n"
055: + //$NON-NLS-1$
056: "p { margin-top: 10px; margin-bottom: 10px }\n"
057: + //$NON-NLS-1$
058: "pre { margin-left: 6 }\n"
059: + //$NON-NLS-1$
060: "ul { margin-top: 0; margin-bottom: 10 }\n"
061: + //$NON-NLS-1$
062: "li { margin-top: 0; margin-bottom: 0 } \n"
063: + //$NON-NLS-1$
064: "li p { margin-top: 0; margin-bottom: 0 } \n"
065: + //$NON-NLS-1$
066: "ol { margin-top: 0; margin-bottom: 10 }\n"
067: + //$NON-NLS-1$
068: "dl { margin-top: 0; margin-bottom: 10 }\n"
069: + //$NON-NLS-1$
070: "dt { margin-top: 0; margin-bottom: 0; font-weight: bold }\n"
071: + //$NON-NLS-1$
072: "dd { margin-top: 0; margin-bottom: 0 }\n" + //$NON-NLS-1$
073: "\n" + //$NON-NLS-1$
074: "/* Styles and colors */\n" + //$NON-NLS-1$
075: "a:link { color: #0000FF }\n" + //$NON-NLS-1$
076: "a:hover { color: #000080 }\n" + //$NON-NLS-1$
077: "a:visited { text-decoration: underline }\n" + //$NON-NLS-1$
078: "h4 { font-style: italic }\n" + //$NON-NLS-1$
079: "strong { font-weight: bold }\n" + //$NON-NLS-1$
080: "em { font-style: italic }\n" + //$NON-NLS-1$
081: "var { font-style: italic }\n" + //$NON-NLS-1$
082: "th { font-weight: bold }\n" + //$NON-NLS-1$
083: ""; //$NON-NLS-1$
084:
085: private boolean fIsFocusable;
086:
087: public RevisionHoverInformationControlCreator(boolean isFocusable) {
088: fIsFocusable = isFocusable;
089: }
090:
091: /*
092: * @see org.eclipse.ui.texteditor.AbstractReusableInformationControlCreator#canReuse(org.eclipse.jface.text.IInformationControl)
093: */
094: public boolean canReuse(IInformationControl control) {
095: if (!super .canReuse(control))
096: return false;
097:
098: if (control instanceof IInformationControlExtension4)
099: ((IInformationControlExtension4) control)
100: .setStatusText(EditorsUI
101: .getTooltipAffordanceString());
102:
103: return true;
104: }
105:
106: /*
107: * @see org.eclipse.jface.internal.text.revisions.AbstractReusableInformationControlCreator#doCreateInformationControl(org.eclipse.swt.widgets.Shell)
108: */
109: protected IInformationControl doCreateInformationControl(
110: Shell parent) {
111: int style = fIsFocusable ? SWT.V_SCROLL | SWT.H_SCROLL
112: : SWT.NONE;
113:
114: if (BrowserInformationControl.isAvailable(parent)) {
115: final int shellStyle = SWT.TOOL
116: | (fIsFocusable ? SWT.RESIZE : SWT.NO_TRIM);
117: return new BrowserInformationControl(parent, shellStyle,
118: style, EditorsUI.getTooltipAffordanceString()) {
119:
120: /*
121: * @see org.eclipse.jface.internal.text.html.BrowserInformationControl#setInformation(java.lang.String)
122: */
123: public void setInformation(String content) {
124: content = addCSSToHTMLFragment(content);
125: super .setInformation(content);
126: }
127:
128: /**
129: * Adds a HTML header and CSS info if <code>html</code> is only an HTML fragment (has no
130: * <html> section).
131: *
132: * @param html the html / text produced by a revision
133: * @return modified html
134: */
135: private String addCSSToHTMLFragment(String html) {
136: int max = Math.min(100, html.length());
137: if (html.substring(0, max).indexOf("<html>") != -1) //$NON-NLS-1$
138: // there is already a header
139: return html;
140:
141: StringBuffer info = new StringBuffer(512 + html
142: .length());
143: HTMLPrinter.insertPageProlog(info, 0, fgStyleSheet);
144: info.append(html);
145: HTMLPrinter.addPageEpilog(info);
146: return info.toString();
147: }
148: };
149: }
150: return new DefaultInformationControl(parent, style,
151: new HTMLTextPresenter(), EditorsUI
152: .getTooltipAffordanceString());
153: }
154: }
|