Skip to content

Commit 9974631

Browse files
ptzieglerazoitl
authored andcommitted
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.
1 parent dda216b commit 9974631

File tree

21 files changed

+404
-67
lines changed

21 files changed

+404
-67
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
## GEF
77
- _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_.
88
- 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.
9+
- The look of the palette viewer can be customized via the ColorPalette interface. Supported model elements and properties are:
10+
- PaletteEntry (hover, selection)
11+
- PaletteTemplateEntry (selection)
912

1013
## Zest
1114
- 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:

org.eclipse.gef.examples.shapes/META-INF/MANIFEST.MF

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ Require-Bundle: org.eclipse.gef;bundle-version="[3.18.0,4.0.0)",
1313
org.eclipse.ui.views;bundle-version="[3.2.0,4.0.0)",
1414
org.eclipse.ui.workbench;bundle-version="[3.2.0,4.0.0)",
1515
org.eclipse.ui.ide;bundle-version="[3.2.0,4.0.0)",
16-
org.eclipse.jface;bundle-version="[3.2.0,4.0.0)"
16+
org.eclipse.jface;bundle-version="[3.2.0,4.0.0)",
17+
org.eclipse.e4.core.contexts;bundle-version="[1.12.600,2.0.0)",
18+
org.eclipse.e4.core.di.annotations;bundle-version="[1.8.400,2.0.0)",
19+
org.eclipse.e4.ui.model.workbench;bundle-version="[2.4.300,3.0.0)"
1720
Bundle-ActivationPolicy: lazy
1821
Bundle-RequiredExecutionEnvironment: JavaSE-17
1922
Automatic-Module-Name: org.eclipse.gef.examples.shapes
23+
Model-Fragment: fragment.e4xmi
2024

org.eclipse.gef.examples.shapes/build.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
###############################################################################
2-
# Copyright (c) 2004, 2010 IBM Corporation and others.
2+
# Copyright (c) 2004, 2024 IBM Corporation and others.
33
#
44
# This program and the accompanying materials are made available under the
55
# terms of the Eclipse Public License 2.0 which is available at
@@ -17,7 +17,8 @@ bin.includes = plugin.xml,\
1717
shapes.gif,\
1818
about.html,\
1919
META-INF/,\
20-
plugin.properties
20+
plugin.properties,\
21+
fragment.e4xmi
2122
src.includes = META-INF/,\
2223
about.html,\
2324
plugin.properties,\
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="ASCII"?>
2+
<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">
3+
<imports xsi:type="menu:Menu" xmi:id="_XpVcIJVKEe-9Ku3Ok7Aflw" elementId="org.eclipse.ui.main.menu"/>
4+
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_OIiGMJVIEe-9Ku3Ok7Aflw" featurename="handlers" parentElementId="xpath:/">
5+
<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"/>
6+
</fragments>
7+
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_Ug_dEJVIEe-9Ku3Ok7Aflw" featurename="commands" parentElementId="xpath:/">
8+
<elements xsi:type="commands:Command" xmi:id="_WIwFEJVIEe-9Ku3Ok7Aflw" elementId="org.eclipse.gef.examples.shapes.command.paletteCommand" commandName="Use Custom Color Palette"/>
9+
</fragments>
10+
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_DGMksJVJEe-9Ku3Ok7Aflw" featurename="menuContributions" parentElementId="xpath:/">
11+
<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">
12+
<children xsi:type="menu:Menu" xmi:id="_VaBhYJVJEe-9Ku3Ok7Aflw" elementId="org.eclipse.gef.examples.shapes.menu.palette" label="%menu.label.1">
13+
<visibleWhen xsi:type="ui:ImperativeExpression" xmi:id="_5lzhoJVKEe-9Ku3Ok7Aflw" contributionURI="bundleclass://org.eclipse.gef.examples.shapes/org.eclipse.gef.examples.shapes.handler.ColorPaletteExpression"/>
14+
<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"/>
15+
</children>
16+
</elements>
17+
</fragments>
18+
</fragment:ModelFragments>

