blob: 619eafb751874eacefa7408f122352ee3bb27159 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jface.text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
/**
* Text based implementation of <code>IInformationControl</code>.
* Displays information in a styled text widget. Before displaying, the
* information set to this information control is processed by an
* <code>IInformationPresenter</code>.
*
* @since 2.0
*/
public class DefaultInformationControl implements IInformationControl, IInformationControlExtension, IInformationControlExtension3, DisposeListener {
/**
* An information presenter determines the style presentation
* of information displayed in the default information control.
* The interface can be implemented by clients.
*/
public static interface IInformationPresenter {
/**
* Updates the given presentation of the given information and
* thereby may manipulate the information to be displayed. The manipulation
* could be the extraction of textual encoded style information etc. Returns the
* manipulated information.
*
* @param display the display of the information control
* @param hoverInfo the information to be presented
* @param presentation the presentation to be updated
* @param maxWidth the maximal width in pixels
* @param maxHeight the maximal height in pixels
*
* @return the manipulated information
*/
String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight);
}
/** Border thickness in pixels. */
private static final int BORDER= 1;
/** The control's shell */
private Shell fShell;
/** The control's text widget */
private StyledText fText;
/** The information presenter */
private IInformationPresenter fPresenter;
/** A cached text presentation */
private TextPresentation fPresentation= new TextPresentation();
/** The control width constraint */
private int fMaxWidth= -1;
/** The control height constraint */
private int fMaxHeight= -1;
/**
* The font of the optional status text label.
*
* @since 3.0
*/
private Font fStatusTextFont;
/**
* Creates a default information control with the given shell as parent. The given
* information presenter is used to process the information to be displayed. The given
* styles are applied to the created styled text widget.
*
* @param parent the parent shell
* @param shellStyle the additional styles for the shell
* @param style the additional styles for the styled text widget
* @param presenter the presenter to be used
*/
public DefaultInformationControl(Shell parent, int shellStyle, int style, IInformationPresenter presenter) {
this(parent, shellStyle, style, presenter, null);
}
/**
* Creates a default information control with the given shell as parent. The given
* information presenter is used to process the information to be displayed. The given
* styles are applied to the created styled text widget.
*
* @param parent the parent shell
* @param shellStyle the additional styles for the shell
* @param style the additional styles for the styled text widget
* @param presenter the presenter to be used
* @param statusFieldText the text to be used in the optional status field
* or <code>null</code> if the status field should be hidden
* @since 3.0
*/
public DefaultInformationControl(Shell parent, int shellStyle, int style, IInformationPresenter presenter, String statusFieldText) {
GridLayout layout;
GridData gd;
fShell= new Shell(parent, SWT.NO_FOCUS | SWT.ON_TOP | shellStyle);
Display display= fShell.getDisplay();
fShell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
Composite composite= fShell;
layout= new GridLayout(1, false);
int border= ((shellStyle & SWT.NO_TRIM) == 0) ? 0 : BORDER;
layout.marginHeight= border;
layout.marginWidth= border;
composite.setLayout(layout);
gd= new GridData(GridData.FILL_BOTH);
composite.setLayoutData(gd);
if (statusFieldText != null) {
composite= new Composite(composite, SWT.NONE);
layout= new GridLayout(1, false);
layout.marginHeight= 0;
layout.marginWidth= 0;
composite.setLayout(layout);
gd= new GridData(GridData.FILL_BOTH);
composite.setLayoutData(gd);
composite.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
composite.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
}
// Text field
fText= new StyledText(composite, SWT.MULTI | SWT.READ_ONLY | style);
gd= new GridData(GridData.BEGINNING | GridData.FILL_BOTH);
fText.setLayoutData(gd);
fText.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
fText.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
fText.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.character == 0x1B) // ESC
fShell.dispose();
}
public void keyReleased(KeyEvent e) {}
});
fPresenter= presenter;
// Status field
if (statusFieldText != null) {
// Horizontal separator line
Label separator= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.LINE_DOT);
separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// Status field label
Label statusField= new Label(composite, SWT.RIGHT);
statusField.setText(statusFieldText);
Font font= statusField.getFont();
FontData[] fontDatas= font.getFontData();
for (int i= 0; i < fontDatas.length; i++)
fontDatas[i].setHeight(fontDatas[i].getHeight() * 9 / 10);
fStatusTextFont= new Font(statusField.getDisplay(), fontDatas);
statusField.setFont(fStatusTextFont);
gd= new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING);
statusField.setLayoutData(gd);
statusField.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
statusField.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
}
addDisposeListener(this);
}
/**
* Creates a default information control with the given shell as parent. The given
* information presenter is used to process the information to be displayed. The given
* styles are applied to the created styled text widget.
*
* @param parent the parent shell
* @param style the additional styles for the styled text widget
* @param presenter the presenter to be used
*/
public DefaultInformationControl(Shell parent,int style, IInformationPresenter presenter) {
this(parent, SWT.NO_TRIM, style, presenter);
}
/**
* Creates a default information control with the given shell as parent. The given
* information presenter is used to process the information to be displayed. The given
* styles are applied to the created styled text widget.
*
* @param parent the parent shell
* @param style the additional styles for the styled text widget
* @param presenter the presenter to be used
* @param statusFieldText the text to be used in the optional status field
* or <code>null</code> if the status field should be hidden
* @since 3.0
*/
public DefaultInformationControl(Shell parent,int style, IInformationPresenter presenter, String statusFieldText) {
this(parent, SWT.NO_TRIM, style, presenter, statusFieldText);
}
/**
* Creates a default information control with the given shell as parent.
* No information presenter is used to process the information
* to be displayed. No additional styles are applied to the styled text widget.
*
* @param parent the parent shell
*/
public DefaultInformationControl(Shell parent) {
this(parent, SWT.NONE, null);
}
/**
* Creates a default information control with the given shell as parent. The given
* information presenter is used to process the information to be displayed.
* No additional styles are applied to the styled text widget.
*
* @param parent the parent shell
* @param presenter the presenter to be used
*/
public DefaultInformationControl(Shell parent, IInformationPresenter presenter) {
this(parent, SWT.NONE, presenter);
}
/*
* @see IInformationControl#setInformation(String)
*/
public void setInformation(String content) {
if (fPresenter == null) {
fText.setText(content);
} else {
fPresentation.clear();
content= fPresenter.updatePresentation(fShell.getDisplay(), content, fPresentation, fMaxWidth, fMaxHeight);
if (content != null) {
fText.setText(content);
TextPresentation.applyTextPresentation(fPresentation, fText);
} else {
fText.setText(""); //$NON-NLS-1$
}
}
}
/*
* @see IInformationControl#setVisible(boolean)
*/
public void setVisible(boolean visible) {
fShell.setVisible(visible);
}
/*
* @see IInformationControl#dispose()
*/
public void dispose() {
if (fShell != null && !fShell.isDisposed())
fShell.dispose();
else
widgetDisposed(null);
}
/*
* @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
* @since 3.0
*/
public void widgetDisposed(DisposeEvent event) {
if (fStatusTextFont != null && !fStatusTextFont.isDisposed())
fStatusTextFont.dispose();
fShell= null;
fText= null;
fStatusTextFont= null;
}
/*
* @see IInformationControl#setSize(int, int)
*/
public void setSize(int width, int height) {
fShell.setSize(width, height);
}
/*
* @see IInformationControl#setLocation(Point)
*/
public void setLocation(Point location) {
Rectangle trim= fShell.computeTrim(0, 0, 0, 0);
Point textLocation= fText.getLocation();
location.x += trim.x - textLocation.x;
location.y += trim.y - textLocation.y;
fShell.setLocation(location);
}
/*
* @see IInformationControl#setSizeConstraints(int, int)
*/
public void setSizeConstraints(int maxWidth, int maxHeight) {
fMaxWidth= maxWidth;
fMaxHeight= maxHeight;
}
/*
* @see IInformationControl#computeSizeHint()
*/
public Point computeSizeHint() {
return fShell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
}
/*
* @see org.eclipse.jface.text.IInformationControlExtension3#computeTrim()
* @since 3.0
*/
public Rectangle computeTrim() {
return fShell.computeTrim(0, 0, 0, 0);
}
/*
* @see org.eclipse.jface.text.IInformationControlExtension3#getBounds()
* @since 3.0
*/
public Rectangle getBounds() {
return fShell.getBounds();
}
/*
* @see org.eclipse.jface.text.IInformationControlExtension3#restoresLocation()
* @since 3.0
*/
public boolean restoresLocation() {
return false;
}
/*
* @see org.eclipse.jface.text.IInformationControlExtension3#restoresSize()
* @since 3.0
*/
public boolean restoresSize() {
return false;
}
/*
* @see IInformationControl#addDisposeListener(DisposeListener)
*/
public void addDisposeListener(DisposeListener listener) {
fShell.addDisposeListener(listener);
}
/*
* @see IInformationControl#removeDisposeListener(DisposeListener)
*/
public void removeDisposeListener(DisposeListener listener) {
fShell.removeDisposeListener(listener);
}
/*
* @see IInformationControl#setForegroundColor(Color)
*/
public void setForegroundColor(Color foreground) {
fText.setForeground(foreground);
}
/*
* @see IInformationControl#setBackgroundColor(Color)
*/
public void setBackgroundColor(Color background) {
fText.setBackground(background);
}
/*
* @see IInformationControl#isFocusControl()
*/
public boolean isFocusControl() {
return fText.isFocusControl();
}
/*
* @see IInformationControl#setFocus()
*/
public void setFocus() {
fShell.forceFocus();
fText.setFocus();
}
/*
* @see IInformationControl#addFocusListener(FocusListener)
*/
public void addFocusListener(FocusListener listener) {
fText.addFocusListener(listener);
}
/*
* @see IInformationControl#removeFocusListener(FocusListener)
*/
public void removeFocusListener(FocusListener listener) {
fText.removeFocusListener(listener);
}
/*
* @see IInformationControlExtension#hasContents()
*/
public boolean hasContents() {
return fText.getCharCount() > 0;
}
}