From 037a87401841fa7ecc5b10140504e2f38c2123ea Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Sat, 12 Oct 2024 17:35:22 +0200 Subject: [PATCH] Make drawer and tool entry figures configurable via PaletteCustomizer 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. --- .../ui/palette/editparts/DrawerEditPart.java | 8 +- .../ui/palette/editparts/DrawerFigure.java | 91 ++++++++++--------- .../palette/editparts/ToolEntryEditPart.java | 38 +++++++- .../eclipse/gef/ui/palette/IColorPalette.java | 22 +++++ .../eclipse/gef/ui/palette/IDrawerFigure.java | 40 ++++++++ .../gef/ui/palette/IGradientPainter.java | 36 ++++++++ .../gef/ui/palette/IToolEntryFigure.java | 27 ++++++ .../gef/ui/palette/PaletteCustomizer.java | 17 ++++ 8 files changed, 229 insertions(+), 50 deletions(-) create mode 100644 org.eclipse.gef/src/org/eclipse/gef/ui/palette/IColorPalette.java create mode 100644 org.eclipse.gef/src/org/eclipse/gef/ui/palette/IDrawerFigure.java create mode 100644 org.eclipse.gef/src/org/eclipse/gef/ui/palette/IGradientPainter.java create mode 100644 org.eclipse.gef/src/org/eclipse/gef/ui/palette/IToolEntryFigure.java diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/DrawerEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/DrawerEditPart.java index cf546c36b..92a4b9905 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/DrawerEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/DrawerEditPart.java @@ -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 @@ -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; @@ -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; } diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/DrawerFigure.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/DrawerFigure.java index e9b9d3250..5f9ab55e3 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/DrawerFigure.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/DrawerFigure.java @@ -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 @@ -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; @@ -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, @@ -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. @@ -130,7 +134,7 @@ protected void paintFigure(Graphics g) { r.shrink(new Insets(0, 0, 1, 0)); } - paintToggleGradient(g, r); + painter.paint(g, r); } } @@ -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; @@ -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); } @@ -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(); } @@ -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); + } + } + } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/ToolEntryEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/ToolEntryEditPart.java index 5d36a607c..f65cae28a 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/ToolEntryEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/ToolEntryEditPart.java @@ -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; @@ -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; @@ -216,9 +222,10 @@ protected boolean handleNativeDragStarted(DragSourceEvent event) { /** * The figure for for a ToolEntryEditPart. */ - class ToolEntryToggle extends Toggle { + class ToolEntryToggle extends Toggle implements IToolEntryFigure { private boolean showHoverFeedback = false; + private IColorPalette colorPalette = new DefaultColorPalette(); ToolEntryToggle(IFigure contents) { super(contents); @@ -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); } } @@ -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$ @@ -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; } @@ -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(); + } + } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/IColorPalette.java b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/IColorPalette.java new file mode 100644 index 000000000..2affb2da8 --- /dev/null +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/IColorPalette.java @@ -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(); +} diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/IDrawerFigure.java b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/IDrawerFigure.java new file mode 100644 index 000000000..b410368b7 --- /dev/null +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/IDrawerFigure.java @@ -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(); +} diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/IGradientPainter.java b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/IGradientPainter.java new file mode 100644 index 000000000..aacd97586 --- /dev/null +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/IGradientPainter.java @@ -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); +} diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/IToolEntryFigure.java b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/IToolEntryFigure.java new file mode 100644 index 000000000..0c227152d --- /dev/null +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/IToolEntryFigure.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * 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.IFigure; + +/** + * Public interface of the + * {@link org.eclipse.gef.internal.ui.palette.editparts.ToolEntryEditPart.ToolEntryToggle}. + * + * @noimplement This interface is not intended to be implemented by clients. + * @noextend This interface is not intended to be extended by clients. + */ +public interface IToolEntryFigure extends IFigure { + void setColorPalette(IColorPalette colorPalette); +} diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/PaletteCustomizer.java b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/PaletteCustomizer.java index 3507bdb87..83dac64c1 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/PaletteCustomizer.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/PaletteCustomizer.java @@ -15,6 +15,8 @@ import java.util.ArrayList; import java.util.List; +import org.eclipse.gef.internal.ui.palette.editparts.DrawerEditPart; +import org.eclipse.gef.internal.ui.palette.editparts.DrawerFigure; import org.eclipse.gef.palette.PaletteContainer; import org.eclipse.gef.palette.PaletteDrawer; import org.eclipse.gef.palette.PaletteEntry; @@ -303,4 +305,19 @@ public void performMoveUp(PaletteEntry entry) { */ public abstract void save(); + /** + * Called by the {@link DrawerEditPart#createFigure()} for every palette drawer + * edit part, to allow clients to customize the created {@link DrawerFigure} + * without accessing the internal class directly. + * + * @param drawerFigure The figure created for one of the palette drawers. + * @since 3.20 + */ + public void configure(IDrawerFigure drawerFigure) { + // may be overwritten by clients + } + + public void configure(IToolEntryFigure drawerFigure) { + // may be overwritten by clients + } }