org.eclipse.gef.examples.shapes/plugin.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212
###############################################################################
1313
Plugin.name=GEF Classic Shape Editor Example
1414
Plugin.providerName=Eclipse GEF
15+
16+
menu.label.1 = Palette
17+
handledmenuitem.label.1 = Use Custom Color Palette
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Patrick Ziegler and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Patrick Ziegler - initial API and implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.gef.examples.shapes.handler;
15+
16+
import org.eclipse.e4.core.contexts.Active;
17+
import org.eclipse.e4.core.di.annotations.Evaluate;
18+
import org.eclipse.e4.core.di.annotations.Optional;
19+
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
20+
import org.eclipse.ui.IEditorPart;
21+
22+
import org.eclipse.gef.examples.shapes.ShapesEditor;
23+
24+
/**
25+
* This imperative expression is invoked by the {@code fragment.e4xmi} to check
26+
* whether the {@code Palette} menu item is visible.
27+
*/
28+
public class ColorPaletteExpression {
29+
@Evaluate
30+
@SuppressWarnings("static-method")
31+
public boolean test(@Optional @Active MPart activePart) {
32+
if (activePart != null) {
33+
return activePart.getContext().get(IEditorPart.class) instanceof ShapesEditor;
34+
}
35+
return false;
36+
}
37+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Patrick Ziegler and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Patrick Ziegler - initial API and implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.gef.examples.shapes.handler;
15+
16+
import org.eclipse.e4.core.contexts.Active;
17+
import org.eclipse.e4.core.di.annotations.CanExecute;
18+
import org.eclipse.e4.core.di.annotations.Execute;
19+
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
20+
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
21+
import org.eclipse.ui.IEditorPart;
22+
23+
import org.eclipse.gef.GraphicalViewer;
24+
import org.eclipse.gef.ui.palette.PaletteViewer;
25+
26+
import org.eclipse.gef.examples.shapes.ShapesEditor;
27+
import org.eclipse.gef.examples.shapes.palette.ShapesColorPalette;
28+
29+
/**
30+
* This handler manages the {@code Use Custom Palette} menu item which switches
31+
* between the default and the custom palette theme.
32+
*/
33+
public class ColorPaletteHandler {
34+
@Execute
35+
@SuppressWarnings("static-method")
36+
public void execute(@Active MPart activePart, MMenuItem menuItem) {
37+
IEditorPart editorPart = activePart.getContext().get(IEditorPart.class);
38+
GraphicalViewer graphicalViewer = editorPart.getAdapter(GraphicalViewer.class);
39+
PaletteViewer paletteViewer = graphicalViewer.getEditDomain().getPaletteViewer();
40+
if (menuItem.isSelected()) {
41+
paletteViewer.setColorPalette(new ShapesColorPalette());
42+
} else {
43+
paletteViewer.setColorPalette(null);
44+
}
45+
paletteViewer.getControl().redraw();
46+
}
47+
48+
@CanExecute
49+
@SuppressWarnings("static-method")
50+
public boolean canExecute(@Active MPart activePart) {
51+
return activePart.getContext().get(IEditorPart.class) instanceof ShapesEditor;
52+
}
53+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Patrick Ziegler and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Patrick Ziegler - initial API and implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.gef.examples.shapes.palette;
15+
16+
import org.eclipse.swt.graphics.Color;
17+
18+
import org.eclipse.draw2d.ColorConstants;
19+
20+
import org.eclipse.gef.ui.palette.DefaultColorPalette;
21+
22+
/**
23+
* Defines arbitrary colors that distinguish themselves from the default
24+
* palette.
25+
*/
26+
public class ShapesColorPalette extends DefaultColorPalette {
27+
@Override
28+
public Color getSelectedColor() {
29+
return ColorConstants.darkGreen;
30+
}
31+
32+
@Override
33+
public Color getHoverColor() {
34+
return ColorConstants.cyan;
35+
}
36+
}

org.eclipse.gef.tests/src/org/eclipse/gef/test/swtbot/ShapesDiagramTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
import org.eclipse.swtbot.eclipse.gef.finder.widgets.SWTBotGefEditor;
2323
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
2424

25+
import org.eclipse.draw2d.ColorConstants;
2526
import org.eclipse.draw2d.IFigure;
2627
import org.eclipse.draw2d.PositionConstants;
2728
import org.eclipse.draw2d.geometry.Dimension;
2829

2930
import org.eclipse.gef.GraphicalEditPart;
31+
import org.eclipse.gef.test.swtbot.utils.SWTBotGefPalette;
32+
import org.eclipse.gef.ui.palette.ColorPalette;
3033

3134
import org.junit.Test;
3235

@@ -58,6 +61,20 @@ public void testResizeEditPart() {
5861
assertEquals(figure.getSize(), new Dimension(200, 200));
5962
}
6063

64+
@Test
65+
public void testCustomPalette() {
66+
bot.menu("Palette").menu("Use Custom Color Palette").click();
67+
68+
SWTBotGefEditor editor = bot.gefEditor("shapesExample1.shapes");
69+
editor.activateTool("Ellipse");
70+
71+
SWTBotGefPalette palette = new SWTBotGefPalette(editor.getSWTBotGefViewer());
72+
ColorPalette colorPalette = palette.getColorPalette();
73+
74+
assertEquals(colorPalette.getHoverColor(), ColorConstants.cyan);
75+
assertEquals(colorPalette.getSelectedColor(), ColorConstants.darkGreen);
76+
}
77+
6178
@Override
6279
protected String getWizardId() {
6380
return "org.eclipse.gef.examples.shapes.ShapesCreationWizard";
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Patrick Ziegler and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Patrick Ziegler - initial API and implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.gef.test.swtbot.utils;
15+
16+
import org.eclipse.swtbot.eclipse.gef.finder.widgets.SWTBotGefEditPart;
17+
import org.eclipse.swtbot.eclipse.gef.finder.widgets.SWTBotGefViewer;
18+
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
19+
20+
import org.eclipse.gef.ui.palette.ColorPalette;
21+
import org.eclipse.gef.ui.palette.PaletteViewer;
22+
23+
/**
24+
* Convenience class to create an SWTBot instance over the palette viewer.
25+
*/
26+
public class SWTBotGefPalette extends SWTBotGefViewer {
27+
28+
public SWTBotGefPalette(SWTBotGefViewer gefViewer) throws WidgetNotFoundException {
29+
super(getPaletteViewer(gefViewer));
30+
}
31+
32+
private static PaletteViewer getPaletteViewer(SWTBotGefViewer gefViewer) {
33+
SWTBotGefEditPart gefEditPart = gefViewer.rootEditPart();
34+
return gefEditPart.part().getViewer().getEditDomain().getPaletteViewer();
35+
}
36+
37+
public PaletteViewer getPaletteViewer() {
38+
return (PaletteViewer) graphicalViewer;
39+
}
40+
41+
public ColorPalette getColorPalette() {
42+
return getPaletteViewer().getColorPalette();
43+
}
44+
}

0 commit comments

Comments
 (0)