Skip to content

Commit

Permalink
Make palette selection and hover colors customizable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ptziegler authored and azoitl committed Oct 31, 2024
1 parent dda216b commit 9974631
Show file tree
Hide file tree
Showing 21 changed files with 404 additions and 67 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/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/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:
Expand Down
6 changes: 5 additions & 1 deletion org.eclipse.gef.examples.shapes/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ Require-Bundle: org.eclipse.gef;bundle-version="[3.18.0,4.0.0)",
org.eclipse.ui.views;bundle-version="[3.2.0,4.0.0)",
org.eclipse.ui.workbench;bundle-version="[3.2.0,4.0.0)",
org.eclipse.ui.ide;bundle-version="[3.2.0,4.0.0)",
org.eclipse.jface;bundle-version="[3.2.0,4.0.0)"
org.eclipse.jface;bundle-version="[3.2.0,4.0.0)",
org.eclipse.e4.core.contexts;bundle-version="[1.12.600,2.0.0)",
org.eclipse.e4.core.di.annotations;bundle-version="[1.8.400,2.0.0)",
org.eclipse.e4.ui.model.workbench;bundle-version="[2.4.300,3.0.0)"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-17
Automatic-Module-Name: org.eclipse.gef.examples.shapes
Model-Fragment: fragment.e4xmi

5 changes: 3 additions & 2 deletions org.eclipse.gef.examples.shapes/build.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###############################################################################
# Copyright (c) 2004, 2010 IBM Corporation and others.
# Copyright (c) 2004, 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 @@ -17,7 +17,8 @@ bin.includes = plugin.xml,\
shapes.gif,\
about.html,\
META-INF/,\
plugin.properties
plugin.properties,\
fragment.e4xmi
src.includes = META-INF/,\
about.html,\
plugin.properties,\
Expand Down
18 changes: 18 additions & 0 deletions org.eclipse.gef.examples.shapes/fragment.e4xmi
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="ASCII"?>
<fragment:ModelFragments xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:commands="http://www.eclipse.org/ui/2010/UIModel/application/commands" xmlns:fragment="http://www.eclipse.org/ui/2010/UIModel/fragment" xmlns:menu="http://www.eclipse.org/ui/2010/UIModel/application/ui/menu" xmlns:ui="http://www.eclipse.org/ui/2010/UIModel/application/ui" xmi:id="_NgZIIJVIEe-9Ku3Ok7Aflw">
<imports xsi:type="menu:Menu" xmi:id="_XpVcIJVKEe-9Ku3Ok7Aflw" elementId="org.eclipse.ui.main.menu"/>
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_OIiGMJVIEe-9Ku3Ok7Aflw" featurename="handlers" parentElementId="xpath:/">
<elements xsi:type="commands:Handler" xmi:id="_SvOkgJVIEe-9Ku3Ok7Aflw" elementId="org.eclipse.gef.examples.shapes.handler.paletteHandler" contributionURI="bundleclass://org.eclipse.gef.examples.shapes/org.eclipse.gef.examples.shapes.handler.ColorPaletteHandler" command="_WIwFEJVIEe-9Ku3Ok7Aflw"/>
</fragments>
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_Ug_dEJVIEe-9Ku3Ok7Aflw" featurename="commands" parentElementId="xpath:/">
<elements xsi:type="commands:Command" xmi:id="_WIwFEJVIEe-9Ku3Ok7Aflw" elementId="org.eclipse.gef.examples.shapes.command.paletteCommand" commandName="Use Custom Color Palette"/>
</fragments>
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_DGMksJVJEe-9Ku3Ok7Aflw" featurename="menuContributions" parentElementId="xpath:/">
<elements xsi:type="menu:MenuContribution" xmi:id="_FRJZUJVJEe-9Ku3Ok7Aflw" elementId="org.eclipse.gef.examples.shapes.menucontribution.paletteMenu" positionInParent="after=edit" parentId="org.eclipse.ui.main.menu">
<children xsi:type="menu:Menu" xmi:id="_VaBhYJVJEe-9Ku3Ok7Aflw" elementId="org.eclipse.gef.examples.shapes.menu.palette" label="%menu.label.1">
<visibleWhen xsi:type="ui:ImperativeExpression" xmi:id="_5lzhoJVKEe-9Ku3Ok7Aflw" contributionURI="bundleclass://org.eclipse.gef.examples.shapes/org.eclipse.gef.examples.shapes.handler.ColorPaletteExpression"/>
<children xsi:type="menu:HandledMenuItem" xmi:id="_knZOAJVKEe-9Ku3Ok7Aflw" elementId="org.eclipse.gef.examples.shapes.handledmenuitem.usecustompalette" label="%handledmenuitem.label.1" enabled="false" type="Check" command="_WIwFEJVIEe-9Ku3Ok7Aflw"/>
</children>
</elements>
</fragments>
</fragment:ModelFragments>
3 changes: 3 additions & 0 deletions org.eclipse.gef.examples.shapes/plugin.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@
###############################################################################
Plugin.name=GEF Classic Shape Editor Example
Plugin.providerName=Eclipse GEF

