/*******************************************************************************
* 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.all;
import java.util.ResourceBundle;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentPartitioningListener;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
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.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.forms.FormColors;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.actions.DataRequestSelectAnnotationRulerAction;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
/**
* super class for all WGA text editors
*
*
*/
public abstract class AbstractWGATextEditor extends TextEditor implements IDocumentPartitioningListener {
private Composite _toolbarComposite;
private Composite _editorComposite;
@Override
protected void doSetInput(IEditorInput newInput) throws CoreException {
super.doSetInput(newInput);
if (getSite().getShell().isVisible()) {
// check if design is compatible
IFile input = getInputFile();
if (input != null) {
if (!WGADesignStructureHelper.isWGAVersionComplianceCompatible(input)) {
MessageDialog.openWarning(getSite().getShell(), "Incompatible design version", "This design uses an incompatible compliance level. Please upgrade to the latest WDS version to work on this design with full feature support.");
}
}
}
getInputDocument().addDocumentPartitioningListener(this);
validateAndMark();
}
protected void editorSaved() {
super.editorSaved();
validateAndMark();
}
protected void validateAndMark() {
IFile file = getInputFile();
if (file != null) {
WGADesignResourceValidator.validate(file);
}
}
@Override
protected void initializeKeyBindingScopes() {
setKeyBindingScopes(getKeyBindingScopes());
}
protected abstract String[] getKeyBindingScopes();
protected IDocument getInputDocument() {
IDocument document = getDocumentProvider().getDocument(getEditorInput());
return document;
}
protected IFile getInputFile() {
IEditorInput editorInput = getEditorInput();
IFile file = null;
if (editorInput instanceof IFileEditorInput) {
IFileEditorInput ife = (IFileEditorInput) getEditorInput();
file = ife.getFile();
}
return file;
}
@Override
protected void createActions() {
super.createActions();
// action for quickfix
ResourceBundle resourceBundle = Plugin.getDefault().getResourceBundle();
DataRequestSelectAnnotationRulerAction rulerAction = new DataRequestSelectAnnotationRulerAction(resourceBundle, "PDESelectAnnotationRulerAction.", this, getVerticalRuler());
setAction(ITextEditorActionConstants.RULER_CLICK, rulerAction);
}
public ISourceViewer getViewer() {
return getSourceViewer();
}
public void documentPartitioningChanged(IDocument document) {
//validateAndMark();
}
public SourceViewerConfiguration getViewerConfiguration() {
return getSourceViewerConfiguration();
}
public void createToolbar(Composite parent) {
}
@Override
protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
Composite composite= new Composite(parent, SWT.NONE);
GridLayout layout= new GridLayout(1, false);
layout.marginHeight= 0;
layout.marginWidth= 0;
layout.horizontalSpacing= 0;
layout.verticalSpacing= 0;
composite.setLayout(layout);
_toolbarComposite= new Composite(composite, SWT.NONE);
GridData layoutData= new GridData(SWT.FILL, SWT.TOP, true, false);
//layoutData.heightHint = 39;
_toolbarComposite.setLayoutData(layoutData);
//
// layout= new GridLayout(1, false);
// layout.marginHeight= 2;
// layout.marginWidth= 2;
// layout.horizontalSpacing= 0;
// layout.verticalSpacing= 0;
// _toolbarComposite.setLayout(layout);
_toolbarComposite.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
int height= _toolbarComposite.getClientArea().height;
Image image= createGradientImage(height, event.display);
_toolbarComposite.setBackgroundImage(image);
}
});
//_toolbarComposite.setBackgroundImage(createGradientImage(39, Display.getCurrent()));
_toolbarComposite.setBackgroundMode(SWT.INHERIT_DEFAULT);
createToolbar(_toolbarComposite);
((GridData) _toolbarComposite.getLayoutData()).exclude= true;
_toolbarComposite.setVisible(false);
_editorComposite= new Composite(composite, SWT.NONE);
_editorComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
FillLayout fillLayout= new FillLayout(SWT.VERTICAL);
fillLayout.marginHeight= 0;
fillLayout.marginWidth= 0;
fillLayout.spacing= 0;
_editorComposite.setLayout(fillLayout);
return super.createSourceViewer(_editorComposite, ruler, styles);
}
private Image createGradientImage(int height, Display display) {
int width= 50;
Image result= new Image(display, width, height);
GC gc= new GC(result);
Color colorC= createColor(SWT.COLOR_WIDGET_BACKGROUND, SWT.COLOR_LIST_BACKGROUND, 35, display);
Color colorD= createColor(SWT.COLOR_WIDGET_BACKGROUND, SWT.COLOR_LIST_BACKGROUND, 45, display);
Color colorE= createColor(SWT.COLOR_WIDGET_BACKGROUND, SWT.COLOR_LIST_BACKGROUND, 80, display);
Color colorF= createColor(SWT.COLOR_WIDGET_BACKGROUND, SWT.COLOR_LIST_BACKGROUND, 70, display);
Color colorG= createColor(SWT.COLOR_WIDGET_BACKGROUND, SWT.COLOR_WHITE, 45, display);
Color colorH= createColor(SWT.COLOR_WIDGET_NORMAL_SHADOW, SWT.COLOR_LIST_BACKGROUND, 35, display);
try {
drawLine(width, 0, colorC, gc);
drawLine(width, 1, colorC, gc);
gc.setForeground(colorD);
gc.setBackground(colorE);
gc.fillGradientRectangle(0, 2, width, 2 + 8, true);
gc.setBackground(colorE);
gc.fillRectangle(0, 2 + 9, width, height - 4);
drawLine(width, height - 3, colorF, gc);
drawLine(width, height - 2, colorG, gc);
drawLine(width, height - 1, colorH, gc);
} finally {
gc.dispose();
colorC.dispose();
colorD.dispose();
colorE.dispose();
colorF.dispose();
colorG.dispose();
colorH.dispose();
}
return result;
}
private Color createColor(int color1, int color2, int ratio, Display display) {
RGB rgb1= display.getSystemColor(color1).getRGB();
RGB rgb2= display.getSystemColor(color2).getRGB();
RGB blend= FormColors.blend(rgb2, rgb1, ratio);
return new Color(display, blend);
}
private void drawLine(int width, int position, Color color, GC gc) {
gc.setForeground(color);
gc.drawLine(0, position, width, position);
}
protected void showToolbar() {
if (_toolbarComposite == null && getInputFile()!=null)
return;
((GridData) _toolbarComposite.getLayoutData()).exclude= false;
_toolbarComposite.setVisible(true);
_toolbarComposite.getParent().layout(true, true);
}
protected void hideToolbar() {
if (_toolbarComposite == null)
return;
((GridData) _toolbarComposite.getLayoutData()).exclude= true;
_toolbarComposite.setVisible(false);
_toolbarComposite.getParent().layout(true, true);
}
}