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 committed Oct 27, 2024
1 parent cd22d9f commit 939f262
Show file tree
Hide file tree
Showing 21 changed files with 390 additions and 65 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-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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,14 +12,20 @@
*******************************************************************************/
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;
import org.eclipse.gef.ui.actions.DeleteRetargetAction;
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).
Expand All @@ -38,6 +44,7 @@ protected void buildActions() {
addRetargetAction(new DeleteRetargetAction());
addRetargetAction(new UndoRetargetAction());
addRetargetAction(new RedoRetargetAction());
addRetargetAction(new ColorPaletteRetargetAction());
}

/**
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 939f262

Please sign in to comment.