Skip to content

Instantly share code, notes, and snippets.

@fabioz
Last active August 29, 2015 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fabioz/59b06c519f505d37fcfb to your computer and use it in GitHub Desktop.
Save fabioz/59b06c519f505d37fcfb to your computer and use it in GitHub Desktop.
Custom Painter
/**
* @author: Fabio Zadrozny
* @licenes: EPL
**/
package com.brainwy.liclipse.theme.e4;
import org.eclipse.e4.ui.css.core.dom.properties.ICSSPropertyHandler;
import org.eclipse.e4.ui.css.core.engine.CSSEngine;
import org.eclipse.e4.ui.css.swt.dom.ControlElement;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
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.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.dialogs.FilteredTree;
import org.python.pydev.shared_core.log.Log;
import org.python.pydev.shared_core.utils.PlatformUtils;
import org.w3c.dom.css.CSSValue;
import com.github.eclipsecolortheme.ColorThemeKeys;
import com.github.eclipsecolortheme.ColorThemeManager;
@SuppressWarnings("restriction")
public final class WidgetProperlyHandler implements ICSSPropertyHandler {
private static final Listener paintListener = new Listener() {
ColorThemeManager colorThemeManager = ColorThemeManager.getSingleton();
public void handleEvent(Event event) {
Widget item = event.item;
if (!(item instanceof TreeItem)) {
return;
}
TreeItem treeItem = (TreeItem) item;
if (treeItem.getItemCount() == 0) {
return;
}
int w = 9;
int h = 9;
int x = event.x - 16;
int y = event.y + 4;
GC gc = event.gc;
//If we didn't change the foreground/curr
Color foreground = colorThemeManager.getSWTColorForeground();
Color background = colorThemeManager.getSWTColorBackground();
if (foreground == null || background == null) {
return;
}
gc.setForeground(foreground);
gc.setBackground(background);
//Erase the arrow
gc.fillRectangle(x, y, w + 1, h + 2);
try {
int halfH = h / 2;
gc.drawRectangle(x + 1, y + 1, w - 1, h - 1);
gc.drawLine(x + 3, y + halfH + 1, x + w - 2, y + halfH + 1);
if (!treeItem.getExpanded()) {
int halfW = w / 2;
gc.drawLine(x + halfW + 1, y + 3, x + halfW + 1, y + h - 2);
}
event.detail &= ~SWT.BACKGROUND;
} catch (Exception e) {
Log.log(e);
}
}
};
Listener selectionListener = new Listener() {
ColorThemeManager colorThemeManager = ColorThemeManager.getSingleton();
public void handleEvent(Event event) {
event.detail &= ~SWT.HOT;
if ((event.detail & SWT.SELECTED) != 0) {
Color foreground = colorThemeManager.getSWTColor(ColorThemeKeys.FOREGROUND);
Color background = colorThemeManager.getSWTColor(ColorThemeKeys.CURRENT_LINE_IN_WIDGETS);
if (foreground == null || background == null) {
return;
}
GC gc = event.gc;
Scrollable tree = (Scrollable) event.widget;
Rectangle area = tree.getClientArea();
//Update clip to cover the whole column.
int columnCount = 1;
if (tree instanceof Tree) {
columnCount = ((Tree) tree).getColumnCount();
} else if (tree instanceof Table) {
columnCount = ((Table) tree).getColumnCount();
}
if (event.index == columnCount - 1 || columnCount == 0) {
int width = area.x + area.width - event.x;
if (width > 0) {
Region region = new Region();
gc.getClipping(region);
region.add(event.x, event.y, width, event.height);
gc.setClipping(region);
region.dispose();
}
}
//Rectangle rect = event.getBounds();
//Color oldforeground = gc.getForeground();
Color oldbackground = gc.getBackground();
gc.setBackground(background);
try {
gc.fillRectangle(0, area.y, area.width + 5, area.height);
} finally {
//Don't restore the old foregroud (if we do, when the tree looses focus, the
//color will become wrong when the tree does not have focus).
//Note that this works for trees but not for tables (https://bugs.eclipse.org/bugs/show_bug.cgi?id=414080).
//gc.setForeground(oldforeground);
gc.setBackground(oldbackground);
}
//event.detail &= ~SWT.FOREGROUND;
//event.detail &= ~SWT.FOCUSED;
event.detail &= ~SWT.SELECTED;
event.detail &= ~SWT.BACKGROUND;
gc.setForeground(foreground);
//event.doit = false; -- if we do that the foreground isn't properly drawn.
}
}
};
public boolean applyCSSProperty(Object element, String property, CSSValue value, String pseudo, CSSEngine engine)
throws Exception {
if (element instanceof ControlElement) {
ControlElement controlElement = (ControlElement) element;
Object nativeWidget = controlElement.getNativeWidget();
try {
if ("liclipse-tree-fix".equals(property)) {
if (nativeWidget instanceof FilteredTree) {
FilteredTree filteredTree = (FilteredTree) nativeWidget;
TreeViewer viewer = filteredTree.getViewer();
nativeWidget = viewer.getTree();
}
if (nativeWidget instanceof Tree) {
Tree tree = (Tree) nativeWidget;
if (PlatformUtils.isWindowsPlatform()) { //Fix for drawing only on windows.
tree.removeListener(SWT.PaintItem, paintListener);
tree.addListener(SWT.PaintItem, paintListener);
}
tree.removeListener(SWT.EraseItem, selectionListener);
tree.addListener(SWT.EraseItem, selectionListener);
} else if (nativeWidget instanceof Table) {
Table tree = (Table) nativeWidget;
tree.removeListener(SWT.EraseItem, selectionListener);
tree.addListener(SWT.EraseItem, selectionListener);
}
}
} catch (Exception e) {
Log.log(e);
}
}
return false;
}
public String retrieveCSSProperty(Object element, String property, String pseudo, CSSEngine engine)
throws Exception {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment