/*******************************************************************************
* * Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.tml;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.AbstractInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.IInformationControlExtension2;
import org.eclipse.jface.util.Geometry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.helpers.ContextInformation;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
public class HTMLInformationControl extends AbstractInformationControl implements IInformationControlExtension2, KeyListener {
private Browser _browser;
private int _width = -1;
private int _height = -1;
private Shell _shell;
private ContextInformation _input;
private Action _openInExternalBrowser;
public HTMLInformationControl(Shell parentShell, int intialWidth, int initialHeight) {
super(parentShell, new ToolBarManager());
_width = intialWidth;
_height = initialHeight;
_shell = parentShell;
_openInExternalBrowser = new Action("Open documentation", ImageDescriptor.createFromImage(Plugin.getDefault().getImageRegistry().get(Plugin.IMAGE_TML_LINK_TO_DOC))) {
public void run() {
WorkbenchUtils.openBrowser(_input.getDocLocation(), _shell.getDisplay(), "OpenWGA Documentation");
}
};
create();
getToolBarManager().add(_openInExternalBrowser);
getToolBarManager().update(true);
if (_width != -1 && _height != -1) {
setSize(_width, _height);
}
}
public HTMLInformationControl(Shell parentShell) {
this(parentShell, -1, -1);
}
@Override
protected void createContent(Composite parent) {
_browser = new Browser(parent, SWT.BORDER);
_browser.addKeyListener(this);
}
@Override
public Rectangle computeTrim() {
return Geometry.add(super.computeTrim(), _browser.computeTrim(0, 0, 0, 0));
}
public boolean hasContents() {
return true;
}
@Override
public void setInformation(String information) {
}
@Override
public IInformationControlCreator getInformationPresenterControlCreator() {
return new HTMLInformationControlCreator();
}
@Override
public boolean restoresSize() {
return true;
}
@Override
public void setVisible(boolean visible) {
if (visible) {
setSize(_width, _height);
}
super.setVisible(visible);
}
@Override
public void setSizeConstraints(int maxWidth, int maxHeight) {
if (_width == -1 && _height == -1) {
_width = maxWidth;
_height = maxHeight;
setSize(_width, _height);
}
}
public void setInput(Object input) {
_width = getBounds().width;
_height = getBounds().height;
if (input instanceof ContextInformation) {
_input = (ContextInformation) input;
if (input != null) {
_browser.setText(_input.getInformation());
_openInExternalBrowser.setEnabled(true);
}
} else if (input instanceof String) {
_input = null;
if (input != null) {
_browser.setText((String)input);
_openInExternalBrowser.setEnabled(false);
}
}
}
public void keyPressed(KeyEvent e) {
// close on esc
if (e.keyCode == SWT.ESC) {
dispose();
}
}
public void keyReleased(KeyEvent e) {
}
}