Skip to content

Commit

Permalink
Make drawer and tool entry figures configurable via PaletteCustomizer
Browse files Browse the repository at this point in the history
This adds the public interfaces IDrawerFigure and IToolEntryFigure,
which are implemented by the figures created in the DrawerEditPart and
ToolEntryEditPart, respectively.

Those interfaces can be accessed with with configure methods in the
PaletteCustomizers. Clients can use them to adjust the figures without
having to re-implement the palette editpart-factory and without having
to reference the internal classes.
  • Loading branch information
ptziegler committed Oct 14, 2024
1 parent e454b47 commit 037a874
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2010 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -36,6 +36,7 @@
import org.eclipse.gef.internal.ui.palette.PaletteColorUtil;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteTemplateEntry;
import org.eclipse.gef.ui.palette.PaletteCustomizer;
import org.eclipse.gef.ui.palette.PaletteViewerPreferences;
import org.eclipse.gef.ui.palette.editparts.IPinnableEditPart;
import org.eclipse.gef.ui.palette.editparts.PaletteAnimator;
Expand Down Expand Up @@ -83,6 +84,11 @@ public void focusGained(FocusEvent fe) {

fig.getScrollpane().getContents().addLayoutListener(getPaletteAnimator());

PaletteCustomizer customizer = getViewer().getCustomizer();
if (customizer != null) {
customizer.configure(fig);
}

return fig;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2022 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -13,6 +13,7 @@
package org.eclipse.gef.internal.ui.palette.editparts;

import java.util.List;
import java.util.Objects;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
Expand Down Expand Up @@ -45,13 +46,15 @@
import org.eclipse.draw2d.geometry.Rectangle;

import org.eclipse.gef.internal.ui.palette.PaletteColorUtil;
import org.eclipse.gef.ui.palette.IDrawerFigure;
import org.eclipse.gef.ui.palette.IGradientPainter;
import org.eclipse.gef.ui.palette.PaletteViewerPreferences;
import org.eclipse.gef.ui.palette.editparts.PaletteToolbarLayout;

/**
* @author Pratik Shah
*/
public class DrawerFigure extends Figure {
public class DrawerFigure extends Figure implements IDrawerFigure {

/** Foreground color constant **/
protected static final Color FG_COLOR = FigureUtilities.mixColors(PaletteColorUtil.WIDGET_NORMAL_SHADOW,
Expand Down Expand Up @@ -79,6 +82,7 @@ public class DrawerFigure extends Figure {
private boolean showPin = true;
private boolean skipNextEvent;
private EditPartTipHelper tipHelper;
private IGradientPainter painter = new DefaultGradientPainter();

/**
* This is the figure for the entire drawer label button.
Expand Down Expand Up @@ -130,7 +134,7 @@ protected void paintFigure(Graphics g) {
r.shrink(new Insets(0, 0, 1, 0));
}

paintToggleGradient(g, r);
painter.paint(g, r);

}
}
Expand Down Expand Up @@ -200,40 +204,6 @@ protected boolean isChildGrowing(IFigure child) {
createHoverHelp(control);
}

/**
* Paints the background gradient on the drawer toggle figure.
*
* @param g the graphics object
* @param rect the rectangle which the background gradient should cover
*/
private void paintToggleGradient(Graphics g, Rectangle rect) {
if (isExpanded()) {
g.setBackgroundColor(PaletteColorUtil.WIDGET_BACKGROUND_LIST_BACKGROUND_85);
g.fillRectangle(rect);
} else if (collapseToggle.getModel().isMouseOver()) {
Color color1 = PaletteColorUtil.WIDGET_BACKGROUND_LIST_BACKGROUND_60;
Color color2 = PaletteColorUtil.WIDGET_BACKGROUND_NORMAL_SHADOW_90;
Color color3 = PaletteColorUtil.WIDGET_BACKGROUND_NORMAL_SHADOW_95;
Color color4 = PaletteColorUtil.WIDGET_BACKGROUND_LIST_BACKGROUND_90;

g.setForegroundColor(color1);
g.setBackgroundColor(color2);
g.fillGradient(rect.x, rect.y, rect.width, rect.height - 4, true);

g.setForegroundColor(color2);
g.setBackgroundColor(color3);
g.fillGradient(rect.x, rect.bottom() - 4, rect.width, 2, true);

g.setForegroundColor(color3);
g.setBackgroundColor(color4);
g.fillGradient(rect.x, rect.bottom() - 2, rect.width, 2, true);
} else {
g.setForegroundColor(PaletteColorUtil.WIDGET_BACKGROUND_LIST_BACKGROUND_85);
g.setBackgroundColor(PaletteColorUtil.WIDGET_BACKGROUND_NORMAL_SHADOW_45);
g.fillGradient(rect, true);
}
}

private void createHoverHelp(final Control control) {
if (control == null) {
return;
Expand All @@ -255,7 +225,7 @@ protected void paintFigure(Graphics graphics) {
Rectangle r = Rectangle.SINGLETON;
r.setBounds(getBounds());
graphics.pushState();
paintToggleGradient(graphics, getBounds());
painter.paint(graphics, getBounds());
graphics.popState();
super.paintFigure(graphics);
}
Expand Down Expand Up @@ -325,17 +295,12 @@ IFigure buildTooltip() {
return null;
}

/**
* @return The Clickable that is used to expand/collapse the drawer.
*/
@Override
public Clickable getCollapseToggle() {
return collapseToggle;
}

/**
* @return The content pane for this figure, i.e. the Figure to which children
* can be added.
*/
@Override
public IFigure getContentPane() {
return scrollpane.getContents();
}
Expand Down Expand Up @@ -501,4 +466,40 @@ public void showPin(boolean show) {
handleExpandStateChanged();
}

@Override
public void setGradientPainter(IGradientPainter painter) {
Objects.requireNonNull(painter, "Gradient painter must not be null"); //$NON-NLS-1$
this.painter = painter;
}

private class DefaultGradientPainter implements IGradientPainter {
@Override
public void paint(Graphics g, Rectangle rect) {
if (isExpanded()) {
g.setBackgroundColor(PaletteColorUtil.WIDGET_BACKGROUND_LIST_BACKGROUND_85);
g.fillRectangle(rect);
} else if (collapseToggle.getModel().isMouseOver()) {
Color color1 = PaletteColorUtil.WIDGET_BACKGROUND_LIST_BACKGROUND_60;
Color color2 = PaletteColorUtil.WIDGET_BACKGROUND_NORMAL_SHADOW_90;
Color color3 = PaletteColorUtil.WIDGET_BACKGROUND_NORMAL_SHADOW_95;
Color color4 = PaletteColorUtil.WIDGET_BACKGROUND_LIST_BACKGROUND_90;

g.setForegroundColor(color1);
g.setBackgroundColor(color2);
g.fillGradient(rect.x, rect.y, rect.width, rect.height - 4, true);

g.setForegroundColor(color2);
g.setBackgroundColor(color3);
g.fillGradient(rect.x, rect.bottom() - 4, rect.width, 2, true);

g.setForegroundColor(color3);
g.setBackgroundColor(color4);
g.fillGradient(rect.x, rect.bottom() - 2, rect.width, 2, true);
} else {
g.setForegroundColor(PaletteColorUtil.WIDGET_BACKGROUND_LIST_BACKGROUND_85);
g.setBackgroundColor(PaletteColorUtil.WIDGET_BACKGROUND_NORMAL_SHADOW_45);
g.fillGradient(rect, true);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
*******************************************************************************/
package org.eclipse.gef.internal.ui.palette.editparts;

import java.util.Objects;

import org.eclipse.swt.SWT;
import org.eclipse.swt.accessibility.ACC;
import org.eclipse.swt.accessibility.AccessibleControlEvent;
import org.eclipse.swt.accessibility.AccessibleEvent;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;

Expand All @@ -42,6 +45,9 @@
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteStack;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.ui.palette.IColorPalette;
import org.eclipse.gef.ui.palette.IToolEntryFigure;
import org.eclipse.gef.ui.palette.PaletteCustomizer;
import org.eclipse.gef.ui.palette.PaletteViewerPreferences;
import org.eclipse.gef.ui.palette.editparts.IPinnableEditPart;
import org.eclipse.gef.ui.palette.editparts.PaletteEditPart;
Expand Down Expand Up @@ -216,9 +222,10 @@ protected boolean handleNativeDragStarted(DragSourceEvent event) {
/**
* The figure for for a <code>ToolEntryEditPart</code>.
*/
class ToolEntryToggle extends Toggle {
class ToolEntryToggle extends Toggle implements IToolEntryFigure {

private boolean showHoverFeedback = false;
private IColorPalette colorPalette = new DefaultColorPalette();

ToolEntryToggle(IFigure contents) {
super(contents);
Expand Down Expand Up @@ -278,10 +285,10 @@ protected void paintFigure(Graphics graphics) {
ButtonModel model = getModel();

if (model.isSelected()) {
graphics.setBackgroundColor(PaletteColorUtil.getSelectedColor());
graphics.setBackgroundColor(colorPalette.getSelectedColor());
graphics.fillRoundRectangle(getSelectionRectangle(getLayoutSetting(), customLabel), 3, 3);
} else if (model.isMouseOver() || showHoverFeedback) {
graphics.setBackgroundColor(PaletteColorUtil.getHoverColor());
graphics.setBackgroundColor(colorPalette.getHoverColor());
graphics.fillRoundRectangle(getSelectionRectangle(getLayoutSetting(), customLabel), 3, 3);
}
}
Expand Down Expand Up @@ -322,6 +329,12 @@ public void setShowHoverFeedback(boolean showHoverFeedback) {
this.showHoverFeedback = showHoverFeedback;
repaint();
}

@Override
public void setColorPalette(IColorPalette colorPalette) {
Objects.requireNonNull(colorPalette, "Color palette must not be null"); //$NON-NLS-1$
this.colorPalette = colorPalette;
}
}

private static final String ACTIVE_STATE = "active"; //$NON-NLS-1$
Expand Down Expand Up @@ -392,8 +405,14 @@ public void getState(AccessibleControlEvent e) {
@Override
public IFigure createFigure() {
customLabel = new DetailedLabelFigure();
Clickable button = new ToolEntryToggle(customLabel);
ToolEntryToggle button = new ToolEntryToggle(customLabel);
button.addActionListener(event -> getViewer().setActiveTool(getModel()));

PaletteCustomizer customizer = getViewer().getCustomizer();
if (customizer != null) {
customizer.configure(button);
}

return button;
}

Expand Down Expand Up @@ -573,4 +592,15 @@ static Rectangle getSelectionRectangle(int layoutMode, DetailedLabelFigure label
return rect;
}

private static class DefaultColorPalette implements IColorPalette {
@Override
public Color getSelectedColor() {
return PaletteColorUtil.getSelectedColor();
}

@Override
public Color getHoverColor() {
return PaletteColorUtil.getHoverColor();
}
}
}
22 changes: 22 additions & 0 deletions org.eclipse.gef/src/org/eclipse/gef/ui/palette/IColorPalette.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*******************************************************************************
* Copyright (c) 2024 Patrick Ziegler and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Patrick Ziegler - initial API and implementation
*******************************************************************************/

package org.eclipse.gef.ui.palette;

import org.eclipse.swt.graphics.Color;

public interface IColorPalette {
Color getSelectedColor();

Color getHoverColor();
}
40 changes: 40 additions & 0 deletions org.eclipse.gef/src/org/eclipse/gef/ui/palette/IDrawerFigure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2024 Patrick Ziegler and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Patrick Ziegler - initial API and implementation
*******************************************************************************/

package org.eclipse.gef.ui.palette;

import org.eclipse.draw2d.Clickable;
import org.eclipse.draw2d.IFigure;

import org.eclipse.gef.internal.ui.palette.editparts.DrawerFigure;

/**
* Public interface of the {@link DrawerFigure}.
*
* @noimplement This interface is not intended to be implemented by clients.
* @noextend This interface is not intended to be extended by clients.
*/
public interface IDrawerFigure extends IFigure {
void setGradientPainter(IGradientPainter painter);

/**
* @return The {@link Clickable} that is used to expand/collapse the drawer.
*/
Clickable getCollapseToggle();

/**
* @return The content pane for this figure, i.e. the Figure to which children
* can be added.
*/
IFigure getContentPane();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright (c) 2024 Patrick Ziegler and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Patrick Ziegler - initial API and implementation
*******************************************************************************/

package org.eclipse.gef.ui.palette;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Rectangle;

import org.eclipse.gef.internal.ui.palette.editparts.DrawerFigure;

/**
* This interface can be used together with the {@link IDrawerFigure} and
* {@link PaletteCustomizer}, to adapt the the background painted in a
* {@link DrawerFigure}.
*
* @since 3.20
*/
public interface IGradientPainter {
/**
* Paints the background gradient on the drawer toggle figure.
*
* @param g the graphics object
* @param rect the rectangle which the background gradient should cover
*/
void paint(Graphics g, Rectangle rect);
}
Loading

0 comments on commit 037a874

Please sign in to comment.