From 939f2621cea46d6e8311cc591613299eac539cd3 Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Fri, 25 Oct 2024 19:54:01 +0200 Subject: [PATCH] Make palette selection and hover colors customizable This defines a new ColorPalette interface that can be set in the PaletteViewer. The colors provided by this interface are then used in the palette figure for e.g. the tool entries. Clients can extend the DefaultColorPalette to define their own colors and therefore change the look of the palette. --- CHANGELOG.md | 3 + .../eclipse/gef/examples/shapes/Messages.java | 2 + .../gef/examples/shapes/ShapesEditor.java | 7 ++ .../ShapesEditorActionBarContributor.java | 17 ++++- .../shapes/action/ColorPaletteAction.java | 51 +++++++++++++++ .../action/ColorPaletteRetargetAction.java | 27 ++++++++ .../gef/examples/shapes/messages.properties | 2 + .../shapes/palette/ShapesColorPalette.java | 36 +++++++++++ .../gef/test/swtbot/ShapesDiagramTests.java | 17 +++++ .../test/swtbot/utils/SWTBotGefPalette.java | 44 +++++++++++++ org.eclipse.gef/.settings/.api_filters | 8 +++ .../internal/ui/palette/PaletteColorUtil.java | 47 +------------- .../ui/palette/editparts/DrawerEditPart.java | 4 +- .../ui/palette/editparts/DrawerFigure.java | 17 +++-- .../palette/editparts/PaletteScrollBar.java | 12 ++-- .../palette/editparts/TemplateEditPart.java | 3 +- .../palette/editparts/ToolEntryEditPart.java | 7 +- .../eclipse/gef/ui/palette/ColorPalette.java | 48 ++++++++++++++ .../gef/ui/palette/DefaultColorPalette.java | 64 +++++++++++++++++++ .../eclipse/gef/ui/palette/PaletteViewer.java | 27 ++++++++ .../ui/palette/editparts/PaletteEditPart.java | 12 ++++ 21 files changed, 390 insertions(+), 65 deletions(-) create mode 100644 org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/action/ColorPaletteAction.java create mode 100644 org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/action/ColorPaletteRetargetAction.java create mode 100644 org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/palette/ShapesColorPalette.java create mode 100644 org.eclipse.gef.tests/src/org/eclipse/gef/test/swtbot/utils/SWTBotGefPalette.java create mode 100644 org.eclipse.gef/src/org/eclipse/gef/ui/palette/ColorPalette.java create mode 100644 org.eclipse.gef/src/org/eclipse/gef/ui/palette/DefaultColorPalette.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fd6ed33c..785084f63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ ## GEF - _Linux Only_ The overlay scrolling of the palette can be configured via the _PaletteViewerPreferences.PREFERENCE_SCROLLBARS_MODE_ preference. Supported values are _SWT.NONE_ and _SWT.SCROLLBAR_OVERLAY_. - The refresh rate of the [TargetingTool](https://github.com/eclipse/gef-classic/blob/master/org.eclipse.gef/src/org/eclipse/gef/tools/TargetingTool.java) class can be configured via the `setRefreshRate(int)` method. Events are then updated only once every given number of milliseconds, to reduce the CPU load when processing large amounts of update requests. + - The look of the palette viewer can be customized via the ColorPalette interface. Supported model elements and properties are: + - PaletteEntry (hover, selection) + - PaletteTemplateEntry (selection) ## Zest - Integration of Zest 2.0 development branch. See the [wiki](https://github.com/eclipse/gef-classic/wiki/Zest#zest-2x) for more details. In case only default layout algorithms are used, the initial migration should be seamless. Otherwise the algorithms can be adapted to run in legacy mode by extending `AbstractLayoutAlgorithm.Zest1` or have to be re-implemented using the new API by extending `AbstractLayoutAlgorithm`. Note that this legacy mode will be removed in a future release. The following list contains the most significant, deprecated features: diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/Messages.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/Messages.java index fb5407479..891cd8790 100644 --- a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/Messages.java +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/Messages.java @@ -18,6 +18,8 @@ public class Messages extends NLS { private static final String BUNDLE_NAME = Messages.class.getPackageName() + ".messages"; //$NON-NLS-1$ public static String ShapeSetConstraintCommand_MoveResize; + public static String ShapesEditorActionBarContributor_PaletteMenu; + public static String ColorPaletteRetargetAction_Text; static { // initialize resource bundle NLS.initializeMessages(BUNDLE_NAME, Messages.class); diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java index 4979cc694..ae3b06906 100644 --- a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditor.java @@ -61,6 +61,7 @@ import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler; import org.eclipse.gef.ui.parts.TreeViewer; +import org.eclipse.gef.examples.shapes.action.ColorPaletteAction; import org.eclipse.gef.examples.shapes.model.ShapesDiagram; import org.eclipse.gef.examples.shapes.parts.ShapesEditPartFactory; import org.eclipse.gef.examples.shapes.parts.ShapesTreeEditPartFactory; @@ -327,6 +328,12 @@ protected void setInput(IEditorInput input) { } } + @Override + protected void createActions() { + super.createActions(); + getActionRegistry().registerAction(new ColorPaletteAction(this)); + } + /** * Creates an outline pagebook for this editor. */ diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditorActionBarContributor.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditorActionBarContributor.java index 87adbd679..d6e022f13 100644 --- a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditorActionBarContributor.java +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/ShapesEditorActionBarContributor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2005 Elias Volanakis and others. + * Copyright (c) 2004, 2024 Elias Volanakis 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 @@ -12,7 +12,10 @@ *******************************************************************************/ package org.eclipse.gef.examples.shapes; +import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IToolBarManager; +import org.eclipse.jface.action.MenuManager; +import org.eclipse.ui.IWorkbenchActionConstants; import org.eclipse.ui.actions.ActionFactory; import org.eclipse.gef.ui.actions.ActionBarContributor; @@ -20,6 +23,9 @@ import org.eclipse.gef.ui.actions.RedoRetargetAction; import org.eclipse.gef.ui.actions.UndoRetargetAction; +import org.eclipse.gef.examples.shapes.action.ColorPaletteAction; +import org.eclipse.gef.examples.shapes.action.ColorPaletteRetargetAction; + /** * Contributes actions to a toolbar. This class is tied to the editor in the * definition of editor-extension (see plugin.xml). @@ -38,6 +44,7 @@ protected void buildActions() { addRetargetAction(new DeleteRetargetAction()); addRetargetAction(new UndoRetargetAction()); addRetargetAction(new RedoRetargetAction()); + addRetargetAction(new ColorPaletteRetargetAction()); } /** @@ -62,4 +69,12 @@ protected void declareGlobalActionKeys() { // currently none } + @Override + public void contributeToMenu(IMenuManager menubar) { + super.contributeToMenu(menubar); + MenuManager paletteMenu = new MenuManager(Messages.ShapesEditorActionBarContributor_PaletteMenu); + paletteMenu.add(getAction(ColorPaletteAction.ID)); + menubar.insertAfter(IWorkbenchActionConstants.M_EDIT, paletteMenu); + } + } \ No newline at end of file diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/action/ColorPaletteAction.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/action/ColorPaletteAction.java new file mode 100644 index 000000000..e93cfb501 --- /dev/null +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/action/ColorPaletteAction.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * 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.examples.shapes.action; + +import org.eclipse.ui.IEditorPart; + +import org.eclipse.gef.GraphicalViewer; +import org.eclipse.gef.ui.actions.EditorPartAction; +import org.eclipse.gef.ui.palette.PaletteViewer; + +import org.eclipse.gef.examples.shapes.palette.ShapesColorPalette; + +/** + * Flips between the custom and the default color palette whenever executed. + */ +public class ColorPaletteAction extends EditorPartAction { + public static final String ID = "Color Palette"; //$NON-NLS-1$ + + public ColorPaletteAction(IEditorPart editor) { + super(editor); + setId(ID); + } + + @Override + public void run() { + GraphicalViewer graphicalViewer = getEditorPart().getAdapter(GraphicalViewer.class); + PaletteViewer paletteViewer = graphicalViewer.getEditDomain().getPaletteViewer(); + if (isChecked()) { + paletteViewer.setColorPalette(new ShapesColorPalette()); + } else { + paletteViewer.setColorPalette(null); + } + paletteViewer.getControl().redraw(); + } + + @Override + protected boolean calculateEnabled() { + return true; + } +} diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/action/ColorPaletteRetargetAction.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/action/ColorPaletteRetargetAction.java new file mode 100644 index 000000000..7f7d05bfc --- /dev/null +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/action/ColorPaletteRetargetAction.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.examples.shapes.action; + +import org.eclipse.ui.actions.RetargetAction; + +import org.eclipse.gef.examples.shapes.Messages; + +/** + * Delegates the action event from the menu contribution to the editor. + */ +public class ColorPaletteRetargetAction extends RetargetAction { + public ColorPaletteRetargetAction() { + super(ColorPaletteAction.ID, Messages.ColorPaletteRetargetAction_Text, AS_CHECK_BOX); + } +} diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/messages.properties b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/messages.properties index eb0eb9908..328825bba 100644 --- a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/messages.properties +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/messages.properties @@ -25,3 +25,5 @@ ShapesEditorPaletteFactory_Shapes=Shapes ShapesEditorPaletteFactory_SolidConnection=Solid connection ShapesEditorPaletteFactory_Tools=Tools ShapeSetConstraintCommand_MoveResize=move / resize +ShapesEditorActionBarContributor_PaletteMenu=Palette +ColorPaletteRetargetAction_Text=Use Custom Color Palette diff --git a/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/palette/ShapesColorPalette.java b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/palette/ShapesColorPalette.java new file mode 100644 index 000000000..bc1472a73 --- /dev/null +++ b/org.eclipse.gef.examples.shapes/src/org/eclipse/gef/examples/shapes/palette/ShapesColorPalette.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.examples.shapes.palette; + +import org.eclipse.swt.graphics.Color; + +import org.eclipse.draw2d.ColorConstants; + +import org.eclipse.gef.ui.palette.DefaultColorPalette; + +/** + * Defines arbitrary colors that distinguish themselves from the default + * palette. + */ +public class ShapesColorPalette extends DefaultColorPalette { + @Override + public Color getSelectedColor() { + return ColorConstants.darkGreen; + } + + @Override + public Color getHoverColor() { + return ColorConstants.cyan; + } +} diff --git a/org.eclipse.gef.tests/src/org/eclipse/gef/test/swtbot/ShapesDiagramTests.java b/org.eclipse.gef.tests/src/org/eclipse/gef/test/swtbot/ShapesDiagramTests.java index fe29d7de3..133aa95d2 100644 --- a/org.eclipse.gef.tests/src/org/eclipse/gef/test/swtbot/ShapesDiagramTests.java +++ b/org.eclipse.gef.tests/src/org/eclipse/gef/test/swtbot/ShapesDiagramTests.java @@ -22,11 +22,14 @@ import org.eclipse.swtbot.eclipse.gef.finder.widgets.SWTBotGefEditor; import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; +import org.eclipse.draw2d.ColorConstants; import org.eclipse.draw2d.IFigure; import org.eclipse.draw2d.PositionConstants; import org.eclipse.draw2d.geometry.Dimension; import org.eclipse.gef.GraphicalEditPart; +import org.eclipse.gef.test.swtbot.utils.SWTBotGefPalette; +import org.eclipse.gef.ui.palette.ColorPalette; import org.junit.Test; @@ -58,6 +61,20 @@ public void testResizeEditPart() { assertEquals(figure.getSize(), new Dimension(200, 200)); } + @Test + public void testCustomPalette() { + bot.menu("Palette").menu("Use Custom Color Palette").click(); + + SWTBotGefEditor editor = bot.gefEditor("shapesExample1.shapes"); + editor.activateTool("Ellipse"); + + SWTBotGefPalette palette = new SWTBotGefPalette(editor.getSWTBotGefViewer()); + ColorPalette colorPalette = palette.getColorPalette(); + + assertEquals(colorPalette.getHoverColor(), ColorConstants.cyan); + assertEquals(colorPalette.getSelectedColor(), ColorConstants.darkGreen); + } + @Override protected String getWizardId() { return "org.eclipse.gef.examples.shapes.ShapesCreationWizard"; diff --git a/org.eclipse.gef.tests/src/org/eclipse/gef/test/swtbot/utils/SWTBotGefPalette.java b/org.eclipse.gef.tests/src/org/eclipse/gef/test/swtbot/utils/SWTBotGefPalette.java new file mode 100644 index 000000000..6db2af9cb --- /dev/null +++ b/org.eclipse.gef.tests/src/org/eclipse/gef/test/swtbot/utils/SWTBotGefPalette.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * 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.test.swtbot.utils; + +import org.eclipse.swtbot.eclipse.gef.finder.widgets.SWTBotGefEditPart; +import org.eclipse.swtbot.eclipse.gef.finder.widgets.SWTBotGefViewer; +import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; + +import org.eclipse.gef.ui.palette.ColorPalette; +import org.eclipse.gef.ui.palette.PaletteViewer; + +/** + * Convenience class to create an SWTBot instance over the palette viewer. + */ +public class SWTBotGefPalette extends SWTBotGefViewer { + + public SWTBotGefPalette(SWTBotGefViewer gefViewer) throws WidgetNotFoundException { + super(getPaletteViewer(gefViewer)); + } + + private static PaletteViewer getPaletteViewer(SWTBotGefViewer gefViewer) { + SWTBotGefEditPart gefEditPart = gefViewer.rootEditPart(); + return gefEditPart.part().getViewer().getEditDomain().getPaletteViewer(); + } + + public PaletteViewer getPaletteViewer() { + return (PaletteViewer) graphicalViewer; + } + + public ColorPalette getColorPalette() { + return getPaletteViewer().getColorPalette(); + } +} diff --git a/org.eclipse.gef/.settings/.api_filters b/org.eclipse.gef/.settings/.api_filters index f711a2d0d..39a55c28c 100644 --- a/org.eclipse.gef/.settings/.api_filters +++ b/org.eclipse.gef/.settings/.api_filters @@ -126,6 +126,14 @@ + + + + + + + + diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/PaletteColorUtil.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/PaletteColorUtil.java index 05838ec52..9d360fdb9 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/PaletteColorUtil.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/PaletteColorUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2010 IBM Corporation and others. + * Copyright (c) 2008, 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 @@ -14,7 +14,6 @@ package org.eclipse.gef.internal.ui.palette; import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.widgets.Display; import org.eclipse.draw2d.ColorConstants; import org.eclipse.draw2d.FigureUtilities; @@ -39,14 +38,6 @@ public class PaletteColorUtil { public static final Color ARROW_HOVER = new Color(null, 229, 229, 219); - private static final Color HOVER_COLOR = ColorConstants.listHoverBackgroundColor; - - private static final Color SELECTED_COLOR = ColorConstants.listSelectedBackgroundColor; - - private static final Color HOVER_COLOR_HICONTRAST = new Color(null, 0, 128, 0); - - private static final Color SELECTED_COLOR_HICONTRAST = new Color(null, 128, 0, 128); - public static final Color WIDGET_BACKGROUND_LIST_BACKGROUND_40 = FigureUtilities .mixColors(PaletteColorUtil.WIDGET_BACKGROUND, PaletteColorUtil.WIDGET_LIST_BACKGROUND, 0.4); @@ -79,40 +70,4 @@ public class PaletteColorUtil { public static final Color WIDGET_BACKGROUND_NORMAL_SHADOW_95 = FigureUtilities .mixColors(PaletteColorUtil.WIDGET_BACKGROUND, PaletteColorUtil.WIDGET_NORMAL_SHADOW, 0.95); - - /** - * Gets the color to be used when hovering over palette items. The color differs - * in high contrast mode. - * - * @return the hover color - * @since 3.4 - */ - public static Color getHoverColor() { - Display display = Display.getCurrent(); - if (display == null) { - display = Display.getDefault(); - } - if (display.getHighContrast()) { - return HOVER_COLOR_HICONTRAST; - } - return HOVER_COLOR; - } - - /** - * Gets the color to be used when selecting palette items. The color differs in - * high contrast mode. - * - * @return the selected color - * @since 3.4 - */ - public static Color getSelectedColor() { - Display display = Display.getCurrent(); - if (display == null) { - display = Display.getDefault(); - } - if (display.getHighContrast()) { - return SELECTED_COLOR_HICONTRAST; - } - return SELECTED_COLOR; - } } 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..75a068b70 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 @@ -65,7 +65,7 @@ public DrawerEditPart(PaletteDrawer drawer) { */ @Override public IFigure createFigure() { - DrawerFigure fig = new DrawerFigure(getViewer().getControl()) { + DrawerFigure fig = new DrawerFigure(getViewer().getControl(), getColorPalette()) { @Override IFigure buildTooltip() { return createToolTip(); 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 d61d16f24..01342beaa 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 @@ -46,6 +46,7 @@ import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.gef.internal.ui.palette.PaletteColorUtil; +import org.eclipse.gef.ui.palette.ColorPalette; import org.eclipse.gef.ui.palette.PaletteViewerPreferences; import org.eclipse.gef.ui.palette.editparts.PaletteToolbarLayout; @@ -80,6 +81,7 @@ public class DrawerFigure extends Figure { private boolean showPin = true; private boolean skipNextEvent; private EditPartTipHelper tipHelper; + private final ColorPalette colorPalette; /** * This is the figure for the entire drawer label button. @@ -139,12 +141,15 @@ public void paintBackground(IFigure figure, Graphics g, Insets insets) { /** * Constructor * - * @param control The Control of the LWS to which this Figure belongs (it is - * used to display the drawer header with an EditPartTipHelper, - * if the header is not completely visible). It can be - * null (the tip won't be displayed). + * @param control The Control of the LWS to which this Figure belongs (it + * is used to display the drawer header with an + * EditPartTipHelper, if the header is not completely + * visible). It can be null (the tip won't be + * displayed). + * @param colorPalette The color palette used for this figure. */ - public DrawerFigure(final Control control) { + public DrawerFigure(final Control control, final ColorPalette colorPalette) { + this.colorPalette = colorPalette; /* * A PaletteToolbarLayout is being used here instead of a ToolbarLayout so that * the ScrollPane can be stretched to take up vertical space. This affects @@ -316,7 +321,7 @@ private void createScrollpane() { scrollpane.getViewport().setContentsTracksWidth(true); scrollpane.setMinimumSize(new Dimension(0, 0)); scrollpane.setHorizontalScrollBarVisibility(ScrollPane.NEVER); - scrollpane.setVerticalScrollBar(new PaletteScrollBar()); + scrollpane.setVerticalScrollBar(new PaletteScrollBar(colorPalette)); scrollpane.getVerticalScrollBar().setStepIncrement(20); scrollpane.setLayoutManager(new OverlayScrollPaneLayout()); scrollpane.setContents(new Figure()); diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/PaletteScrollBar.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/PaletteScrollBar.java index bd662b26d..3b01bd149 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/PaletteScrollBar.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/PaletteScrollBar.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 @@ -35,6 +35,7 @@ import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.gef.internal.ui.palette.PaletteColorUtil; +import org.eclipse.gef.ui.palette.ColorPalette; public final class PaletteScrollBar extends ScrollBar { @@ -81,7 +82,10 @@ public final class PaletteScrollBar extends ScrollBar { protected Label upLabel; - public PaletteScrollBar() { + protected final ColorPalette colorPalette; + + public PaletteScrollBar(ColorPalette colorPalette) { + this.colorPalette = colorPalette; } @Override @@ -114,8 +118,8 @@ protected void paintFigure(Graphics g) { if (!getModel().isMouseOver()) { g.drawImage(TRANSPARENCY, new Rectangle(0, 0, 1, 1), getBounds()); } else { - g.setBackgroundColor(getModel().isArmed() ? PaletteColorUtil.getSelectedColor() - : PaletteColorUtil.getHoverColor()); + g.setBackgroundColor(getModel().isArmed() ? colorPalette.getSelectedColor() + : colorPalette.getHoverColor()); g.fillRectangle(getBounds()); } diff --git a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/TemplateEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/TemplateEditPart.java index 0e6600c05..530babeb5 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/TemplateEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/internal/ui/palette/editparts/TemplateEditPart.java @@ -25,7 +25,6 @@ import org.eclipse.gef.AccessibleEditPart; import org.eclipse.gef.DragTracker; import org.eclipse.gef.Request; -import org.eclipse.gef.internal.ui.palette.PaletteColorUtil; import org.eclipse.gef.palette.PaletteEntry; import org.eclipse.gef.palette.PaletteTemplateEntry; import org.eclipse.gef.ui.palette.PaletteViewerPreferences; @@ -87,7 +86,7 @@ protected void paintFigure(Graphics graphics) { super.paintFigure(graphics); if (isSelected()) { - graphics.setBackgroundColor(PaletteColorUtil.getSelectedColor()); + graphics.setBackgroundColor(getColorPalette().getSelectedColor()); } graphics.fillRoundRectangle(ToolEntryEditPart.getSelectionRectangle(getLayoutSetting(), this), 3, 3); } 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..28514b66e 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 @@ -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 @@ -38,7 +38,6 @@ import org.eclipse.gef.DragTracker; import org.eclipse.gef.Request; import org.eclipse.gef.RequestConstants; -import org.eclipse.gef.internal.ui.palette.PaletteColorUtil; import org.eclipse.gef.palette.PaletteEntry; import org.eclipse.gef.palette.PaletteStack; import org.eclipse.gef.palette.ToolEntry; @@ -278,10 +277,10 @@ protected void paintFigure(Graphics graphics) { ButtonModel model = getModel(); if (model.isSelected()) { - graphics.setBackgroundColor(PaletteColorUtil.getSelectedColor()); + graphics.setBackgroundColor(getColorPalette().getSelectedColor()); graphics.fillRoundRectangle(getSelectionRectangle(getLayoutSetting(), customLabel), 3, 3); } else if (model.isMouseOver() || showHoverFeedback) { - graphics.setBackgroundColor(PaletteColorUtil.getHoverColor()); + graphics.setBackgroundColor(getColorPalette().getHoverColor()); graphics.fillRoundRectangle(getSelectionRectangle(getLayoutSetting(), customLabel), 3, 3); } } diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/ColorPalette.java b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/ColorPalette.java new file mode 100644 index 000000000..7f7ff7cbd --- /dev/null +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/ColorPalette.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * 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; + +/** + * This interface defines the colors that are used for painting the figures of + * the {@link PaletteViewer}. Clients may implement this class and assign it to + * their palette via {@link PaletteViewer#setColorPalette(ColorPalette)} to set + * their own color theme. + * + * IMPORTANT: This interface is not intended to be implemented + * by clients. Clients should inherit from {@link DefaultColorPalette}. New + * methods may be added in the future. + * + * @since 3.20 + * @noextend This interface is not intended to be extended by clients. + * @noimplement This interface is not intended to be implemented by clients. + */ +public interface ColorPalette { + /** + * Gets the color to be used when hovering over palette items. The color may + * differ in high contrast mode. + * + * @return the hover color. + */ + Color getSelectedColor(); + + /** + * Gets the color to be used when selecting palette items. The color may differ + * in high contrast mode. + * + * @return the selected color. + */ + Color getHoverColor(); +} diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/DefaultColorPalette.java b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/DefaultColorPalette.java new file mode 100644 index 000000000..16e457457 --- /dev/null +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/DefaultColorPalette.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * 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; +import org.eclipse.swt.widgets.Display; + +import org.eclipse.draw2d.ColorConstants; + +/** + * Default colors used by the {@link PaletteViewer} which are used when painting + * the palette figures. Clients may extend this class to define their own + * colors. The color palette can be set via + * {@link PaletteViewer#setColorPalette(ColorPalette)}. + * + * @since 3.20 + */ +public class DefaultColorPalette implements ColorPalette { + private static final Color HOVER_COLOR_HICONTRAST = new Color(null, 0, 128, 0); + private static final Color SELECTED_COLOR_HICONTRAST = new Color(null, 128, 0, 128); + + public static final ColorPalette INSTANCE = new DefaultColorPalette(); + + protected DefaultColorPalette() { + // This class should never be instantiated directly but instead, the singleton + // should be used. It may however be extended by clients to define their own + // colors. + } + + @Override + public Color getSelectedColor() { + Display display = Display.getCurrent(); + if (display == null) { + display = Display.getDefault(); + } + if (display.getHighContrast()) { + return SELECTED_COLOR_HICONTRAST; + } + return ColorConstants.listSelectedBackgroundColor; + } + + @Override + public Color getHoverColor() { + Display display = Display.getCurrent(); + if (display == null) { + display = Display.getDefault(); + } + if (display.getHighContrast()) { + return HOVER_COLOR_HICONTRAST; + } + return ColorConstants.listHoverBackgroundColor; + } +} diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/PaletteViewer.java b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/PaletteViewer.java index ec5bf6327..7228f8b14 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/PaletteViewer.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/PaletteViewer.java @@ -96,6 +96,7 @@ private void refreshAllEditParts(EditPart part) { private PaletteViewerPreferences prefs = PREFERENCE_STORE; private Font font = null; private PaletteAnimator paletteAnimator; + private ColorPalette colorPalette = DefaultColorPalette.INSTANCE; /** * Constructor @@ -230,6 +231,16 @@ public PaletteViewerPreferences getPaletteViewerPreferences() { return prefs; } + /** + * @return The color palette that is used for painting the figures of this + * viewer. If no palette has been set by the user, + * {@link DefaultColorPalette} is returned instead of {@code null}. + * @since 3.20 + */ + public ColorPalette getColorPalette() { + return colorPalette; + } + private ToolEntryEditPart getToolEntryEditPart(ToolEntry entry) { return (ToolEntryEditPart) getEditPartForModel(entry); } @@ -464,6 +475,22 @@ public void setPaletteViewerPreferences(PaletteViewerPreferences prefs) { } } + /** + * Sets the color palette that is used for painting the figures of this viewer. + * If {@code null} is passed as an argument, the palette is reset to its default + * configuration. + * + * @param colorPalette The color palette for this viewer. + * @since 3.20 + */ + public void setColorPalette(ColorPalette colorPalette) { + if (colorPalette == null) { + this.colorPalette = DefaultColorPalette.INSTANCE; + } else { + this.colorPalette = colorPalette; + } + } + /** * @see org.eclipse.gef.ui.parts.AbstractEditPartViewer#unhookControl() */ diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/editparts/PaletteEditPart.java b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/editparts/PaletteEditPart.java index af0de1a38..6d548aa53 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/editparts/PaletteEditPart.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/editparts/PaletteEditPart.java @@ -42,6 +42,7 @@ import org.eclipse.gef.palette.PaletteEntry; import org.eclipse.gef.palette.PaletteSeparator; import org.eclipse.gef.tools.SelectEditPartTracker; +import org.eclipse.gef.ui.palette.ColorPalette; import org.eclipse.gef.ui.palette.PaletteMessages; import org.eclipse.gef.ui.palette.PaletteViewer; import org.eclipse.gef.ui.palette.PaletteViewerPreferences; @@ -347,6 +348,17 @@ public PaletteViewer getViewer() { return (PaletteViewer) super.getViewer(); } + /** + * Convenience method that simply delegates to + * {@link PaletteViewer#getColorPalette()}. + * + * @return The color palette used for this edit part. + * @since 3.20 + */ + protected final ColorPalette getColorPalette() { + return getViewer().getColorPalette(); + } + /** * Determine if the name is needed in the tool tip. *