menu.label.1 = Palette
handledmenuitem.label.1 = Use Custom Color Palette
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* 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.handler;

import org.eclipse.e4.core.contexts.Active;
import org.eclipse.e4.core.di.annotations.Evaluate;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.ui.IEditorPart;

import org.eclipse.gef.examples.shapes.ShapesEditor;

/**
* This imperative expression is invoked by the {@code fragment.e4xmi} to check
* whether the {@code Palette} menu item is visible.
*/
public class ColorPaletteExpression {
@Evaluate
@SuppressWarnings("static-method")
public boolean test(@Optional @Active MPart activePart) {
if (activePart != null) {
return activePart.getContext().get(IEditorPart.class) instanceof ShapesEditor;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*******************************************************************************
* 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.handler;

import org.eclipse.e4.core.contexts.Active;
import org.eclipse.e4.core.di.annotations.CanExecute;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
import org.eclipse.ui.IEditorPart;

import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.ui.palette.PaletteViewer;

import org.eclipse.gef.examples.shapes.ShapesEditor;
import org.eclipse.gef.examples.shapes.palette.ShapesColorPalette;

/**
* This handler manages the {@code Use Custom Palette} menu item which switches
* between the default and the custom palette theme.
*/
public class ColorPaletteHandler {
@Execute
@SuppressWarnings("static-method")
public void execute(@Active MPart activePart, MMenuItem menuItem) {
IEditorPart editorPart = activePart.getContext().get(IEditorPart.class);
GraphicalViewer graphicalViewer = editorPart.getAdapter(GraphicalViewer.class);
PaletteViewer paletteViewer = graphicalViewer.getEditDomain().getPaletteViewer();
if (menuItem.isSelected()) {
paletteViewer.setColorPalette(new ShapesColorPalette());
} else {
paletteViewer.setColorPalette(null);
}
paletteViewer.getControl().redraw();
}

@CanExecute
@SuppressWarnings("static-method")
public boolean canExecute(@Active MPart activePart) {
return activePart.getContext().get(IEditorPart.class) instanceof ShapesEditor;
}
}
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.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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
8 changes: 8 additions & 0 deletions org.eclipse.gef/.settings/.api_filters
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/gef/ui/palette/DefaultColorPalette.java" type="org.eclipse.gef.ui.palette.DefaultColorPalette">
<filter comment="Default implementation for ColorPalette" id="576725006">
<message_arguments>
<message_argument value="ColorPalette"/>
<message_argument value="DefaultColorPalette"/>
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/gef/ui/palette/DefaultPaletteViewerPreferences.java" type="org.eclipse.gef.ui.palette.DefaultPaletteViewerPreferences">
<filter id="576725006">
<message_arguments>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}
}
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 @@ -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();
Expand Down
Loading

0 comments on commit 9974631

Please sign in to comment.